Advertisement
Guest User

Untitled

a guest
Sep 25th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.02 KB | None | 0 0
  1. <?php
  2. class plgSearchHikashop_products extends JPlugin{
  3.     function plgSearchHikashop_products(&$subject, $config){
  4.         $this->loadLanguage('plg_search_hikashop_products');
  5.         $this->loadLanguage('plg_search_hikashop_products_override');
  6.         parent::__construct($subject, $config);
  7.         if(!isset($this->params)){
  8.             $plugin = JPluginHelper::getPlugin('search', 'hikashop_products');
  9.             if(version_compare(JVERSION,'2.5','<')){
  10.                 jimport('joomla.html.parameter');
  11.                 $this->params = new JParameter($plugin->params);
  12.             } else {
  13.                 $this->params = new JRegistry($plugin->params);
  14.             }
  15.         }
  16.     }
  17.  
  18.     function onContentSearchAreas(){
  19.         return $this->onSearchAreas();
  20.     }
  21.     function onContentSearch( $text, $phrase='', $ordering='', $areas=null ){
  22.         return $this->onSearch( $text, $phrase, $ordering, $areas );
  23.     }
  24.  
  25.     function &onSearchAreas(){
  26.         $areas = array(
  27.             'products' => JText::_('PRODUCTS')
  28.         );
  29.         return $areas;
  30.     }
  31.  
  32.     function onSearch( $text, $phrase='', $ordering='', $areas=null ){
  33.         if(!defined('DS'))
  34.             define('DS', DIRECTORY_SEPARATOR);
  35.         if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')) return array();
  36.         $db     = JFactory::getDBO();
  37.         if (is_array( $areas )) {
  38.             if (!array_intersect( $areas, array_keys( $this->onSearchAreas() ) )) {
  39.                 return array();
  40.             }
  41.         }
  42.  
  43.         $limit = $this->params->def( 'search_limit', 50 );
  44.  
  45.         $text = trim( $text );
  46.         if ( $text == '' ) {
  47.             return array();
  48.         }
  49.  
  50.         switch($ordering){
  51.             case 'alpha':
  52.                 $order = 'a.product_name ASC';
  53.                 break;
  54.             case 'newest':
  55.                 $order = 'a.product_modified DESC';
  56.                 break;
  57.             case 'oldest':
  58.                 $order = 'a.product_created ASC';
  59.                 break;
  60.             case 'popular':
  61.                 $order = 'a.product_hit DESC';
  62.                 break;
  63.             case 'category':
  64.             default:
  65.                 $order = 'a.product_name DESC';
  66.                 break;
  67.         }
  68.         $trans=hikashop_get('helper.translation');
  69.         $multi=$trans->isMulti();
  70.         $trans_table = 'jf_content';
  71.         if($trans->falang){
  72.             $trans_table = 'falang_content';
  73.         }
  74.  
  75.         $rows = array();
  76.  
  77.         $filters = array('a.product_published=1');
  78.  
  79.         $variants = (int)$this->params->get('variants','0');
  80.         if(!$variants){
  81.             $filters[]='a.product_type=\'main\'';
  82.         }
  83.         $out_of_stock = (int)$this->params->get('out_of_stock_display','1');
  84.         if(!$out_of_stock){
  85.             $filters[]='a.product_quantity!=0';
  86.         }
  87.  
  88.         hikashop_addACLFilters($filters,'product_access','a');
  89.         $leftjoin='';
  90.  
  91.         if(hikashop_level(2)){
  92.             $catFilters = array('category_published=1','category_type=\'product\'');
  93.             hikashop_addACLFilters($catFilters,'category_access');
  94.             $db->setQuery('SELECT category_id FROM '.hikashop_table('category').' WHERE '.implode(' AND ',$catFilters));
  95.             if(!HIKASHOP_J25){
  96.                 $cats = $db->loadResultArray();
  97.             } else {
  98.                 $cats = $db->loadColumn();
  99.             }
  100.             if(!empty($cats)){
  101.                 $filters[]='b.category_id IN ('.implode(',',$cats).')';
  102.             }
  103.         }
  104.  
  105.  
  106.         if($variants){
  107.             $leftjoin=' INNER JOIN '.hikashop_table('product_category').' AS b ON a.product_parent_id=b.product_id OR a.product_id=b.product_id';
  108.         }else{
  109.             $leftjoin=' INNER JOIN '.hikashop_table('product_category').' AS b ON a.product_id=b.product_id';
  110.         }
  111.  
  112.         $filters2 = array();
  113.  
  114.         if($multi){
  115.             $registry = JFactory::getConfig();
  116.             if(!HIKASHOP_J25){
  117.                 $code = $registry->getValue('config.jflang');
  118.             }else{
  119.                 $code = $registry->get('language');
  120.             }
  121.             $lg = $trans->getId($code);
  122.             $filters2[] = "b.reference_table='hikashop_product'";
  123.             $filters2[] = "b.published=1";
  124.             $filters2[] = 'b.language_id='.$lg;
  125.         }
  126.  
  127.         $fields = $this->params->get('fields','');
  128.         if(empty($fields)){
  129.             $fields = array('product_name','product_description');
  130.         }else{
  131.             $fields = explode(',',$fields);
  132.         }
  133.  
  134.         switch($phrase){
  135.             case 'exact':
  136.                 $text       = $db->Quote( '%'.hikashop_getEscaped( $text, true ).'%', false );
  137.                 $filters1 = array();
  138.                 foreach($fields as $f){
  139.                     $filters1[] = "a.".$f." LIKE ".$text;
  140.                 }
  141.  
  142.                 if($multi){
  143.                     $filters2[] = "b.value LIKE ".$text;
  144.                 }
  145.                 break;
  146.             case 'all':
  147.             case 'any':
  148.             default:
  149.                 $words = explode( ' ', $text );
  150.                 $wordFilters = array();
  151.                 //$subWordFilters1 = array();
  152.                 //$subWordFilters2 = array();
  153.                 $subWordFiltersX = array();
  154.                 $wordFilters2 = array();
  155.                 foreach ($words as $word) {
  156.                     $word       = $db->Quote( '%'.hikashop_getEscaped( $word, true ).'%', false );
  157.                     foreach($fields as $i => $f){
  158.                         $subWordFiltersX[$i][] = "a.".$f." LIKE ".$word;
  159.                     }
  160.                     //$subWordFilters1[]    = "a.product_name LIKE ".$word;
  161.                     //$subWordFilters2[]    = "a.product_description LIKE ".$word;
  162.                     if($multi){
  163.                         $wordFilters2[] = "b.value LIKE ".$word;
  164.                     }
  165.                 }
  166.                 foreach($subWordFiltersX as $i => $subWordFilters){
  167.                     $wordFilters[$i]= '((' .implode( ($phrase == 'all' ? ') AND (' : ') OR ('),$subWordFilters). '))';
  168.                 }
  169.                 //$wordFilters[0]= '(' .implode( ($phrase == 'all' ? ') AND (' : ') OR ('),$subWordFilters1). ')';
  170.                 //$wordFilters[1]= '(' .implode( ($phrase == 'all' ? ') AND (' : ') OR ('),$subWordFilters2). ')';
  171.                 $filters[] = '((' . implode( ') OR (', $wordFilters ) . '))';
  172.                 if($multi){
  173.                     $filters2[] = '((' . implode( ($phrase == 'all' ? ') AND (' : ') OR ('), $wordFilters2 ) . '))';
  174.                 }
  175.                 break;
  176.         }
  177.  
  178.         $new_page = (int)$this->params->get('new_page','1');
  179.  
  180.         $select = ' a.product_id AS id, a.product_name, a.product_alias, a.product_canonical, a.product_created AS created , a.product_description, "'.$new_page.'" AS browsernav';
  181.         if($variants){
  182.             $select.=', a.product_type, a.product_parent_id';
  183.         }
  184.         $count = 0;
  185.         if($multi && !empty($lg)){
  186.             $db->setQuery('SET SQL_BIG_SELECTS=1');
  187.             $db->query();
  188.             $query = ' SELECT DISTINCT '.$select.' FROM '.hikashop_table($trans_table,false) . ' AS b LEFT JOIN '.hikashop_table('product').' AS a ON b.reference_id=a.product_id WHERE '.implode(' AND ',$filters2).' ORDER BY '.$order;
  189.             $db->setQuery($query, 0, $limit);
  190.             $rows = $db->loadObjectList("id");
  191.             $count = count($rows);
  192.             if($count){
  193.                 $limit = $limit-$count;
  194.                 $filters[]='a.product_id NOT IN ('.implode(',',array_keys($rows)).')';
  195.             }
  196.         }
  197.  
  198.         if($limit){
  199.             if(!empty($leftjoin)){
  200.                 $select.=', b.category_id as category_id';
  201.             }
  202.             $db->setQuery('SET SQL_BIG_SELECTS=1');
  203.             $db->query();
  204.             $filters = implode(' AND ',$filters);
  205.             if(isset($filters1)){
  206.                 $filters = '('.$filters.') AND ('.implode(' OR ',$filters1).')';
  207.             }
  208.             $query = ' SELECT DISTINCT '.$select.' FROM '.hikashop_table('product') . ' AS a '.$leftjoin.' WHERE '.$filters.' GROUP BY (a.product_id) ORDER BY '.$order;
  209.             $db->setQuery( $query, 0, $limit );
  210.             $mainRows = $db->loadObjectList("id");
  211.             if(!empty($mainRows)){
  212.                 foreach($mainRows as $k => $main){
  213.                     $rows[$k]=$main;
  214.                 }
  215.                 $count = count( $rows );
  216.             }
  217.         }
  218.         if($count){
  219.  
  220.             if($multi && !empty($lg)){
  221.                 $query = ' SELECT * FROM '.hikashop_table($trans_table,false) . ' WHERE reference_table=\'hikashop_product\' AND language_id=\''.$lg.'\' AND published=1 AND reference_id IN ('.implode(',',array_keys($rows)).')';
  222.                 $db->setQuery($query);
  223.                 $trans = $db->loadObjectList();
  224.                 foreach($trans as $item){
  225.                     foreach($rows as $key => $row){
  226.                         if($row->id==$item->reference_id){
  227.                             if($item->reference_field=='product_name'){
  228.                                 $row->product_name=$item->value;
  229.                             }elseif($item->reference_field=='product_description'){
  230.                                 $row->product_description=$item->value;
  231.                             }else{
  232.                                 $row->product_name=$item->value;
  233.                             }
  234.                             break;
  235.                         }
  236.                     }
  237.                 }
  238.             }
  239.             $parent = '';
  240.             $item_id = $this->params->get('item_id','');
  241.             $menuClass = hikashop_get('class.menus');
  242.             $config =& hikashop_config();
  243.             $pathway_sef_name = $config->get('pathway_sef_name','category_pathway');
  244.             $menus=array();
  245.             $Itemid='';
  246.             if(!empty($item_id)){
  247.                 $Itemid='&Itemid='.$item_id;
  248.                 if($this->params->get('full_path',1)){
  249.                     $menuData = $menus[$item_id] = $menuClass->get($item_id);
  250.                     if(!empty($menuData->hikashop_params['selectparentlisting'])){
  251.                         $parent = '&'.$pathway_sef_name.'='.(int)$menuData->hikashop_params['selectparentlisting'];
  252.                     }
  253.                 }
  254.             }
  255.             $itemids=array();
  256.             $app= JFactory::getApplication();
  257.             $class = hikashop_get('class.product');
  258.             foreach ( $rows as $k => $row ) {
  259.                 if(!empty($row->category_id)){
  260.                     if(empty($item_id)){
  261.                         if(!isset($itemids[$row->category_id])) $itemids[$row->category_id] = $menuClass->getItemidFromCategory($row->category_id);
  262.                         $item_id = $itemids[$row->category_id];
  263.                     }
  264.                     if(!empty($item_id)){
  265.                         $Itemid='&Itemid='.$item_id;
  266.                     }
  267.                     if($this->params->get('full_path',1)){
  268.                         $parent = '&'.$pathway_sef_name.'='.(int)$row->category_id;
  269.                     }
  270.                     if(!$this->params->get('item_id','')) $item_id = '';
  271.                 }
  272.                 $class->addAlias($row);
  273.                 $row->title=$row->product_name;
  274.                 $row->text=$row->product_description;
  275.                 if($variants && $row->product_type=='variant'){
  276.                     static $mains = array();
  277.                     if(!isset($mains[$row->product_parent_id])){
  278.                         $mains[$row->product_parent_id] = $class->get((int)$row->product_parent_id);
  279.                         $class->addAlias($mains[$row->product_parent_id]);
  280.                     }
  281.                     $db = JFactory::getDBO();
  282.                     $db->setQuery('SELECT * FROM '.hikashop_table('variant').' AS a LEFT JOIN '.hikashop_table('characteristic') .' AS b ON a.variant_characteristic_id=b.characteristic_id WHERE a.variant_product_id='.(int)$row->id.' ORDER BY a.ordering');
  283.                     $row->characteristics = $db->loadObjectList();
  284.                     $class->checkVariant($row,$mains[$row->product_parent_id]);
  285.                     if(empty($row->title)){
  286.                         $row->title = $row->product_name;
  287.                     }
  288.                     if(empty($row->text)){
  289.                         $row->text = $mains[$row->product_parent_id]->product_description;
  290.                     }
  291.                 }
  292.                 if(empty($row->product_canonical)){
  293.                     $rows[$k]->href = 'index.php?option=com_hikashop&ctrl=product&task=show&name='.$row->alias.'&cid='.$row->id.$Itemid.$parent;
  294.                 }else{
  295.                     $rows[$k]->href = $row->product_canonical;
  296.                 }
  297.                 $rows[$k]->section  = JText::_( 'PRODUCT' );
  298.             }
  299.  
  300.         }
  301.         return $rows;
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement