PageRenderTime 60ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/kernel/workflow/edit.php

https://github.com/alexsebbane/ezpublish
PHP | 371 lines | 322 code | 30 blank | 19 comment | 25 complexity | cdab577dcf474c292e9b9bfd5819ac18 MD5 | raw file
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 1999-2011 eZ Systems AS. All rights reserved.
  4. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  5. * @version //autogentag//
  6. * @package kernel
  7. */
  8. $Module = $Params['Module'];
  9. $WorkflowID = ( isset( $Params["WorkflowID"] ) ) ? $Params["WorkflowID"] : false;
  10. switch ( $Params["FunctionName"] )
  11. {
  12. case "up":
  13. case "down":
  14. {
  15. if ( $WorkflowID !== false )
  16. {
  17. if ( isset( $Params["EventID"] ) )
  18. {
  19. $event = eZWorkflowEvent::fetch( $Params["EventID"], true, 1,
  20. array( "workflow_id", "version", "placement" ) );
  21. $event->move( $Params["FunctionName"] == "up" ? false : true );
  22. $Module->redirectTo( $Module->functionURI( 'edit' ) . '/' . $WorkflowID );
  23. return;
  24. }
  25. else
  26. {
  27. eZDebug::writeError( "Missing parameter EventID for function: " . $params["Function"] );
  28. $Module->setExitStatus( eZModule::STATUS_FAILED );
  29. return;
  30. }
  31. }
  32. else
  33. {
  34. eZDebug::writeError( "Missing parameter WorkfowID for function: " . $params["Function"] );
  35. $Module->setExitStatus( eZModule::STATUS_FAILED );
  36. return;
  37. }
  38. } break;
  39. case "edit":
  40. {
  41. } break;
  42. default:
  43. {
  44. eZDebug::writeError( "Undefined function: " . $params["Function"] );
  45. $Module->setExitStatus( eZModule::STATUS_FAILED );
  46. return;
  47. }
  48. }
  49. $GroupID = ( isset( $Params["GroupID"] ) ) ? $Params["GroupID"] : false;
  50. $GroupName = ( isset( $Params["GroupName"] ) ) ? $Params["GroupName"] : false;
  51. if ( is_numeric( $WorkflowID ) )
  52. {
  53. // try to fetch temporary version of workflow
  54. $workflow = eZWorkflow::fetch( $WorkflowID, true, 1 );
  55. // If temporary version does not exist fetch the current
  56. if ( !is_object( $workflow ) )
  57. {
  58. $workflow = eZWorkflow::fetch( $WorkflowID, true, 0 );
  59. if ( is_object( $workflow ) )
  60. {
  61. $workflowGroups = eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 0, true );
  62. $db = eZDB::instance();
  63. $db->begin();
  64. foreach ( $workflowGroups as $workflowGroup )
  65. {
  66. $groupID = $workflowGroup->attribute( "group_id" );
  67. $groupName = $workflowGroup->attribute( "group_name" );
  68. $ingroup = eZWorkflowGroupLink::create( $WorkflowID, 1, $groupID, $groupName );
  69. $ingroup->store();
  70. }
  71. $db->commit();
  72. }
  73. else
  74. {
  75. eZDebug::writeError( "Cannot fetch workflow with WorkfowID = " . $WorkflowID );
  76. $Module->setExitStatus( eZModule::STATUS_FAILED );
  77. return;
  78. }
  79. }
  80. }
  81. else
  82. {
  83. // if WorkflowID was not given then create new workflow
  84. $user = eZUser::currentUser();
  85. $user_id = $user->attribute( "contentobject_id" );
  86. $workflow = eZWorkflow::create( $user_id );
  87. $workflowCount = eZWorkflow::fetchListCount();
  88. ++$workflowCount;
  89. $workflow->setAttribute( "name", ezpI18n::tr( 'kernel/workflow/edit', "New Workflow" ) . "$workflowCount" );
  90. $db = eZDB::instance();
  91. $db->begin();
  92. $workflow->store();
  93. $WorkflowID = $workflow->attribute( "id" );
  94. $WorkflowVersion = $workflow->attribute( "version" );
  95. $ingroup = eZWorkflowGroupLink::create( $WorkflowID, $WorkflowVersion, $GroupID, $GroupName );
  96. $ingroup->store();
  97. $db->commit();
  98. return $Module->redirectTo( $Module->functionURI( 'edit' ) . '/' . $WorkflowID . '/' . $GroupID );
  99. }
  100. $http = eZHTTPTool::instance();
  101. $WorkflowVersion = $workflow->attribute( "version" );
  102. if ( $http->hasPostVariable( "DiscardButton" ) )
  103. {
  104. $workflow->setVersion( 1 );
  105. $db = eZDB::instance();
  106. $db->begin();
  107. $workflow->removeThis( true );
  108. eZWorkflowGroupLink::removeWorkflowMembers( $WorkflowID, $WorkflowVersion );
  109. $db->commit();
  110. $workflowGroups= eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 0, true );
  111. $groupID = false;
  112. if ( count( $workflowGroups ) > 0 )
  113. $groupID = $workflowGroups[0]->attribute( 'group_id' );
  114. if ( !$groupID )
  115. $groupID = $GroupID;
  116. if ( $groupID )
  117. return $Module->redirectToView( 'workflowlist', array( $groupID ) );
  118. else
  119. return $Module->redirectToView( 'grouplist' );
  120. }
  121. $validation = array( 'processed' => false,
  122. 'groups' => array(),
  123. 'attributes' => array(),
  124. 'events' => array() );
  125. if ( $http->hasPostVariable( "AddGroupButton" ) && $http->hasPostVariable( "Workflow_group") )
  126. {
  127. $selectedGroup = $http->postVariable( "Workflow_group" );
  128. eZWorkflowFunctions::addGroup( $WorkflowID, $WorkflowVersion, $selectedGroup );
  129. }
  130. if ( $http->hasPostVariable( "DeleteGroupButton" ) && $http->hasPostVariable( "group_id_checked" ) )
  131. {
  132. $selectedGroup = $http->postVariable( "group_id_checked" );
  133. if ( !eZWorkflowFunctions::removeGroup( $WorkflowID, $WorkflowVersion, $selectedGroup ) )
  134. {
  135. $validation['groups'][] = array( 'text' => ezpI18n::tr( 'kernel/workflow', 'You have to have at least one group that the workflow belongs to!' ) );
  136. $validation['processed'] = true;
  137. }
  138. }
  139. // Fetch events and types
  140. $event_list = $workflow->fetchEvents();
  141. $type_list = eZWorkflowType::fetchRegisteredTypes();
  142. if ( $http->hasPostVariable( "DeleteButton" ) )
  143. {
  144. $db = eZDB::instance();
  145. $db->begin();
  146. if ( eZHTTPPersistence::splitSelected( "WorkflowEvent", $event_list,
  147. $http, "id",
  148. $keepers, $rejects ) )
  149. {
  150. $event_list = $keepers;
  151. foreach ( $rejects as $reject )
  152. {
  153. $reject->remove();
  154. }
  155. }
  156. $db->commit();
  157. $event_list = $workflow->fetchEvents();
  158. }
  159. // Validate input
  160. $canStore = true;
  161. $requireFixup = false;
  162. foreach( $event_list as $event )
  163. {
  164. $eventType = $event->eventType();
  165. if ( !$eventType instanceof eZWorkflowEventType )
  166. {
  167. // Can't find eventype. Most likely deactivated while workflow has not been cleant up
  168. eZDebug::writeError( "Couldn't load eventype '{$event->attribute( 'workflow_type_string' )}' for workflow. Is it activated ?", 'eZWorkflow edit' );
  169. continue;
  170. }
  171. $status = $eventType->validateHTTPInput( $http, "WorkflowEvent", $event, $validation );
  172. if ( $status == eZInputValidator::STATE_INTERMEDIATE )
  173. $requireFixup = true;
  174. else if ( $status == eZInputValidator::STATE_INVALID )
  175. $canStore = false;
  176. }
  177. // Fixup input
  178. if ( $requireFixup )
  179. {
  180. foreach( $event_list as $event )
  181. {
  182. $eventType = $event->eventType();
  183. if ( !$eventType instanceof eZWorkflowEventType )
  184. {
  185. eZDebug::writeError( "Couldn't load eventype '{$event->attribute( 'workflow_type_string' )}' for workflow. Is it activated ?" );
  186. continue;
  187. }
  188. $status = $eventType->fixupHTTPInput( $http, "WorkflowEvent", $event );
  189. }
  190. }
  191. $cur_type = 0;
  192. // Apply HTTP POST variables
  193. eZHTTPPersistence::fetch( "WorkflowEvent", eZWorkflowEvent::definition(),
  194. $event_list, $http, true );
  195. eZHTTPPersistence::fetch( "Workflow", eZWorkflow::definition(),
  196. $workflow, $http, false );
  197. if ( $http->hasPostVariable( "WorkflowTypeString" ) )
  198. $cur_type = $http->postVariable( "WorkflowTypeString" );
  199. // set temporary version to edited workflow
  200. $workflow->setVersion( 1, $event_list );
  201. // Set new modification date
  202. $date_time = time();
  203. $workflow->setAttribute( "modified", $date_time );
  204. $user = eZUser::currentUser();
  205. $user_id = $user->attribute( "contentobject_id" );
  206. $workflow->setAttribute( "modifier_id", $user_id );
  207. /********** Custom Action Code Start ***************/
  208. $customAction = false;
  209. $customActionAttributeID = null;
  210. // Check for custom actions
  211. if ( $http->hasPostVariable( "CustomActionButton" ) )
  212. {
  213. $customActionArray = $http->postVariable( "CustomActionButton" );
  214. $customActionString = key( $customActionArray );
  215. $customActionAttributeID = preg_match( "#^([0-9]+)_(.*)$#", $customActionString, $matchArray );
  216. $customActionAttributeID = $matchArray[1];
  217. $customAction = $matchArray[2];
  218. }
  219. /********** Custom Action Code End ***************/
  220. // Fetch HTTP input
  221. foreach( $event_list as $event )
  222. {
  223. $eventType = $event->eventType();
  224. if ( !$eventType instanceof eZWorkflowEventType )
  225. {
  226. eZDebug::writeError( "Couldn't load eventype '{$event->attribute( 'workflow_type_string' )}' for workflow. Is it activated ?" );
  227. continue;
  228. }
  229. $eventType->fetchHTTPInput( $http, "WorkflowEvent", $event );
  230. if ( $customActionAttributeID == $event->attribute( "id" ) )
  231. {
  232. $event->customHTTPAction( $http, $customAction );
  233. }
  234. }
  235. if ( $http->hasPostVariable( "StoreButton" ) and $canStore )
  236. {
  237. // Discard existing events, workflow version 1 and store version 0
  238. $db = eZDB::instance();
  239. $db->begin();
  240. $workflow->store( $event_list ); // store changes.
  241. // Remove old version 0 first
  242. eZWorkflowGroupLink::removeWorkflowMembers( $WorkflowID, 0 );
  243. $workflowgroups = eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 1 );
  244. foreach( $workflowgroups as $workflowgroup )
  245. {
  246. $workflowgroup->setAttribute("workflow_version", 0 );
  247. $workflowgroup->store();
  248. }
  249. // Remove version 1
  250. eZWorkflowGroupLink::removeWorkflowMembers( $WorkflowID, 1 );
  251. eZWorkflow::removeEvents( false, $WorkflowID, 0 );
  252. $workflow->removeThis( true );
  253. $workflow->setVersion( 0, $event_list );
  254. $workflow->adjustEventPlacements( $event_list );
  255. // $workflow->store( $event_list );
  256. $workflow->storeDefined( $event_list );
  257. $workflow->cleanupWorkFlowProcess();
  258. $db->commit();
  259. $workflowGroups= eZWorkflowGroupLink::fetchGroupList( $WorkflowID, 0, true );
  260. $groupID = false;
  261. if ( count( $workflowGroups ) > 0 )
  262. $groupID = $workflowGroups[0]->attribute( 'group_id' );
  263. if ( $groupID )
  264. return $Module->redirectToView( 'workflowlist', array( $groupID ) );
  265. else
  266. return $Module->redirectToView( 'grouplist' );
  267. }
  268. // Remove events which are to be deleted
  269. else if ( $http->hasPostVariable( "DeleteButton" ) )
  270. {
  271. if ( $canStore )
  272. $workflow->store( $event_list );
  273. }
  274. // Add new workflow event
  275. else if ( $http->hasPostVariable( "NewButton" ) )
  276. {
  277. $new_event = eZWorkflowEvent::create( $WorkflowID, $cur_type );
  278. $new_event_type = $new_event->eventType();
  279. $db = eZDB::instance();
  280. $db->begin();
  281. if ($canStore)
  282. $workflow->store( $event_list );
  283. $new_event_type->initializeEvent( $new_event );
  284. $new_event->store();
  285. $db->commit();
  286. $event_list[] = $new_event;
  287. }
  288. else if ( $canStore )
  289. {
  290. $workflow->store( $event_list );
  291. }
  292. $Module->setTitle( ezpI18n::tr( 'kernel/workflow', 'Edit workflow' ) . ' ' . $workflow->attribute( "name" ) );
  293. // Template handling
  294. $tpl = eZTemplate::factory();
  295. $res = eZTemplateDesignResource::instance();
  296. $res->setKeys( array( array( "workflow", $workflow->attribute( "id" ) ) ) );
  297. if ( isset( $GLOBALS['eZWaitUntilDateSelectedClass'] ) )
  298. $tpl->setVariable( "selectedClass", $GLOBALS['eZWaitUntilDateSelectedClass'] );
  299. $tpl->setVariable( "http", $http );
  300. $tpl->setVariable( "can_store", $canStore );
  301. $tpl->setVariable( "require_fixup", $requireFixup );
  302. $tpl->setVariable( "module", $Module );
  303. $tpl->setVariable( "workflow", $workflow );
  304. $tpl->setVariable( "event_list", $event_list );
  305. $tpl->setVariable( "workflow_type_list", $type_list );
  306. $tpl->setVariable( "workflow_type", $cur_type );
  307. $tpl->setVariable( 'validation', $validation );
  308. if ( isset( $GroupID ) )
  309. {
  310. $tpl->setVariable( "group_id", $GroupID );
  311. }
  312. $Result = array();
  313. $Result['content'] = $tpl->fetch( "design:workflow/edit.tpl" );
  314. $Result['path'] = array( array( 'text' => ezpI18n::tr( 'kernel/workflow', 'Workflow' ),
  315. 'url' => false ),
  316. array( 'text' => ezpI18n::tr( 'kernel/workflow', 'Edit' ),
  317. 'url' => false ) );
  318. ?>