workflow_cleanup.pages.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * @file
  4. * Contains an Admin page to delete obsolete states and transitions.
  5. */
  6. /**
  7. * The main cleanup page.
  8. */
  9. function workflow_cleanup_form($form, &$form_state) {
  10. $form = array();
  11. // Get all of the states, indexed by sid.
  12. $states = $orphans = $inactive = array();
  13. foreach ($states = workflow_state_load_multiple() as $state) {
  14. // Does the associated workflow exist?
  15. if (!$state->getWorkflow()) {
  16. $orphans[$state->sid] = $state->getName();
  17. }
  18. else {
  19. // Is the state still active?
  20. if (!$state->isActive()) {
  21. $inactive[$state->sid] = $state->getName();
  22. }
  23. }
  24. }
  25. $form['#workflow_states'] = $states;
  26. $form['no_workflow'] = array(
  27. '#type' => 'container',
  28. '#title' => t('Orphaned States'),
  29. '#description' => t('These states no longer belong to an existing workflow.'),
  30. '#tree' => TRUE,
  31. );
  32. foreach ($orphans as $sid => $name) {
  33. $form['no_workflow'][$sid]['check'] = array(
  34. '#type' => 'checkbox',
  35. '#return_value' => $sid,
  36. );
  37. $form['no_workflow'][$sid]['name'] = array(
  38. '#type' => 'markup',
  39. '#markup' => check_plain($name),
  40. );
  41. }
  42. $form['inactive'] = array(
  43. '#type' => 'container',
  44. '#title' => t('Inactive (Deleted) States'),
  45. '#description' => t('These states belong to a workflow, but have been marked inactive (deleted).'),
  46. '#tree' => TRUE,
  47. );
  48. foreach ($inactive as $sid => $name) {
  49. $form['inactive'][$sid]['check'] = array(
  50. '#type' => 'checkbox',
  51. '#return_value' => $sid,
  52. );
  53. $form['inactive'][$sid]['name'] = array(
  54. '#type' => 'markup',
  55. '#markup' => check_plain($name),
  56. );
  57. $form['inactive'][$sid]['wf'] = array(
  58. '#type' => 'markup',
  59. '#markup' => (!empty($sid)) ? check_plain($states[$sid]->getWorkflow()->getName()) : '',
  60. );
  61. }
  62. $form['submit'] = array('#type' => 'submit', '#value' => t('Delete selected states'));
  63. return $form;
  64. }
  65. /**
  66. * Theme the main form.
  67. */
  68. function theme_workflow_cleanup_form($variables) {
  69. $form = $variables['form'];
  70. $output = '';
  71. $header = array(t('Select'), t('State'));
  72. $rows = array();
  73. foreach (element_children($form['no_workflow']) as $sid) {
  74. $rows[] = array(
  75. drupal_render($form['no_workflow'][$sid]['check']),
  76. drupal_render($form['no_workflow'][$sid]['name']),
  77. );
  78. }
  79. $output .= '<h3>' . $form['no_workflow']['#title'] . '</h3>';
  80. $output .= '<div class="description">' . $form['no_workflow']['#description'] . '</div>';
  81. $output .= theme('table', array(
  82. 'header' => $header,
  83. 'rows' => $rows,
  84. 'empty' => 'All states are fine',
  85. 'attributes' => array('style' => 'width: auto;'),
  86. ));
  87. $header[] = t('Workflow');
  88. $rows = array();
  89. foreach (element_children($form['inactive']) as $sid) {
  90. $rows[] = array(
  91. drupal_render($form['inactive'][$sid]['check']),
  92. drupal_render($form['inactive'][$sid]['name']),
  93. drupal_render($form['inactive'][$sid]['wf']),
  94. );
  95. }
  96. $output .= '<h3>' . $form['inactive']['#title'] . '</h3>';
  97. $output .= '<div class="description">' . $form['inactive']['#description'] . '</div>';
  98. $output .= theme(
  99. 'table',
  100. array(
  101. 'header' => $header,
  102. 'rows' => $rows,
  103. 'empty' => 'All states are fine',
  104. 'attributes' => array('style' => 'width: auto;'),
  105. )
  106. );
  107. $output .= drupal_render_children($form);
  108. return $output;
  109. }
  110. /**
  111. * Submission handler for main cleanup form.
  112. */
  113. function workflow_cleanup_form_submit($form, $form_state) {
  114. $states = $form['#workflow_states'];
  115. foreach (array('no_workflow', 'inactive') as $section) {
  116. if (!isset($form_state['values'][$section])) {
  117. continue;
  118. }
  119. foreach ($form_state['values'][$section] as $sid => $data) {
  120. // FAPI returns either a 0 or the sid.
  121. if ($data['check']) {
  122. $state = $states[$sid];
  123. $state_name = $state->getName();
  124. // Delete any transitions this state is involved in.
  125. $trans_del = db_delete('workflow_transitions')->condition('target_sid', $sid)->execute();
  126. $trans_del += db_delete('workflow_transitions')->condition('sid', $sid)->execute();
  127. if ($trans_del) {
  128. drupal_set_message(t('@count transitions for the "@state" state have been deleted.',
  129. array('@state' => $state_name, '@count' => $trans_del)));
  130. }
  131. // Remove history records too.
  132. $hist_del = db_delete('workflow_node_history')->condition('sid', $sid)->execute();
  133. if ($hist_del) {
  134. drupal_set_message(t('@count history records for the "@state" state have been deleted.',
  135. array('@state' => $state_name, '@count' => $hist_del)));
  136. }
  137. // Go ahead and delete the state.
  138. db_delete('workflow_states')->condition('sid', $sid)->execute();
  139. drupal_set_message(t('The "@state" state has been deleted.',
  140. array('@state' => $state_name)));
  141. }
  142. }
  143. }
  144. }