Guest User

Search.php

a guest
Dec 11th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.71 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.   * Search class, Search.php
  5.   * Search management
  6.   * @category classes
  7.   *
  8.   * @author PrestaShop <[email protected]>
  9.   * @copyright PrestaShop
  10.   * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
  11.   * @version 1.2
  12.   *
  13. */
  14.  
  15. define('PS_SEARCH_MAX_WORD_LENGTH', 15);
  16.  
  17. /* Copied from Drupal search module, except for -\x{2f} in SEARCH_EXCLUDE because it's used to exclude words in a search */
  18. define('PREG_CLASS_SEARCH_EXCLUDE',
  19. '\x{0}\x{3a}-\x{40}\x{5b}-\x{60}\x{7b}-\x{bf}\x{d7}\x{f7}\x{2b0}-'.
  20. '\x{385}\x{387}\x{3f6}\x{482}-\x{489}\x{559}-\x{55f}\x{589}-\x{5c7}\x{5f3}-'.
  21. '\x{61f}\x{640}\x{64b}-\x{65e}\x{66a}-\x{66d}\x{670}\x{6d4}\x{6d6}-\x{6ed}'.
  22. '\x{6fd}\x{6fe}\x{700}-\x{70f}\x{711}\x{730}-\x{74a}\x{7a6}-\x{7b0}\x{901}-'.
  23. '\x{903}\x{93c}\x{93e}-\x{94d}\x{951}-\x{954}\x{962}-\x{965}\x{970}\x{981}-'.
  24. '\x{983}\x{9bc}\x{9be}-\x{9cd}\x{9d7}\x{9e2}\x{9e3}\x{9f2}-\x{a03}\x{a3c}-'.
  25. '\x{a4d}\x{a70}\x{a71}\x{a81}-\x{a83}\x{abc}\x{abe}-\x{acd}\x{ae2}\x{ae3}'.
  26. '\x{af1}-\x{b03}\x{b3c}\x{b3e}-\x{b57}\x{b70}\x{b82}\x{bbe}-\x{bd7}\x{bf0}-'.
  27. '\x{c03}\x{c3e}-\x{c56}\x{c82}\x{c83}\x{cbc}\x{cbe}-\x{cd6}\x{d02}\x{d03}'.
  28. '\x{d3e}-\x{d57}\x{d82}\x{d83}\x{dca}-\x{df4}\x{e31}\x{e34}-\x{e3f}\x{e46}-'.
  29. '\x{e4f}\x{e5a}\x{e5b}\x{eb1}\x{eb4}-\x{ebc}\x{ec6}-\x{ecd}\x{f01}-\x{f1f}'.
  30. '\x{f2a}-\x{f3f}\x{f71}-\x{f87}\x{f90}-\x{fd1}\x{102c}-\x{1039}\x{104a}-'.
  31. '\x{104f}\x{1056}-\x{1059}\x{10fb}\x{10fc}\x{135f}-\x{137c}\x{1390}-\x{1399}'.
  32. '\x{166d}\x{166e}\x{1680}\x{169b}\x{169c}\x{16eb}-\x{16f0}\x{1712}-\x{1714}'.
  33. '\x{1732}-\x{1736}\x{1752}\x{1753}\x{1772}\x{1773}\x{17b4}-\x{17db}\x{17dd}'.
  34. '\x{17f0}-\x{180e}\x{1843}\x{18a9}\x{1920}-\x{1945}\x{19b0}-\x{19c0}\x{19c8}'.
  35. '\x{19c9}\x{19de}-\x{19ff}\x{1a17}-\x{1a1f}\x{1d2c}-\x{1d61}\x{1d78}\x{1d9b}-'.
  36. '\x{1dc3}\x{1fbd}\x{1fbf}-\x{1fc1}\x{1fcd}-\x{1fcf}\x{1fdd}-\x{1fdf}\x{1fed}-'.
  37. '\x{1fef}\x{1ffd}-\x{2070}\x{2074}-\x{207e}\x{2080}-\x{2101}\x{2103}-\x{2106}'.
  38. '\x{2108}\x{2109}\x{2114}\x{2116}-\x{2118}\x{211e}-\x{2123}\x{2125}\x{2127}'.
  39. '\x{2129}\x{212e}\x{2132}\x{213a}\x{213b}\x{2140}-\x{2144}\x{214a}-\x{2b13}'.
  40. '\x{2ce5}-\x{2cff}\x{2d6f}\x{2e00}-\x{3005}\x{3007}-\x{303b}\x{303d}-\x{303f}'.
  41. '\x{3099}-\x{309e}\x{30a0}\x{30fb}-\x{30fe}\x{3190}-\x{319f}\x{31c0}-\x{31cf}'.
  42. '\x{3200}-\x{33ff}\x{4dc0}-\x{4dff}\x{a015}\x{a490}-\x{a716}\x{a802}\x{a806}'.
  43. '\x{a80b}\x{a823}-\x{a82b}\x{d800}-\x{f8ff}\x{fb1e}\x{fb29}\x{fd3e}\x{fd3f}'.
  44. '\x{fdfc}-\x{fe6b}\x{feff}-\x{ff0f}\x{ff1a}-\x{ff20}\x{ff3b}-\x{ff40}\x{ff5b}-'.
  45. '\x{ff65}\x{ff70}\x{ff9e}\x{ff9f}\x{ffe0}-\x{fffd}');
  46.  
  47. define('PREG_CLASS_NUMBERS',
  48. '\x{30}-\x{39}\x{b2}\x{b3}\x{b9}\x{bc}-\x{be}\x{660}-\x{669}\x{6f0}-\x{6f9}'.
  49. '\x{966}-\x{96f}\x{9e6}-\x{9ef}\x{9f4}-\x{9f9}\x{a66}-\x{a6f}\x{ae6}-\x{aef}'.
  50. '\x{b66}-\x{b6f}\x{be7}-\x{bf2}\x{c66}-\x{c6f}\x{ce6}-\x{cef}\x{d66}-\x{d6f}'.
  51. '\x{e50}-\x{e59}\x{ed0}-\x{ed9}\x{f20}-\x{f33}\x{1040}-\x{1049}\x{1369}-'.
  52. '\x{137c}\x{16ee}-\x{16f0}\x{17e0}-\x{17e9}\x{17f0}-\x{17f9}\x{1810}-\x{1819}'.
  53. '\x{1946}-\x{194f}\x{2070}\x{2074}-\x{2079}\x{2080}-\x{2089}\x{2153}-\x{2183}'.
  54. '\x{2460}-\x{249b}\x{24ea}-\x{24ff}\x{2776}-\x{2793}\x{3007}\x{3021}-\x{3029}'.
  55. '\x{3038}-\x{303a}\x{3192}-\x{3195}\x{3220}-\x{3229}\x{3251}-\x{325f}\x{3280}-'.
  56. '\x{3289}\x{32b1}-\x{32bf}\x{ff10}-\x{ff19}');
  57.  
  58. define('PREG_CLASS_PUNCTUATION',
  59. '\x{21}-\x{23}\x{25}-\x{2a}\x{2c}-\x{2f}\x{3a}\x{3b}\x{3f}\x{40}\x{5b}-\x{5d}'.
  60. '\x{5f}\x{7b}\x{7d}\x{a1}\x{ab}\x{b7}\x{bb}\x{bf}\x{37e}\x{387}\x{55a}-\x{55f}'.
  61. '\x{589}\x{58a}\x{5be}\x{5c0}\x{5c3}\x{5f3}\x{5f4}\x{60c}\x{60d}\x{61b}\x{61f}'.
  62. '\x{66a}-\x{66d}\x{6d4}\x{700}-\x{70d}\x{964}\x{965}\x{970}\x{df4}\x{e4f}'.
  63. '\x{e5a}\x{e5b}\x{f04}-\x{f12}\x{f3a}-\x{f3d}\x{f85}\x{104a}-\x{104f}\x{10fb}'.
  64. '\x{1361}-\x{1368}\x{166d}\x{166e}\x{169b}\x{169c}\x{16eb}-\x{16ed}\x{1735}'.
  65. '\x{1736}\x{17d4}-\x{17d6}\x{17d8}-\x{17da}\x{1800}-\x{180a}\x{1944}\x{1945}'.
  66. '\x{2010}-\x{2027}\x{2030}-\x{2043}\x{2045}-\x{2051}\x{2053}\x{2054}\x{2057}'.
  67. '\x{207d}\x{207e}\x{208d}\x{208e}\x{2329}\x{232a}\x{23b4}-\x{23b6}\x{2768}-'.
  68. '\x{2775}\x{27e6}-\x{27eb}\x{2983}-\x{2998}\x{29d8}-\x{29db}\x{29fc}\x{29fd}'.
  69. '\x{3001}-\x{3003}\x{3008}-\x{3011}\x{3014}-\x{301f}\x{3030}\x{303d}\x{30a0}'.
  70. '\x{30fb}\x{fd3e}\x{fd3f}\x{fe30}-\x{fe52}\x{fe54}-\x{fe61}\x{fe63}\x{fe68}'.
  71. '\x{fe6a}\x{fe6b}\x{ff01}-\x{ff03}\x{ff05}-\x{ff0a}\x{ff0c}-\x{ff0f}\x{ff1a}'.
  72. '\x{ff1b}\x{ff1f}\x{ff20}\x{ff3b}-\x{ff3d}\x{ff3f}\x{ff5b}\x{ff5d}\x{ff5f}-'.
  73. '\x{ff65}');  
  74.  
  75. class Search
  76. {
  77.     public static function sanitize($string, $id_lang, $indexation = false)
  78.     {
  79.         $string = Tools::strtolower(strip_tags($string));
  80.         $string = html_entity_decode($string, ENT_NOQUOTES, 'utf-8');
  81.  
  82.         $string = preg_replace('/(['.PREG_CLASS_NUMBERS.']+)['.PREG_CLASS_PUNCTUATION.']+(?=['.PREG_CLASS_NUMBERS.'])/u', '\1', $string);
  83.         if ($indexation)
  84.         {
  85.             $string = preg_replace('/[._-]+/', '', $string);
  86.             $string = preg_replace('/['.PREG_CLASS_SEARCH_EXCLUDE.'-\x{2f}]+/u', ' ', $string);
  87.         }
  88.         else
  89.         {
  90.             $string = preg_replace('/[._]+/', '', $string);
  91.             $string = ltrim(preg_replace('/([^ ])-/', '$1', ' '.$string));
  92.             $string = preg_replace('/[._]+/', '', $string);
  93.             $string = preg_replace('/[^\s]-+/', '', $string);
  94.             $string = preg_replace('/['.PREG_CLASS_SEARCH_EXCLUDE.']+/u', ' ', $string);
  95.         }
  96.         $blacklist = Configuration::get('PS_SEARCH_BLACKLIST', $id_lang);
  97.         if (!empty($blacklist))
  98.         {
  99.             $string = preg_replace('/(?<=\s)('.$blacklist.')(?=\s)/Su', '', $string);
  100.             $string = preg_replace('/^('.$blacklist.')(?=\s)/Su', '', $string);
  101.             $string = preg_replace('/(?<=\s)('.$blacklist.')$/Su', '', $string);
  102.             $string = preg_replace('/^('.$blacklist.')$/Su', '', $string);
  103.         }
  104.        
  105.         if (!$indexation)
  106.         {
  107.             $alias = new Alias(NULL, $string);
  108.             if (Validate::isLoadedObject($alias))
  109.                 $string = $alias->search;
  110.         }
  111.        
  112.         if ($indexation)
  113.         {
  114.             $minWordLen = Configuration::get('PS_SEARCH_MINWORDLEN');
  115.             if ($minWordLen)
  116.             {
  117.                 $string = preg_replace('/(?<=\s)[^\s]{1,'.$minWordLen.'}(?=\s)/Su', ' ', $string);
  118.                 $string = preg_replace('/^[^\s]{1,'.$minWordLen.'}(?=\s)/Su', '', $string);
  119.                 $string = preg_replace('/(?<=\s)[^\s]{1,'.$minWordLen.'}$/Su', '', $string);
  120.                 $string = preg_replace('/^[^\s]{1,'.$minWordLen.'}$/Su', '', $string);
  121.             }
  122.         }
  123.        
  124.         $string = trim(preg_replace('/\s+/', ' ', $string));
  125.         return $string;
  126.     }
  127.  
  128.     public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false)
  129.     {
  130.         global $cookie;
  131.  
  132.         // TODO : smart page management
  133.         if ($pageNumber < 1) $pageNumber = 1;
  134.         if ($pageSize < 1) $pageSize = 1;
  135.        
  136.         if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay))
  137.             die(Tools::displayError());
  138.            
  139.         $whereArray = array();
  140.         $scoreArray = array();
  141.         $words = explode(' ', Search::sanitize($expr, $id_lang));
  142.  
  143.         foreach ($words as $key => $word)
  144.             if (!empty($word))
  145.             {
  146.                 $word = str_replace('%', '\\%', $word);
  147.                 $word = str_replace('_', '\\_', $word);
  148.                 $whereArray[] = ' p.id_product '.($word[0] == '-' ? 'NOT' : '').' IN (
  149.                     SELECT id_product
  150.                     FROM '._DB_PREFIX_.'search_word sw
  151.                     LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word
  152.                     WHERE sw.id_lang = '.intval($id_lang).'
  153.                     AND sw.word LIKE '.($word[0] == '-' ? ' \''.pSQL(substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\'' : '\''.pSQL(substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'').'
  154.                 ) ';
  155.                 //echo $whereArray[count($whereArray)-1];
  156.                 if ($word[0] != '-')
  157.                     $scoreArray[] = 'sw.word LIKE \''.pSQL(substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'';
  158.             }
  159.             else
  160.                 unset($words[$key]);
  161.         if (!sizeof($words))
  162.             return ($ajax ? array() : array('total' => 0, 'result' => array()));
  163.            
  164.         $score = '';
  165.         if (sizeof($scoreArray))
  166.             $score = ',(
  167.                 SELECT SUM(weight)
  168.                 FROM '._DB_PREFIX_.'search_word sw
  169.                 LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word
  170.                 WHERE sw.id_lang = '.intval($id_lang).'
  171.                 AND si.id_product = p.id_product
  172.                 AND ('.implode(' OR ', $scoreArray).')
  173.             ) as position';
  174.        
  175.         if ($ajax)
  176.         {
  177.             $queryResults = '
  178.             SELECT p.id_product, pl.name as pname, IF(cl.name REGEXP "^[0-9]{2}\\.", SUBSTRING(cl.name, 4), cl.name) as cname   '.$score.', cl.link_rewrite as crewrite, pl.link_rewrite as prewrite
  179.             FROM '._DB_PREFIX_.'product p
  180.             LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
  181.             LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.intval($id_lang).')
  182.             LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)
  183.             INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`)
  184.             '.($cookie->id_customer ? 'INNER JOIN `'._DB_PREFIX_.'customer_group` cg ON (cg.`id_group` = ctg.`id_group`)' : '').'
  185.             WHERE '.implode(' AND ', $whereArray).'
  186.             AND p.active = 1
  187.             AND ('.($cookie->id_customer ? 'cg.`id_customer` = '.intval($cookie->id_customer).' OR' : '').' ctg.`id_group` = 1)
  188.             GROUP BY p.`id_product`
  189.             ORDER BY position DESC
  190.             LIMIT 10';
  191.             return Db::getInstance()->ExecuteS($queryResults);
  192.         }
  193.        
  194.         $queryResults = '
  195.         SELECT SQL_CALC_FOUND_ROWS p.*, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`,
  196.         t.`rate`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name '.($cookie->id_customer ? ', cg.`id_group`' : '').'
  197.         '.$score.'
  198.         FROM '._DB_PREFIX_.'product p
  199.         LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
  200.         LEFT OUTER JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  201.         LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
  202.         LEFT JOIN `'._DB_PREFIX_.'tax` t ON (p.`id_tax` = t.`id_tax`)
  203.         LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
  204.         LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)
  205.         INNER JOIN `'._DB_PREFIX_.'category_group` ctg ON (ctg.`id_category` = cp.`id_category`)
  206.         '.($cookie->id_customer ? 'INNER JOIN `'._DB_PREFIX_.'customer_group` cg ON (cg.`id_group` = ctg.`id_group`)' : '').'
  207.         WHERE '.implode(' AND ', $whereArray).'
  208.         AND p.active = 1
  209.         AND ('.($cookie->id_customer ? 'cg.`id_customer` = '.intval($cookie->id_customer).' OR' : '').' ctg.`id_group` = 1)
  210.         GROUP BY p.`id_product`
  211.         '.($orderBy ? 'ORDER BY  '.$orderBy : '').($orderWay ? ' '.$orderWay : '').'
  212.         LIMIT '.intval(($pageNumber - 1) * $pageSize).','.intval($pageSize);
  213.         $result = Db::getInstance()->ExecuteS($queryResults);
  214.         $total = Db::getInstance()->getValue('SELECT FOUND_ROWS()');
  215.  
  216.         Module::hookExec('search', array('expr' => $expr, 'total' => $total));
  217.  
  218.         return array('total' => $total,'result' => Product::getProductsProperties($id_lang, $result));
  219.     }
  220.    
  221.     public static function getTags($db, $id_product, $id_lang)
  222.     {
  223.         $tags = '';
  224.         $tagsArray = $db->ExecuteS('
  225.         SELECT t.name FROM '._DB_PREFIX_.'product_tag pt
  226.         LEFT JOIN '._DB_PREFIX_.'tag t ON (pt.id_tag = t.id_tag AND t.id_lang = '.intval($id_lang).')
  227.         WHERE pt.id_product = '.intval($id_product));
  228.         foreach ($tagsArray as $tag)
  229.             $tags .= $tag['name'].' ';
  230.         return $tags;
  231.     }
  232.  
  233.     public static function getAttributes($db, $id_product, $id_lang)
  234.     {
  235.         $attributes = '';
  236.         $attributesArray = $db->ExecuteS('
  237.         SELECT al.name FROM '._DB_PREFIX_.'product_attribute pa
  238.         LEFT JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pa.id_product_attribute = pac.id_product_attribute
  239.         LEFT JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang = '.intval($id_lang).')
  240.         WHERE pa.id_product = '.intval($id_product));
  241.         foreach ($attributesArray as $attribute)
  242.             $attributes .= $attribute['name'].' ';
  243.         return $attributes;
  244.     }
  245.  
  246.     public static function getFeatures($db, $id_product, $id_lang)
  247.     {
  248.         $features = '';
  249.         $featuresArray = $db->ExecuteS('
  250.         SELECT fvl.value FROM '._DB_PREFIX_.'feature_product fp
  251.         LEFT JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fp.id_feature_value = fvl.id_feature_value AND fvl.id_lang = '.intval($id_lang).')
  252.         WHERE fp.id_product = '.intval($id_product));
  253.         foreach ($featuresArray as $feature)
  254.             $features .= $feature['value'].' ';
  255.         return $features;
  256.     }
  257.    
  258.     public static function indexation($full = false)
  259.     {
  260.         $db = Db::getInstance();
  261.        
  262.         if ($full)
  263.         {
  264.             $db->Execute('TRUNCATE '._DB_PREFIX_.'search_index');
  265.             $db->Execute('TRUNCATE '._DB_PREFIX_.'search_word');
  266.             $db->Execute('UPDATE '._DB_PREFIX_.'product SET indexed = 0');
  267.         }
  268.         else
  269.             $db->Execute('DELETE FROM '._DB_PREFIX_.'search_index WHERE id_product IN (SELECT id_product FROM '._DB_PREFIX_.'product WHERE indexed = 0)');
  270.    
  271.         $weightArray = array(
  272.             'pname' => Configuration::get('PS_SEARCH_WEIGHT_PNAME'),
  273.             'reference' => Configuration::get('PS_SEARCH_WEIGHT_REF'),
  274.             'ean13' => Configuration::get('PS_SEARCH_WEIGHT_REF'),
  275.             'description_short' => Configuration::get('PS_SEARCH_WEIGHT_SHORTDESC'),
  276.             'description' => Configuration::get('PS_SEARCH_WEIGHT_DESC'),
  277.             'cname' => Configuration::get('PS_SEARCH_WEIGHT_CNAME'),
  278.             'mname' => Configuration::get('PS_SEARCH_WEIGHT_MNAME'),
  279.             'tags' => Configuration::get('PS_SEARCH_WEIGHT_TAG'),
  280.             'attributes' => Configuration::get('PS_SEARCH_WEIGHT_ATTRIBUTE'),
  281.             'features' => Configuration::get('PS_SEARCH_WEIGHT_FEATURE')
  282.         );
  283.        
  284.         $products = $db->ExecuteS('
  285.         SELECT p.id_product, pl.id_lang, pl.name as pname, p.reference, p.ean13, pl.description_short, pl.description, cl.name as cname, m.name as mname
  286.         FROM '._DB_PREFIX_.'product p
  287.         LEFT JOIN '._DB_PREFIX_.'product_lang pl ON p.id_product = pl.id_product
  288.         LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang)
  289.         LEFT JOIN '._DB_PREFIX_.'manufacturer m ON m.id_manufacturer = p.id_manufacturer
  290.         WHERE p.indexed = 0', false);
  291.        
  292.         while ($product = $db->nextRow($products))
  293.         {
  294.             $product['tags'] = self::getTags($db, $product['id_product'], $product['id_lang']);
  295.             $product['attributes'] = self::getAttributes($db, $product['id_product'], $product['id_lang']);
  296.             $product['features'] = self::getFeatures($db, $product['id_product'], $product['id_lang']);
  297.  
  298.             $pArray = array();
  299.             foreach ($product as $key => $value)
  300.                 if (strncmp($key, 'id_', 3))
  301.                 {
  302.                     if ($key == 'cname')
  303.                         $value = Category::hideCategoryPosition($value);
  304.                     //$words = explode(' ', self::sanitize($value, $product['id_lang'], true));
  305.                    
  306.                     $val = self::sanitize(str_replace('-',' ',$value), $product['id_lang'], true);
  307.                    
  308.                     $l = strlen($val);
  309.                     $nval = '';
  310.                     $last = true;
  311.                     for ($q=0;$q<$l;$q++) {
  312.                         if ($last == true) {
  313.                         $last = is_numeric(substr($val,$q,1));
  314.                         $nval .= substr($val,$q,1);
  315.                         } else {
  316.                         $last = is_numeric(substr($val,$q,1));
  317.                         if (is_numeric(substr($val,$q,1))) {
  318.                             $nval .= ' '. substr($val,$q,1);
  319.                         } else {
  320.                             $nval .= substr($val,$q,1);
  321.                         }
  322.                         }
  323.                     }  
  324.                     $words2 = explode(' ', $nval);
  325.                         $words = explode(' ', self::sanitize(str_replace('-',' ',$value), $product['id_lang'], true));
  326.                     $words = array_merge($words, $words2);
  327.                     foreach ($words as $word)
  328.                         if (!empty($word))
  329.                         {
  330.                             $word = substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH);
  331.                             if (!isset($pArray[$word]))
  332.                                 $pArray[$word] = $weightArray[$key];
  333.                             else
  334.                                 $pArray[$word] += $weightArray[$key];
  335.                         }
  336.                 }
  337.             $queryArray = array();
  338.             $queryArray2 = array();
  339.             foreach ($pArray as $word => $weight)
  340.             {
  341.                 $queryArray[] = '('.intval($product['id_lang']).',\''.pSQL($word).'\')';
  342.                 $queryArray2[] = '('.intval($product['id_product']).',(SELECT id_word FROM '._DB_PREFIX_.'search_word WHERE word = \''.pSQL($word).'\' AND id_lang = '.intval($product['id_lang']).' LIMIT 1),'.intval($weight).')';
  343.             }
  344.             if (sizeof($queryArray) AND sizeof($queryArray2))
  345.             {
  346.                 if (!($rows = $db->Execute('INSERT IGNORE INTO '._DB_PREFIX_.'search_word (id_lang, word) VALUES '.implode(',',$queryArray))) OR $rows != sizeof($queryArray))
  347.                     Tools::d(array(mysql_error(), $queryArray));
  348.                 if (!($rows = $db->Execute('INSERT INTO '._DB_PREFIX_.'search_index (id_product, id_word, weight) VALUES '.implode(',',$queryArray2).' ON DUPLICATE KEY UPDATE weight = weight + VALUES(weight)')) OR $rows != sizeof($queryArray2))
  349.                     Tools::d(array(mysql_error(), $queryArray2));
  350.             }
  351.             $db->Execute('UPDATE '._DB_PREFIX_.'product SET indexed = 1 WHERE id_product = '.intval($product['id_product']));
  352.         }
  353.         $db->Execute('DELETE FROM '._DB_PREFIX_.'search_word WHERE id_word NOT IN (SELECT id_word FROM '._DB_PREFIX_.'search_index)');
  354.         return true;
  355.     }
  356. }
  357.    
  358. ?>
Advertisement
Add Comment
Please, Sign In to add comment