Index: flexinode.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flexinode/flexinode.module,v
retrieving revision 1.46.2.2
diff -U3 -r1.46.2.2 flexinode.module
--- flexinode.module	2 May 2005 16:37:16 -0000	1.46.2.2
+++ flexinode.module	5 Sep 2005 15:46:36 -0000
@@ -137,6 +137,10 @@
     foreach (flexinode_content_types() as $ctype) {
       $items[] = array('path' => 'node/add/flexinode-'. $ctype->ctype_id, 'title' => t($ctype->name),
         'access' => user_access('create '. $ctype->name .' content'));
+      $items[] = $items[] = array('path' => 'search/flexinode/'. $ctype->ctype_id, 'title' => t($ctype->name),
+        'callback' => 'flexinode_page_search_form',
+        'access' => (user_access('access content') && user_access('search content')),
+        'type' => MENU_LOCAL_TASK);
     }
 
     $items[] = array('path' => 'flexinode/list', 'title' => t('list view'),
@@ -145,9 +149,10 @@
     $items[] = array('path' => 'flexinode/table', 'title' => t('tabular view'),
       'callback' => 'flexinode_page_table', 'access' => user_access('access content'),
       'type' => MENU_CALLBACK);
-    $items[] = array('path' => 'flexinode/search', 'title' => t('search'),
-      'callback' => 'flexinode_page_search_form', 'access' => user_access('access content'),
-      'type' => MENU_CALLBACK);
+    $items[] = array('path' => 'search/flexinode', 'title' => t('advanced'),
+      'callback' => 'flexinode_page_search_form',
+      'access' => (user_access('access content') && user_access('search content')),
+      'type' => MENU_LOCAL_TASK);
     $items[] = array('path' => 'flexinode/feed', 'title' => t('rss feed'),
       'callback' => 'flexinode_feed', 'access' => user_access('access content'),
       'type' => MENU_CALLBACK);
@@ -210,12 +215,12 @@
  * Menu callback; presents a search form for nodes of one type.
  */
 function flexinode_page_search_form($ctype_id = 0) {
-  if (!$ctype_id) {
+  if (!$ctype_id && !$ctype_id = arg(2)) {
     drupal_not_found();
   }
 
   if ($_POST['op'] == t('Search')) {
-    return flexinode_page_table($ctype_id);
+    return flexinode_search_results($ctype_id);
   }
 
   $output = '';
@@ -261,25 +266,6 @@
   $extra_where = count($where_clauses) > 0 ? ' AND ' . implode(' AND ', $where_clauses) : '';
   $sql = 'SELECT n.nid, n.title '. $extra_fields .' FROM {node} n '. implode(' ', $table_joins) ." WHERE n.status = 1 AND n.type = '$type'". $extra_where;
 
-  if ($_POST['op'] == t('Search')) {
-    $edit = $_POST['edit'];
-    foreach ($ctype->fields as $field) {
-      $clause = flexinode_invoke('search_clause', $field, $edit);
-      if ($clause) {
-        $sql .= ' AND '. $clause;
-      }
-    }
-
-    $search = db_escape_string($edit['search']);
-    if ($search) {
-      $sql .= " AND (n.title LIKE '%". $search ."%'";
-      foreach ($ctype->fields as $field) {
-        $sql .= 'OR flexinode_'. $field->field_id .".textual_data LIKE '%". $search ."%'";
-      }
-      $sql .= ')';
-    }
-  }
-
   // Build the columns.
   $header[] = array('data' => t('title'), 'field' => 'n.title');
   foreach ($ctype->fields as $field) {
@@ -312,10 +298,15 @@
     $rows[] = $row;
   }
 
-  $output .= theme('table', $header, $rows);
-  $output .= theme('pager', NULL, 20, 0, tablesort_pager());
+  if ($rows) {
+    $output .= theme('table', $header, $rows);
+    $output .= theme('pager', NULL, 20, 0, tablesort_pager());
+    drupal_set_title(t('%type search results', array('%type' => t($ctype->name))));
+  }
+  else {
+     $output .= theme('box', t('Your search yielded no results'), search_help('search#noresults'));
+  }
 
-  drupal_set_title(t('%type search results', array('%type' => t($ctype->name))));
   print theme('page', $output);
 }
 
@@ -1085,6 +1076,91 @@
   }
 }
 
+/**
+ * Render the search results
+ **/
+function flexinode_search_results($ctype_id = 0) {
+  if (!$ctype_id) {
+    drupal_not_found();
+  }
+
+  $output = '';
+  $ctype = flexinode_load_content_type($ctype_id);
+
+  // Build the query.
+  $fields_to_select = array();
+  $table_joins = array();
+  $where_clauses = array();
+
+  foreach ($ctype->fields as $field) {
+    $fieldname = 'flexinode_'. $field->field_id;
+
+    $fields_to_select[] = flexinode_invoke('db_select', $field);
+    $table_joins[] = 'LEFT JOIN {flexinode_data} '. $fieldname .' ON n.nid = '. $fieldname .'.nid';
+    $where_clauses[] = $fieldname .'.field_id = '. $field->field_id;
+  }
+  $type = 'flexinode-' . db_escape_string($ctype_id);
+  $extra_fields = count($fields_to_select) > 0 ? ', ' . implode(', ', $fields_to_select) : '';
+  $extra_where = count($where_clauses) > 0 ? ' AND ' . implode(' AND ', $where_clauses) : '';
+  $sql = 'SELECT n.nid, n.title '. $extra_fields .' FROM {node} n '. implode(' ', $table_joins) ." WHERE n.status = 1 AND n.type = '$type'". $extra_where;
+
+  if ($_POST['op'] == t('Search')) {
+    $edit = $_POST['edit'];
+    foreach ($ctype->fields as $field) {
+      $clause = flexinode_invoke('search_clause', $field, $edit);
+      if ($clause) {
+        $sql .= ' AND '. $clause;
+      }
+    }
+
+    $search = db_escape_string($edit['search']);
+    if ($search == '') {
+      $search = '%'; // no keywords will match all.
+    }
+    $sql .= " AND (n.title LIKE '%". $search ."%'";
+    foreach ($ctype->fields as $field) {
+      $sql .= 'OR flexinode_'. $field->field_id .".textual_data LIKE '%". $search ."%'";
+    }
+    $sql .= ')';
+  }
+
+  //build and theme the results.
+  if ($search == '%') {
+    $search = '*';
+  }
+  $nodes = pager_query(db_rewrite_sql($sql), 20);
+  while ($res = db_fetch_object($nodes)) {
+    if ($node = node_load(array('nid' => $res->nid))) {
+      $entry = array('link' => url('node/'. $item), 
+                    'type' => node_invoke($node, 'node_name'),
+                    'title' => $node->title,
+                    'user' => format_name($node),
+                    'date' => $node->changed,
+                    'snippet' =>search_excerpt($search, check_output($node->body, $node->format)));
+      $type = 'flexinode';
+      $output .= theme('search_item', $entry, $type);
+  
+      foreach ($ctype->fields as $field) {
+        if ($field->show_table) {
+          $data = flexinode_invoke('format', $field, $node, TRUE);
+          $data ? $entry['extra'][] =  $data : NULL;
+        }
+      }
+    }
+  }
+
+  if ($output) {
+    $output .= theme('pager', NULL, 20, 0, tablesort_pager());
+    drupal_set_title(t('%type search results', array('%type' => t($ctype->name))));
+  }
+  else {
+     drupal_set_message(t('Your search yielded no results'));
+  }
+  watchdog('search', t('Search: %keys (%type).', array('%keys' => theme('placeholder', $keys), '%type' => node_invoke($node, 'node_name'))), WATCHDOG_NOTICE);
+  
+  print theme('page', $output);
+}
+
 function flexinode_feed($ctype_id = NULL) {
   global $base_url;
 
