Updated with support for Enano's new search API
authorDan
Wed, 21 Nov 2007 15:22:13 -0500
changeset 13 f6ca7cead82c
parent 10 b38afb86dcad
child 14 e47e4be549ad
Updated with support for Enano's new search API
plugins/gallery/browser.css
plugins/gallery/browser.php
plugins/gallery/search.php
plugins/gallery/sidebar.php
--- a/plugins/gallery/browser.css	Thu Aug 09 12:34:39 2007 -0400
+++ b/plugins/gallery/browser.css	Wed Nov 21 15:22:13 2007 -0500
@@ -11,6 +11,8 @@
   display: table;
   margin: 0 auto;
   padding: 4px;
+  width: 80px;
+  white-space: nowrap;
 }
 
 div.gallery_icon:hover {
@@ -20,5 +22,28 @@
 div.gallery_icon img.gallery_thumb {
   display: block;
   border-width: 0;
-  margin: 0 auto 6px auto;
+  margin: auto auto 6px auto;
+}
+
+ul.snapr-gallery {
+  list-style-type: none !important;
+  list-style-image: none !important;
+  margin-left: 0;
+  padding-left: 0;
 }
+
+ul.snapr-gallery li.snapr-icon {
+  float: left;
+  list-style-type: none !important;
+  list-style-image: none !important;
+  width: 80px;
+  margin-right: 15px;
+}
+
+a.snapr-imagelink {
+  display: block;
+  height: 80px;
+  vertical-align: middle;
+}
+
+
--- a/plugins/gallery/browser.php	Thu Aug 09 12:34:39 2007 -0400
+++ b/plugins/gallery/browser.php	Wed Nov 21 15:22:13 2007 -0500
@@ -37,20 +37,6 @@
 {
   
   /**
-   * Counter for how many cells we've printed out in this row.
-   * @var int
-   */
-  
-  var $cell_count = 0;
-  
-  /**
-   * Icons to print per row.
-   * @var int
-   */
-  
-  var $icons_per_row = 5;
-  
-  /**
    * Main render method, called from pagination function
    * @access private
    */
@@ -59,14 +45,7 @@
   {
     global $db, $session, $paths, $template, $plugins; // Common objects
     
-    $out = '';
-    
-    if ( $this->cell_count == $this->icons_per_row )
-    {
-      $out .= '</tr><tr>';
-      $this->cell_count = 0;
-    }
-    $this->cell_count++;
+    $out = '<li class="snapr-icon">';
     
     $title_safe = $row['img_title'];
     $title_safe = htmlspecialchars($title_safe);
@@ -94,18 +73,17 @@
     $image_url_js = addslashes($image_link);
     $jsclick = ( $session->user_level < USER_LEVEL_ADMIN ) ? ' onclick="window.location=\'' . $image_url_js . '\'"' : '';
     
-    $out .= '<td style="text-align: center;">
-            <div class="gallery_icon"' . $jsclick . '>';
+    $out .= '<div class="gallery_icon"' . $jsclick . '>';
     
-    $out .= '<a href="' . $image_link . '"><img alt="&lt;Thumbnail&gt;" class="gallery_thumb" src="' . $image_url . '" /></a>';
+    $out .= '<a class="snapr-imagelink" href="' . $image_link . '"><img alt="&lt;Thumbnail&gt;" class="gallery_thumb" src="' . $image_url . '" /></a>';
     
     if ( $session->user_level < USER_LEVEL_ADMIN )
     {
-      $out .= $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' );
+      $out .= '<span class="snapr-icon-label">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</span>';
     }
     else if ( $session->user_level >= USER_LEVEL_ADMIN )
     {
-      $out .= '<div class="menu_nojs" style="text-align: center;"><a href="#" onclick="return false;" style="width: 74px;">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</a>';
+      $out .= '<div class="menu_nojs snapr-icon-label" style="text-align: center;"><a href="#" onclick="return false;" style="width: 74px;">' . $title_safe . ( isset($row['score']) ? "<br /><small>Relevance: {$row['score']}</small>" : '' ) . '</a>';
       
       $url_delete = makeUrlNS('Special', 'GalleryUpload', 'rm=' . $row['img_id'], true);
       $url_edit   = makeUrlNS('Special', 'GalleryUpload', 'edit_img=' . $row['img_id'], true);
@@ -121,7 +99,7 @@
     
     $out .= '  </div>';
     
-    $out .= '</td>';
+    $out .= '</li>';
     
     return $out;
   }
@@ -486,9 +464,9 @@
     $start = intval($_GET['start']);
   }
   
-  $per_page = $rows_in_browser * 5;
+  $per_page = 25;
   
-  $html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'sort=' . $sort_column . '&order=' . $sort_order . '&start=%s', false), $start, $per_page, $callers, '<table border="0" cellspacing="8"><tr>', '</tr></table>');
+  $html = paginate($img_query, '{img_id}', $db->numrows($img_query), makeUrl($paths->fullpage, 'sort=' . $sort_column . '&order=' . $sort_order . '&start=%s', false), $start, $per_page, $callers, '<ul class="snapr-gallery">', '</ul><span class="menuclear"></span>');
   echo $html;
   
   if ( $session->user_level >= USER_LEVEL_ADMIN )
--- a/plugins/gallery/search.php	Thu Aug 09 12:34:39 2007 -0400
+++ b/plugins/gallery/search.php	Wed Nov 21 15:22:13 2007 -0500
@@ -17,6 +17,7 @@
 //
 
 $plugins->attachHook('search_results', 'gal_searcher($q, $offset);');
+$plugins->attachHook('search_global_inner', 'snapr_search_new_api($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);');
 
 $plugins->attachHook('compile_template', '
   // CSS for gallery browser
@@ -27,6 +28,8 @@
 function gal_searcher($q, $offset)
 {
   global $db, $session, $paths, $template, $plugins; // Common objects
+  if ( defined('SNAPR_SEARCH_USING_NEW_API') )
+    return false;
   
   $fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)';
   $sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery
@@ -41,7 +44,7 @@
   echo "<h3>Image results</h3>";
   if ( $row = $db->fetchrow() )
   {
-    echo '<table border="0" cellspacing="8"><tr>';
+    echo '<ul class="snapr-gallery">';
     $renderer = new SnaprFormatter();
     $fullpage = $paths->fullpage;
     $paths->fullpage = $paths->nslist['Special'] . 'Gallery';
@@ -51,7 +54,7 @@
     }
     while ( $row = $db->fetchrow() );
     $paths->fullpage = $fullpage;
-    echo '</tr></table>';
+    echo '</ul><span class="menuclear"></span>';
   }
   else
   {
@@ -59,4 +62,125 @@
   }
 }
 
+function snapr_search_new_api(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list)
+{
+  global $db, $session, $paths, $template, $plugins; // Common objects
+  
+  if ( !defined('SNAPR_SEARCH_USING_NEW_API') )
+    define('SNAPR_SEARCH_USING_NEW_API', 1);
+  
+  // Let's do this all in one query
+  $terms = array(
+      'any' => array_merge($query['any'], $query_phrase['any']),
+      'req' => array_merge($query['req'], $query_phrase['req']),
+      'not' => $query['not']
+    );
+  $where = array('any' => array(), 'req' => array(), 'not' => array());
+  $where_any =& $where['any'];
+  $where_req =& $where['req'];
+  $where_not =& $where['not'];
+  $title_col = ( $case_sensitive ) ? 'img_title' : 'lcase(img_title)';
+  $desc_col = ( $case_sensitive ) ? 'img_desc' : 'lcase(img_desc)';
+  foreach ( $terms['any'] as $term )
+  {
+    $term = escape_string_like($term);
+    if ( !$case_sensitive )
+      $term = strtolower($term);
+    $where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
+  }
+  foreach ( $terms['req'] as $term )
+  {
+    $term = escape_string_like($term);
+    if ( !$case_sensitive )
+      $term = strtolower($term);
+    $where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
+  }
+  foreach ( $terms['not'] as $term )
+  {
+    $term = escape_string_like($term);
+    if ( !$case_sensitive )
+      $term = strtolower($term);
+    $where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'";
+  }
+  if ( empty($where_any) )
+    unset($where_any, $where['any']);
+  if ( empty($where_req) )
+    unset($where_req, $where['req']);
+  if ( empty($where_not) )
+    unset($where_not, $where['not']);
+  
+  $where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')';
+  
+  if ( isset($where_req) )
+    $where_req = implode(' AND ', $where_req);
+  if ( isset($where_not) )
+  $where_not = implode( 'AND ', $where_not);
+  
+  $where = implode(' AND ', $where);
+  $sql = "SELECT img_id, img_title, img_desc FROM " . table_prefix . "gallery WHERE ( $where ) AND is_folder = 0;";
+  
+  if ( !($q = $db->sql_unbuffered_query($sql)) )
+  {
+    $db->_die('Error is in auto-generated SQL query in the Snapr plugin search module');
+  }
+  
+  if ( $row = $db->fetchrow() )
+  {
+    do
+    {
+      $idstring = 'ns=Image;pid=' . $row['img_id'];
+      foreach ( $word_list as $term )
+      {
+        if ( $case_sensitive )
+        {
+          if ( strstr($row['img_title'], $term) || strstr($row['img_desc'], $term) )
+          {
+            ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1;
+          }
+        }
+        else
+        {
+          if ( strstr(strtolower($row['img_title']), strtolower($term)) || strstr(strtolower($row['img_desc']), strtolower($term)) )
+          {
+            ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1;
+          }
+        }
+      }
+      // Generate text...
+      $text = highlight_and_clip_search_result(htmlspecialchars($row['img_desc']), $word_list);
+      
+      $preview_and_text = '
+        <table border="0" width="100%" cellspacing="0" cellpadding="0">
+          <tr>
+            <td valign="top">
+              ' . $text . '
+            </td>
+            <td valign="top" style="text-align: right; width: 80px; padding-left: 10px;">
+              <a href="' . makeUrlNS('Image', $row['img_id']) . '"><img alt="[thumbnail]" src="' . makeUrlNS('Special', "GalleryFetcher/thumb/{$row['img_id']}") . '" /></a>
+            </td>
+          </tr>
+        </table>
+      ';
+      
+      // Inject result
+      
+      if ( isset($scores[$idstring]) )
+      {
+        // echo('adding image "' . $row['img_title'] . '" to results<br />');
+        $page_data[$idstring] = array(
+          'page_name' => highlight_search_result(htmlspecialchars($row['img_title']), $word_list),
+          'page_text' => $preview_and_text,
+          'score' => $scores[$idstring],
+          'page_note' => '[Gallery image]',
+          'page_id' => strval($row['img_id']),
+          'namespace' => 'Image',
+          'page_length' => strlen($row['img_desc']),
+        );
+      }
+    }
+    while ( $row = $db->fetchrow() );
+    
+  }
+}
+
 ?>
--- a/plugins/gallery/sidebar.php	Thu Aug 09 12:34:39 2007 -0400
+++ b/plugins/gallery/sidebar.php	Wed Nov 21 15:22:13 2007 -0500
@@ -38,7 +38,7 @@
     $rand = array_rand($images);
     $image = $images[$rand];
     $acl = $session->fetch_page_acl(strval($rand), 'Gallery');
-    if ( $acl->get_permissions('read') )
+    if ( is_object($acl) && $acl->get_permissions('read') )
     {
       $my_image = $image;
       break;