Phoca Gallery Search Plugin: Multiple keywords (any, exact, and all)

The Phoca Gallery suite of Joomla! components, plugins, and modules add excellent features, are quite capable, and are actively developed/updated. Nonetheless, these are free extensions, and occasionally you will encounter bugs or undesirable intended functionality in such items. One functionality that affected a couple of my projects was lacking features in the Phoca Gallery Search Plugin. I have a couple concerns with the Search Plugin:

    1. Lacking support for multiple keywords and the any, exact, and all radio options
    2. No preview image
    3. When selecting a result you are taken to the category - and not necessarily the search result image

There are justifiable reasons for some of these, and the resolution for some of these issues can require unique implementation depending on your Joomla! configuration (menu configuration, SEF options, etc. can affect how the code must work).

Getting to the point, I am going to address concern (1) - Lacking support for multiple keywords and the any, exact, and all radio options. I am saving preview images and direct image linking from search results for later posts. A couple of notes/pre-requisites:

    1. I have posted this solution multiple times on the Phoca forums, the only differences here are the plugin/component versions.
    2. You should use the Phoca forums as a first reference for questions/feature requests.
    3. You should consider donations/endorsements for Phoca extensions if you use them regularly.
    4. This is not a how-to for Phoca Gallery use or writing a custom Joomla! Search Plugin, I will gloss over explanations of how/why things work in favor of accomplishing the goal more quickly.
    5. This post references code from the Phoca Gallery component version 2.7.1 and the Phoca Gallery Search Plugin version 2.7.1. The Joomla! installation is version 1.5.18, and all template/configuration options for Joomla! are the defaults from a clean installation.

I uploaded and configured 9 images. The images are title and described with keyword combinations of font type (bold, italic, normal) and font color (blue, green, red). Since we are reviewing particular search functionalities, I will add that the title/description values all follow these formats: "bold and blue" and "Some bold and blue text on white background". I added a Phoca Gallery Category Layout menu item that links to the Phoca Gallery Category where my images are configured. I also added a Joomla! Search Internal Link menu item and enable the Phoca Gallery Search Plugin. I changed the Phoca Gallery Parameters to display the description in the Category Layout, and I reduced the display number to 5 images in the Category Layout.

Default Joomla! display of Phoca Gallery with sample images.

The Problem
Given the configuration described, when searching only Phoca Gallery content with the keywords "blue and bold", no results are returned for All words, Any words, or Exact Phrase searches. All words and Any words should both have at least one result (the "bold and blue" image). Exact Phrase is working as expected, since there is no image available including title or description includeing "blue and bold". Note that All words and Any words searches will strip common terms, thus removing the "and" from our search. Exact Phrase does not strip terms.

Sample screenshot of an errant search results set.

To highlight the root cause of the issue, when we search only Phoca Gallery content with the keywords "bold and blue", and use the Exact Phrase search technique, we get one result back (which is appropriate). If we supplement this action by searching only Phoca Gallery content with the keyword "bold", we see that a single word search returns appropriate results for all three search techniques. This indicates a likelihood that the Phoca Gallery Search Plugin is executing a search against the entered String, not the Array of keywords.

Screenshot of what seems like a successful search result set.

The Solution
We must update the search functionality in the Phoca Gallery Search Plugin to convert a keyword String to an Array of keywords, and then query the content based on the Array of keywords.

The logical code for the Phoca Gallery Search Plugin is located in the JOOMLA_ROOT/plugins/search/phocagallery.php file. To reiterate, the line numbers reference version 2.7.1.

First, we must add a case statement that generates unique WHERE clause contents depending on the search technique (We do not want to search based on an Array of keywords if the user selected the Exact Phrase search technique.). Add this case statement at line 52, note that I am including the preceding and following statements for reference:

   if ($text == '') {
      return array();
   }

  $wheres = array();
   switch ($phrase) {
      case 'exact':
         $text      = $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
         $wheres2    = array();
         $wheres2[]    = 'a.title LIKE '.$text;
         $wheres2[]    = 'a.description LIKE '.$text;
         $wheres2[]    = 'a.metakey LIKE '.$text;
         $wheres2[]    = 'a.metadesc LIKE '.$text;
         $where       = '(' . implode( ') OR (', $wheres2 ) . ')';
         break;

      case 'all':
      case 'any':
      default:
         $words = explode( ' ', $text );
         $wheres = array();
         foreach ($words as $word) {
            $word      = $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
            $wheres2    = array();
            $wheres2[]    = 'a.title LIKE '.$word;
            $wheres2[]    = 'a.description LIKE '.$word;
            $wheres2[]    = 'a.metakey LIKE '.$word;
            $wheres2[]    = 'a.metadesc LIKE '.$word;
            $wheres[]    = implode( ' OR ', $wheres2 );
         }
         $where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
         break;
   }

   $section = JText::_( 'Phoca Gallery');

Now we are generating an appropriate group of WHERE clause constraints, but the search plugin is not using our new constraints. We must update the both of the SQL statements (the Phoca Gallery Search Plugin initiates two SQL statements, one for Categories and one for Images) to use our updated "$where" constraints.

Locate the categories SQL statement (should be at or near line 106 if you made the case statement code insertion; there is a "// Categories" comment at the top of the query if you need to search for the location).

Update the first portion of the original categories WHERE clause. Replace the following:

    . ' WHERE ( a.title LIKE '.$text
		. ' OR a.name LIKE '.$text
		. ' OR a.metakey LIKE '.$text
		. ' OR a.metadesc LIKE '.$text
		. ' OR a.description LIKE '.$text.' )'

With the updated:

    . ' WHERE ( '.$where.' )'

Now locate the images SQL statement ("// Images" comment at or around 132, "$query" value assigned at or around 152).

Update the first portion of the original images WHERE clause. Replace the following:

		. ' WHERE ( a.title LIKE '.$text
		. ' OR a.filename LIKE '.$text
		. ' OR a.metakey LIKE '.$text
		. ' OR a.metadesc LIKE '.$text
		. ' OR a.description LIKE '.$text.' )'

With the updated:

    . ' WHERE ( '.$where.' )'

Save these updates, and try the search exercise again. Notice an Any words search for "bold blue" returns the appropriate 5 results, and an All words search for "bold blue" returns the appropriate 1 result.

Screenshot of successful search result set after updating the search plugin code.

Happy searching. Now, wouldn't it be nice if those search results displayed a thumbnail image?

UPDATE: After some troubleshooting and a few questions on this and thumbnail inclusion, I posted an updated version of this information here. Check it out.

Comments are closed.