Index: freelinking.module
===================================================================
--- freelinking.module	(revision 1722)
+++ freelinking.module	(working copy)
@@ -36,38 +36,165 @@
 }
 
 function freelinking_page($thetitle = NULL) {
+
+  if ($_POST['operation'] == 'delete' && $_POST['links']) {
+    return drupal_get_form('freelinking_multiple_delete_confirm', $_POST['links']);
+  } elseif ($_POST['operation'] == 'delete-all') {
+    return drupal_get_form('freelinking_delete_all_confirm');
+  }
+
   if ($thetitle) { // find the matching title
     $freelink = _freelinking_make_link($thetitle);
     drupal_goto($freelink['path'], $freelink['args'] ? $freelink['args'] : '');
   }
   else { // no title was passed -- show a list of wikiwords and status
-    $header = array('phrase', 'target');
-    $query = "SELECT phrase, path FROM {freelinking} ORDER BY phrase";
-    $result = db_query($query);
-    $i = 0;
-    while ($freelink = db_fetch_object($result)) { // looping through phrase, target pairs
-      $rows[$i][] = urldecode($freelink->phrase);
-      if (preg_match('/^(http|mailto|https|ftp):/', $freelink->path)) { // an absolute link
-        $rows[$i][] = '<a class="freelink" href="' . $freelink->path . '">' . $flpair->path . '</a>';
+    return drupal_get_form('freelinking_page_form', user_access('administer freelinking'));
+  } // big else
+} // endfunction freelinking_page
+
+function freelinking_page_form($admin = TRUE) {
+  $query = "SELECT hash, phrase, path FROM {freelinking} ORDER BY phrase";
+  $result = pager_query($query,50);
+
+  if ($admin) {
+    $form['options'] = array('#type' => 'fieldset',
+      '#title' => t('Update options'),
+      '#prefix' => '<div class="container-inline">',
+      '#suffic' => '</div>',
+    );
+    
+    $options = array('delete' => t('Delete'), 'delete-all' => t('Delete All'));
+    $form['options']['operation'] = array('#type' => 'select', '#options' => $options,  '#default_value' => 'delete');
+    $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update'));
+  }
+
+  while ($freelink = db_fetch_object($result)) { // looping through phrase, target pairs
+    $hash = $freelink->hash;
+    $links[$hash] = '';
+    $form['phrase'][$hash] = array('#value' => urldecode($freelink->phrase));
+
+    if (preg_match('/^(http|mailto|https|ftp):/', $freelink->path)) { // an absolute link
+      $link = '<a class="freelink" href="' . $freelink->path . '">' . $flpair->path . '</a>';
+      $form['target'][$hash] = array('#value' => $link);
+    }
+    else { // a freelink
+      $fltargetnid = _freelinking_exists($freelink->phrase);
+      $freelink = _freelinking_make_link($freelink->phrase);
+      if ($fltargetnid) {
+	$link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid));
       }
-      else { // a freelink
-        $fltargetnid = _freelinking_exists($freelink->phrase);
-        $freelink = _freelinking_make_link($freelink->phrase);
-        if ($fltargetnid) {
-          $link = l(t('see this content'), drupal_get_path_alias('node/' . $fltargetnid));
-        }
-        else { // content not found, show link to create
-          $link = '<a href=' . url($freelink['path'], $freelink['args']) . '>' . t('create this content') . '</a>';
-        }
-        $rows[$i][] = $link;
+      else { // content not found, show link to create
+	$link = '<a href=' . url($freelink['path'], $freelink['args']) . '>' . t('create this content') . '</a>';
       }
-      $i++;
+      $form['target'][$hash] = array('#value' => $link);
     }
+  }
 
-    return theme('table', $header, $rows);
+  if ($admin) { $form['links'] = array('#type' => 'checkboxes', '#options' => $links); }
+  $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
+  return $form;
+  
+} // endfunction freelinking_page_form
+
+function theme_freelinking_page_form($form) {
+  if (isset($form['options']) && is_array($form['options'])) {
+    // Only admins see the options form and the tableselect checkbox
+    $output .= drupal_render($form['options']);
+    $header = array(theme('table_select_header_cell'), t('Path'), t('Target'));
+  } else {
+    $header = array(t('Path'), t('Target'));
   }
-} // endfunction freelinking_page
 
+  if (isset($form['phrase']) && is_array($form['phrase'])) {
+    foreach (element_children($form['phrase']) as $key) {
+      $row = array();
+      if (isset($form['links'][$key])) { // Only admins see the checkbox
+	$row[] = drupal_render($form['links'][$key]);
+      }
+      $row[] = drupal_render($form['phrase'][$key]);
+      $row[] = drupal_render($form['target'][$key]);
+      $rows[] = $row;
+    }
+  } else  {
+    $rows[] = array(array('data' => t('No freelinks available.'), 'colspan' => $admin?'3':'2'));
+  }
+
+  $output .= theme('table', $header, $rows);
+  if ($form['pager']['#value']) {
+    $output .= drupal_render($form['pager']);
+  }
+
+  $output .= drupal_render($form);
+
+  return $output;
+}
+
+function freelinking_page_form_validate($form_id, $form_values) {
+  $links = array_filter($form_values['links']);
+  if (count($links) == 0) {
+    form_set_error('', t('No items selected.'));
+  }
+}
+
+
+function freelinking_page_form_submit($form_id, $form_values) {
+  // Can I assume at this point the user has the right privs, or they wouldn't have been able to render the form in the first place?
+  // (probly not, i spose, but we'll make this simplifying assumption for now..)
+  $op = $form_values['operation'];
+
+  // Filter out unchecked links
+  $links = array_filter($form_values['links']); 
+
+  // perhaps perform other operations besides delete (which requires a confirm form, from the _page callback)
+  // maybe this would be better done with a multistep form, that would allow for more complex operations like
+  // creating a bunch of nodes at once?
+  // for now, don't do anything, and let the _page callback confirm the delete
+}
+
+function freelinking_multiple_delete_confirm($links = array()) {
+  $form['links'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
+  // array_filter returns only elements with TRUE values
+  foreach ($links as $hash => $value) {
+    $phrase = db_result(db_query('SELECT phrase FROM {freelinking} WHERE hash = "%s"', $hash));
+    $form['links'][$hash] = array(
+      '#type' => 'hidden', 
+      '#value' => $hash, 
+      '#prefix' =>  '<li>', 
+      '#suffix' => check_plain($phrase) . "</li>\n");
+  }
+  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
+
+  return confirm_form($form,
+                      t('Are you sure you want to delete these items?'),
+                      'freelinking', t('This action cannot be undone.'),
+                      t('Delete all'), t('Cancel'));
+}
+
+function freelinking_multiple_delete_confirm_submit($form_id, $form_values) {
+  if ($form_values['confirm']) {
+    foreach ($form_values['links'] as $hash => $phrase) {
+      _freelinking_delete($hash);
+    }
+    drupal_set_message(t('The freelinks have been deleted.'));
+  }
+  return 'freelinking';
+}
+
+function freelinking_delete_all_confirm() {
+  $form['operation'] = array('#type' => 'hidden', '#value' => 'delete-all');
+  return confirm_form($form, t('Are you sure you want to delete ALL freelinks?'),
+                      'freelinking', t('This action cannnot be undone.'),
+                      t('Delete ALL'), t('Cancel'));
+}
+
+function freelinking_delete_all_confirm_submit($form_id, $form_values) {
+  if ($form_values['confirm']) {
+    _freelinking_delete_all();
+    drupal_set_message(t('ALL freelinks have been deleted.'));
+  }
+  return 'freelinking';
+}
+
 function freelinking_block($op = 'list', $delta = 0) {
   switch ($op) {
     case 'list':
@@ -311,6 +438,16 @@
   } // endifelse row found
 } // endfunction _freelinking_store
 
+function _freelinking_delete($hash) {
+  $query = "DELETE FROM {freelinking} WHERE hash = '%s'";
+  $result = db_query($query, $hash);
+   // do something with result?
+}
+
+function _freelinking_delete_all() {
+  db_query("DELETE FROM {freelinking}");
+}
+
 function _freelinking_exists($thetitle) { // helper function for freelinking_page
   // looks through the db for nodes matching $title. Returns the nid if such a node exists, otherwise, returns 0
   $title = urldecode($thetitle);
