workflow_rules.rules.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the Workflow module
  5. */
  6. /**
  7. * Implements hook_rules_event_info().
  8. */
  9. function workflow_rules_rules_event_info() {
  10. $events = array(
  11. 'workflow_state_changed' => array(
  12. 'group' => t('Workflow'),
  13. 'label' => t('Workflow state has changed'),
  14. 'variables' => rules_events_node_variables(t('updated content'), TRUE),
  15. ),
  16. 'workflow_comment_added' => array(
  17. 'group' => t('Workflow'),
  18. 'label' => t('Workflow comment was added, but state did not change'),
  19. 'variables' => rules_events_node_variables(t('updated content'), TRUE),
  20. ),
  21. );
  22. return $events;
  23. }
  24. /**
  25. * Implements hook_rules_condition_info().
  26. */
  27. function workflow_rules_rules_condition_info() {
  28. return array(
  29. 'workflow_check_transition' => array(
  30. 'group' => t('Workflow'),
  31. 'label' => t('Check workflow transition'),
  32. 'parameter' => array(
  33. 'node' => array(
  34. 'type' => 'node',
  35. 'label' => t('Node'),
  36. 'description' => t('The node whose workflow state is being checked.'),
  37. ),
  38. 'old_state' => array(
  39. 'type' => 'list<integer>',
  40. 'label' => t('Old workflow state'),
  41. 'options list' => '_workflow_rules_condition_select',
  42. 'description' => t('The workflow state moved from.'),
  43. ),
  44. 'new_state' => array(
  45. 'type' => 'list<integer>',
  46. 'label' => t('New workflow state'),
  47. 'options list' => '_workflow_rules_condition_select',
  48. 'description' => t('The workflow state moved to.'),
  49. ),
  50. ),
  51. ),
  52. 'workflow_check_state' => array(
  53. 'group' => t('Workflow'),
  54. 'label' => t('Content has a workflow state'),
  55. 'parameter' => array(
  56. 'node' => array(
  57. 'type' => 'node',
  58. 'label' => t('Node'),
  59. 'description' => t('The node to compare the current workflow state of.'),
  60. ),
  61. 'workflow_state' => array(
  62. 'type' => 'list<integer>',
  63. 'label' => t('Compare workflow state'),
  64. 'options list' => '_workflow_rules_condition_select',
  65. 'description' => t('The possible workflow states to compare against.'),
  66. ),
  67. ),
  68. ),
  69. 'workflow_check_previous_state' => array(
  70. 'group' => t('Workflow'),
  71. 'label' => t('Content has a previous workflow state'),
  72. 'parameter' => array(
  73. 'node' => array(
  74. 'type' => 'node',
  75. 'label' => t('Node'),
  76. 'description' => t('The node to compare the previous workflow state of.'),
  77. ),
  78. 'workflow_state' => array(
  79. 'type' => 'list<integer>',
  80. 'label' => t('Compare workflow state'),
  81. 'options list' => '_workflow_rules_condition_select',
  82. 'description' => t('The possible workflow states to compare against.'),
  83. ),
  84. ),
  85. ),
  86. );
  87. }
  88. /**
  89. * Implements hook_rules_action_info().
  90. */
  91. function workflow_rules_rules_action_info() {
  92. return array(
  93. 'workflow_rules_set_state' => array(
  94. 'group' => t('Workflow'),
  95. 'label' => t('Set workflow state for content'),
  96. 'parameter' => array(
  97. 'node' => array(
  98. 'type' => 'node',
  99. 'label' => t('Node'),
  100. 'description' => t('The node to set the current workflow state of.'),
  101. // 'save' => TRUE,
  102. ),
  103. 'workflow_state' => array(
  104. 'type' => 'list<integer>',
  105. 'label' => t('New workflow state'),
  106. 'options list' => '_workflow_rules_action_select',
  107. 'description' => t('The workflow state to set (select only one).'),
  108. ),
  109. 'workflow_comment' => array(
  110. 'type' => 'text',
  111. 'label' => t('Workflow Comment'),
  112. 'description' => t('The workflow comment to set.'),
  113. 'optional' => TRUE,
  114. ),
  115. ),
  116. ),
  117. );
  118. }
  119. /**
  120. * Condition callback: gather all workflow states.
  121. */
  122. function _workflow_rules_condition_select() {
  123. $options['ANY'] = 'ANY state';
  124. foreach (workflow_get_workflows() as $workflow) {
  125. foreach (workflow_get_workflow_states_by_wid($workflow->wid) as $state) {
  126. $states[$state->sid] = check_plain($workflow->name) . ': ' . check_plain($state->state);
  127. }
  128. }
  129. $options = $options + $states;
  130. return $options;
  131. }
  132. /**
  133. * Condition callback: gather all workflow states.
  134. */
  135. function _workflow_rules_action_select() {
  136. foreach (workflow_get_workflows() as $workflow) {
  137. foreach (workflow_get_workflow_states_by_wid($workflow->wid) as $state) {
  138. $states[$state->sid] = check_plain($workflow->name) . ': ' . check_plain($state->state);
  139. }
  140. }
  141. return $states;
  142. }
  143. /**
  144. * Condition implementation: check state transition.
  145. */
  146. function workflow_check_transition($node, $old_states, $new_states) {
  147. $node_current_state = workflow_node_current_state($node);
  148. $node_old_state = workflow_node_previous_state($node);
  149. if (in_array('ANY', $old_states)) {
  150. if (in_array('ANY', $new_states)) {
  151. return TRUE;
  152. }
  153. return in_array($node_current_state, $new_states);
  154. }
  155. if (in_array('ANY', $new_states)) {
  156. return in_array($node_old_state, $old_states);
  157. }
  158. return in_array($node_old_state, $old_states) && in_array($node_current_state, $new_states);
  159. }
  160. /**
  161. * Condition implementation: check current state.
  162. */
  163. function workflow_check_state($node, $states) {
  164. $node_state = workflow_node_current_state($node);
  165. return workflow_check_given_state($node, $states, $node_state);
  166. }
  167. /**
  168. * Condition implementation: check previous state.
  169. */
  170. function workflow_check_previous_state($node, $states) {
  171. $node_state = workflow_node_previous_state($node);
  172. return workflow_check_given_state($node, $states, $node_state);
  173. }
  174. /**
  175. * Condition implementation helper function: check given state.
  176. */
  177. function workflow_check_given_state($node, $states, $node_state) {
  178. if (in_array('ANY', $states)) {
  179. return TRUE;
  180. }
  181. if (in_array($node_state, $states)) {
  182. return TRUE;
  183. }
  184. return FALSE;
  185. }
  186. /**
  187. * Action implementation: set current state, ignoring current user permissione.
  188. */
  189. function workflow_rules_set_state($node, $states, $comment = NULL) {
  190. // Select the last state on the list.
  191. $sid = array_pop($states);
  192. if (!empty($comment)) {
  193. $node->workflow_comment = $comment;
  194. }
  195. workflow_transition($node, $sid, TRUE);
  196. unset($node->workflow_comment);
  197. }