workflow_revert.module 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @file
  4. * Adds an 'Revert' link to the first workflow history row.
  5. */
  6. /**
  7. * Implements hook_permission().
  8. */
  9. function workflow_revert_permission() {
  10. return array(
  11. 'revert workflow' => array(
  12. 'title' => t('Revert workflow'),
  13. 'description' => t('Allow user to revert workflow state.'),
  14. ),
  15. );
  16. }
  17. /**
  18. * Implements hook_menu().
  19. */
  20. function workflow_revert_menu() {
  21. $items = array();
  22. $items['workflow_revert'] = array(
  23. 'title' => 'Workflow Undo',
  24. 'access arguments' => array('revert workflow'),
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('workflow_revert_form'),
  27. 'type' => MENU_CALLBACK,
  28. );
  29. return $items;
  30. }
  31. /**
  32. * Menu callback to do the revert function.
  33. */
  34. function workflow_revert_form($form, $form_state, $nid = NULL, $sid = NULL) {
  35. $args = array('#nid' => $nid, '#sid' => $sid);
  36. if (drupal_valid_token($_GET['token'], 'workflow_revert ' . $sid)) {
  37. $states = workflow_get_workflow_states_all();
  38. $node = node_load($nid);
  39. $args['#node'] = $node;
  40. $question = t('Are you sure you want to revert %title to the "@state" state?', array(
  41. '@state' => $states[$sid],
  42. '%title' => $node->title,
  43. ));
  44. return confirm_form($args,
  45. $question,
  46. "node/$nid",
  47. t('The worflow state will be changed.')
  48. );
  49. }
  50. else {
  51. watchdog('workflow_revert', 'Invalid token', array(), WATCHDOG_ERROR);
  52. drupal_set_message(t('Invalid token. Your information has been recorded.'), 'error');
  53. drupal_goto("node/$nid");
  54. }
  55. }
  56. function workflow_revert_form_submit($form, $form_state) {
  57. $node = $form['#node'];
  58. $comment = t('State reverted');
  59. $sid = $form['#sid'];
  60. // Force the transition because it's probably not valid.
  61. workflow_execute_transition($node, $sid, $comment, TRUE);
  62. drupal_set_message(t('Workflow reverted.', array()));
  63. drupal_goto('node/' . $form['#nid'] . '/workflow');
  64. }
  65. /**
  66. * Implements hook_workflow_history_alter().
  67. * Add an 'undo' operation for the most recent history change.
  68. *
  69. * @param $variables
  70. * The current workflow history information as an array.
  71. * 'old_sid' - The state ID of the previous state.
  72. * 'old_state_name' - The state name of the previous state.
  73. * 'sid' - The state ID of the current state.
  74. * 'state_name' - The state name of the current state.
  75. * 'history' - The row from the workflow_node_history table.
  76. *
  77. * If you want to add additional data, place it in the 'extra' value.
  78. */
  79. function workflow_revert_workflow_history_alter(&$variables) {
  80. static $first = TRUE;
  81. // Only mark the first row.
  82. if ($first) {
  83. // Let's ask other modules if the reversion is allowed.
  84. $node = node_load($variables['history']->nid);
  85. $result = module_invoke_all('workflow', 'transition permitted', $variables['sid'], $variables['old_sid'], $node);
  86. // Did anybody veto this choice?
  87. if (!in_array(FALSE, $result)) {
  88. // If not vetoed, mark it.
  89. $options = array('query' => array('token' => drupal_get_token('workflow_revert ' . $variables['old_sid'])));
  90. $path = 'workflow_revert/' . $variables['history']->nid . '/' . $variables['old_sid'];
  91. $variables['extra'] = l('Revert state change', $path, $options);
  92. }
  93. // That was your one chance...
  94. $first = FALSE;
  95. }
  96. }