workflow_vbo.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Provide workflow actions for VBO.
  5. * Split out from workflow_actions.
  6. */
  7. /**
  8. * Implements hook_action_info().
  9. */
  10. function workflow_vbo_action_info() {
  11. return array(
  12. 'workflow_vbo_next_state_action' => array(
  13. 'type' => 'node',
  14. 'label' => t('Change workflow state of post to next state'),
  15. 'configurable' => FALSE,
  16. 'triggers' => array('any'),
  17. ),
  18. 'workflow_vbo_given_state_action' => array(
  19. 'type' => 'node',
  20. 'label' => t('Change workflow state of post to new state'),
  21. 'configurable' => TRUE,
  22. 'triggers' => array('any'),
  23. ),
  24. );
  25. }
  26. /**
  27. * Implements a Drupal action. Move a node to the next state in the workfow.
  28. */
  29. function workflow_vbo_next_state_action($node, $context) {
  30. // If this action is being fired because it's attached to a workflow transition
  31. // then the node's new state (now its current state) should be in $node->workflow
  32. // because that is where the value from the workflow form field is stored;
  33. // otherwise the current state is placed in $node->workflow by our nodeapi load.
  34. if (!isset($node->nid)) {
  35. watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
  36. return;
  37. }
  38. if (!isset($node->workflow)) {
  39. watchdog('workflow_vbo', 'Unable to get current workflow state of node %nid.',
  40. array('%nid' => $node->nid));
  41. return;
  42. }
  43. $current_state = $node->workflow;
  44. $new_state = $current_state;
  45. // Get the node's new state.
  46. $choices = workflow_field_choices($node);
  47. foreach ($choices as $sid => $name) {
  48. if (isset($flag)) {
  49. $new_state = $sid;
  50. $new_state_name = $name;
  51. break;
  52. }
  53. if ($sid == $current_state) {
  54. $flag = TRUE;
  55. }
  56. }
  57. // Fire the transition.
  58. workflow_execute_transition($node, $new_state);
  59. }
  60. /**
  61. * Implements a Drupal action. Move a node to a specified state.
  62. */
  63. function workflow_vbo_given_state_action($node, $context) {
  64. global $user;
  65. if (!isset($node->nid)) {
  66. watchdog('workflow_vbo', 'Unable to get current node id state of node - node is not yet saved.');
  67. return;
  68. }
  69. $comment = t($context['workflow_comment'], array(
  70. '%title' => check_plain($node->title),
  71. '%state' => check_plain($context['state_name']),
  72. '%user' => theme('username', array('account' => $user)),
  73. ));
  74. workflow_execute_transition($node, $context['target_sid'], $comment, $context['force']);
  75. }
  76. /**
  77. * Configuration form for "Change workflow state of post to new state" action.
  78. *
  79. * @see workflow_vbo_given_state_action()
  80. */
  81. function workflow_vbo_given_state_action_form($context) {
  82. $previous_workflow = '';
  83. $options = array();
  84. // Get all states, only where active.
  85. foreach (workflow_get_workflow_states(array('status' => 1)) as $data) {
  86. $options[$data->name][$data->sid] = check_plain($data->state);
  87. }
  88. $form['target_sid'] = array(
  89. '#type' => 'select',
  90. '#title' => t('Target state'),
  91. '#description' => t('Please select that state that should be assigned when this action runs.'),
  92. '#default_value' => isset($context['target_sid']) ? $context['target_sid'] : '',
  93. '#options' => $options,
  94. );
  95. $form['force'] = array(
  96. '#type' => 'checkbox',
  97. '#title' => t('Force transition'),
  98. '#description' => t('If this box is checked, the new state will be assigned even if workflow ' .
  99. 'permissions disallow it.'),
  100. '#default_value' => isset($context['force']) ? $context['force'] : '',
  101. );
  102. $form['workflow_comment'] = array(
  103. '#type' => 'textfield',
  104. '#title' => t('Message'),
  105. '#description' => t('This message will be written into the workflow history log when the action ' .
  106. 'runs. You may include the following variables: %state, %title, %user'),
  107. '#default_value' => isset($context['workflow_history']) ? $context['workflow_history'] : t('Action set %title to %state by %user.'),
  108. );
  109. return $form;
  110. }
  111. /**
  112. * Submit handler for "Change workflow state of post to new state" action
  113. * configuration form.
  114. *
  115. * @see workflow_vbo_given_state_action_form()
  116. */
  117. function workflow_vbo_given_state_action_submit($form_id, $form_state) {
  118. if ($state = workflow_get_workflow_states_by_sid($form_state['values']['target_sid'])) {
  119. return array(
  120. 'target_sid' => $form_state['values']['target_sid'],
  121. 'state_name' => check_plain($state->state),
  122. 'force' => $form_state['values']['force'],
  123. 'workflow_comment' => $form_state['values']['workflow_comment'],
  124. );
  125. }
  126. }