diff --git a/modules/blocklayered/blocklayered.php b/modules/blocklayered/blocklayered.php index 749d45e23..0ef132b9d 100644 --- a/modules/blocklayered/blocklayered.php +++ b/modules/blocklayered/blocklayered.php @@ -20,7 +20,7 @@ * * @author PrestaShop SA * @copyright 2007-2011 PrestaShop SA -* @version Release: $Revision$ +* @version Release: $Revision: 9630 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registred Trademark & Property of PrestaShop SA */ @@ -37,7 +37,7 @@ class BlockLayered extends Module { $this->name = 'blocklayered'; $this->tab = 'front_office_features'; - $this->version = 1.4; + $this->version = 1.5; $this->author = 'PrestaShop'; $this->need_instance = 0; @@ -68,20 +68,20 @@ class BlockLayered extends Module $this->installIndexableAttributeTable(); $this->installProductAttributeTable(); - $this->indexUrl(); + $this->indexUrl(); $this->indexAttribute(); - if(Db::getInstance()->getValue('SELECT count(*) FROM `'._DB_PREFIX_.'product`') < 10000) // Lock price indexation if too many products + if (Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'product`') < 10000) // Lock price indexation if too many products self::fullPricesIndexProcess(); - + return true; - } + } else { // Installation failed (or hook registration) => uninstall the module $this->uninstall(); - return false; - } + return false; + } } public function uninstall() @@ -117,7 +117,7 @@ class BlockLayered extends Module private function installFriendlyUrlTable() { - Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_friendly_url`'); + Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_friendly_url`'); Db::getInstance()->execute(' CREATE TABLE `'._DB_PREFIX_.'layered_friendly_url` ( `id_layered_friendly_url` INT NOT NULL AUTO_INCREMENT, @@ -133,7 +133,7 @@ class BlockLayered extends Module private function installIndexableAttributeTable() { // Attributes Groups - Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_attribute_group`'); + Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_attribute_group`'); Db::getInstance()->execute(' CREATE TABLE `'._DB_PREFIX_.'layered_indexable_attribute_group` ( `id_attribute_group` INT NOT NULL, @@ -164,7 +164,7 @@ class BlockLayered extends Module // Features - Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_feature`'); + Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_feature`'); Db::getInstance()->execute(' CREATE TABLE `'._DB_PREFIX_.'layered_indexable_feature` ( `id_feature` INT NOT NULL, @@ -218,7 +218,7 @@ class BlockLayered extends Module public function indexAttribute($id_product = null) { if (is_null($id_product)) - Db::getInstance()->execute('TRUNCATE '._DB_PREFIX_.'layered_product_attribute'); + Db::getInstance()->execute('TRUNCATE '._DB_PREFIX_.'layered_product_attribute'); else Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_product_attribute WHERE id_product = '.(int)$id_product); @@ -227,7 +227,7 @@ class BlockLayered extends Module FROM '._DB_PREFIX_.'product_attribute pa INNER JOIN '._DB_PREFIX_.'product_attribute_combination pac ON pac.id_product_attribute = pa.id_product_attribute INNER JOIN '._DB_PREFIX_.'attribute a ON (a.id_attribute = pac.id_attribute) - INNER JOIN '._DB_PREFIX_.'attribute_group ag ON ag.id_attribute_group = a.id_attribute_group + INNER JOIN '._DB_PREFIX_.'attribute_group ag ON ag.id_attribute_group = a.id_attribute_group '.(is_null($id_product) ? '' : 'AND pa.id_product = '.(int)$id_product).' GROUP BY a.id_attribute, pa.id_product'); @@ -238,7 +238,7 @@ class BlockLayered extends Module */ public function indexUrl($ajax = false, $truncate = true) { - if($truncate) + if ($truncate) Db::getInstance()->execute('TRUNCATE '._DB_PREFIX_.'layered_friendly_url'); $attributeValuesByLang = array(); @@ -319,7 +319,7 @@ class BlockLayered extends Module FROM '._DB_PREFIX_.'category c INNER JOIN '._DB_PREFIX_.'category_lang cl ON (c.id_category = cl.id_category) WHERE cl.id_lang = '.(int)$filter['id_lang']); - foreach($categories as $category) + foreach ($categories as $category) { if (!isset($attributeValuesByLang[$category['id_lang']])) $attributeValuesByLang[$category['id_lang']] = array(); @@ -372,23 +372,23 @@ class BlockLayered extends Module 'category_name' => $filter['link_rewrite'], 'type' => $filter['type']); break; } - + // Foreach langs foreach ($attributeValuesByLang as $id_lang => $attributeValues) { // Foreach attributes generate a couple "/_". For example: color_blue foreach ($attributeValues as $attribute) - foreach($attribute as $param) + foreach ($attribute as $param) { - $selectedFilters = array(); - $link = '/'.str_replace('-', '_', Tools::link_rewrite($param['name'])).'-'.str_replace('-', '_', Tools::link_rewrite($param['value'])); - $selectedFilters[$param['type']] = array(); - if (!isset($param['id_id_value'])) - $param['id_id_value'] = $param['id_value']; - $selectedFilters[$param['type']][$param['id_id_value']] = $param['id_value']; + $selectedFilters = array(); + $link = '/'.str_replace('-', '_', Tools::link_rewrite($param['name'])).'-'.str_replace('-', '_', Tools::link_rewrite($param['value'])); + $selectedFilters[$param['type']] = array(); + if (!isset($param['id_id_value'])) + $param['id_id_value'] = $param['id_value']; + $selectedFilters[$param['type']][$param['id_id_value']] = $param['id_value']; $urlKey = md5($link); - $idLayeredFriendlyUrl = Db::getInstance()->getValue('SELECT id_layered_friendly_url - FROM `'._DB_PREFIX_.'layered_friendly_url` WHERE `id_lang` = '.$id_lang.' AND `url_key` = \''.$urlKey.'\''); + $idLayeredFriendlyUrl = Db::getInstance()->getValue('SELECT id_layered_friendly_url + FROM `'._DB_PREFIX_.'layered_friendly_url` WHERE `id_lang` = '.$id_lang.' AND `url_key` = \''.$urlKey.'\''); if ($idLayeredFriendlyUrl == false) { Db::getInstance()->AutoExecute(_DB_PREFIX_.'layered_friendly_url', array('url_key' => $urlKey, 'data' => serialize($selectedFilters), 'id_lang' => $id_lang), 'INSERT'); @@ -399,7 +399,7 @@ class BlockLayered extends Module if ($ajax) return '{"result": 1}'; else - return 1; + return 1; } public function translateWord($string, $id_lang ) @@ -409,7 +409,7 @@ class BlockLayered extends Module $file = _PS_MODULE_DIR_.$this->name.'/'.Language::getIsoById($id_lang).'.php'; - if (!array_key_exists($id_lang,$_MODULES)) + if (!array_key_exists($id_lang, $_MODULES)) { if (!file_exists($file)) return $string; @@ -426,16 +426,16 @@ class BlockLayered extends Module if (isset($_MODULES[$id_lang][$currentKey])) $ret = stripslashes($_MODULES[$id_lang][$currentKey]); - elseif (isset($_MODULES[$id_lang][Tools::strtolower($currentKey)])) + else if (isset($_MODULES[$id_lang][Tools::strtolower($currentKey)])) $ret = stripslashes($_MODULES[$id_lang][Tools::strtolower($currentKey)]); - elseif (isset($_MODULES[$id_lang][$defaultKey])) + else if (isset($_MODULES[$id_lang][$defaultKey])) $ret = stripslashes($_MODULES[$id_lang][$defaultKey]); - elseif (isset($_MODULES[$id_lang][Tools::strtolower($defaultKey)])) + else if (isset($_MODULES[$id_lang][Tools::strtolower($defaultKey)])) $ret = stripslashes($_MODULES[$id_lang][Tools::strtolower($defaultKey)]); else $ret = stripslashes($string); - return str_replace('"', '"', $ret); + return str_replace('"', '"', $ret); } public function hookProductListAssign($params) @@ -485,7 +485,7 @@ class BlockLayered extends Module Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_feature_lang_value VALUES ('.(int)$params['id_feature'].', '.$id_lang.', \''.pSQL(Tools::link_rewrite(Tools::getValue('url_name_'.$id_lang))).'\', \''.pSQL(Tools::safeOutput(Tools::getValue('meta_title_'.$id_lang), true)).'\')'); - } + } } public function hookAfterSaveFeatureValue($params) @@ -660,7 +660,7 @@ class BlockLayered extends Module Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value VALUES ('.(int)$params['id_attribute_group'].', '.$id_lang.', \''.pSQL(Tools::link_rewrite(Tools::getValue('url_name_'.$id_lang))).'\', \''.pSQL(Tools::safeOutput(Tools::getValue('meta_title_'.$id_lang), true)).'\')'); - } + } } public function hookPostProcessAttributeGroup($params) @@ -708,7 +708,67 @@ class BlockLayered extends Module foreach ($result as $data) $langValue[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']); - if($indexable === false) + if ($indexable === false) + $on = true; + else + $on = (bool)$indexable; + + $return = '
+ +
+ '; + foreach ($languages as $language) + $return .= ' +
+ + '.$this->l('Invalid characters:').' <>;=#{}_  +

'.$this->l('Specific format in url block layered generation').'

+
'; + $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true); + $return .= ' +
+
+ +
'; + foreach ($languages as $language) + $return .= ' +
+ +

'.$this->l('Specific format for meta title').'

+
'; + $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true); + $return .= ' +
+
+ +
+ + + + +

'.$this->l('Use this attribute in url generated by the module block layered navigation').'

+
'; + return $return; + } + + public function hookFeatureForm($params) + { + $languages = Language::getLanguages(false); + $default_form_language = (int)(Configuration::get('PS_LANG_DEFAULT')); + $indexable = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT indexable FROM '._DB_PREFIX_.'layered_indexable_feature WHERE id_feature = '.(int)$params['id_feature']); + $langValue = array(); + + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( + 'SELECT url_name, meta_title, id_lang FROM '._DB_PREFIX_.'layered_indexable_feature_lang_value + WHERE id_feature = '.(int)$params['id_feature']); + if ($result) + foreach ($result as $data) + $langValue[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']); + + + if ($indexable === false) $on = true; else $on = (bool)$indexable; @@ -744,69 +804,9 @@ class BlockLayered extends Module
- + - - -

'.$this->l('Use this attribute in url generated by the module block layered navigation').'

-
'; - return $return; - } - - public function hookFeatureForm($params) - { - $languages = Language::getLanguages(false); - $default_form_language = (int)(Configuration::get('PS_LANG_DEFAULT')); - $indexable = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT indexable FROM '._DB_PREFIX_.'layered_indexable_feature WHERE id_feature = '.(int)$params['id_feature']); - $langValue = array(); - - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS( - 'SELECT url_name, meta_title, id_lang FROM '._DB_PREFIX_.'layered_indexable_feature_lang_value - WHERE id_feature = '.(int)$params['id_feature']); - if ($result) - foreach ($result as $data) - $langValue[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']); - - - if($indexable === false) - $on = true; - else - $on = (bool)$indexable; - - $return = '
- -
- '; - foreach ($languages as $language) - $return .= ' -
- - '.$this->l('Invalid characters:').' <>;=#{}_  -

'.$this->l('Specific format in url block layered generation').'

-
'; - $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true); - $return .= ' -
-
- -
'; - foreach ($languages as $language) - $return .= ' -
- -

'.$this->l('Specific format for meta title').'

-
'; - $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true); - $return .= ' -
-
- -
- - - +

'.$this->l('Use this attribute in url generated by the module block layered navigation').'

'; @@ -858,7 +858,8 @@ class BlockLayered extends Module if (($nbProducts > 0 && !$full || $cursor < $nbProducts && $full) && !$ajax) { $token = substr(Tools::encrypt('blocklayered/index'), 0, 10); - if (!Tools::file_get_contents(Tools::getProtocol().Tools::getHttpHost().__PS_BASE_URI__.'modules/blocklayered/blocklayered-price-indexer.php?token='.$token.'&cursor='.(int)$cursor.'&full='.(int)$full)) + if (!Tools::file_get_contents(Tools::getCurrentUrlProtocolPrefix().Tools::getHttpHost(). + __PS_BASE_URI__.'modules/blocklayered/blocklayered-price-indexer.php?token='.$token.'&cursor='.(int)$cursor.'&full='.(int)$full)) self::indexPrices((int)$cursor, (int)$full); return $cursor; } @@ -913,8 +914,8 @@ class BlockLayered extends Module if (is_null($groups)) { $groups = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_group FROM `'._DB_PREFIX_.'group_reduction`'); - if(!$groups) - $groups = array(); + if (!$groups) + $groups = array(); } static $currencyList = null; @@ -1003,9 +1004,9 @@ class BlockLayered extends Module $values = array(); foreach ($currencyList as $currency) $values[] = '('.(int)$idProduct.', '.(int)$currency['id_currency'].', - '.(int)$minPrice[$currency['id_currency']].', '.(int)($maxPrice[$currency['id_currency']] * (100+$maxTaxRate) / 100).')'; + '.(int)$minPrice[$currency['id_currency']].', '.(int)($maxPrice[$currency['id_currency']] * (100 + $maxTaxRate) / 100).')'; - Db::getInstance()->execute(' + Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'layered_price_index` (id_product, id_currency, price_min, price_max) VALUES '.implode(',', $values).' ON DUPLICATE KEY UPDATE id_product = id_product # avoid duplicate keys'); @@ -1036,7 +1037,7 @@ class BlockLayered extends Module $idLang = (int)$cookie->id_lang; $category = new Category((int)Tools::getValue('id_category')); $categoryMetas = Tools::getMetaTags($idLang, ''); - $categoryTitle = (empty($category->meta_title[$idLang])?$category->name[$idLang]:$category->meta_title[$idLang]); + $categoryTitle = (empty($category->meta_title[$idLang]) ? $category->name[$idLang] : $category->meta_title[$idLang]); // Generate meta title and meta description $title = ''; @@ -1059,7 +1060,7 @@ class BlockLayered extends Module $this->context->controller->addJs($this->_path.'blocklayered.js'); $this->context->controller->addJqueryUI('ui.slider'); - $this->context->controller->addCSS($this->_path.'blocklayered.css', 'all'); + $this->context->controller->addCSS($this->_path.'blocklayered.css', 'all'); Tools::addJS(_PS_JS_DIR_.'jquery/jquery.scrollTo-1.4.2-min.js'); } @@ -1105,97 +1106,97 @@ class BlockLayered extends Module if (Tools::isSubmit('SubmitFilter')) { - if(!Tools::getValue('layered_tpl_name')) - $html .= ' + if (!Tools::getValue('layered_tpl_name')) + $html .= '
X '.$this->l('Filter template name required (cannot be empty)').' -
'; + '; else - { + { if (isset($_POST['id_layered_filter']) && $_POST['id_layered_filter']) - Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_filter WHERE id_layered_filter = '.(int)Tools::getValue('id_layered_filter')); - - if (Tools::getValue('scope') == 1) - { - Db::getInstance()->execute('TRUNCATE TABLE '._DB_PREFIX_.'layered_filter'); - $categories = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_category FROM '._DB_PREFIX_.'category'); + Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_filter WHERE id_layered_filter = '.(int)Tools::getValue('id_layered_filter')); + + if (Tools::getValue('scope') == 1) + { + Db::getInstance()->execute('TRUNCATE TABLE '._DB_PREFIX_.'layered_filter'); + $categories = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_category FROM '._DB_PREFIX_.'category'); foreach ($categories as $category) - $_POST['categoryBox'][] = (int)$category['id_category']; - } - + $_POST['categoryBox'][] = (int)$category['id_category']; + } + if (count($_POST['categoryBox'])) - { - /* Clean categoryBox before use */ + { + /* Clean categoryBox before use */ if (isset($_POST['categoryBox']) && is_array($_POST['categoryBox'])) foreach ($_POST['categoryBox'] as &$categoryBoxTmp) $categoryBoxTmp = (int)$categoryBoxTmp; - + Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_category WHERE id_category IN ('.implode(',', array_map('intval', $_POST['categoryBox'])).')'); - - $filterValues = array(); + + $filterValues = array(); foreach ($_POST['categoryBox'] as $idc) $filterValues['categories'][] = (int)$idc; - - $sqlToInsert = 'INSERT INTO '._DB_PREFIX_.'layered_category (id_category, id_value, type, position) VALUES '; + + $sqlToInsert = 'INSERT INTO '._DB_PREFIX_.'layered_category (id_category, id_value, type, position) VALUES '; foreach ($_POST['categoryBox'] as $id_category_layered) - { - $n = 0; + { + $n = 0; foreach ($_POST as $key => $value) if (substr($key, 0, 17) == 'layered_selection' && $value == 'on') - { - $filterValues[$key] = $value; - $n++; - if ($key == 'layered_selection_stock') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'quantity\','.(int)$n.'),'; - elseif ($key == 'layered_selection_subcategories') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'category\','.(int)$n.'),'; - elseif ($key == 'layered_selection_condition') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'condition\','.(int)$n.'),'; - elseif ($key == 'layered_selection_weight_slider') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'weight\','.(int)$n.'),'; - elseif ($key == 'layered_selection_price_slider') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'price\','.(int)$n.'),'; - elseif ($key == 'layered_selection_manufacturer') - $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'manufacturer\','.(int)$n.'),'; - elseif (substr($key, 0, 21) == 'layered_selection_ag_') - $sqlToInsert .= '('.(int)$id_category_layered.','.(int)str_replace('layered_selection_ag_', '', $key).',\'id_attribute_group\','.(int)$n.'),'; - elseif (substr($key, 0, 23) == 'layered_selection_feat_') - $sqlToInsert .= '('.(int)$id_category_layered.','.(int)str_replace('layered_selection_feat_', '', $key).',\'id_feature\','.(int)$n.'),'; - } - } - - Db::getInstance()->execute(rtrim($sqlToInsert, ',')); - + { + $filterValues[$key] = $value; + $n++; + if ($key == 'layered_selection_stock') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'quantity\','.(int)$n.'),'; + else if ($key == 'layered_selection_subcategories') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'category\','.(int)$n.'),'; + else if ($key == 'layered_selection_condition') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'condition\','.(int)$n.'),'; + else if ($key == 'layered_selection_weight_slider') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'weight\','.(int)$n.'),'; + else if ($key == 'layered_selection_price_slider') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'price\','.(int)$n.'),'; + else if ($key == 'layered_selection_manufacturer') + $sqlToInsert .= '('.(int)$id_category_layered.',NULL,\'manufacturer\','.(int)$n.'),'; + else if (substr($key, 0, 21) == 'layered_selection_ag_') + $sqlToInsert .= '('.(int)$id_category_layered.','.(int)str_replace('layered_selection_ag_', '', $key).',\'id_attribute_group\','.(int)$n.'),'; + else if (substr($key, 0, 23) == 'layered_selection_feat_') + $sqlToInsert .= '('.(int)$id_category_layered.','.(int)str_replace('layered_selection_feat_', '', $key).',\'id_feature\','.(int)$n.'),'; + } + } + + Db::getInstance()->execute(rtrim($sqlToInsert, ',')); + $valuesToInsert = array( 'name' => pSQL(Tools::getValue('layered_tpl_name')), 'filters' => pSQL(serialize($filterValues)), 'n_categories' => (int)count($filterValues['categories']), 'date_add' => date('Y-m-d H:i:s')); if (isset($_POST['id_layered_filter']) && $_POST['id_layered_filter']) - $valuesToInsert['id_layered_filter'] = (int)Tools::getValue('id_layered_filter'); - - Db::getInstance()->AutoExecute(_DB_PREFIX_.'layered_filter', $valuesToInsert, 'INSERT'); + $valuesToInsert['id_layered_filter'] = (int)Tools::getValue('id_layered_filter'); + + Db::getInstance()->AutoExecute(_DB_PREFIX_.'layered_filter', $valuesToInsert, 'INSERT'); echo '
'.$this->l('Your filter').' "'.Tools::safeOutput(Tools::getValue('layered_tpl_name')).'" '. ((isset($_POST['id_layered_filter']) && $_POST['id_layered_filter']) ? $this->l('was updated successfully.') : $this->l('was added successfully.')).'
'; + } } } + else if (Tools::isSubmit('submitLayeredSettings')) + { + Configuration::updateValue('PS_LAYERED_HIDE_0_VALUES', Tools::getValue('ps_layered_hide_0_values')); + Configuration::updateValue('PS_LAYERED_SHOW_QTIES', Tools::getValue('ps_layered_show_qties')); + + $html .= ' +
+ '.$this->l('Settings saved successfully').' +
'; } - elseif (Tools::isSubmit('submitLayeredSettings')) - { - Configuration::updateValue('PS_LAYERED_HIDE_0_VALUES', Tools::getValue('ps_layered_hide_0_values')); - Configuration::updateValue('PS_LAYERED_SHOW_QTIES', Tools::getValue('ps_layered_show_qties')); - - $html .= ' -
- '.$this->l('Settings saved successfully').' -
'; - } - elseif (isset($_GET['deleteFilterTemplate'])) + else if (isset($_GET['deleteFilterTemplate'])) { $layeredValues = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT filters @@ -1232,7 +1233,7 @@ class BlockLayered extends Module '.$this->l('Indexes and caches').' '; - if(!Configuration::get('PS_LAYERED_INDEXED')) + if (!Configuration::get('PS_LAYERED_INDEXED')) $html .= ' '; $categoryList = array(); - foreach(Db::getInstance()->executeS('SELECT id_category FROM `'._DB_PREFIX_.'category`') as $category) - if($category['id_category'] != 1) + foreach (Db::getInstance()->executeS('SELECT id_category FROM `'._DB_PREFIX_.'category`') as $category) + if ($category['id_category'] != 1) $categoryList[] = $category['id_category']; + $base_url = Tools::getCurrentUrlProtocolPrefix().Tools::getHttpHost().__PS_BASE_URI__; $html .= ' '. + href="'.$base_url.'modules/blocklayered/blocklayered-price-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'">'. $this->l('Index all missing prices').'
'. + href="'.$base_url.'modules/blocklayered/blocklayered-price-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&full=1">'. $this->l('Re-build entire price index').'
'. + href="'.$base_url.'modules/blocklayered/blocklayered-attribute-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'">'. $this->l('Build attribute index').'
'. + href="'.$base_url.'modules/blocklayered/blocklayered-url-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&truncate=1">'. $this->l('Build url index').'

'.$this->l('You can set a cron job that will re-build price index using the following URL:').'
'. - Tools::getProtocol().Tools::getHttpHost().__PS_BASE_URI__.'modules/blocklayered/blocklayered-price-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&full=1 + $base_url.'modules/blocklayered/blocklayered-price-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&full=1
'.$this->l('You can set a cron job that will re-build url index using the following URL:').'
'. - Tools::getProtocol().Tools::getHttpHost().__PS_BASE_URI__.'modules/blocklayered/blocklayered-url-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&truncate=1 + $base_url.'modules/blocklayered/blocklayered-url-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&truncate=1
'.$this->l('You can set a cron job that will re-build attribute index using the following URL:').'
'. - Tools::getProtocol().Tools::getHttpHost().__PS_BASE_URI__.'modules/blocklayered/blocklayered-attribute-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).' + $base_url.'modules/blocklayered/blocklayered-attribute-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'

'.$this->l('A nightly rebuild is recommended.').' @@ -1724,7 +1728,7 @@ class BlockLayered extends Module '.$this->l('No').' '.$this->l('No').' - +

@@ -1742,17 +1746,17 @@ class BlockLayered extends Module // Force attributes selection (by url '.../2-mycategory/color-blue' or by get parameter 'selected_filters') if (strpos($_SERVER['SCRIPT_FILENAME'], 'blocklayered-ajax.php') === false || Tools::getValue('selected_filters') !== false) { - if(Tools::getValue('selected_filters')) + if (Tools::getValue('selected_filters')) $url = Tools::getValue('selected_filters'); else $url = preg_replace('/\/(?:\w*)\/(?:[0-9]+[-\w]*)([^\?]*)\??.*/', '$1', Tools::safeOutput($_SERVER['REQUEST_URI'], true)); - $urlAttributes = explode('/',$url); + $urlAttributes = explode('/', $url); array_shift($urlAttributes); $selectedFilters = array('category' => array()); - if(!empty($urlAttributes)) + if (!empty($urlAttributes)) { - foreach($urlAttributes as $urlAttribute) + foreach ($urlAttributes as $urlAttribute) { $urlParameters = explode('-', $urlAttribute); $attributeName = array_shift($urlParameters); @@ -1760,24 +1764,24 @@ class BlockLayered extends Module $selectedFilters[$attributeName] = array($urlParameters[0], $urlParameters[1]); else { - foreach($urlParameters as $urlParameter) - { - $data = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'layered_friendly_url` WHERE `url_key` = \''.md5('/'.$attributeName.'-'.$urlParameter).'\''); - if($data) - foreach(unserialize($data) as $keyParams => $params) - { - if(!isset($selectedFilters[$keyParams])) - $selectedFilters[$keyParams] = array(); - foreach($params as $keyParam => $param) + foreach ($urlParameters as $urlParameter) + { + $data = Db::getInstance()->getValue('SELECT data FROM `'._DB_PREFIX_.'layered_friendly_url` WHERE `url_key` = \''.md5('/'.$attributeName.'-'.$urlParameter).'\''); + if ($data) + foreach (unserialize($data) as $keyParams => $params) { - if(!isset($selectedFilters[$keyParams][$keyParam])) - $selectedFilters[$keyParams][$keyParam] = array(); - $selectedFilters[$keyParams][$keyParam] = $param; + if (!isset($selectedFilters[$keyParams])) + $selectedFilters[$keyParams] = array(); + foreach ($params as $keyParam => $param) + { + if (!isset($selectedFilters[$keyParams][$keyParam])) + $selectedFilters[$keyParams][$keyParam] = array(); + $selectedFilters[$keyParams][$keyParam] = $param; + } } - } + } } } - } return $selectedFilters; } } @@ -1799,21 +1803,21 @@ class BlockLayered extends Module $selectedFilters['condition'][] = $value; else if ($res[1] == 'quantity' && (!$value || $value == 1)) $selectedFilters['quantity'][] = $value; - elseif (in_array($res[1], array('category', 'manufacturer'))) + else if (in_array($res[1], array('category', 'manufacturer'))) { if (!isset($selectedFilters[$res[1].($id_key ? '_'.$id_key : '')])) $selectedFilters[$res[1].($id_key ? '_'.$id_key : '')] = array(); $selectedFilters[$res[1].($id_key ? '_'.$id_key : '')][] = (int)$value; } - elseif (in_array($res[1], array('id_attribute_group', 'id_feature'))) + else if (in_array($res[1], array('id_attribute_group', 'id_feature'))) { if (!isset($selectedFilters[$res[1]])) $selectedFilters[$res[1]] = array(); $selectedFilters[$res[1]][(int)$value] = $id_key.'_'.(int)$value; } - elseif ($res[1] == 'weight') + else if ($res[1] == 'weight') $selectedFilters[$res[1]] = $tmpTab; - elseif ($res[1] == 'price') + else if ($res[1] == 'price') $selectedFilters[$res[1]] = $tmpTab; } } @@ -1856,12 +1860,12 @@ class BlockLayered extends Module $subQueries = array(); foreach ($filterValues as $filterValue) { - $filterValueArray = explode('_',$filterValue); - if(!isset($subQueries[$filterValueArray[0]])) + $filterValueArray = explode('_', $filterValue); + if (!isset($subQueries[$filterValueArray[0]])) $subQueries[$filterValueArray[0]] = array(); $subQueries[$filterValueArray[0]][] = 'fp.`id_feature_value` = '.(int)$filterValueArray[1]; } - foreach($subQueries as $subQuery) + foreach ($subQueries as $subQuery) { $queryFiltersWhere .= ' AND p.id_product IN (SELECT `id_product` FROM `'._DB_PREFIX_.'feature_product` fp WHERE '; $queryFiltersWhere .= implode(' OR ', $subQuery).') '; @@ -1874,17 +1878,17 @@ class BlockLayered extends Module foreach ($filterValues as $filterValue) { - $filterValueArray = explode('_',$filterValue); + $filterValueArray = explode('_', $filterValue); if (!isset($subQueries[$filterValueArray[0]])) $subQueries[$filterValueArray[0]] = array(); $subQueries[$filterValueArray[0]][] = 'pac.`id_attribute` = '.(int)$filterValueArray[1]; } - foreach($subQueries as $subQuery) + foreach ($subQueries as $subQuery) { $queryFiltersWhere .= ' AND p.id_product IN (SELECT pa.`id_product` - FROM `'._DB_PREFIX_.'product_attribute_combination` pac - LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa - ON (pa.`id_product_attribute` = pac.`id_product_attribute`) WHERE '; + FROM `'._DB_PREFIX_.'product_attribute_combination` pac + LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa + ON (pa.`id_product_attribute` = pac.`id_product_attribute`) WHERE '; $queryFiltersWhere .= implode(' OR ', $subQuery).') '; } break; @@ -1935,7 +1939,7 @@ class BlockLayered extends Module } } - $idCurrency = (int)$this->context->currency->id;; + $idCurrency = (int)$this->context->currency->id; $priceFilterQueryIn = ''; // All products with price range between price filters limits $priceFilterQueryOut = ''; // All products with a price filters limit on it price range if (isset($priceFilter) && $priceFilter) @@ -2028,7 +2032,7 @@ class BlockLayered extends Module GROUP BY `type`, id_value ORDER BY position ASC'); // Remove all empty selected filters foreach ($selectedFilters as $key => $value) - switch($key) + switch ($key) { case 'price': case 'weight': @@ -2045,7 +2049,7 @@ class BlockLayered extends Module foreach ($filters as $filter) { $sqlQuery = array('select' => '', 'from' => '', 'join' => '', 'where' => '', 'group' => ''); - switch($filter['type']) + switch ($filter['type']) { // conditions + quantities + weight + price case 'price': @@ -2061,10 +2065,11 @@ class BlockLayered extends Module INNER JOIN '._DB_PREFIX_.'category_product cp ON (cp.id_product = p.id_product) INNER JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category AND c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright.') '; $sqlQuery['where'] = 'WHERE p.`active` = 1 '; + $sqlQuery['group'] = ' GROUP BY p.id_product '; break; case 'manufacturer': - $sqlQuery['select'] = 'SELECT m.name, count(p.id_product) nbr, m.id_manufacturer '; + $sqlQuery['select'] = 'SELECT m.name, COUNT(DISTINCT p.id_product) nbr, m.id_manufacturer '; $sqlQuery['from'] = ' FROM `'._DB_PREFIX_.'category_product` cp INNER JOIN `'._DB_PREFIX_.'category` c ON (c.id_category = cp.id_category) @@ -2077,7 +2082,7 @@ class BlockLayered extends Module case 'id_attribute_group':// attribute group $sqlQuery['select'] = ' - SELECT count(lpa.id_attribute) nbr, lpa.id_attribute_group, + SELECT COUNT(DISTINCT p.id_product) nbr, lpa.id_attribute_group, a.color, al.name attribute_name, agl.public_name attribute_group_name , lpa.id_attribute, ag.is_color_group, liagl.url_name name_url_name, liagl.meta_title name_meta_title, lial.url_name value_url_name, lial.meta_title value_meta_title'; $sqlQuery['from'] = ' @@ -2106,13 +2111,13 @@ class BlockLayered extends Module INNER JOIN '._DB_PREFIX_.'category c ON (c.id_category = cp.id_category AND c.nleft >= '.(int)$parent->nleft.' AND c.nright <= '.(int)$parent->nright.')) '; $sqlQuery['group'] = ' GROUP BY lpa.id_attribute - ORDER BY id_attribute_group, id_attribute'; + ORDER BY id_attribute_group, id_attribute '; break; case 'id_feature': $sqlQuery['select'] = 'SELECT fl.name feature_name, fp.id_feature, fv.id_feature_value, fvl.value, - count(fv.id_feature_value) nbr, + COUNT(DISTINCT p.id_product) nbr, lifl.url_name name_url_name, lifl.meta_title name_meta_title, lifvl.url_name value_url_name, lifvl.meta_title value_meta_title '; $sqlQuery['from'] = ' FROM '._DB_PREFIX_.'feature_product fp @@ -2134,17 +2139,17 @@ class BlockLayered extends Module case 'category': $sqlQuery['select'] = ' - SELECT c.id_category, c.id_parent, cl.name, (SELECT count(*) # '; + SELECT c.id_category, c.id_parent, cl.name, (SELECT count(DISTINCT p.id_product) # '; $sqlQuery['from'] = ' FROM '._DB_PREFIX_.'category_product cp - LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = cp.id_product) '; + LEFT JOIN '._DB_PREFIX_.'product p ON (p.id_product = cp.id_product AND p.active = 1) '; $sqlQuery['where'] = ' WHERE cp.id_category = c.id_category '; $sqlQuery['group'] = ') count_products FROM '._DB_PREFIX_.'category c LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = c.id_category AND cl.id_lang = '.(int)$cookie->id_lang.') WHERE c.id_parent = '.(int)$id_parent.' - GROUP BY c.id_category ORDER BY level_depth'; + GROUP BY c.id_category ORDER BY level_depth, c.position'; } foreach ($filters as $filterTmp) { @@ -2164,29 +2169,29 @@ class BlockLayered extends Module } foreach ($subQueryFilter as $key => $value) $sqlQuery[$key] .= $value; - } } + } $products = false; if (!empty($sqlQuery['from'])) $products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sqlQuery['select']."\n".$sqlQuery['from']."\n".$sqlQuery['join']."\n".$sqlQuery['where']."\n".$sqlQuery['group']); - foreach ($filters as $filterTmp) - { - $methodName = 'filterProductsBy'.ucfirst($filterTmp['type']); + foreach ($filters as $filterTmp) + { + $methodName = 'filterProductsBy'.ucfirst($filterTmp['type']); if (method_exists('BlockLayered', $methodName) && (!in_array($filter['type'], array('price', 'weight')) && $filter['type'] != $filterTmp['type'] || $filter['type'] == $filterTmp['type'])) - if ($filter['type'] == $filterTmp['type']) - $products = self::$methodName(array(), $products); - else - $products = self::$methodName(@$selectedFilters[$filterTmp['type']], $products); - } + if ($filter['type'] == $filterTmp['type']) + $products = self::$methodName(array(), $products); + else + $products = self::$methodName(@$selectedFilters[$filterTmp['type']], $products); + } - switch($filter['type']) - { - case 'price': - $priceArray = array('type_lite' => 'price', 'type' => 'price', 'id_key' => 0, 'name' => $this->l('Price'), - 'slider' => true, 'max' => '0', 'min' => null, 'values' => array ('1' => 0), 'unit' => (int)$this->context->currency->sign); + switch ($filter['type']) + { + case 'price': + $priceArray = array('type_lite' => 'price', 'type' => 'price', 'id_key' => 0, 'name' => $this->l('Price'), + 'slider' => true, 'max' => '0', 'min' => null, 'values' => array ('1' => 0), 'unit' => (int)$this->context->currency->sign); if (isset($products) && $products) foreach ($products as $product) { @@ -2195,12 +2200,12 @@ class BlockLayered extends Module $priceArray['min'] = $product['price_min']; $priceArray['values'][0] = $product['price_min']; } - elseif ($priceArray['min'] > $product['price_min']) + else if ($priceArray['min'] > $product['price_min']) { $priceArray['min'] = $product['price_min']; $priceArray['values'][0] = $product['price_min']; } - + if ($priceArray['max'] < $product['price_max']) { $priceArray['max'] = $product['price_max']; @@ -2212,17 +2217,17 @@ class BlockLayered extends Module { if (isset($selectedFilters['price']) && isset($selectedFilters['price'][0]) && isset($selectedFilters['price'][1])) - { - $priceArray['values'][0] = $selectedFilters['price'][0]; - $priceArray['values'][1] = $selectedFilters['price'][1]; - } + { + $priceArray['values'][0] = $selectedFilters['price'][0]; + $priceArray['values'][1] = $selectedFilters['price'][1]; + } $filterBlocks[] = $priceArray; } - break; + break; - case 'weight': - $weightArray = array('type_lite' => 'weight', 'type' => 'weight', 'id_key' => 0, 'name' => $this->l('Weight'), 'slider' => true, - 'max' => '0', 'min' => null, 'values' => array ('1' => 0), 'unit' => Configuration::get('PS_WEIGHT_UNIT')); + case 'weight': + $weightArray = array('type_lite' => 'weight', 'type' => 'weight', 'id_key' => 0, 'name' => $this->l('Weight'), 'slider' => true, + 'max' => '0', 'min' => null, 'values' => array ('1' => 0), 'unit' => Configuration::get('PS_WEIGHT_UNIT')); if (isset($products) && $products) foreach ($products as $product) { @@ -2231,7 +2236,7 @@ class BlockLayered extends Module $weightArray['min'] = $product['weight']; $weightArray['values'][0] = $product['weight']; } - elseif ($weightArray['min'] > $product['weight']) + else if ($weightArray['min'] > $product['weight']) { $weightArray['min'] = $product['weight']; $weightArray['values'][0] = $product['weight']; @@ -2247,58 +2252,58 @@ class BlockLayered extends Module { if (isset($selectedFilters['weight']) && isset($selectedFilters['weight'][0]) && isset($selectedFilters['weight'][1])) - { - $weightArray['values'][0] = $selectedFilters['weight'][0]; - $weightArray['values'][1] = $selectedFilters['weight'][1]; - } + { + $weightArray['values'][0] = $selectedFilters['weight'][0]; + $weightArray['values'][1] = $selectedFilters['weight'][1]; + } $filterBlocks[] = $weightArray; } - break; + break; - case 'condition': - $conditionArray = array('new' => array('name' => $this->l('New'), 'nbr' => 0), - 'used' => array('name' => $this->l('Used'), 'nbr' => 0), 'refurbished' => array('name' => $this->l('Refurbished'), 'nbr' => 0)); + case 'condition': + $conditionArray = array('new' => array('name' => $this->l('New'), 'nbr' => 0), + 'used' => array('name' => $this->l('Used'), 'nbr' => 0), 'refurbished' => array('name' => $this->l('Refurbished'), 'nbr' => 0)); if (isset($products) && $products) foreach ($products as $product) if (isset($selectedFilters['condition']) && in_array($product['condition'], $selectedFilters['condition'])) - $conditionArray[$product['condition']]['checked'] = true; - foreach ($conditionArray as $key => $condition) + $conditionArray[$product['condition']]['checked'] = true; + foreach ($conditionArray as $key => $condition) if (isset($selectedFilters['condition']) && in_array($key, $selectedFilters['condition'])) - $conditionArray[$key]['checked'] = true; + $conditionArray[$key]['checked'] = true; if (isset($products) && $products) foreach ($products as $product) - if(isset($conditionArray[$product['condition']])) - $conditionArray[$product['condition']]['nbr']++; + if (isset($conditionArray[$product['condition']])) + $conditionArray[$product['condition']]['nbr']++; $filterBlocks[] = array('type_lite' => 'condition', 'type' => 'condition', 'id_key' => 0, 'name' => $this->l('Condition'), 'values' => $conditionArray); break; - - case 'quantity': - $quantityArray = array (0 => array('name' => $this->l('Not available'), 'nbr' => 0), 1 => array('name' => $this->l('In stock'), 'nbr' => 0)); - foreach ($quantityArray as $key => $quantity) + + case 'quantity': + $quantityArray = array (0 => array('name' => $this->l('Not available'), 'nbr' => 0), 1 => array('name' => $this->l('In stock'), 'nbr' => 0)); + foreach ($quantityArray as $key => $quantity) if (isset($selectedFilters['quantity']) && in_array($key, $selectedFilters['quantity'])) - $quantityArray[$key]['checked'] = true; + $quantityArray[$key]['checked'] = true; if (isset($products) && $products) foreach ($products as $product) $quantityArray[(int)($product['quantity'] > 0)]['nbr']++; - $filterBlocks[] = array('type_lite' => 'quantity', 'type' => 'quantity', 'id_key' => 0, 'name' => $this->l('Availability'), 'values' => $quantityArray); - break; + $filterBlocks[] = array('type_lite' => 'quantity', 'type' => 'quantity', 'id_key' => 0, 'name' => $this->l('Availability'), 'values' => $quantityArray); + break; - case 'manufacturer': + case 'manufacturer': if (isset($products) && $products) { $manufaturersArray = array(); - foreach ($products as $manufacturer) - { - $manufaturersArray[$manufacturer['id_manufacturer']] = array('name' => $manufacturer['name'], 'nbr' => $manufacturer['nbr']); + foreach ($products as $manufacturer) + { + $manufaturersArray[$manufacturer['id_manufacturer']] = array('name' => $manufacturer['name'], 'nbr' => $manufacturer['nbr']); if (isset($selectedFilters['manufacturer']) && in_array((int)$manufacturer['id_manufacturer'], $selectedFilters['manufacturer'])) - $manufaturersArray[$manufacturer['id_manufacturer']]['checked'] = true; - } + $manufaturersArray[$manufacturer['id_manufacturer']]['checked'] = true; + } $filterBlocks[] = array('type_lite' => 'manufacturer', 'type' => 'manufacturer', 'id_key' => 0, 'name' => $this->l('Manufacturer'), 'values' => $manufaturersArray); } - break; + break; - case 'id_attribute_group': - $attributesArray = array(); + case 'id_attribute_group': + $attributesArray = array(); if (isset($products) && $products) { foreach ($products as $attributes) @@ -2317,9 +2322,9 @@ class BlockLayered extends Module } $filterBlocks = array_merge($filterBlocks, $attributesArray); } - break; - case 'id_feature': - $featureArray = array (); + break; + case 'id_feature': + $featureArray = array(); if (isset($products) && $products) { foreach ($products as $feature) @@ -2336,10 +2341,10 @@ class BlockLayered extends Module } $filterBlocks = array_merge($filterBlocks, $featureArray); } - break; + break; - case 'category': - $tmpArray = array(); + case 'category': + $tmpArray = array(); if (isset($products) && $products) { foreach ($products as $category) @@ -2350,11 +2355,11 @@ class BlockLayered extends Module } $filterBlocks[] = array ('type_lite' => 'category', 'type' => 'category', 'id_key' => 0, 'name' => $this->l('Categories'), 'values' => $tmpArray); } - break; + break; - } + } } - + // All non indexable attribute and feature $nonIndexable = array(); @@ -2385,7 +2390,7 @@ class BlockLayered extends Module $titleValues = array(); $link = new Link(); - $linkBase = $link->getCategoryLink($id_parent,Category::getLinkRewrite($id_parent, (int)($cookie->id_lang)), (int)($cookie->id_lang)); + $linkBase = $link->getCategoryLink($id_parent, Category::getLinkRewrite($id_parent, (int)($cookie->id_lang)), (int)($cookie->id_lang)); $filterBlockList = array(); //get filters checked by group @@ -2400,30 +2405,30 @@ class BlockLayered extends Module { $valueName = !empty($value['url_name']) ? $value['url_name'] : $value['name']; $paramGroupSelected .= '-'.str_replace('-', '_', Tools::link_rewrite($valueName)); - $paramGroupSelectedArray [Tools::link_rewrite($filterName)][] = Tools::link_rewrite($valueName); + $paramGroupSelectedArray[Tools::link_rewrite($filterName)][] = Tools::link_rewrite($valueName); if (!isset($titleValues[$filterName])) $titleValues[$filterName] = array(); $titleValues[$filterName][] = $valueName; } - else - $paramGroupSelectedArray [Tools::link_rewrite($filterName)][] = array(); + else + $paramGroupSelectedArray[Tools::link_rewrite($filterName)][] = array(); } - if(!empty($paramGroupSelected)) + if (!empty($paramGroupSelected)) { $paramSelected .= '/'.str_replace('-', '_', Tools::link_rewrite($filterName)).$paramGroupSelected; $optionCheckedArray[Tools::link_rewrite($filterName)] = $paramGroupSelected; } } - + $blackList = array('weight','price'); $nofollow = false; foreach ($filterBlocks as &$typeFilter) - { + { $filterName = (!empty($typeFilter['url_name']) ? $typeFilter['url_name'] : $typeFilter['name']); if (count($typeFilter) > 0 && !in_array($typeFilter['type'], $blackList)) - { + { foreach ($typeFilter['values'] as $key => $values) { $nofollow = false; @@ -2439,19 +2444,22 @@ class BlockLayered extends Module $optionCheckedCloneArray[Tools::link_rewrite($filterName)] = $optionCheckedCloneArray[Tools::link_rewrite($filterName)].'-'.str_replace('-', '_', Tools::link_rewrite($valueName)); $nofollow = true; } - else + else $optionCheckedCloneArray[Tools::link_rewrite($filterName)] = '-'.str_replace('-', '_', Tools::link_rewrite($valueName)); } else { // Remove selected parameters - $optionCheckedCloneArray[Tools::link_rewrite($filterName)] = str_replace('-'.str_replace('-', '_', Tools::link_rewrite($valueName)), '', $optionCheckedCloneArray[Tools::link_rewrite($filterName)]); + $optionCheckedCloneArray[Tools::link_rewrite($filterName)] = str_replace( + '-'.str_replace('-', '_', Tools::link_rewrite($valueName)), + '', + $optionCheckedCloneArray[Tools::link_rewrite($filterName)]); if (empty($optionCheckedCloneArray[Tools::link_rewrite($filterName)])) unset($optionCheckedCloneArray[Tools::link_rewrite($filterName)]); } $parameters = ''; foreach ($optionCheckedCloneArray as $keyGroup => $valueGroup) - $parameters .= '/'.str_replace('-', '_',$keyGroup).$valueGroup; + $parameters .= '/'.str_replace('-', '_', $keyGroup).$valueGroup; // Check if there is an non indexable attribute or feature in the url foreach ($nonIndexable as $value) @@ -2461,19 +2469,19 @@ class BlockLayered extends Module if (!Configuration::get('PS_REWRITING_SETTINGS')) $typeFilter['values'][$key]['link'] = $linkBase.'&selected_filters='.$parameters; else - $typeFilter['values'][$key]['link'] = $linkBase.$parameters; + $typeFilter['values'][$key]['link'] = $linkBase.$parameters; - $typeFilter['values'][$key]['rel'] = ($nofollow)?'nofollow':''; + $typeFilter['values'][$key]['rel'] = ($nofollow) ? 'nofollow' : ''; } } } $nFilters = 0; - if(isset($selectedFilters['price'])) - if($priceArray['min'] == $selectedFilters['price'][0] && $priceArray['max'] == $selectedFilters['price'][1]) + if (isset($selectedFilters['price'])) + if ($priceArray['min'] == $selectedFilters['price'][0] && $priceArray['max'] == $selectedFilters['price'][1]) unset($selectedFilters['price']); - if(isset($selectedFilters['weight'])) - if($weightArray['min'] == $selectedFilters['weight'][0] && $weightArray['max'] == $selectedFilters['weight'][1]) + if (isset($selectedFilters['weight'])) + if ($weightArray['min'] == $selectedFilters['weight'][0] && $weightArray['max'] == $selectedFilters['weight'][1]) unset($selectedFilters['weight']); foreach ($selectedFilters as $filters) @@ -2482,7 +2490,7 @@ class BlockLayered extends Module $cache = array('layered_show_qties' => (int)Configuration::get('PS_LAYERED_SHOW_QTIES'), 'id_category_layered' => (int)$id_parent, 'selected_filters' => $selectedFilters, 'n_filters' => (int)$nFilters, 'nbr_filterBlocks' => count($filterBlocks), 'filters' => $filterBlocks, 'title_values' => $titleValues, 'current_friendly_url' => htmlentities($paramSelected), 'nofollow' => !empty($paramSelected) || $nofollow); - + return $cache; } @@ -2502,15 +2510,15 @@ class BlockLayered extends Module public function generateFiltersBlock($selectedFilters) { global $smarty; - if($filterBlock = $this->getFilterBlock($selectedFilters)) + if ($filterBlock = $this->getFilterBlock($selectedFilters)) { if ($filterBlock['nbr_filterBlocks'] == 0) return false; $smarty->assign($filterBlock); - return $this->display(__FILE__, 'blocklayered.tpl'); - } + return $this->display(__FILE__, 'blocklayered.tpl'); + } else return false; } @@ -2527,7 +2535,6 @@ class BlockLayered extends Module } else { - $priceFilterQuery = ' INNER JOIN `'._DB_PREFIX_.'layered_price_index` psi ON (psi.id_product = p.id_product AND psi.id_currency = '.(int)$idCurrency.') '; @@ -2563,7 +2570,7 @@ class BlockLayered extends Module return array(); } - private static function getFeatureFilterSubQuery($filterValue, $ignoreJoin) + private static function getId_featureFilterSubQuery($filterValue, $ignoreJoin) { if (empty($filterValue)) return array(); @@ -2583,7 +2590,7 @@ class BlockLayered extends Module FROM `'._DB_PREFIX_.'product_attribute_combination` pac LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (pa.`id_product_attribute` = pac.`id_product_attribute`) WHERE '; - + foreach ($filterValue as $filterVal) $queryFilters .= 'pac.`id_attribute` = '.(int)$filterVal.' OR '; $queryFilters = rtrim($queryFilters, 'OR ').') '; @@ -2620,10 +2627,10 @@ class BlockLayered extends Module else { array_walk($filterValue, create_function('&$id_manufacturer', '$id_manufacturer = (int)$id_manufacturer;')); - $queryFilters = ' AND p.id_manufacturer IN ('.implode($filterValue, ',').')'; + $queryFilters = ' AND p.id_manufacturer IN ('.implode($filterValue, ',').')'; } - if($ignoreJoin) - return array('where' => $queryFilters, 'select' => ', m.name'); + if ($ignoreJoin) + return array('where' => $queryFilters, 'select' => ', m.name'); else return array('where' => $queryFilters, 'select' => ', m.name', 'join' => 'LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.id_manufacturer = p.id_manufacturer) '); } @@ -2820,13 +2827,14 @@ class BlockLayered extends Module $smarty->assign($pagination_infos); $smarty->assign('comparator_max_item', (int)(Configuration::get('PS_COMPARATOR_MAX_ITEM'))); $smarty->assign('products', $products); + $smarty->assign('products_per_page', (int)Configuration::get('PS_PRODUCTS_PER_PAGE')); // Prevent bug with old template where category.tpl contain the title of the category and category-count.tpl do not exists - if(file_exists(_PS_THEME_DIR_.'category-count.tpl')) + if (file_exists(_PS_THEME_DIR_.'category-count.tpl')) $categoryCount = $smarty->fetch(_PS_THEME_DIR_.'category-count.tpl'); else $categoryCount = ''; - + if ($nbProducts == 0) $product_list_tpl = 'blocklayered-no-products.tpl'; else @@ -2876,7 +2884,7 @@ class BlockLayered extends Module /* Set memory limit to 128M only if current is lower */ $memory_limit = @ini_get('memory_limit'); if (substr($memory_limit, -1) != 'G' && ((substr($memory_limit, -1) == 'M' && substr($memory_limit, 0, -1) < 128) || is_numeric($memory_limit) && (intval($memory_limit) < 131072))) - @ini_set('memory_limit','128M'); + @ini_set('memory_limit', '128M'); /* Delete and re-create the layered categories table */ Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_category'); @@ -2907,7 +2915,7 @@ class BlockLayered extends Module /* Set memory limit to 128M only if current is lower */ $memory_limit = @ini_get('memory_limit'); if (substr($memory_limit, -1) != 'G' && ((substr($memory_limit, -1) == 'M' && substr($memory_limit, 0, -1) < 128) || is_numeric($memory_limit) && (intval($memory_limit) < 131072))) - @ini_set('memory_limit','128M'); + @ini_set('memory_limit', '128M'); $db = Db::getInstance(_PS_USE_SQL_SLAVE_); $nCategories = array(); @@ -3026,78 +3034,4 @@ class BlockLayered extends Module Db::getInstance()->execute(rtrim($queryCategory, ',')); } } - - public function filterProducts($products, $selectedFilters, $excludeType = false) - { - static $priceStatics = array(); - $productsToKeep = array(); - $filterByLetter = array('id_attribute_group' => 'a', 'id_feature' => 'f', 'category' => 'c', 'manufacturer' => 'id_manufacturer', - 'quantity' => 'quantity', 'condition' => 'condition', 'weight' => 'weight', 'price' => 'price'); - - foreach ($selectedFilters as $type => $filters) - { - if ($type == $excludeType || !count($filters)) - continue; - else - { - $type = preg_match('/^(.*[^_0-9])/', $type, $res); - $type = $res[1]; - - switch ($type) - { - case 'category': - foreach ($products as $k => $product) - if ($filter = Tools::getValue('id_category_layered')) - $productsToKeep[] = (int)$k; - //don't break me - case 'id_attribute_group': - case 'id_feature': - foreach ($products as $k => $product) - foreach ($filters as $filter) - if (in_array($filter, $product[$filterByLetter[$type]])) - $productsToKeep[] = (int)$k; - break; - - case 'manufacturer': - case 'condition': - case 'quantity': - foreach ($products as $k => $product) - foreach ($filters as $filter) - if ($product[$filterByLetter[$type]] == $filter) - $productsToKeep[] = (int)$k; - break; - - case 'weight': - $min = $filters[0]; - $max = $filters[1]; - foreach ($products as $k => $product) - if ((float)$min <= (float)$product[$filterByLetter[$type]] && (float)$product[$filterByLetter[$type]] <= (float)$max) - $productsToKeep[] = (int)$k; - break; - case 'price': - $min = $filters[0]; - $max = $filters[1]; - foreach ($products as $k => $product) - if ((float)$min <= (float)$product['price_min'] && (float)$product['price_max'] <= (float)$max) - $productsToKeep[] = (int)$k; - else if ((float)$product['price_min'] < (float)$max && (float)$product['price_max'] > (float)$max - || (float)$product['price_min'] < (float)$min && (float)$product['price_max'] > (float)$min) - { - if (!isset($priceStatics[(int)$k])) - $priceStatics[(int)$k] = Product::getPriceStatic((int)$k); - $price = $priceStatics[(int)$k]; - if ((float)$min <= $price && $price <= (float)$max) - $productsToKeep[] = (int)$k; - } - break; - } - - foreach ($products as $k => $product) - if (!in_array($k, $productsToKeep)) - unset($products[(int)$k]); - $productsToKeep = array(); - } - } - return $products; - } }