You are here

function workflow_cleanup_form_submit in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_cleanup/workflow_cleanup.module \workflow_cleanup_form_submit()

Submission handler for main cleanup form.

File

workflow_cleanup/workflow_cleanup.pages.inc, line 133
Contains an Admin page to delete obsolete states and transitions.

Code

function workflow_cleanup_form_submit($form, $form_state) {
  $states = $form['#workflow_states'];
  foreach (array(
    'no_workflow',
    'inactive',
  ) as $section) {
    if (!isset($form_state['values'][$section])) {
      continue;
    }
    foreach ($form_state['values'][$section] as $sid => $data) {

      // FAPI returns either a 0 or the sid.
      if ($data['check']) {
        $state = $states[$sid];
        $state_name = $state
          ->getName();

        // Delete any transitions this state is involved in.
        $trans_del = db_delete('workflow_transitions')
          ->condition('target_sid', $sid)
          ->execute();
        $trans_del += db_delete('workflow_transitions')
          ->condition('sid', $sid)
          ->execute();
        if ($trans_del) {
          drupal_set_message(t('@count transitions for the "@state" state have been deleted.', array(
            '@state' => $state_name,
            '@count' => $trans_del,
          )));
        }

        // Remove history records too.
        $hist_del = db_delete('workflow_node_history')
          ->condition('sid', $sid)
          ->execute();
        if ($hist_del) {
          drupal_set_message(t('@count history records for the "@state" state have been deleted.', array(
            '@state' => $state_name,
            '@count' => $hist_del,
          )));
        }

        // Go ahead and delete the state.
        db_delete('workflow_states')
          ->condition('sid', $sid)
          ->execute();
        drupal_set_message(t('The "@state" state has been deleted.', array(
          '@state' => $state_name,
        )));
      }
    }
  }
}