PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/classes/workflowtypes/event/ezapprove/ezapprovetype.php

http://github.com/ezsystems/ezpublish
PHP | 785 lines | 648 code | 68 blank | 69 comment | 94 complexity | 036188683e75523e323b523cf07149c9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZApproveType class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZApproveType ezapprovetype.php
  12. \brief Event type for user approvals
  13. WorkflowEvent storage fields : data_text1 - selected_sections
  14. data_text2 - selected_usergroups
  15. data_text3 - approve_users
  16. data_text4 - approve_groups
  17. data_int2 - language_list
  18. data_int3 - content object version option
  19. */
  20. class eZApproveType extends eZWorkflowEventType
  21. {
  22. const WORKFLOW_TYPE_STRING = "ezapprove";
  23. const COLLABORATION_NOT_CREATED = 0;
  24. const COLLABORATION_CREATED = 1;
  25. const VERSION_OPTION_FIRST_ONLY = 1;
  26. const VERSION_OPTION_EXCEPT_FIRST = 2;
  27. const VERSION_OPTION_ALL = 3;
  28. public function __construct()
  29. {
  30. parent::__construct( eZApproveType::WORKFLOW_TYPE_STRING, ezpI18n::tr( 'kernel/workflow/event', "Approve" ) );
  31. $this->setTriggerTypes( array( 'content' => array( 'publish' => array( 'before' ) ) ) );
  32. }
  33. function attributeDecoder( $event, $attr )
  34. {
  35. switch ( $attr )
  36. {
  37. case 'selected_sections':
  38. {
  39. $attributeValue = trim( $event->attribute( 'data_text1' ) );
  40. $returnValue = empty( $attributeValue ) ? array( -1 ) : explode( ',', $attributeValue );
  41. }break;
  42. case 'approve_users':
  43. {
  44. $attributeValue = trim( $event->attribute( 'data_text3' ) );
  45. $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
  46. }break;
  47. case 'approve_groups':
  48. {
  49. $attributeValue = trim( $event->attribute( 'data_text4' ) );
  50. $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
  51. }break;
  52. case 'selected_usergroups':
  53. {
  54. $attributeValue = trim( $event->attribute( 'data_text2' ) );
  55. $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
  56. }break;
  57. case 'language_list':
  58. {
  59. $returnValue = array();
  60. $attributeValue = $event->attribute( 'data_int2' );
  61. if ( $attributeValue != 0 )
  62. {
  63. $languages = eZContentLanguage::languagesByMask( $attributeValue );
  64. foreach ( $languages as $language )
  65. {
  66. $returnValue[$language->attribute( 'id' )] = $language->attribute( 'name' );
  67. }
  68. }
  69. }break;
  70. case 'version_option':
  71. {
  72. $returnValue = eZApproveType::VERSION_OPTION_ALL & $event->attribute( 'data_int3' );
  73. }break;
  74. default:
  75. $returnValue = null;
  76. }
  77. return $returnValue;
  78. }
  79. function typeFunctionalAttributes( )
  80. {
  81. return array( 'selected_sections',
  82. 'approve_users',
  83. 'approve_groups',
  84. 'selected_usergroups',
  85. 'language_list',
  86. 'version_option' );
  87. }
  88. function attributes()
  89. {
  90. return array_merge( array( 'sections',
  91. 'languages',
  92. 'users',
  93. 'usergroups' ),
  94. eZWorkflowEventType::attributes() );
  95. }
  96. function hasAttribute( $attr )
  97. {
  98. return in_array( $attr, $this->attributes() );
  99. }
  100. function attribute( $attr )
  101. {
  102. switch( $attr )
  103. {
  104. case 'sections':
  105. {
  106. $sections = eZSection::fetchList( false );
  107. foreach ( $sections as $key => $section )
  108. {
  109. $sections[$key]['Name'] = $section['name'];
  110. $sections[$key]['value'] = $section['id'];
  111. }
  112. return $sections;
  113. }break;
  114. case 'languages':
  115. {
  116. return eZContentLanguage::fetchList();
  117. }break;
  118. }
  119. return eZWorkflowEventType::attribute( $attr );
  120. }
  121. function execute( $process, $event )
  122. {
  123. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process, 'eZApproveType::execute' );
  124. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'eZApproveType::execute' );
  125. $parameters = $process->attribute( 'parameter_list' );
  126. $versionID = $parameters['version'];
  127. $objectID = $parameters['object_id'];
  128. $object = eZContentObject::fetch( $objectID );
  129. if ( !$object )
  130. {
  131. eZDebugSetting::writeError( 'kernel-workflow-approve', "No object with ID $objectID", 'eZApproveType::execute' );
  132. return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
  133. }
  134. $version = $object->version( $versionID );
  135. if ( !$version )
  136. {
  137. eZDebugSetting::writeError( 'kernel-workflow-approve', "No version $versionID for object with ID $objectID", 'eZApproveType::execute' );
  138. return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
  139. }
  140. // only check this in cronjob
  141. if( $process->attribute( 'status' ) == eZWorkflow::STATUS_DEFERRED_TO_CRON )
  142. {
  143. $nodeAssignmentList = $version->attribute( 'node_assignments' );
  144. if( !empty( $nodeAssignmentList ) )
  145. {
  146. foreach ( $nodeAssignmentList as $nodeAssignment )
  147. {
  148. $parentNode = $nodeAssignment->getParentNode();
  149. if( $parentNode === null )
  150. {
  151. eZDebugSetting::writeError( 'kernel-workflow-approve', "No parent node for object with ID $objectID version $versionID", 'eZApproveType::execute' );
  152. return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
  153. }
  154. }
  155. }
  156. }
  157. // version option checking
  158. $version_option = $event->attribute( 'version_option' );
  159. if ( ( $version_option == eZApproveType::VERSION_OPTION_FIRST_ONLY and $parameters['version'] > 1 ) or
  160. ( $version_option == eZApproveType::VERSION_OPTION_EXCEPT_FIRST and $parameters['version'] == 1 ) )
  161. {
  162. return eZWorkflowType::STATUS_ACCEPTED;
  163. }
  164. /*
  165. If we run event first time ( when we click publish in admin ) we do not have user_id set in workflow process,
  166. so we take current user and store it in workflow process, so next time when we run event from cronjob we fetch
  167. user_id from there.
  168. */
  169. if ( $process->attribute( 'user_id' ) == 0 )
  170. {
  171. $user = eZUser::currentUser();
  172. $process->setAttribute( 'user_id', $user->id() );
  173. }
  174. else
  175. {
  176. $user = eZUser::instance( $process->attribute( 'user_id' ) );
  177. }
  178. $userGroups = array_merge( $user->attribute( 'groups' ), array( $user->attribute( 'contentobject_id' ) ) );
  179. $workflowSections = explode( ',', $event->attribute( 'data_text1' ) );
  180. $workflowGroups = $event->attribute( 'data_text2' ) == '' ? array() : explode( ',', $event->attribute( 'data_text2' ) );
  181. $editors = $event->attribute( 'data_text3' ) == '' ? array() : explode( ',', $event->attribute( 'data_text3' ) );
  182. $approveGroups = $event->attribute( 'data_text4' ) == '' ? array() : explode( ',', $event->attribute( 'data_text4' ) );
  183. $languageMask = $event->attribute( 'data_int2' );
  184. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user, 'eZApproveType::execute::user' );
  185. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, 'eZApproveType::execute::userGroups' );
  186. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $editors, 'eZApproveType::execute::editor' );
  187. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $approveGroups, 'eZApproveType::execute::approveGroups' );
  188. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections, 'eZApproveType::execute::workflowSections' );
  189. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups, 'eZApproveType::execute::workflowGroups' );
  190. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $languageMask, 'eZApproveType::execute::languageMask' );
  191. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $object->attribute( 'section_id'), 'eZApproveType::execute::section_id' );
  192. $section = $object->attribute( 'section_id' );
  193. $correctSection = false;
  194. if ( !in_array( $section, $workflowSections ) && !in_array( -1, $workflowSections ) )
  195. {
  196. $assignedNodes = $object->attribute( 'assigned_nodes' );
  197. if ( $assignedNodes )
  198. {
  199. foreach( $assignedNodes as $assignedNode )
  200. {
  201. $parent = $assignedNode->attribute( 'parent' );
  202. $parentObject = $parent->object();
  203. $section = $parentObject->attribute( 'section_id');
  204. if ( in_array( $section, $workflowSections ) )
  205. {
  206. $correctSection = true;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. else
  213. $correctSection = true;
  214. $inExcludeGroups = count( array_intersect( $userGroups, $workflowGroups ) ) != 0;
  215. $userIsEditor = ( in_array( $user->id(), $editors ) ||
  216. count( array_intersect( $userGroups, $approveGroups ) ) != 0 );
  217. // All languages match by default
  218. $hasLanguageMatch = true;
  219. if ( $languageMask != 0 )
  220. {
  221. // Examine if the published version contains one of the languages we
  222. // match for.
  223. // If the language ID is part of the mask the result is non-zero.
  224. $languageID = (int)$version->attribute( 'initial_language_id' );
  225. $hasLanguageMatch = (bool)( $languageMask & $languageID );
  226. }
  227. if ( $hasLanguageMatch and
  228. !$userIsEditor and
  229. !$inExcludeGroups and
  230. $correctSection )
  231. {
  232. /* Get user IDs from approve user groups */
  233. $userClassIDArray = eZUser::contentClassIDs();
  234. $approveUserIDArray = array();
  235. foreach ( $approveGroups as $approveUserGroupID )
  236. {
  237. if ( $approveUserGroupID != false )
  238. {
  239. $approveUserGroup = eZContentObject::fetch( $approveUserGroupID );
  240. if ( isset( $approveUserGroup ) )
  241. {
  242. foreach ( $approveUserGroup->attribute( 'assigned_nodes' ) as $assignedNode )
  243. {
  244. $userNodeArray = $assignedNode->subTree( array( 'ClassFilterType' => 'include',
  245. 'ClassFilterArray' => $userClassIDArray,
  246. 'Limitation' => array() ) );
  247. foreach ( $userNodeArray as $userNode )
  248. {
  249. $approveUserIDArray[] = $userNode->attribute( 'contentobject_id' );
  250. }
  251. }
  252. }
  253. }
  254. }
  255. $approveUserIDArray = array_merge( $approveUserIDArray, $editors );
  256. $approveUserIDArray = array_unique( $approveUserIDArray );
  257. $collaborationID = false;
  258. $db = eZDB::instance();
  259. $taskResult = $db->arrayQuery( 'select workflow_process_id, collaboration_id from ezapprove_items where workflow_process_id = ' . $process->attribute( 'id' ) );
  260. if ( count( $taskResult ) > 0 )
  261. $collaborationID = $taskResult[0]['collaboration_id'];
  262. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $collaborationID, 'approve collaborationID' );
  263. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process->attribute( 'event_state'), 'approve $process->attribute( \'event_state\')' );
  264. if ( $collaborationID === false )
  265. {
  266. $this->createApproveCollaboration( $process, $event, $user->id(), $object->attribute( 'id' ), $versionID, $approveUserIDArray );
  267. $this->setInformation( "We are going to create approval" );
  268. $process->setAttribute( 'event_state', eZApproveType::COLLABORATION_CREATED );
  269. $process->store();
  270. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve execute' );
  271. return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
  272. }
  273. else if ( $process->attribute( 'event_state') == eZApproveType::COLLABORATION_NOT_CREATED )
  274. {
  275. eZApproveCollaborationHandler::activateApproval( $collaborationID );
  276. $process->setAttribute( 'event_state', eZApproveType::COLLABORATION_CREATED );
  277. $process->store();
  278. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve re-execute' );
  279. return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
  280. }
  281. else //eZApproveType::COLLABORATION_CREATED
  282. {
  283. $this->setInformation( "we are checking approval now" );
  284. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'check approval' );
  285. return $this->checkApproveCollaboration( $process, $event );
  286. }
  287. }
  288. else
  289. {
  290. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections , "we are not going to create approval " . $object->attribute( 'section_id') );
  291. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, "we are not going to create approval" );
  292. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups, "we are not going to create approval" );
  293. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user->id(), "we are not going to create approval " );
  294. return eZWorkflowType::STATUS_ACCEPTED;
  295. }
  296. }
  297. function initializeEvent( $event )
  298. {
  299. }
  300. function validateUserIDList( $userIDList, &$reason )
  301. {
  302. $returnState = eZInputValidator::STATE_ACCEPTED;
  303. foreach ( $userIDList as $userID )
  304. {
  305. if ( !is_numeric( $userID ) or
  306. !eZUser::isUserObject( eZContentObject::fetch( $userID ) ) )
  307. {
  308. $returnState = eZInputValidator::STATE_INVALID;
  309. $reason[ 'list' ][] = $userID;
  310. }
  311. }
  312. $reason[ 'text' ] = "Some of passed user IDs are not valid, must be IDs of existing users only.";
  313. return $returnState;
  314. }
  315. function validateGroupIDList( $userGroupIDList, &$reason )
  316. {
  317. $returnState = eZInputValidator::STATE_ACCEPTED;
  318. $groupClassNames = eZUser::fetchUserGroupClassNames();
  319. if ( count( $groupClassNames ) > 0 )
  320. {
  321. foreach( $userGroupIDList as $userGroupID )
  322. {
  323. if ( !is_numeric( $userGroupID ) or
  324. !is_object( $userGroup = eZContentObject::fetch( $userGroupID ) ) or
  325. !in_array( $userGroup->attribute( 'class_identifier' ), $groupClassNames ) )
  326. {
  327. $returnState = eZInputValidator::STATE_INVALID;
  328. $reason[ 'list' ][] = $userGroupID;
  329. }
  330. }
  331. $reason[ 'text' ] = "Some of passed user-group IDs are not valid, must be IDs of existing user groups only.";
  332. }
  333. else
  334. {
  335. $returnState = eZInputValidator::STATE_INVALID;
  336. $reason[ 'text' ] = "There is no one user-group classes among the user accounts, please choose standalone users.";
  337. }
  338. return $returnState;
  339. }
  340. /**
  341. * @param eZHTTPTool $http
  342. * @param $base
  343. * @param eZWorkflowEvent $workflowEvent
  344. * @param $validation
  345. *
  346. * @return bool|int
  347. */
  348. function validateHTTPInput( $http, $base, $workflowEvent, &$validation )
  349. {
  350. $returnState = eZInputValidator::STATE_ACCEPTED;
  351. $reason = array();
  352. if ( !$http->hasSessionVariable( 'BrowseParameters' ) )
  353. {
  354. // No validation when deleting to avoid blocking deletion of invalid items
  355. if (
  356. $http->hasPostVariable( 'DeleteApproveUserIDArray_' . $workflowEvent->attribute( 'id' ) ) ||
  357. $http->hasPostVariable( 'DeleteApproveGroupIDArray_' . $workflowEvent->attribute( 'id' ) )
  358. )
  359. {
  360. return eZInputValidator::STATE_ACCEPTED;
  361. }
  362. // check approve-users
  363. $approversIDs = array_unique( $this->attributeDecoder( $workflowEvent, 'approve_users' ) );
  364. if ( is_array( $approversIDs ) and
  365. count( $approversIDs ) > 0 )
  366. {
  367. $returnState = eZApproveType::validateUserIDList( $approversIDs, $reason );
  368. }
  369. else
  370. $returnState = false;
  371. if ( $returnState != eZInputValidator::STATE_INVALID )
  372. {
  373. // check approve-groups
  374. $userGroupIDList = array_unique( $this->attributeDecoder( $workflowEvent, 'approve_groups' ) );
  375. if ( is_array( $userGroupIDList ) and
  376. count( $userGroupIDList ) > 0 )
  377. {
  378. $returnState = eZApproveType::validateGroupIDList( $userGroupIDList, $reason );
  379. }
  380. else if ( $returnState === false )
  381. {
  382. // if no one user or user-group was passed as approvers
  383. $returnState = eZInputValidator::STATE_INVALID;
  384. $reason[ 'text' ] = "There must be passed at least one valid user or user group who approves content for the event.";
  385. }
  386. // check excluded-users
  387. /*
  388. if ( $returnState != eZInputValidator::STATE_INVALID )
  389. {
  390. // TODO:
  391. // ....
  392. }
  393. */
  394. // check excluded-groups
  395. if ( $returnState != eZInputValidator::STATE_INVALID )
  396. {
  397. $userGroupIDList = array_unique( $this->attributeDecoder( $workflowEvent, 'selected_usergroups' ) );
  398. if ( is_array( $userGroupIDList ) and
  399. count( $userGroupIDList ) > 0 )
  400. {
  401. $returnState = eZApproveType::validateGroupIDList( $userGroupIDList, $reason );
  402. }
  403. }
  404. }
  405. }
  406. else
  407. {
  408. $browseParameters = $http->sessionVariable( 'BrowseParameters' );
  409. if ( isset( $browseParameters['custom_action_data'] ) )
  410. {
  411. $customData = $browseParameters['custom_action_data'];
  412. if ( isset( $customData['event_id'] ) and
  413. $customData['event_id'] == $workflowEvent->attribute( 'id' ) )
  414. {
  415. if ( !$http->hasPostVariable( 'BrowseCancelButton' ) and
  416. $http->hasPostVariable( 'SelectedObjectIDArray' ) )
  417. {
  418. $objectIDArray = $http->postVariable( 'SelectedObjectIDArray' );
  419. if ( is_array( $objectIDArray ) and
  420. count( $objectIDArray ) > 0 )
  421. {
  422. switch( $customData['browse_action'] )
  423. {
  424. case "AddApproveUsers":
  425. {
  426. $returnState = eZApproveType::validateUserIDList( $objectIDArray, $reason );
  427. } break;
  428. case 'AddApproveGroups':
  429. case 'AddExcludeUser':
  430. {
  431. $returnState = eZApproveType::validateGroupIDList( $objectIDArray, $reason );
  432. } break;
  433. case 'AddExcludedGroups':
  434. {
  435. // TODO:
  436. // .....
  437. } break;
  438. }
  439. }
  440. }
  441. }
  442. }
  443. }
  444. if ( $returnState == eZInputValidator::STATE_INVALID )
  445. {
  446. $validation[ 'processed' ] = true;
  447. $validation[ 'events' ][] = array( 'id' => $workflowEvent->attribute( 'id' ),
  448. 'placement' => $workflowEvent->attribute( 'placement' ),
  449. 'workflow_type' => &$this,
  450. 'reason' => $reason );
  451. }
  452. return $returnState;
  453. }
  454. function fetchHTTPInput( $http, $base, $event )
  455. {
  456. $sectionsVar = $base . "_event_ezapprove_section_" . $event->attribute( "id" );
  457. if ( $http->hasPostVariable( $sectionsVar ) )
  458. {
  459. $sectionsArray = $http->postVariable( $sectionsVar );
  460. if ( in_array( '-1', $sectionsArray ) )
  461. {
  462. $sectionsArray = array( -1 );
  463. }
  464. $sectionsString = implode( ',', $sectionsArray );
  465. $event->setAttribute( "data_text1", $sectionsString );
  466. }
  467. $languageVar = $base . "_event_ezapprove_languages_" . $event->attribute( "id" );
  468. if ( $http->hasPostVariable( $languageVar ) )
  469. {
  470. $languageArray = $http->postVariable( $languageVar );
  471. if ( in_array( '-1', $languageArray ) )
  472. {
  473. $languageArray = array();
  474. }
  475. $languageMask = 0;
  476. foreach ( $languageArray as $languageID )
  477. {
  478. $languageMask |= $languageID;
  479. }
  480. $event->setAttribute( "data_int2", $languageMask );
  481. }
  482. $versionOptionVar = $base . "_event_ezapprove_version_option_" . $event->attribute( "id" );
  483. if ( $http->hasPostVariable( $versionOptionVar ) )
  484. {
  485. $versionOptionArray = $http->postVariable( $versionOptionVar );
  486. $versionOption = 0;
  487. if ( is_array( $versionOptionArray ) )
  488. {
  489. foreach ( $versionOptionArray as $vv )
  490. {
  491. $versionOption = $versionOption | $vv;
  492. }
  493. }
  494. $versionOption = $versionOption & eZApproveType::VERSION_OPTION_ALL;
  495. $event->setAttribute( 'data_int3', $versionOption );
  496. }
  497. if ( $http->hasSessionVariable( 'BrowseParameters' ) )
  498. {
  499. $browseParameters = $http->sessionVariable( 'BrowseParameters' );
  500. if ( isset( $browseParameters['custom_action_data'] ) )
  501. {
  502. $customData = $browseParameters['custom_action_data'];
  503. if ( isset( $customData['event_id'] ) &&
  504. $customData['event_id'] == $event->attribute( 'id' ) )
  505. {
  506. if ( !$http->hasPostVariable( 'BrowseCancelButton' ) and
  507. $http->hasPostVariable( 'SelectedObjectIDArray' ) )
  508. {
  509. $objectIDArray = $http->postVariable( 'SelectedObjectIDArray' );
  510. if ( is_array( $objectIDArray ) and
  511. count( $objectIDArray ) > 0 )
  512. {
  513. switch( $customData['browse_action'] )
  514. {
  515. case 'AddApproveUsers':
  516. {
  517. foreach( $objectIDArray as $key => $userID )
  518. {
  519. if ( !eZUser::isUserObject( eZContentObject::fetch( $userID ) ) )
  520. {
  521. unset( $objectIDArray[$key] );
  522. }
  523. }
  524. $event->setAttribute( 'data_text3', implode( ',',
  525. array_unique( array_merge( $this->attributeDecoder( $event, 'approve_users' ),
  526. $objectIDArray ) ) ) );
  527. } break;
  528. case 'AddApproveGroups':
  529. {
  530. $event->setAttribute( 'data_text4', implode( ',',
  531. array_unique( array_merge( $this->attributeDecoder( $event, 'approve_groups' ),
  532. $objectIDArray ) ) ) );
  533. } break;
  534. case 'AddExcludeUser':
  535. {
  536. $event->setAttribute( 'data_text2', implode( ',',
  537. array_unique( array_merge( $this->attributeDecoder( $event, 'selected_usergroups' ),
  538. $objectIDArray ) ) ) );
  539. } break;
  540. case 'AddExcludedGroups':
  541. {
  542. // TODO:
  543. // .....
  544. } break;
  545. }
  546. }
  547. $http->removeSessionVariable( 'BrowseParameters' );
  548. }
  549. }
  550. }
  551. }
  552. }
  553. function createApproveCollaboration( $process, $event, $userID, $contentobjectID, $contentobjectVersion, $editors )
  554. {
  555. if ( $editors === null )
  556. return false;
  557. $authorID = $userID;
  558. $collaborationItem = eZApproveCollaborationHandler::createApproval( $contentobjectID, $contentobjectVersion,
  559. $authorID, $editors );
  560. $db = eZDB::instance();
  561. $db->query( 'INSERT INTO ezapprove_items( workflow_process_id, collaboration_id )
  562. VALUES(' . $process->attribute( 'id' ) . ',' . $collaborationItem->attribute( 'id' ) . ' ) ' );
  563. }
  564. /*
  565. */
  566. function customWorkflowEventHTTPAction( $http, $action, $workflowEvent )
  567. {
  568. $eventID = $workflowEvent->attribute( "id" );
  569. $module =& $GLOBALS['eZRequestedModule'];
  570. //$siteIni = eZINI::instance();
  571. switch ( $action )
  572. {
  573. case 'AddApproveUsers' :
  574. {
  575. $userClassNames = eZUser::fetchUserClassNames();
  576. if ( count( $userClassNames ) > 0 )
  577. {
  578. eZContentBrowse::browse( array( 'action_name' => 'SelectMultipleUsers',
  579. 'from_page' => '/workflow/edit/' . $workflowEvent->attribute( 'workflow_id' ),
  580. 'custom_action_data' => array( 'event_id' => $eventID,
  581. 'browse_action' => $action ),
  582. 'class_array' => $userClassNames ),
  583. $module );
  584. }
  585. } break;
  586. case 'RemoveApproveUsers' :
  587. {
  588. if ( $http->hasPostVariable( 'DeleteApproveUserIDArray_' . $eventID ) )
  589. {
  590. $workflowEvent->setAttribute( 'data_text3', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'approve_users' ),
  591. $http->postVariable( 'DeleteApproveUserIDArray_' . $eventID ) ) ) );
  592. }
  593. } break;
  594. case 'AddApproveGroups' :
  595. case 'AddExcludeUser' :
  596. {
  597. $groupClassNames = eZUser::fetchUserGroupClassNames();
  598. if ( count( $groupClassNames ) > 0 )
  599. {
  600. eZContentBrowse::browse( array( 'action_name' => 'SelectMultipleUsers',
  601. 'from_page' => '/workflow/edit/' . $workflowEvent->attribute( 'workflow_id' ),
  602. 'custom_action_data' => array( 'event_id' => $eventID,
  603. 'browse_action' => $action ),
  604. 'class_array' => $groupClassNames ),
  605. $module );
  606. }
  607. } break;
  608. case 'RemoveApproveGroups' :
  609. {
  610. if ( $http->hasPostVariable( 'DeleteApproveGroupIDArray_' . $eventID ) )
  611. {
  612. $workflowEvent->setAttribute( 'data_text4', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'approve_groups' ),
  613. $http->postVariable( 'DeleteApproveGroupIDArray_' . $eventID ) ) ) );
  614. }
  615. } break;
  616. case 'RemoveExcludeUser' :
  617. {
  618. if ( $http->hasPostVariable( 'DeleteExcludeUserIDArray_' . $eventID ) )
  619. {
  620. $workflowEvent->setAttribute( 'data_text2', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'selected_usergroups' ),
  621. $http->postVariable( 'DeleteExcludeUserIDArray_' . $eventID ) ) ) );
  622. }
  623. } break;
  624. case 'AddExcludedGroups' :
  625. {
  626. // TODO:
  627. // .....
  628. } break;
  629. case 'RemoveExcludedGroups' :
  630. {
  631. // TODO:
  632. // .....
  633. } break;
  634. }
  635. }
  636. /*
  637. */
  638. function cleanupAfterRemoving( $attr = array() )
  639. {
  640. foreach ( array_keys( $attr ) as $attrKey )
  641. {
  642. switch ( $attrKey )
  643. {
  644. case 'DeleteContentObject':
  645. {
  646. $contentObjectID = (int)$attr[ $attrKey ];
  647. $db = eZDB::instance();
  648. // Cleanup "User who approves content"
  649. $db->query( "UPDATE ezworkflow_event
  650. SET data_int1 = '0'
  651. WHERE workflow_type_string = '{$this->TypeString}' AND
  652. data_int1 = $contentObjectID" );
  653. // Cleanup "Excluded user groups"
  654. $excludedGroupsID = $db->arrayQuery( "SELECT data_text2, id
  655. FROM ezworkflow_event
  656. WHERE workflow_type_string = '{$this->TypeString}' AND
  657. data_text2 like '%$contentObjectID%'" );
  658. if ( is_array( $excludedGroupsID ) )
  659. {
  660. foreach ( $excludedGroupsID as $groupID )
  661. {
  662. // $IDArray will contain IDs of "Excluded user groups"
  663. $IDArray = explode( ',', $groupID[ 'data_text2' ] );
  664. // $newIDArray will contain array without $contentObjectID
  665. $newIDArray = array_filter( $IDArray, function ( $v ) use ( $contentObjectID ) {
  666. return $v != $contentObjectID;
  667. });
  668. $newValues = $db->escapeString( implode( ',', $newIDArray ) );
  669. $db->query( "UPDATE ezworkflow_event
  670. SET data_text2 = '$newValues'
  671. WHERE id = {$groupID['id']}" );
  672. }
  673. }
  674. } break;
  675. }
  676. }
  677. }
  678. function checkApproveCollaboration( $process, $event )
  679. {
  680. $db = eZDB::instance();
  681. $taskResult = $db->arrayQuery( 'select workflow_process_id, collaboration_id from ezapprove_items where workflow_process_id = ' . $process->attribute( 'id' ) );
  682. $collaborationID = $taskResult[0]['collaboration_id'];
  683. $collaborationItem = eZCollaborationItem::fetch( $collaborationID );
  684. $contentObjectVersion = eZApproveCollaborationHandler::contentObjectVersion( $collaborationItem );
  685. $approvalStatus = eZApproveCollaborationHandler::checkApproval( $collaborationID );
  686. if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_WAITING )
  687. {
  688. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval still waiting' );
  689. return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
  690. }
  691. else if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_ACCEPTED )
  692. {
  693. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval was accepted' );
  694. $status = eZWorkflowType::STATUS_ACCEPTED;
  695. }
  696. else if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_DENIED or
  697. $approvalStatus == eZApproveCollaborationHandler::STATUS_DEFERRED )
  698. {
  699. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval was denied' );
  700. $contentObjectVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
  701. $status = eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
  702. }
  703. else
  704. {
  705. eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, "approval unknown status '$approvalStatus'" );
  706. $contentObjectVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_REJECTED );
  707. $status = eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
  708. }
  709. $contentObjectVersion->sync();
  710. if ( $approvalStatus != eZApproveCollaborationHandler::STATUS_DEFERRED )
  711. $db->query( 'DELETE FROM ezapprove_items WHERE workflow_process_id = ' . $process->attribute( 'id' ) );
  712. return $status;
  713. }
  714. }
  715. eZWorkflowEventType::registerEventType( eZApproveType::WORKFLOW_TYPE_STRING, "eZApproveType" );
  716. ?>