Index: modules/trip_search/trip_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/trip_search/trip_search.module,v
retrieving revision 1.13
diff -u -r1.13 trip_search.module
--- modules/trip_search/trip_search.module	13 Jul 2004 03:56:23 -0000	1.13
+++ modules/trip_search/trip_search.module	30 Sep 2004 17:13:52 -0000
@@ -1,58 +1,84 @@
 <?php
 // $Id: trip_search.module,v 1.13 2004/07/13 03:56:23 weitzman Exp $
 
+/**
+ * @file
+ * An alternative to the core search module, providing site-wide searching without custom indexing.
+ */
+
+/**
+ * Implementation of hook_help().
+ */
 function trip_search_help($section){
-  $output = "";
-  
   switch ($section) {
-    case "admin/modules#description":
-      $output = t("A simple search module replacement");
-      break;
-    }
-  return $output;
+    case 'admin/modules#description':
+      return t('An alternative to the core search module, providing site-wide searching without custom indexing.');
+    case 'admin/settings/trip_search':
+      return t('The trip_search module provides site-wide search capability.  Below you can refine the search presentation.');
+    case 'admin/help#trip_search':
+      return t("
+      <p>The trip_search module .</p>
+      In <a href=\"%trip_search-config\">administer &raquo; settings &raquo; trip_search</a> you can set up an introductory text for display at the top of search forms and a ceiling on the number of hits that may be returned.</p>
+      <h3>User access permission for searching</h3>
+      <p><strong>search content:</strong> Allows a role to view the search form, perform a search, and view results.</p>
+      ", array('%trip_search-config' => url('admin/settings/trip_search')));
+    case 'trip_search':
+      return variable_get('trip_search_help', '');
+  }
 }
-    
-function trip_search_block($op = "list", $delta = 0) {
 
-  if ($op == "list") {
-    $blocks[]["info"] = t("Search");
+/**
+ * Implementation of hook_block().
+ */
+function trip_search_block($op = 'list', $delta = 0) {
+  if ($op == 'list') {
+    $blocks[]['info'] = t('Search');
     return $blocks;
-  } else {
-    
-    $form .= form_textfield(t("Search for"), "keys", $edit[keys], 15, 200);
-    $form .= form_submit(t("search"));
-    
-    $block["subject"] = t("Search");
-    $block["content"] = "<div class=\"trip-search-block\">". form($form, "post", url("trip_search")) ."</div>" ;
-    return $block;    
-    }
+  }
+  else {
+    $form = trip_search_form ($edit);
+    $block['subject'] = t('Search');
+    $block['content'] = $form;
+    return $block;
+  }
 }
 
-
+/**
+ * Implementation of hook_menu().
+ */
 function trip_search_menu() {
   $items[] = array('path' => 'trip_search', 'title' => t('search'),
   'callback' => 'trip_search_page',
   'access' => user_access('search content'),
   'type' => MENU_NORMAL_ITEM);
-  drupal_set_html_head(trip_search_html_head()); // is there a better place to put 'run once' per request code?
   return $items;
 }
 
+/**
+ * Implementation of hook_settings().  Set html head with styling information.
+ */
+function trip_search_init() {
+  drupal_set_html_head(trip_search_html_head());
+}
+
+/**
+ * Implementation of hook_perm().
+ */
 function trip_search_perm() {
-  return array("search content");
+  return array('search content');
 }
 
-function trip_search_page($keys = NULL) {
+/**
+ * Menu callback; displays a search form and, if available, search results.
+ */
+function trip_search_page() {
   $op = $_POST['op'];
   $edit = $_POST['edit'];
   $hook = $_REQUEST['hook'];
   $count = 0;
  
-  // allow kewords to be passed a second argument in $q  
-  $edit['keys'] = $keys;
-
   // output search form
-  $output .= theme('trip_search_form', $edit);
+  $output .= trip_search_form($edit);
 
   // output search results
   if (!empty($edit[keys])) {
@@ -62,34 +88,33 @@
       foreach (array_keys($find) as $type) { 
         $type_name = $find[$type]['name'] ? $find[$type]['name'] : $type;
         unset($find[$type]['name']);
-        $output .= theme_item_list($find[$type], t("$type_name results")). "\n";
+        $output .= theme_item_list($find[$type], t('%type_name results', array('%type_name' => $type_name))). "\n";
         $count = $count + count($find[$type]);
       }
     
     } 
     else {
-      $output .= t("Your search yielded no results.");
+      $output .= t('Your search yielded no results.');
     }
 
     // log the search query
     $num .= $count ? $count : 0;
-    $msg = t("%num matches for '%keys' %more", array ("%num" => $num, "%keys" => $edit[keys], "%more" => $hook ? "(more $hook)" : ""));
+    $msg = t('%num matches for "%keys" %more', array ('%num' => $num, '%keys' => $edit[keys], '%more' => $hook ? "(more $hook)" : ''));
     $link = $_SERVER['REQUEST_METHOD'] == 'GET' ? l(t('view results'), request_uri()) : l(t('view results'), "trip_search/$edit[keys]");
-    watchdog("message", $msg, $link);
+    watchdog('message', $msg, $link);
   }
-  print theme("page", $output);
+  print theme('page', $output);
 
 }
 
 /*
- * Peform a search
+ * Peform a search.
  */
-
 function trip_search_get($keys, $hook = NULL) {
   
   // Some black magic to get better results
-  $keys = str_replace(",", "", $keys);
-  $keys = str_replace("*", "%%", $keys);
+  $keys = str_replace(',', '', $keys);
+  $keys = str_replace('*', '%%', $keys);
   
   $max = variable_get('trip_search_max', 25);
   $from = $_REQUEST['from'] ? $_REQUEST['from'] : 0;
@@ -102,18 +127,20 @@
   return $find ? $find : array();
 }
 
-/* 
-** A function to display search form, can be used in themes
-**/
-  function theme_trip_search_form($edit = NULL) {
-    $form .= form_textfield(t("Search for"), "keys", $edit[keys], 50, 200, t('You may use the * character as a wildcard.'));
-    $form .= form_submit(t("search"));
-    return "<div class=\"trip-search-form\">".form($form, "post", url("trip_search"))."</div>";
-  }
+/**
+ * Generate a search form.
+ */
+function trip_search_form($edit = NULL) {
+  $form .= form_textfield(t('Search for'), 'keys', $edit[keys], 18, 200, t('You may use the * character as a wildcard.'));
+  $form .= form_submit(t('search'));
+  return "<div class=\"trip-search-form\">".form($form, "post", url("trip_search"))."</div>";
+}
 
-// provide some default formatting
+/**
+ * Provide some default formatting.
+ */
 function trip_search_html_head() {
-  $output = "
+  $output = '
   <style>
     .trip-search-form input.form-submit {
        margin-bottom: 1em;
@@ -122,60 +149,67 @@
       font-weight: bold;
       font-style: italic;
     }
-  </style>";
+  </style>';
   return $output;
 }
 
+/**
+ * Implementation of hook_settings().
+ */
 function trip_search_settings() {
-  $output = form_select(t("Maximum Items"), "trip_search_max", variable_get("trip_search_max", 25), array(2 => 2,10 => 10,25 => 25,50 => 50,75 => 75,100 => 100), t("The maximum number of results to show for any given search result type."), 0, 0);
+  $output .= form_textarea(t('Search explanation'), 'trip_search_help', variable_get('trip_search_help', ''), 70, 5, t('This text will be displayed at the top of the search form.  It is useful for helping or instructing your users.'));
+  $output .= form_select(t('Maximum items'), 'trip_search_max', variable_get('trip_search_max', 25), array(2 => 2,10 => 10,25 => 25,50 => 50,75 => 75,100 => 100), t('The maximum number of results to show for any given search result type.'), 0, 0);
   return $output;
 }
 
-// strips tags and presents a teaser of appropriate length
+/**
+ * Strip tags and presents a teaser of appropriate length.
+ */
 function trip_search_teaser($text) {
   if ($len = strlen($text)) {
     $teaser = '<div class="trip-search-teaser">';
-    $teaser .= $len > 100 ? substr(strip_tags($text), 0, 100). " ..." : strip_tags($text);
+    $teaser .= $len > 100 ? substr(strip_tags($text), 0, 100) . ' ...' : strip_tags($text);
     $teaser .= '</div>';
   }
   return $teaser;
 }
+
 /*
-** these functions actually gather the results. they eventually will move to their respective modules
+** These functions actually gather the results. They could eventually move to their respective modules.
 */
-  
+
 function taxonomy_trip_search($keys, $from, $max) {
 
   $find = array();
-  $words = explode(" ", $keys);
+  $words = explode(' ', $keys);
   $terms = array();
   
   foreach ($words as $word) {
 
-    $result = db_query_range("SELECT tid, name, description FROM {term_data} WHERE name LIKE '%s'", $word, 0, $max);
+    $result = db_query_range('SELECT tid, name, description FROM {term_data} WHERE name LIKE "%s"', $word, 0, $max);
     while ($term = db_fetch_object($result)) {
       if (!in_array($term->tid, $terms)) {  
         // this hook is used to customize the link presented for a 'categories' result. used initially for glossary results.
         $custom = module_invoke_all('trip_search_taxonomy', $term);
-        $find["categories"][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/page/or/$term->tid"). trip_search_teaser($term->description);
+        $find['categories'][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/term/$term->tid"). trip_search_teaser($term->description);
         $terms[$term->tid] = $term->description;
       }
     }
 
-    $result = db_query_range("SELECT d.tid, d.name, d.description, s.name as synonym FROM {term_data} d, {term_synonym} s WHERE s.tid = d.tid AND s.name  LIKE '%s'", $word, 0, $max);
+    $result = db_query_range('SELECT d.tid, d.name, d.description, s.name as synonym FROM {term_data} d, {term_synonym} s WHERE s.tid = d.tid AND s.name  LIKE "%s"', $word, 0, $max);
     while ($term = db_fetch_object($result)) {
       if (!in_array($term->tid, $terms)) {  
         $custom = module_invoke_all('trip_search_taxonomy', $term);
-        $find["categories"][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/page/or/$term->tid"). trip_search_teaser($term->description);
+        $find['categories'][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/term/$term->tid"). trip_search_teaser($term->description);
         $terms[$term->tid] = $term->description;
       }
     }
 
-    $result = db_query_range("SELECT tid, name, description FROM {term_data} WHERE description LIKE '%%%s%%'", $word, 0, $max);
+    $result = db_query_range('SELECT tid, name, description FROM {term_data} WHERE description LIKE "%%%s%%"', $word, 0, $max);
     while ($term = db_fetch_object($result)) {
       if (!in_array($term->tid, $terms)) {  
         $custom = module_invoke_all('trip_search_taxonomy', $term);
-        $find["categories"][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/page/or/$term->tid"). trip_search_teaser($term->description);
+        $find['categories'][] = $custom ? implode(', ', $custom) : l($term->name, "taxonomy/term/$term->tid"). trip_search_teaser($term->description);
         $terms[$term->tid] = $term->description;
       }
     }
@@ -189,8 +223,8 @@
       foreach ($related as $rel_tid => $rel_term) {
         if (!in_array($rel_tid, $terms)) {  
           $custom = module_invoke_all('trip_search_taxonomy', $rel_term);
-          $find["categories"][] = $custom ? implode(', ', $custom) : l($rel_term->name, "taxonomy/page/or/$rel_term->tid"). trip_search_teaser($description);
-          // $find["categories"]['name'] = t('related categories');
+          $find['categories'][] = $custom ? implode(', ', $custom) : l($rel_term->name, "taxonomy/term/$rel_term->tid"). trip_search_teaser($description);
+          // $find['categories']['name'] = t('related categories');
         }
       }
     }
@@ -200,20 +234,20 @@
     $count = count($find['categories']);
     $find['categories'] = array_slice($find['categories'], $from, $max);
     if ($count > $from+$max) {
-      $find['categories'][] = l(t("More categories results"), "trip_search/". urlencode($keys), array("class" => "trip-search-more-link"), "hook=taxonomy&from=$max");
+      $find['categories'][] = l(t('More categories results'), 'trip_search/'. urlencode($keys), array('class' => 'trip-search-more-link'), "hook=taxonomy&from=$max");
     }
     return $find;
   }
 }
 
 function user_trip_search($keys, $from, $max) {
-  $result = db_query_range("SELECT * FROM {users} WHERE name LIKE '%$keys%'", $from, $max+1);
+  $result = db_query_range('SELECT * FROM {users} WHERE name LIKE "%' . $keys . '%"', $from, $max+1);
   while ($account = db_fetch_object($result)) {
-    $find["account"][] = l($account->name, "user/view/$account->uid");
+    $find['account'][] = l($account->name, "user/view/$account->uid");
   }
   if (db_num_rows($result) > $from+$max) {
     array_pop($find['account']); // we selected one more than needed in order to assure that we have more to see
-    $find['account'][] = l(t("More $name results"), "trip_search/". urlencode($keys), array("class" => "trip-search-more-link"), "hook=user&from=$max");
+    $find['account'][] = l(t('More %name results', array('%name' => $name)), 'trip_search/'. urlencode($keys), array('class' => 'trip-search-more-link'), "hook=user&from=$max");
   }
   return $find;
 }
@@ -222,17 +256,17 @@
 function node_trip_search($keys, $from, $max) {
     
   if ($type = $_REQUEST['type']) {
-    $sql = "SELECT n.nid, n.type, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE (title LIKE '%$keys%' OR teaser LIKE '% $keys %' OR body LIKE '% $keys %') AND n.status = '1' AND n.type = '$type' ORDER BY n.created DESC";
+    $sql = 'SELECT n.nid, n.type, u.name FROM {node} n '. node_access_join_sql() . ' INNER JOIN {users} u ON n.uid = u.uid WHERE (title LIKE "%' . $keys . '%" OR teaser LIKE "%' . $keys . '%" OR body LIKE "%' . $keys . '%") AND ' . node_access_where_sql() . ' AND n.status = "1" AND n.type = "' . $type . '" ORDER BY n.created DESC';
     $result = db_query_range($sql, $from, $max+1); //select an extra so we assure that there are more
     $find[$type] = node_trip_search_type($result, $keys, $from, $max);
   }
   else {
     foreach (node_list() as $type) {
-      $sql = "SELECT n.nid, n.type, u.name FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE (title LIKE '%$keys%' OR teaser LIKE '% $keys %' OR body LIKE '% $keys %') AND n.status = '1' AND n.type = '$type' ORDER BY n.created DESC";
+      $sql = 'SELECT n.nid, n.type, u.name FROM {node} n '. node_access_join_sql() .' INNER JOIN {users} u ON n.uid = u.uid WHERE (title LIKE "%' . $keys . '%" OR teaser LIKE "%' . $keys . '%" OR body LIKE "%' . $keys . '%") AND ' . node_access_where_sql() . ' AND n.status = "1" AND n.type = "' . $type . '" ORDER BY n.created DESC';
       $result = db_query_range($sql, $from, $max+1); //select an extra so we assure that there are more
       if (db_num_rows($result)) {
         $find[$type] = node_trip_search_type($result, $keys, $from, $max);
-        $find[$type]['name'] = node_invoke($type, "node_name");
+        $find[$type]['name'] = node_invoke($type, 'node_name');
       }
     }
   }
@@ -242,27 +276,27 @@
 function node_trip_search_type($result, $keys, $from, $max) {
   if ($num = db_num_rows($result)) {
     while ($node = db_fetch_object($result)) {
-      $node = node_load(array("nid" => $node->nid));
+      $node = node_load(array('nid' => $node->nid));
       $type = $node->type;
-      $links[] = l($node->title, "node/view/$node->nid", array('title' => "by $node->name")). trip_search_teaser($node->teaser) ;
+      $links[] = l($node->title, "node/$node->nid", array('title' => "by $node->name")). trip_search_teaser($node->teaser) ;
     }
-    $name = node_invoke($type, "node_name");
+    $name = node_invoke($type, 'node_name');
     if ($num > ($from+$max)) { 
       array_pop($links); // remove the extra
-      $links[] = l(t("More $name results"), "trip_search/". urlencode($keys), array("class" => "trip-search-more-link"), "hook=node&from=$max&type=". urlencode($type));
+      $links[] = l(t('More %name results', array('%name' => $name)), 'trip_search/'. urlencode($keys), array('class' => 'trip-search-more-link'), "hook=node&from=$max&type=". urlencode($type));
     }
   }
   return $links ? $links : array();
 }
 
 function comment_trip_search($keys, $from, $max) {
-  $result = db_query_range("SELECT c.*, u.name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.status = 0 AND (c.subject LIKE '%$keys%' OR c.comment LIKE '% $keys %') ORDER BY c.timestamp DESC", $from, $max+1);
+  $result = db_query_range('SELECT c.*, u.name FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.status = 0 AND (c.subject LIKE "%' . $keys . '%" OR c.comment LIKE "%' . $keys . '%") ORDER BY c.timestamp DESC', $from, $max+1);
   while ($comment = db_fetch_object($result)) {
-    $find['comment'][] = l($comment->subject, "node/view/$comment->nid#$comment->cid", array('title' => t("Author: %name",  array("%name" => $comment->name)))). trip_search_teaser($comment->comment);
+    $find['comment'][] = l($comment->subject, "node/$comment->nid#$comment->cid", array('title' => t('Author: %name',  array('%name' => $comment->name)))). trip_search_teaser($comment->comment);
   }
   if (db_num_rows($result) > $from+$max) {
     array_pop($find['comment']); // remove the extra
-    $find['comment'][] = l(t("More $name results"), "trip_search/". urlencode($keys), array("class" => "trip-search-more-link"), "hook=comment&from=$max");
+    $find['comment'][] = l(t('More %name results', array('%name' => $name)), 'trip_search/'. urlencode($keys), array('class' => 'trip-search-more-link'), "hook=comment&from=$max");
   }
   return $find ? $find : array();
 }
