Location: PHPKode > projects > HikaShop > back/extensions/plg_search_hikashop_products/hikashop_products.php
<?php
/**
 * @package		HikaShop for Joomla!
 * @version		1.4.3
 * @author		hikashop.com
 * @copyright	(C) 2010 HIKARI SOFTWARE. All rights reserved.
 * @license		GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
<?php
class plgSearchHikashop_products extends JPlugin{
	function plgSearchHikashop_products(&$subject, $config){
		$this->loadLanguage('plg_search_hikashop_products');
		$this->loadLanguage('plg_search_hikashop_products_override');
		parent::__construct($subject, $config);
		if(!isset($this->params)){
			$plugin =& JPluginHelper::getPlugin('search', 'hikashop_products');
			jimport('joomla.html.parameter');
			$this->params = new JParameter( $plugin->params );
		}
    }
	function onContentSearchAreas(){
    	return $this->onSearchAreas();
    }
	function onContentSearch(){
    	return $this->onSearch();
    }
	function &onSearchAreas(){
		$areas = array(
			'products' => JText::_('PRODUCTS')
		);
		return $areas;
	}
	function onSearch( $text, $phrase='', $ordering='', $areas=null ){
		if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return array();
		$db		=& JFactory::getDBO();
		if (is_array( $areas )) {
			if (!array_intersect( $areas, array_keys( $this->onSearchAreas() ) )) {
				return array();
			}
		}
		$limit = $this->params->def( 'search_limit', 50 );
		$text = trim( $text );
		if ( $text == '' ) {
			return array();
		}
		switch($ordering){
			case 'alpha':
				$order = 'a.product_name ASC';
				break;
			case 'newest':
				$order = 'a.product_modified DESC';
				break;
			case 'oldest':
				$order = 'a.product_created ASC';
				break;
			case 'popular':
				$order = 'a.product_hit DESC';
				break;
			case 'category':
			default:
				$order = 'a.product_name DESC';
				break;
		}
		$trans=hikashop::get('helper.translation');
		$multi=$trans->isMulti();
		$rows = array();
		$filters = array('a.product_published=1','a.product_type=\'main\'');
		$filters2 = array();
		if($multi){
			$registry =& JFactory::getConfig();
			$lang = $trans->getId($registry->getValue("config.jflang"));
			$filters2[] = "b.reference_table='hikashop_product'";
			$filters2[] = "b.published=1";
			$filters2[] = 'b.language_id='.$lang;
		}
		switch($phrase){
			case 'exact':
				$text		= $db->Quote( '%'.$db->getEscaped( $text, true ).'%', false );
				$filters[] = "a.product_name LIKE ".$text;
				$filters[] = "a.product_description LIKE ".$text;
				if($multi){
					$filters2[] = "b.value LIKE ".$text;
				}
				break;
			case 'all':
			case 'any':
			default:
				$words = explode( ' ', $text );
				$wordFilters = array();
				$subWordFilters1 = array();
				$subWordFilters2 = array();
				$wordFilters2 = array();
				foreach ($words as $word) {
					$word		= $db->Quote( '%'.$db->getEscaped( $word, true ).'%', false );
					$subWordFilters1[] 	= "a.product_name LIKE ".$word;
					$subWordFilters2[] 	= "a.product_description LIKE ".$word;
					if($multi){
						$wordFilters2[] = "b.value LIKE ".$word;
					}
				}
				$wordFilters[0]= '(' .implode( ($phrase == 'all' ? ') AND (' : ') OR ('),$subWordFilters1). ')';
				$wordFilters[1]= '(' .implode( ($phrase == 'all' ? ') AND (' : ') OR ('),$subWordFilters2). ')';
				$filters[] = '(' . implode( ') OR (', $wordFilters ) . ')';
				if($multi){
					$filters2[] = '(' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wordFilters2 ) . ')';
				}
				break;
		}
		$new_page = (int)$this->params->get('new_page','1');
		$select = ' a.product_id AS id, a.product_name AS title, a.product_created AS created , a.product_description AS text, "'.$new_page.'" AS browsernav';
		if($multi && !empty($lang)){
			$query = ' SELECT DISTINCT '.$select.' FROM '.hikashop::table('jf_content',false) . ' AS b LEFT JOIN '.hikashop::table('product').' AS a ON b.reference_id=a.product_id WHERE '.implode(' AND ',$filters2).' ORDER BY '.$order;
			$db->setQuery($query, 0, $limit);
			$rows = $db->loadObjectList("id");
			$count = count($rows);
			if($count){
				$limit = $limit-$count;
				$filters[]='a.product_id NOT IN ('.implode(',',array_keys($rows)).')';
			}
		}
		if($limit){
			$query = ' SELECT '.$select.' FROM '.hikashop::table('product') . ' AS a WHERE '.implode(' AND ',$filters).' ORDER BY '.$order;
			$db->setQuery( $query, 0, $limit );
			$mainRows = $db->loadObjectList("id");
			if(!empty($mainRows)){
				foreach($mainRows as $k => $main){
					$rows[$k]=$main;
				}
				$count = count( $rows );
			}
		}
		if($count){
			if($multi && !empty($lang)){
				$query = ' SELECT * FROM '.hikashop::table('jf_content',false) . ' WHERE reference_table=\'hikashop_product\' AND language_id=\''.$lang.'\' AND published=1 AND reference_id IN ('.implode(',',array_keys($rows)).')';
				$db->setQuery($query);
				$trans = $db->loadObjectList();
				foreach($trans as $item){
					foreach($rows as $key => $row){
						if($row->id==$item->reference_id){
							if($item->reference_field=='product_name'){
								$row->title=$item->value;
							}elseif($item->reference_field=='product_description'){
								$row->text=$item->value;
							}
							break;
						}
					}
				}
			}
			$item_id = $this->params->get('item_id','');
			if(!empty($item_id)){
				$item_id='&Itemid='.$item_id;
				if($this->params->get('full_path',1)){
					$menuClass = hikashop::get('class.menus');
					$menuData = $menuClass->get($item_id);
					if(!empty($menuData->hikashop_params['selectparentlisting'])){
						$parent = '&category_pathway='.(int)$menuData->hikashop_params['selectparentlisting'];
					}
				}
			}
			foreach ( $rows as $k => $row ) {
				$rows[$k]->href = hikashop::completeLink('product&task=show&name='.strtolower(preg_replace('#[^a-z0-9_-]#i','',$row->title)).'&cid='.$row->id.$item_id.$parent);
				$rows[$k]->section 	= JText::_( 'PRODUCT' );
			}
		}
		return $rows;
	}
}
Return current item: HikaShop