Search Plugin - support exact, any, all options

Phoca plugins - support for all Phoca plugins except Phoca Gallery plugins
imperialWicket
Phoca Enthusiast
Phoca Enthusiast
Posts: 78
Joined: 28 Feb 2010, 15:39

Search Plugin - support exact, any, all options

Post by imperialWicket »

I needed to support the exact/any/all words radio buttons within the Phoca Gallery search, and made a couple minor updates to the Phoca Gallery Search Plugin. Here they are in case anyone else is looking for this functionality.

These changes apply to the JOOMLA_ROOT/plugins/search/phocagallery.php file. Line numbers and file references are based on the original phocagallery.php search plugin file from the plg_search_phocagallery_v2.6.0 package.

You must insert a case statement to generate unique WHERE clause contents based on the radio button selection (@ line 44, snippet includes the preceding and following statements for reference):

Code: Select all

	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;
			$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;
				$wheres[] 	= implode( ' OR ', $wheres2 );
			}
			$where = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wheres ) . ')';
			break;
	}

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


Now you must update the WHERE clause in both SQL queries so that the initial parenthetical is the string generated in the above code.

Change the original (@ lines 75-77):

Code: Select all

	        . ' WHERE ( a.title LIKE '.$text
		. ' OR a.name LIKE '.$text
		. ' OR a.description LIKE '.$text.' )'
To read:

Code: Select all

		. ' WHERE ( '.$where.' )'
Change the original (@ lines 153-155):

Code: Select all

		. ' WHERE ( a.title LIKE '.$text
		. ' OR a.filename LIKE '.$text
		. ' OR a.description LIKE '.$text.' )'
To read:

Code: Select all

		. ' WHERE ( '.$where.' )'
Enjoy.
User avatar
Jan
Phoca Hero
Phoca Hero
Posts: 48705
Joined: 10 Nov 2007, 18:23
Location: Czech Republic
Contact:

Re: Search Plugin - support exact, any, all options

Post by Jan »

Hi, great thank you, maybe it will be good to paste such plugin into joomlacode.org site as an alternative to standard plugin.

Thank you, Jan
If you find Phoca extensions useful, please support the project
Post Reply