workflow_actions.module 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * @file
  4. * Enables actions to be fired upon a Workflow State change.
  5. *
  6. * N.B. This module's name is incorrect. It provides Triggers, not Actions.
  7. *
  8. * Why it's own module? Some sites prefer rules, some prefer actions,
  9. * all prefer a lower code footprint and better performance.
  10. * Additional credit to gcassie ( http://drupal.org/user/80260 ) for
  11. * the initial push to split actions out of core workflow.
  12. */
  13. /**
  14. * Implements hook_workflow_operations().
  15. */
  16. function workflow_actions_workflow_operations($op, $workflow = NULL, $state = NULL) {
  17. switch ($op) {
  18. case 'workflow':
  19. $actions = array();
  20. if ($workflow && $workflow->getStates()) {
  21. $wid = $workflow->getWorkflowId();
  22. $actions = array(
  23. 'workflow_overview_actions' => array(
  24. 'title' => t('Actions'),
  25. 'href' => "admin/structure/trigger/workflow/$wid",
  26. ),
  27. );
  28. }
  29. else {
  30. // Generate a dummy, if no states exist.
  31. $actions = array(
  32. 'workflow_overview_actions' => array(
  33. 'title' => '',
  34. 'href' => '',
  35. ),
  36. );
  37. }
  38. return $actions;
  39. }
  40. }
  41. /**
  42. * Implements hook_workflow().
  43. */
  44. function workflow_actions_workflow($op, $id, $new_sid, $entity, $force = FALSE, $entity_type = 'node', $field_name = '', $transition = NULL) {
  45. switch ($op) {
  46. case 'transition post':
  47. // Reminder: event 'transition post' does not occur for Workflow Field.
  48. _workflow_actions_do($transition);
  49. break;
  50. case 'transition delete':
  51. // @todo: implement delete triggers upon 'transition delete'.
  52. // The below code generates an error PDOException: SQLSTATE[42S22].
  53. // So, it is commented out as per d.o. #2200089. In 7.x-1.2, this trigger
  54. // was never invoked. So, 'delete trigger upon transition delete' is now
  55. // a feature request.
  56. // $tid = $id;
  57. // $actions = _workflow_actions_get_actions_by_tid($tid);
  58. // foreach ($actions as $aid) {
  59. // _workflow_actions_remove($tid, $aid);
  60. // }
  61. break;
  62. }
  63. }
  64. /**
  65. * Implements hook_entity_insert().
  66. *
  67. * Trigger the 'transition post' event for workflow_field.
  68. * Do this only for Trigger & Actions, since Rules has its own way.
  69. * And I ndo't like activating an extra hook for nothing.
  70. */
  71. function workflow_actions_entity_insert($entity, $type) {
  72. workflow_actions_entity_update($entity, $type);
  73. }
  74. /**
  75. * Implements hook_entity_update().
  76. *
  77. * @see WorkflowTransition->execute()
  78. */
  79. function workflow_actions_entity_update($entity, $entity_type) {
  80. // For workflow_field, the 'transition post' event is not triggered in
  81. // WorkflowTransition->execute(), since we are still IN a transition.
  82. // This is now triggered here.
  83. // (But without hook_workflow, since it crashes workflow_access.)
  84. // P.S. You should not mix workflow field and workflow node!!
  85. if (module_exists('workflowfield') && !module_exists('workflownode')) {
  86. if (isset($entity->workflow_transitions)) {
  87. foreach ($entity->workflow_transitions as &$transition) {
  88. // $transition->post_execute(); // equivalent with hook_entity_save().
  89. _workflow_actions_do($transition);
  90. }
  91. }
  92. }
  93. elseif (module_exists('workflownode')) {
  94. // This is already done in workflow_actions_workflow(). But we cannot move
  95. // that here, since node_save()/entity_save() isn't always triggered.
  96. }
  97. }
  98. /**
  99. * Implements hook_trigger_info().
  100. *
  101. * Expose each transition as a hook.
  102. */
  103. function workflow_actions_trigger_info() {
  104. static $pseudohooks = array();
  105. if ($pseudohooks) {
  106. return $pseudohooks;
  107. }
  108. // If we come from a specific workflow, only show triggers for that workflow.
  109. // Removed: it is confusing.
  110. $trigger_page = FALSE;
  111. $wid = 0;
  112. /*
  113. if (drupal_substr($_GET['q'], 0, 32) == 'admin/structure/trigger/workflow') {
  114. $trigger_page = TRUE;
  115. $wid = arg(4);
  116. }
  117. */
  118. $workflows = workflow_load_multiple($wid ? array($wid) : FALSE);
  119. // Get the mapping for Node API.
  120. // Build an array of Wid => array of types.
  121. $workflow_node_type_map = module_exists('workflownode') ? workflow_get_workflow_type_map() : array();
  122. foreach ($workflow_node_type_map as $type => $wid) {
  123. $group = 'workflow';
  124. $type_map[$wid][] = array('entity_type' => 'node', 'bundle' => $type, $group);
  125. }
  126. // Get the mapping for Field API.
  127. $fields = _workflow_info_fields($entity = NULL, $entity_type = '');
  128. foreach ($fields as $field_name => $field) {
  129. if ($field['type'] == 'workflow') {
  130. foreach ($field['bundles'] as $entity_type => $bundles) {
  131. foreach ($bundles as $bundle) {
  132. // Add the trigger to the approriate Tab on admin/structure/trigger.
  133. switch ($entity_type) {
  134. case 'node': $group = $entity_type; break;
  135. case 'taxonomy_term': $group = 'taxonomy'; break;
  136. default: $group = 'workflow'; break;
  137. }
  138. $type_map[$field['settings']['wid']][] = array(
  139. 'entity_type' => $entity_type,
  140. 'bundle' => $bundle,
  141. 'group' => $group,
  142. );
  143. }
  144. }
  145. }
  146. }
  147. // Initialize the Workflow tab on admin/structure/trigger/workflow.
  148. $pseudohooks['workflow'] = array();
  149. // Create a trigger for each possible combination.
  150. foreach ($workflows as $wid => $workflow) {
  151. $states = $workflow->getStates('CREATION');
  152. foreach ($workflow->getTransitions() as $config_transition) {
  153. $state = $states[$config_transition->sid];
  154. $target_state = $states[$config_transition->target_sid];
  155. if (!$state || !$target_state || !$state->isActive() || !$target_state->isActive()) {
  156. continue;
  157. }
  158. // Add hook for Node API.
  159. if (isset($type_map[$wid])) {
  160. $creation_flag = $state->isCreationState();
  161. foreach ($type_map[$wid] as $type_bundle) {
  162. // Add the trigger to the appropriate Tab on admin/structure/trigger.
  163. $group = isset($type_bundle['group']) ? $type_bundle['group'] : 'workflow';
  164. $label = t('When @entity_type %bundle moves %workflow from %state to %target_state',
  165. array(
  166. '@entity_type' => $type_bundle['entity_type'],
  167. '%bundle' => $type_bundle['bundle'],
  168. '%workflow' => $workflow->label(),
  169. '%state' => $state->label(),
  170. '%target_state' => $target_state->label(),
  171. )
  172. );
  173. $pseudohooks[$group]['workflow-' . $type_bundle['entity_type']
  174. . '-' . $type_bundle['bundle']
  175. . '-' . $config_transition->tid] = array(
  176. 'label' => $label,
  177. 'workflow_creation_state' => $creation_flag,
  178. );
  179. }
  180. }
  181. }
  182. }
  183. // $pseudohooks will not be set if no workflows have been assigned
  184. // to node types.
  185. if ($pseudohooks) {
  186. return $pseudohooks;
  187. }
  188. elseif ($trigger_page) {
  189. drupal_set_message(t('Either no transitions have been set up or this
  190. workflow has not yet been assigned to a content type. To enable the
  191. assignment of actions, edit the workflow to assign permissions for roles
  192. to do transitions. After that is completed, transitions will appear here
  193. and you will be able to assign actions to them.')
  194. );
  195. }
  196. else {
  197. return array();
  198. }
  199. }
  200. /**
  201. * Implements hook_drupal_alter().
  202. */
  203. function workflow_actions_action_info_alter(&$info) {
  204. $triggers = workflow_actions_trigger_info();
  205. if (empty($triggers)) {
  206. return;
  207. }
  208. // Loop through all available node actions and add them as triggers.
  209. foreach ($triggers as $groups) {
  210. foreach ($groups as $trigger_name => $data) {
  211. foreach (node_action_info() as $action => $data) {
  212. $info[$action]['triggers'][] = $trigger_name;
  213. }
  214. }
  215. }
  216. }
  217. /**
  218. * Helper function, that triggers actions.
  219. *
  220. * @param $transition.
  221. * A Workflow Transition object.
  222. */
  223. function _workflow_actions_do(&$transition) {
  224. $entity_type = $transition->entity_type;
  225. $entity = $transition->getEntity();
  226. $old_sid = $transition->old_sid;
  227. $new_sid = $transition->new_sid;
  228. // A transition occurred; fire off actions associated with this transition.
  229. $workflow = $transition->getWorkflow();
  230. $config_transitions = $workflow->getTransitionsBySidTargetSid($old_sid, $new_sid);
  231. $config_transition = reset($config_transitions);
  232. if ($config_transition) {
  233. list(, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  234. $hook = 'workflow-' . $entity_type . '-' . $entity_bundle . '-' . $config_transition->tid;
  235. $aids = trigger_get_assigned_actions($hook);
  236. if (!$aids) {
  237. // With 7x.-2.x, the pseudohooks have an extra 'entity_type' in the name.
  238. // This is to be backwards compatible with 7.x-1.2
  239. $hook = 'workflow-' . $entity_bundle . '-' . $config_transition->tid;
  240. $aids = trigger_get_assigned_actions($hook);
  241. }
  242. if ($aids && !isset($transition->workflow_actions_done[$hook])) {
  243. // Avoid multiple executions if the entity_save is repeated in an action.
  244. $transition->workflow_actions_done[$hook] = TRUE;
  245. $context = array(
  246. 'hook' => $hook,
  247. 'entity_type' => $entity_type,
  248. );
  249. // We need to get the expected object if the action's type is not 'node'.
  250. // We keep the object in $objects so we can reuse it if we have
  251. // multiple actions that make changes to an object.
  252. $objects = array();
  253. foreach ($aids as $aid => $action_info) {
  254. if ($action_info['type'] != 'node') {
  255. if (!isset($objects[$action_info['type']])) {
  256. $objects[$action_info['type']] = _trigger_normalize_node_context($action_info['type'], $entity);
  257. }
  258. // Since we know the node, we pass it along to the action.
  259. $context['node'] = $entity;
  260. $result = actions_do($aid, $objects[$action_info['type']], $context);
  261. }
  262. else {
  263. actions_do($aid, $entity, $context);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. /**
  270. * Remove an action assignment programmatically.
  271. *
  272. * Helpful when deleting a workflow.
  273. *
  274. * @param int $tid
  275. * Transition ID.
  276. * @param int $aid
  277. * Action ID.
  278. */
  279. function _workflow_actions_remove($tid, $aid) {
  280. foreach (_workflow_actions_get_trigger_assignments_by_aid($aid) as $data) {
  281. // Transition ID is the last part, e.g., foo-bar-1.
  282. $transition = array_pop(explode('-', $data->hook));
  283. if ($tid == $transition) {
  284. $hooks[] = $data->hook;
  285. }
  286. }
  287. foreach ($hooks as $hook) {
  288. _workflow_actions_delete_trigger_assignments_by_aid_op($aid, $hook);
  289. foreach (_workflow_actions_get_actions_by_aid($aid) as $action) {
  290. watchdog('workflow', 'Action %action has been unassigned.',
  291. array('%action' => $action->description));
  292. }
  293. }
  294. }
  295. /**
  296. * DB functions.
  297. */
  298. /**
  299. * Get all trigger assignments for workflow.
  300. */
  301. function _workflow_actions_get_trigger_assignments() {
  302. $results = db_query('SELECT hook FROM {trigger_assignments} WHERE hook = "workflow"');
  303. return $results->fetchAll();
  304. }
  305. /**
  306. * Get all trigger assignments for workflow and a given action.
  307. */
  308. function _workflow_actions_get_trigger_assignments_by_aid($aid) {
  309. $results = db_query('SELECT hook FROM {trigger_assignments} WHERE hook = "workflow" AND aid = ":aid"', array(':aid' => $aid));
  310. return $results->fetchAll();
  311. }
  312. /**
  313. * Delete assignments, by action and operation.
  314. */
  315. function _workflow_actions_delete_trigger_assignments_by_aid_op($aid, $op) {
  316. return db_delete('trigger_assignments')->condition('hook', 'workflow')->condition('hook', $op)->condition('aid', $aid)->execute();
  317. }
  318. /**
  319. * Get a specific action.
  320. */
  321. function _workflow_actions_get_actions_by_aid($aid) {
  322. $results = db_query('SELECT * FROM {actions} WHERE aid = ":aid"', array(':aid' => $aid));
  323. return $results->fetchAll();
  324. }
  325. /**
  326. * Gets the actions associated with a given transition.
  327. *
  328. * Array of action ids in the same format as _trigger_get_hook_aids().
  329. */
  330. function _workflow_actions_get_actions_by_tid($tid) {
  331. $aids = array();
  332. foreach (_workflow_actions_get_trigger_assignments() as $data) {
  333. // Transition ID is the last part, e.g., foo-bar-1.
  334. $transition = array_pop(explode('-', $data->hook));
  335. if ($tid == $transition) {
  336. // Specialized, TODO separate this SQL out later.
  337. $results = db_query('SELECT aa.aid, a.type FROM {trigger_assignments} aa
  338. LEFT JOIN {actions} a ON aa.aid = a.aid
  339. WHERE aa.hook = ":hook"
  340. ORDER BY weight', array(':hook' => $data->hook));
  341. foreach ($results as $action) {
  342. $aids[$action->aid]['type'] = $action->type;
  343. }
  344. }
  345. }
  346. return $aids;
  347. }