Index: modules/taxonomy/taxonomy.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/taxonomy/taxonomy.module,v
retrieving revision 1.335
diff -u -r1.335 taxonomy.module
--- modules/taxonomy/taxonomy.module	25 Jan 2007 22:14:06 -0000	1.335
+++ modules/taxonomy/taxonomy.module	27 Jan 2007 19:52:02 -0000
@@ -118,8 +118,8 @@
   );
   $items['admin/content/taxonomy/%'] = array(
     'title' => t('List terms'),
-    'page callback' => 'taxonomy_overview_terms',
-    'page arguments' => array(3),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('taxonomy_overview_terms', 3),
     'access arguments' => array('administer taxonomy'),
     'map arguments' => array('taxonomy_get_vocabulary', 3),
     'type' => MENU_CALLBACK,
@@ -169,42 +169,181 @@
 }
 
 /**
- * Display a tree of all the terms in a vocabulary, with options to edit
- * each one.
+ * Implementation of hook_term_operations().
  */
-function taxonomy_overview_terms($vocabulary) {
-  $destination = drupal_get_destination();
+function taxonomy_term_operations() {
+  $operations = array(
+    'delete' => array(
+      'label' => t('Delete')
+    )
+  );
 
-  $header = array(t('Name'), t('Operations'));
+  return $operations;
+}
 
-  drupal_set_title(check_plain($vocabulary->name));
-  $start_from      = $_GET['page'] ? $_GET['page'] : 0;
-  $total_entries   = 0;  // total count for pager
-  $page_increment  = 25; // number of tids per page
-  $displayed_count = 0;  // number of tids shown
+/**
+ * Manage vocabulary terms.
+ * 
+ * @param $vid
+ *   Vocabulary ID of the vocabulary whose terms are to be managed.
+ * @param $form_values
+ *   $form_values for the multi-step form. This parameter is automatically added
+ * by the forms API.
+ * 
+ * @return $form
+ *   A multi-step form array.
+ */
+function taxonomy_overview_terms($vid, $form_values = NULL) {
+  if (!isset($form_values)) {
+    $step = 1;
+    $form = array();
+  }
+  else {
+    $step = $form_values['step'] + 1;
+  }
 
-  $tree = taxonomy_get_tree($vocabulary->vid);
-  foreach ($tree as $term) {
-    $total_entries++; // we're counting all-totals, not displayed
-    if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) {
-      continue;
-    }
-    $rows[] = array(str_repeat('--', $term->depth) .' '. l($term->name, "taxonomy/term/$term->tid"), l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array(), $destination));
-    $displayed_count++; // we're counting tids displayed
+  $form['step'] = array('#type' => 'hidden', '#value' => $step);
+
+  switch ($step) {
+    case 1:
+      $vocabulary = taxonomy_get_vocabulary($vid);
+      if (!$vocabulary) {
+        return drupal_not_found();
+      }
+
+      drupal_set_title(check_plain($vocabulary->name));
+
+      $start_from = $_GET['page'] ? $_GET['page'] : 0;
+      // Total count for pager.
+      $total_entries = 0;
+      // Number of tids per page.
+      $page_increment = 50;
+      // Number of tids shown.
+      $displayed_count = 0;
+
+      $destination = drupal_get_destination();
+
+      $tree = taxonomy_get_tree($vocabulary->vid);
+      $terms = array();
+      foreach ($tree as $term) {
+        // Count all tids.
+        $total_entries++;
+        if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) {
+          continue;
+        }
+        // Count the tids displayed.
+        $displayed_count++;
+
+        $terms[$term->tid] = '';
+        $form['name'][$term->tid] = array('#value' => str_repeat('--', $term->depth) .' '. l($term->name, "taxonomy/term/$term->tid"));
+        $form['operations'][$term->tid] = array('#value' => l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array(), $destination));
+      }
+      $form['terms'] = array('#type' => 'checkboxes', '#options' => $terms);
+
+      $GLOBALS['pager_page_array'][] = $start_from;  // FIXME
+      $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; // FIXME
+
+      if ($total_entries >= $page_increment) {
+        $form['pager'] = array('#value' => theme('pager', NULL, $page_increment));
+      }
+
+      $form['options'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('Update options'),
+        '#prefix' => '<div class="container-inline">',
+        '#suffix' => '</div>'
+      );
+      $options = array();
+
+      // Allow modules to hook in.
+      foreach (module_invoke_all('term_operations') as $operation => $array) {
+        $options[$operation] = $array['label'];
+      }
+
+      $form['options']['operation'] = array(
+        '#type' => 'select',
+        '#options' => $options,
+        '#default_value' => 'approve'
+      );
+      $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
+      break;
+    case 2:
+      if ($form_values['operation'] == 'delete') {
+          $form = confirm_form(
+            $form,
+            t('Are you sure you want to delete the selected terms?'),
+            'admin/content/taxonomy/'. $vid,
+            t('This will delete all selected terms. This action cannot be undone.'),
+            t('Delete terms')
+          );
+      }
+      // Forward current values to subsequent steps. This can possibly be moved
+      // and into the step checking if block and made available to other modules
+      // by default.
+      $form['operation'] = array('#type' => 'value', '#value' => $form_values['operation']);
+      $form['terms'] = array('#type' => 'value', '#value' => $form_values['terms']);
+      $form['vid'] = array('#type' => 'value', '#value' => $vid);
   }
 
-  if (!$rows) {
-    $rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
+  $form['#multistep'] = TRUE;
+  $form['#redirect'] = FALSE;
+
+  return $form;
+}
+
+/**
+ * Process the taxonomy_overview_terms form submission.
+ */
+function taxonomy_overview_terms_submit($form, $form_values) {
+  if ($form_values['step'] == 2 && $form_values['operation'] == 'delete' && $form_values['confirm'] == 1) {
+    foreach ($form_values['terms'] as $tid) {
+      taxonomy_del_term($tid);
+    }
+    // Ideally, an entry would be made for each term deleted.
+    drupal_set_message(t('The terms have been deleted.'));
+    watchdog('taxonomy', t('A mass term deletion operation has been performed.'));
+
+    // Rather than toggling the #redirect of the multi-step form, just use a
+    // goto.
+    drupal_goto('admin/content/taxonomy/'. $form_values['vid']);
   }
+}
 
-  $GLOBALS['pager_page_array'][] = $start_from;  // FIXME
-  $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; // FIXME
+/**
+ * Process the taxonomy_overview_terms form submission.
+ */
+function theme_taxonomy_overview_terms($form) {
+  $output = '';
+  // This form is only displayed during step 1 of the multi-step form.
+  if ($form['step']['#value'] == 1) {
+    // Overview table.
+    $header = array(theme('table_select_header_cell'), t('Name'), t('Operations'));
+
+    $output .= drupal_render($form['options']);
+
+    if (isset($form['name']) && is_array($form['name'])) {
+      foreach (element_children($form['name']) as $key) {
+        $row = array();
+        $row[] = drupal_render($form['terms'][$key]);
+        $row[] = drupal_render($form['name'][$key]);
+        $row[] = drupal_render($form['operations'][$key]);
+        $rows[] = $row;
+      }
+    }
+    else  {
+      $rows[] = array(array('data' => t('No terms available.'), 'colspan' => '3'));
+    }
+
+    $output .= theme('table', $header, $rows);
 
-  if ($total_entries >= $page_increment) {
-    $rows[] = array(array('data' => theme('pager', NULL, $page_increment), 'colspan' => '2'));
+    if ($form['pager']['#value']) {
+      $output .= drupal_render($form['pager']);
+    }
   }
 
-  return theme('table', $header, $rows, array('id' => 'taxonomy'));
+  $output .= drupal_render($form);
+
+  return $output;
 }
 
 /**
