workflow_revert.pages.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @file
  4. * Contains 'workflow_revert' page.
  5. */
  6. /**
  7. * Menu callback to do the revert function.
  8. *
  9. * @todo: add support for Field in workflow_revert.
  10. */
  11. function workflow_revert_form($form, &$form_state, $entity_type, $id, $field_name, $previous_sid = NULL) {
  12. $entity = entity_load_single($entity_type, $id);
  13. $uri = entity_uri($entity_type, $entity);
  14. $return_uri = $uri['path'] . '/workflow';
  15. if (drupal_valid_token($_GET['token'], 'workflow_revert ' . $previous_sid)) {
  16. $state = workflow_state_load_single($previous_sid);
  17. $args['#previous_sid'] = $previous_sid;
  18. $args['#entity_id'] = $id;
  19. $args['#entity'] = $entity;
  20. $args['#entity_type'] = $entity_type;
  21. $args['#field_name'] = $field_name;
  22. $question = t('Are you sure you want to revert %title to the "@state" state?', array(
  23. '@state' => $state->label(),
  24. '%title' => entity_label($entity_type, $entity),
  25. )
  26. );
  27. return confirm_form($args,
  28. $question,
  29. $return_uri,
  30. t('The workflow state will be changed.')
  31. );
  32. }
  33. else {
  34. watchdog('workflow_revert', 'Invalid token', array(), WATCHDOG_ERROR);
  35. drupal_set_message(t('Invalid token. Your information has been recorded.'), 'error');
  36. drupal_goto($return_uri);
  37. }
  38. }
  39. /**
  40. * Submit callback function.
  41. *
  42. * The function is magically called, due to its name <form>_submit.
  43. */
  44. function workflow_revert_form_submit($form, &$form_state) {
  45. global $user;
  46. $previous_sid = $form['#previous_sid'];
  47. // $id = $form['#entity_id'];
  48. $entity = $form['#entity'];
  49. $entity_type = $form['#entity_type'];
  50. $field_name = $form['#field_name'];
  51. $comment = t('State reverted.');
  52. $uri = entity_uri($entity_type, $entity);
  53. $return_uri = $uri['path'];
  54. // If Rules is available, signal the reversion.
  55. if (module_exists('rules')) {
  56. rules_invoke_event('workflow_state_reverted', $entity);
  57. }
  58. $current_sid = workflow_node_current_state($entity, $entity_type, $field_name);
  59. $transition = new WorkflowTransition();
  60. $transition->setValues($entity_type, $entity, $field_name, $current_sid, $previous_sid, $user->uid, REQUEST_TIME, $comment);
  61. // Force the transition because it's probably not valid.
  62. $new_sid = workflow_execute_transition($entity_type, $entity, $field_name, $transition, TRUE);
  63. ($previous_sid == $new_sid) ? drupal_set_message($comment) : drupal_set_message(t('State could not be reverted.'), 'warning');
  64. drupal_goto($return_uri . '/workflow');
  65. }