/modules/blocklayered/blocklayered.php
PHP | 4229 lines | 3761 code | 362 blank | 106 comment | 598 complexity | eda597ac1a48f2650aec10c29cc20341 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /*
- * 2007-2012 PrestaShop
- *
- * NOTICE OF LICENSE
- *
- * This source file is subject to the Academic Free License (AFL 3.0)
- * that is bundled with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://opensource.org/licenses/afl-3.0.php
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@prestashop.com so we can send you a copy immediately.
- *
- * DISCLAIMER
- *
- * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
- * versions in the future. If you wish to customize PrestaShop for your
- * needs please refer to http://www.prestashop.com for more information.
- *
- * @author PrestaShop SA <contact@prestashop.com>
- * @copyright 2007-2012 PrestaShop SA
- * @version Release: $Revision: 14437 $
- * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
- * International Registred Trademark & Property of PrestaShop SA
- */
- if (!defined('_PS_VERSION_'))
- exit;
- class BlockLayered extends Module
- {
- private $products;
- private $nbr_products;
-
- private $page = 1;
- public function __construct()
- {
- $this->name = 'blocklayered';
- $this->tab = 'front_office_features';
- $this->version = '1.8.9';
- $this->author = 'PrestaShop';
- $this->need_instance = 0;
- parent::__construct();
- $this->displayName = $this->l('Layered navigation block');
- $this->description = $this->l('Displays a block with layered navigation filters.');
-
- if ((int)Tools::getValue('p'))
- $this->page = (int)Tools::getValue('p');
- }
-
- public function install()
- {
- if (parent::install() && $this->registerHook('leftColumn') && $this->registerHook('header') && $this->registerHook('footer')
- && $this->registerHook('categoryAddition') && $this->registerHook('categoryUpdate') && $this->registerHook('attributeGroupForm')
- && $this->registerHook('afterSaveAttributeGroup') && $this->registerHook('afterDeleteAttributeGroup') && $this->registerHook('featureForm')
- && $this->registerHook('afterDeleteFeature') && $this->registerHook('afterSaveFeature') && $this->registerHook('categoryDeletion')
- && $this->registerHook('afterSaveProduct') && $this->registerHook('productListAssign') && $this->registerHook('postProcessAttributeGroup')
- && $this->registerHook('postProcessFeature') && $this->registerHook('featureValueForm') && $this->registerHook('postProcessFeatureValue')
- && $this->registerHook('afterDeleteFeatureValue') && $this->registerHook('afterSaveFeatureValue') && $this->registerHook('attributeForm')
- && $this->registerHook('postProcessAttribute') && $this->registerHook('afterDeleteAttribute') && $this->registerHook('afterSaveAttribute'))
- {
- Configuration::updateValue('PS_LAYERED_HIDE_0_VALUES', 1);
- Configuration::updateValue('PS_LAYERED_SHOW_QTIES', 1);
- Configuration::updateValue('PS_LAYERED_FULL_TREE', 1);
- Configuration::updateValue('PS_LAYERED_FILTER_PRICE_USETAX', 1);
- Configuration::updateValue('PS_LAYERED_FILTER_CATEGORY_DEPTH', 1);
- Configuration::updateValue('PS_LAYERED_FILTER_INDEX_QTY', 0);
- Configuration::updateValue('PS_LAYERED_FILTER_INDEX_CDT', 0);
- Configuration::updateValue('PS_LAYERED_FILTER_INDEX_MNF', 0);
- Configuration::updateValue('PS_LAYERED_FILTER_INDEX_CAT', 0);
-
- $this->rebuildLayeredStructure();
-
- $products_count = Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'product`');
-
- if ($products_count < 20000) // Lock template filter creation if too many products
- $this->rebuildLayeredCache();
- self::installPriceIndexTable();
- $this->installFriendlyUrlTable();
- $this->installIndexableAttributeTable();
- $this->installProductAttributeTable();
-
- if ($products_count < 5000) // Lock indexation if too many products
- {
- $this->indexUrl();
- $this->indexAttribute();
- self::fullPricesIndexProcess();
- }
-
- return true;
- }
- else
- {
- // Installation failed (or hook registration) => uninstall the module
- $this->uninstall();
- return false;
- }
- }
- public function uninstall()
- {
- /* Delete all configurations */
- Configuration::deleteByName('PS_LAYERED_HIDE_0_VALUES');
- Configuration::deleteByName('PS_LAYERED_SHOW_QTIES');
- Configuration::deleteByName('PS_LAYERED_FULL_TREE');
- Configuration::deleteByName('PS_LAYERED_INDEXED');
- Configuration::deleteByName('PS_LAYERED_FILTER_PRICE_USETAX');
- Configuration::deleteByName('PS_LAYERED_FILTER_CATEGORY_DEPTH');
- Configuration::deleteByName('PS_LAYERED_FILTER_INDEX_QTY');
- Configuration::deleteByName('PS_LAYERED_FILTER_INDEX_CDT');
- Configuration::deleteByName('PS_LAYERED_FILTER_INDEX_MNF');
- Configuration::deleteByName('PS_LAYERED_FILTER_INDEX_CAT');
-
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_price_index');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_friendly_url');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_indexable_attribute_group');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_indexable_feature');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_indexable_feature_lang_value');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_category');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_filter');
- Db::getInstance()->Execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_filter_shop');
- Db::getInstance()->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_product_attribute');
- return parent::uninstall();
- }
-
- private static function installPriceIndexTable()
- {
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_price_index`');
-
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_price_index` (
- `id_product` INT NOT NULL,
- `id_currency` INT NOT NULL,
- `id_shop` INT NOT NULL,
- `price_min` INT NOT NULL,
- `price_max` INT NOT NULL,
- PRIMARY KEY (`id_product`, `id_currency`, `id_shop`),
- INDEX `id_currency` (`id_currency`),
- INDEX `price_min` (`price_min`), INDEX `price_max` (`price_max`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
- }
-
- private function installFriendlyUrlTable()
- {
- 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,
- `url_key` varchar(32) NOT NULL,
- `data` varchar(200) NOT NULL,
- `id_lang` INT NOT NULL,
- PRIMARY KEY (`id_layered_friendly_url`),
- INDEX `id_lang` (`id_lang`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
- Db::getInstance()->execute('CREATE INDEX `url_key` ON `'._DB_PREFIX_.'layered_friendly_url`(url_key(5))');
- }
-
- private function installIndexableAttributeTable()
- {
- // Attributes Groups
- 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,
- `indexable` BOOL NOT NULL DEFAULT 0,
- PRIMARY KEY (`id_attribute_group`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
- Db::getInstance()->execute('
- INSERT INTO `'._DB_PREFIX_.'layered_indexable_attribute_group`
- SELECT id_attribute_group, 1 FROM `'._DB_PREFIX_.'attribute_group`');
-
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_attribute_group_lang_value`');
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_indexable_attribute_group_lang_value` (
- `id_attribute_group` INT NOT NULL,
- `id_lang` INT NOT NULL,
- `url_name` VARCHAR(20),
- `meta_title` VARCHAR(20),
- PRIMARY KEY (`id_attribute_group`, `id_lang`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
-
- // Attributes
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_attribute_lang_value`');
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_indexable_attribute_lang_value` (
- `id_attribute` INT NOT NULL,
- `id_lang` INT NOT NULL,
- `url_name` VARCHAR(20),
- `meta_title` VARCHAR(20),
- PRIMARY KEY (`id_attribute`, `id_lang`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
-
-
- // Features
- 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,
- `indexable` BOOL NOT NULL DEFAULT 0,
- PRIMARY KEY (`id_feature`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
-
- Db::getInstance()->execute('
- INSERT INTO `'._DB_PREFIX_.'layered_indexable_feature`
- SELECT id_feature, 1 FROM `'._DB_PREFIX_.'feature`');
-
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_feature_lang_value`');
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_indexable_feature_lang_value` (
- `id_feature` INT NOT NULL,
- `id_lang` INT NOT NULL,
- `url_name` VARCHAR(20) NOT NULL,
- `meta_title` VARCHAR(20),
- PRIMARY KEY (`id_feature`, `id_lang`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
-
- // Features values
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_indexable_feature_value_lang_value`');
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_indexable_feature_value_lang_value` (
- `id_feature_value` INT NOT NULL,
- `id_lang` INT NOT NULL,
- `url_name` VARCHAR(20),
- `meta_title` VARCHAR(20),
- PRIMARY KEY (`id_feature_value`, `id_lang`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
- }
-
- /**
- *
- * create table product attribute
- */
- public function installProductAttributeTable()
- {
- Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'layered_product_attribute`');
- Db::getInstance()->execute('
- CREATE TABLE `'._DB_PREFIX_.'layered_product_attribute` (
- `id_attribute` int(10) unsigned NOT NULL,
- `id_product` int(10) unsigned NOT NULL,
- `id_attribute_group` int(10) unsigned NOT NULL DEFAULT "0",
- `id_shop` int(10) unsigned NOT NULL DEFAULT "1",
- KEY `id_attribute` (`id_attribute`)
- ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;');
- }
-
- /**
- *
- * Generate data product attribute
- */
- public function indexAttribute($id_product = null)
- {
- if (is_null($id_product))
- 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);
-
- Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'layered_product_attribute` (`id_attribute`, `id_product`, `id_attribute_group`, `id_shop`)
- SELECT pac.id_attribute, pa.id_product, ag.id_attribute_group, '.(version_compare(_PS_VERSION_,'1.5','>') ? 'product_attribute_shop.`id_shop`' : '1').'
- FROM '._DB_PREFIX_.'product_attribute pa
- '.(version_compare(_PS_VERSION_,'1.5','>') ? Shop::addSqlAssociation('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
- '.(is_null($id_product) ? '' : 'AND pa.id_product = '.(int)$id_product).'
- GROUP BY a.id_attribute, pa.id_product '.(version_compare(_PS_VERSION_,'1.5','>') ? ', product_attribute_shop.`id_shop`' : ''));
-
- return 1;
- }
- /*
- * Url indexation
- */
- public function indexUrl($ajax = false, $truncate = true)
- {
- if ($truncate)
- Db::getInstance()->execute('TRUNCATE '._DB_PREFIX_.'layered_friendly_url');
-
- $attribute_values_by_lang = array();
- $filters = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT lc.*, id_lang, name, link_rewrite, cl.id_category
- FROM '._DB_PREFIX_.'layered_category lc
- INNER JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = lc.id_category AND lc.id_category <> 1 )
- GROUP BY type, id_value, id_lang');
- if (!$filters)
- return;
- foreach ($filters as $filter)
- switch ($filter['type'])
- {
- case 'id_attribute_group':
- $attributes = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
- SELECT agl.public_name name, a.id_attribute_group id_name, al.name value, a.id_attribute id_value, al.id_lang,
- liagl.url_name name_url_name, lial.url_name value_url_name
- FROM '._DB_PREFIX_.'attribute_group ag
- INNER JOIN '._DB_PREFIX_.'attribute_group_lang agl ON (agl.id_attribute_group = ag.id_attribute_group)
- INNER JOIN '._DB_PREFIX_.'attribute a ON (a.id_attribute_group = ag.id_attribute_group)
- INNER JOIN '._DB_PREFIX_.'attribute_lang al ON (al.id_attribute = a.id_attribute)
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_attribute_group liag ON (liag.id_attribute_group = a.id_attribute_group)
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value liagl
- ON (liagl.id_attribute_group = ag.id_attribute_group AND liagl.id_lang = '.(int)$filter['id_lang'].')
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_attribute_lang_value lial
- ON (lial.id_attribute = a.id_attribute AND lial.id_lang = '.(int)$filter['id_lang'].')
- WHERE a.id_attribute_group = '.(int)$filter['id_value'].' AND agl.id_lang = al.id_lang AND agl.id_lang = '.(int)$filter['id_lang']);
- foreach ($attributes as $attribute)
- {
- if (!isset($attribute_values_by_lang[$attribute['id_lang']]))
- $attribute_values_by_lang[$attribute['id_lang']] = array();
- if (!isset($attribute_values_by_lang[$attribute['id_lang']]['c'.$attribute['id_name']]))
- $attribute_values_by_lang[$attribute['id_lang']]['c'.$attribute['id_name']] = array();
- $attribute_values_by_lang[$attribute['id_lang']]['c'.$attribute['id_name']][] = array(
- 'name' => (!empty($attribute['name_url_name']) ? $attribute['name_url_name'] : $attribute['name']),
- 'id_name' => 'c'.$attribute['id_name'],
- 'value' => (!empty($attribute['value_url_name']) ? $attribute['value_url_name'] : $attribute['value']),
- 'id_value' => $attribute['id_name'].'_'.$attribute['id_value'],
- 'id_id_value' => $attribute['id_value'],
- 'category_name' => $filter['link_rewrite'],
- 'type' => $filter['type']);
- }
- break;
-
- case 'id_feature':
- $features = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
- SELECT fl.name name, fl.id_feature id_name, fvl.id_feature_value id_value, fvl.value value, fl.id_lang, fl.id_lang,
- lifl.url_name name_url_name, lifvl.url_name value_url_name
- FROM '._DB_PREFIX_.'feature_lang fl
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_feature lif ON (lif.id_feature = fl.id_feature)
- INNER JOIN '._DB_PREFIX_.'feature_value fv ON (fv.id_feature = fl.id_feature)
- INNER JOIN '._DB_PREFIX_.'feature_value_lang fvl ON (fvl.id_feature_value = fv.id_feature_value)
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_feature_lang_value lifl
- ON (lifl.id_feature = fl.id_feature AND lifl.id_lang = '.(int)$filter['id_lang'].')
- LEFT JOIN '._DB_PREFIX_.'layered_indexable_feature_value_lang_value lifvl
- ON (lifvl.id_feature_value = fvl.id_feature_value AND lifvl.id_lang = '.(int)$filter['id_lang'].')
- WHERE fl.id_feature = '.(int)$filter['id_value'].' AND fvl.id_lang = fl.id_lang AND fvl.id_lang = '.(int)$filter['id_lang']);
- foreach ($features as $feature)
- {
- if (!isset($attribute_values_by_lang[$feature['id_lang']]))
- $attribute_values_by_lang[$feature['id_lang']] = array();
- if (!isset($attribute_values_by_lang[$feature['id_lang']]['f'.$feature['id_name']]))
- $attribute_values_by_lang[$feature['id_lang']]['f'.$feature['id_name']] = array();
- $attribute_values_by_lang[$feature['id_lang']]['f'.$feature['id_name']][] = array(
- 'name' => (!empty($feature['name_url_name']) ? $feature['name_url_name'] : $feature['name']),
- 'id_name' => 'f'.$feature['id_name'],
- 'value' => (!empty($feature['value_url_name']) ? $feature['value_url_name'] : $feature['value']),
- 'id_value' => $feature['id_name'].'_'.$feature['id_value'],
- 'id_id_value' => $feature['id_value'],
- 'category_name' => $filter['link_rewrite'],
- 'type' => $filter['type']);
- }
- break;
-
- case 'category':
- $categories = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
- SELECT cl.name, cl.id_lang, c.id_category
- 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)
- {
- if (!isset($attribute_values_by_lang[$category['id_lang']]))
- $attribute_values_by_lang[$category['id_lang']] = array();
- if (!isset($attribute_values_by_lang[$category['id_lang']]['category']))
- $attribute_values_by_lang[$category['id_lang']]['category'] = array();
- $attribute_values_by_lang[$category['id_lang']]['category'][] = array('name' => $this->translateWord('Categories', $category['id_lang']),
- 'id_name' => null, 'value' => $category['name'], 'id_value' => $category['id_category'],
- 'category_name' => $filter['link_rewrite'], 'type' => $filter['type']);
- }
- break;
-
- case 'manufacturer':
- $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
- SELECT m.name as name,l.id_lang as id_lang, id_manufacturer
- FROM '._DB_PREFIX_.'manufacturer m , '._DB_PREFIX_.'lang l
- WHERE l.id_lang = '.(int)$filter['id_lang'].' ');
-
- foreach ($manufacturers as $manufacturer)
- {
- if (!isset($attribute_values_by_lang[$manufacturer['id_lang']]))
- $attribute_values_by_lang[$manufacturer['id_lang']] = array();
- if (!isset($attribute_values_by_lang[$manufacturer['id_lang']]['manufacturer']))
- $attribute_values_by_lang[$manufacturer['id_lang']]['manufacturer'] = array();
- $attribute_values_by_lang[$manufacturer['id_lang']]['manufacturer'][] = array('name' => $this->translateWord('Manufacturer', $manufacturer['id_lang']),
- 'id_name' => null, 'value' => $manufacturer['name'], 'id_value' => $manufacturer['id_manufacturer'],
- 'category_name' => $filter['link_rewrite'], 'type' => $filter['type']);
- }
- break;
-
- case 'quantity':
- $avaibility_list = array(
- $this->translateWord('Not available', (int)$filter['id_lang']),
- $this->translateWord('In stock', (int)$filter['id_lang'])
- );
- foreach ($avaibility_list as $key => $quantity)
- $attribute_values_by_lang[$filter['id_lang']]['quantity'][] = array('name' => $this->translateWord('Availability', (int)$filter['id_lang']),
- 'id_name' => null, 'value' => $quantity, 'id_value' => $key, 'id_id_value' => 0,
- 'category_name' => $filter['link_rewrite'], 'type' => $filter['type']);
- break;
-
- case 'condition':
- $condition_list = array(
- 'new' => $this->translateWord('New', (int)$filter['id_lang']),
- 'used' => $this->translateWord('Used', (int)$filter['id_lang']),
- 'refurbished' => $this->translateWord('Refurbished', (int)$filter['id_lang'])
- );
- foreach ($condition_list as $key => $condition)
- $attribute_values_by_lang[$filter['id_lang']]['condition'][] = array('name' => $this->translateWord('Condition', (int)$filter['id_lang']),
- 'id_name' => null, 'value' => $condition, 'id_value' => $key,
- 'category_name' => $filter['link_rewrite'], 'type' => $filter['type']);
- break;
- }
-
- // Foreach langs
- foreach ($attribute_values_by_lang as $id_lang => $attribute_values)
- {
- // Foreach attributes generate a couple "/<attribute_name>_<atttribute_value>". For example: color_blue
- foreach ($attribute_values as $attribute)
- foreach ($attribute as $param)
- {
- $selected_filters = array();
- $link = '/'.str_replace('-', '_', Tools::link_rewrite($param['name'])).'-'.str_replace('-', '_', Tools::link_rewrite($param['value']));
- $selected_filters[$param['type']] = array();
- if (!isset($param['id_id_value']))
- $param['id_id_value'] = $param['id_value'];
- $selected_filters[$param['type']][$param['id_id_value']] = $param['id_value'];
- $url_key = md5($link);
- $id_layered_friendly_url = Db::getInstance()->getValue('SELECT id_layered_friendly_url
- FROM `'._DB_PREFIX_.'layered_friendly_url` WHERE `id_lang` = '.$id_lang.' AND `url_key` = \''.$url_key.'\'');
- if ($id_layered_friendly_url == false)
- {
- Db::getInstance()->AutoExecute(_DB_PREFIX_.'layered_friendly_url', array('url_key' => $url_key, 'data' => serialize($selected_filters), 'id_lang' => $id_lang), 'INSERT');
- $id_layered_friendly_url = Db::getInstance()->Insert_ID();
- }
- }
- }
- if ($ajax)
- return '{"result": 1}';
- else
- return 1;
- }
-
- public function translateWord($string, $id_lang )
- {
- static $_MODULES = array();
- global $_MODULE;
- $file = _PS_MODULE_DIR_.$this->name.'/'.Language::getIsoById($id_lang).'.php';
- if (!array_key_exists($id_lang, $_MODULES))
- {
- if (!file_exists($file))
- return $string;
- include($file);
- $_MODULES[$id_lang] = $_MODULE;
- }
- $string = str_replace('\'', '\\\'', $string);
- // set array key to lowercase for 1.3 compatibility
- $_MODULES[$id_lang] = array_change_key_case($_MODULES[$id_lang]);
- $current_key = '<{'.strtolower( $this->name).'}'.strtolower(_THEME_NAME_).'>'.strtolower($this->name).'_'.md5($string);
- $default_key = '<{'.strtolower( $this->name).'}prestashop>'.strtolower($this->name).'_'.md5($string);
-
- if (isset($_MODULES[$id_lang][$current_key]))
- $ret = stripslashes($_MODULES[$id_lang][$current_key]);
- else if (isset($_MODULES[$id_lang][Tools::strtolower($current_key)]))
- $ret = stripslashes($_MODULES[$id_lang][Tools::strtolower($current_key)]);
- else if (isset($_MODULES[$id_lang][$default_key]))
- $ret = stripslashes($_MODULES[$id_lang][$default_key]);
- else if (isset($_MODULES[$id_lang][Tools::strtolower($default_key)]))
- $ret = stripslashes($_MODULES[$id_lang][Tools::strtolower($default_key)]);
- else
- $ret = stripslashes($string);
- return str_replace('"', '"', $ret);
- }
-
- public function hookProductListAssign($params)
- {
- global $smarty;
- if (version_compare(_PS_VERSION_,'1.5','<') && !Configuration::get('PS_LAYERED_INDEXED')
- || version_compare(_PS_VERSION_,'1.5','>') && !Configuration::getGlobalValue('PS_LAYERED_INDEXED'))
- return;
- // Inform the hook was executed
- $params['hookExecuted'] = true;
- // List of product to overrride categoryController
- $params['catProducts'] = array();
- $selected_filters = $this->getSelectedFilters();
- $filter_block = $this->getFilterBlock($selected_filters);
- $title = '';
- if (is_array($filter_block['title_values']))
- foreach ($filter_block['title_values'] as $key => $val)
- $title .= ' – '.$key.' '.implode('/', $val);
-
- $smarty->assign('categoryNameComplement', $title);
- $this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range);
- // Need a nofollow on the pagination links?
- $smarty->assign('no_follow', $filter_block['no_follow']);
- }
-
- public function hookAfterSaveProduct($params)
- {
- if (!$params['id_product'])
- return;
-
- self::indexProductPrices((int)$params['id_product']);
- $this->indexAttribute((int)$params['id_product']);
- }
- public function hookAfterSaveFeature($params)
- {
- if (!$params['id_feature'] || Tools::getValue('layered_indexable') === false)
- return;
-
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_feature WHERE id_feature = '.(int)$params['id_feature']);
- Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_feature VALUES ('.(int)$params['id_feature'].', '.(int)Tools::getValue('layered_indexable').')');
-
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_feature_lang_value WHERE id_feature = '.(int)$params['id_feature']); // don't care about the id_lang
- foreach (Language::getLanguages(false) as $language)
- {
- // Data are validated by method "hookPostProcessFeature"
- $id_lang = (int)$language['id_lang'];
- 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)
- {
- if (!$params['id_feature_value'])
- return;
-
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_feature_value_lang_value WHERE id_feature_value = '.(int)$params['id_feature_value']); // don't care about the id_lang
- foreach (Language::getLanguages(false) as $language)
- {
- // Data are validated by method "hookPostProcessFeatureValue"
- $id_lang = (int)$language['id_lang'];
- Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_feature_value_lang_value
- VALUES ('.(int)$params['id_feature_value'].', '.$id_lang.', \''.pSQL(Tools::link_rewrite(Tools::getValue('url_name_'.$id_lang))).'\',
- \''.pSQL(Tools::safeOutput(Tools::getValue('meta_title_'.$id_lang), true)).'\')');
- }
- }
-
- public function hookAfterDeleteFeatureValue($params)
- {
- if (!$params['id_feature_value'])
- return;
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_feature_value_lang_value WHERE id_feature_value = '.(int)$params['id_feature_value']);
- }
-
- public function hookPostProcessFeatureValue($params)
- {
- $this->hookPostProcessAttributeGroup($params);
- }
-
- public function hookFeatureValueForm($params)
- {
- $languages = Language::getLanguages(false);
- $default_form_language = (int)(Configuration::get('PS_LANG_DEFAULT'));
- $lang_value = array();
-
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $return = '
- <script type="text/javascript">
- flag_fields = \'\';
- </script>';
- else
- $return = '';
-
- $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
- 'SELECT url_name, meta_title, id_lang FROM '._DB_PREFIX_.'layered_indexable_feature_value_lang_value
- WHERE id_feature_value = '.(int)$params['id_feature_value']);
- if ($result)
- foreach ($result as $data)
- $lang_value[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']);
- $return .= '<div class="clear"></div>
- <label>'.$this->l('URL:').'</label>
- <div class="margin-form">
- <script type="text/javascript">
- flag_fields += \'¤url_name¤meta_title\';
- </script>
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="url_name_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="url_name_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['url_name'], true).'" />
- <span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}_<span class="hint-pointer"> </span></span>
- <p style="clear: both">'.$this->l('Specific URL format in block layered generation').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Meta title:').' </label>
- <div class="margin-form">
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="meta_title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="meta_title_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['meta_title'], true).'" />
- <p style="clear: both">'.$this->l('Specific format for meta title').'</p>
- </div>';
-
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>';
- return $return;
- }
-
- public function hookAfterSaveAttribute($params)
- {
- if (!$params['id_attribute'])
- return;
-
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_lang_value WHERE id_attribute = '.(int)$params['id_attribute']); // don't care about the id_lang
- foreach (Language::getLanguages(false) as $language)
- {
- // Data are validated by method "hookPostProcessAttribute"
- $id_lang = (int)$language['id_lang'];
- Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_attribute_lang_value
- VALUES ('.(int)$params['id_attribute'].', '.$id_lang.', \''.pSQL(Tools::link_rewrite(Tools::getValue('url_name_'.$id_lang))).'\',
- \''.pSQL(Tools::safeOutput(Tools::getValue('meta_title_'.$id_lang), true)).'\')');
- }
- }
-
- public function hookAfterDeleteAttribute($params)
- {
- if (!$params['id_attribute'])
- return;
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_lang_value WHERE id_attribute = '.(int)$params['id_attribute']);
- }
-
- public function hookPostProcessAttribute($params)
- {
- $this->hookPostProcessAttributeGroup($params);
- }
-
- public function hookAttributeForm($params)
- {
- $languages = Language::getLanguages(false);
- $default_form_language = (int)(Configuration::get('PS_LANG_DEFAULT'));
- $lang_value = array();
-
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $return = '
- <script type="text/javascript">
- flag_fields = \'\';
- </script>';
- else
- $return = '';
-
- $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
- 'SELECT url_name, meta_title, id_lang FROM '._DB_PREFIX_.'layered_indexable_attribute_lang_value
- WHERE id_attribute = '.(int)$params['id_attribute']);
- if ($result)
- foreach ($result as $data)
- $lang_value[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']);
- $return .= '<div class="clear"></div>
- <label>'.$this->l('URL:').'</label>
- <div class="margin-form">
- <script type="text/javascript">
- flag_fields += \'¤url_name¤meta_title\';
- </script>
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="url_name_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="url_name_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['url_name'], true).'" />
- <span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}_<span class="hint-pointer"> </span></span>
- <p style="clear: both">'.$this->l('Specific URL format in block layered generation').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Meta title:').' </label>
- <div class="margin-form">
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="meta_title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="meta_title_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['meta_title'], true).'" />
- <p style="clear: both">'.$this->l('Specific format for meta title').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>';
- return $return;
- }
-
- public function hookPostProcessFeature($params)
- {
- $this->hookPostProcessAttributeGroup($params);
- }
- public function hookAfterDeleteFeature($params)
- {
- if (!$params['id_feature'])
- return;
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_feature WHERE id_feature = '.(int)$params['id_feature']);
- }
-
- public function hookAfterSaveAttributeGroup($params)
- {
- if (!$params['id_attribute_group'] || Tools::getValue('layered_indexable') === false)
- return;
-
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_group WHERE id_attribute_group = '.(int)$params['id_attribute_group']);
- Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'layered_indexable_attribute_group VALUES ('.(int)$params['id_attribute_group'].', '.(int)Tools::getValue('layered_indexable').')');
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value WHERE id_attribute_group = '.(int)$params['id_attribute_group']); // don't care about the id_lang
- foreach (Language::getLanguages(false) as $language)
- {
- // Data are validated by method "hookPostProcessAttributeGroup"
- $id_lang = (int)$language['id_lang'];
- 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)
- {
- // Limit to one call
- static $once = false;
- if ($once)
- return;
- $once = true;
-
- $errors = array();
- foreach (Language::getLanguages(false) as $language)
- {
- $id_lang = $language['id_lang'];
- if (Tools::getValue('url_name_'.$id_lang))
- if (Tools::link_rewrite(Tools::getValue('url_name_'.$id_lang)) != strtolower( Tools::getValue('url_name_'.$id_lang)))
- {
- // Here use the reference "errors" to stop saving process
- $params['errors'][] = Tools::displayError(sprintf($this->l('"%s" is not a valid url'), Tools::getValue('url_name_'.$id_lang)));
- }
- }
- }
-
- public function hookAfterDeleteAttributeGroup($params)
- {
- if (!$params['id_attribute_group'])
- return;
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_group WHERE id_attribute_group = '.(int)$params['id_attribute_group']);
- Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value WHERE id_attribute_group = '.(int)$params['id_attribute_group']);
- }
-
- public function hookAttributeGroupForm($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_attribute_group
- WHERE id_attribute_group = '.(int)$params['id_attribute_group']);
- $lang_value = array();
-
- $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
- 'SELECT url_name, meta_title, id_lang FROM '._DB_PREFIX_.'layered_indexable_attribute_group_lang_value
- WHERE id_attribute_group = '.(int)$params['id_attribute_group']);
- if ($result)
- foreach ($result as $data)
- $lang_value[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']);
- if ($indexable === false)
- $on = true;
- else
- $on = (bool)$indexable;
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $return = '
- <script type="text/javascript">
- flag_fields = \'\';
- </script>';
- else
- $return = '';
-
- $return .= '<div class="clear"></div>
- <label>'.$this->l('URL:').'</label>
- <div class="margin-form">
- <script type="text/javascript">
- flag_fields += \'¤url_name¤meta_title\';
- </script>
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="url_name_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="url_name_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['url_name'], true).'" />
- <span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}_<span class="hint-pointer"> </span></span>
- <p style="clear: both">'.$this->l('Specific URL format in block layered generation').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Meta title:').' </label>
- <div class="margin-form">
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="meta_title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="meta_title_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['meta_title'], true).'" />
- <p style="clear: both">'.$this->l('Specific format for meta title').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Indexable:').' </label>
- <div class="margin-form">
- <input type="radio" '.(($on) ? 'checked="checked"' : '').' value="1" id="indexable_on" name="layered_indexable">
- <label for="indexable_on" class="t"><img title="Yes" alt="Enabled" src="../img/admin/enabled.gif"></label>
- <input type="radio" '.((!$on) ? 'checked="checked"' : '').' value="0" id="indexable_off" name="layered_indexable">
- <label for="indexable_off" class="t"><img title="No" alt="Disabled" src="../img/admin/disabled.gif"></label>
- <p>'.$this->l('Use this attribute in URL generated by the layered navigation module').'</p>
- </div>';
- 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']);
- $lang_value = 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)
- $lang_value[$data['id_lang']] = array('url_name' => $data['url_name'], 'meta_title' => $data['meta_title']);
-
-
- if ($indexable === false)
- $on = true;
- else
- $on = (bool)$indexable;
-
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $return = '
- <script type="text/javascript">
- flag_fields = \'\';
- </script>';
- else
- $return = '';
-
- $return .= '<div class="clear"></div>
- <label>'.$this->l('URL:').'</label>
- <div class="margin-form">
- <script type="text/javascript">
- flag_fields += \'¤url_name¤meta_title\';
- </script>
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="url_name_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="url_name_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['url_name'], true).'" />
- <span class="hint" name="help_box">'.$this->l('Invalid characters:').' <>;=#{}_<span class="hint-pointer"> </span></span>
- <p style="clear: both">'.$this->l('Specific URL format in block layered generation').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'url_name', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Meta title:').' </label>
- <div class="margin-form">
- <div class="translatable">';
- foreach ($languages as $language)
- $return .= '
- <div class="lang_'.$language['id_lang'].'" id="meta_title_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $default_form_language ? 'block' : 'none').'; float: left;">
- <input size="33" type="text" name="meta_title_'.$language['id_lang'].'" value="'.Tools::safeOutput(@$lang_value[$language['id_lang']]['meta_title'], true).'" />
- <p style="clear: both">'.$this->l('Specific format for meta title').'</p>
- </div>';
- if (version_compare(_PS_VERSION_,'1.5','<'))
- $return .= $this->displayFlags($languages, $default_form_language, 'flag_fields', 'meta_title', true, true);
- $return .= '
- </div>
- <div class="clear"></div>
- </div>
- <label>'.$this->l('Indexable:').' </label>
- <div class="margin-form">
- <input type="radio" '.(($on) ? 'checked="checked"' : '').' value="1" id="indexable_on" name="layered_indexable">
- <label for="indexable_on" class="t"><img title="Yes" alt="Enabled" src="../img/admin/enabled.gif"></label>
- <input type="radio" '.((!$on) ? 'checked="checked"' : '').' value="0" id="indexable_off" name="layered_indexable">
- <label for="indexable_off" class="t"><img title="No" alt="Disabled" src="../img/admin/disabled.gif"></label>
- <p>'.$this->l('Use this attribute in URL generated by the layered navigation module').'</p>
- </div>';
- return $return;
- }
-
- /*
- * $cursor $cursor in order to restart indexing from the last state
- */
- public static function fullPricesIndexProcess($cursor = 0, $ajax = false, $smart = false)
- {
- if ($cursor == 0 && !$smart)
- self::installPriceIndexTable();
-
- return self::indexPrices($cursor, true, $ajax, $smart);
- }
-
- /*
- * $cursor $cursor in order to restart indexing from the last state
- */
- public static function pricesIndexProcess($cursor = 0, $ajax = false)
- {
- return self::indexPrices($cursor, false, $ajax);
- }
-
- private static function indexPrices($cursor = null, $full = false, $ajax = false, $smart = false)
- {
- if ($full)
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $nb_products = (int)Db::getInstance()->getValue('
- SELECT count(DISTINCT p.`id_product`)
- FROM '._DB_PREFIX_.'product p
- INNER JOIN `'._DB_PREFIX_.'product_shop` ps
- ON (ps.`id_product` = p.`id_product` AND ps.`active` = 1)');
- else
- $nb_products = (int)Db::getInstance()->getValue('
- SELECT count(DISTINCT p.`id_product`)
- FROM '._DB_PREFIX_.'product p
- WHERE `active` = 1');
- else
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $nb_products = (int)Db::getInstance()->getValue('
- SELECT COUNT(DISTINCT p.`id_product`) FROM `'._DB_PREFIX_.'product` p
- INNER JOIN `'._DB_PREFIX_.'product_shop` ps
- ON (ps.`id_product` = p.`id_product` AND ps.`active` = 1)
- LEFT JOIN `'._DB_PREFIX_.'layered_price_index` psi ON (psi.id_product = p.id_product)
- WHERE psi.id_product IS NULL');
- else
- $nb_products = (int)Db::getInstance()->getValue('
- SELECT COUNT(DISTINCT p.`id_product`) FROM `'._DB_PREFIX_.'product` p
- LEFT JOIN `'._DB_PREFIX_.'layered_price_index` psi ON (psi.id_product = p.id_product)
- WHERE `active` = 1 AND psi.id_product IS NULL');
-
- $max_executiontime = @ini_get('max_execution_time');
- if ($max_executiontime > 5 || $max_executiontime <= 0)
- $max_executiontime = 5;
-
- $start_time = microtime(true);
-
- do
- {
- $cursor = (int)self::indexPricesUnbreakable((int)$cursor, $full, $smart);
- $time_elapsed = microtime(true) - $start_time;
- }
- while ($cursor < $nb_products && (Tools::getMemoryLimit()) > memory_get_peak_usage() && $time_elapsed < $max_executiontime);
-
- if (($nb_products > 0 && !$full || $cursor < $nb_products && $full) && !$ajax)
- {
- $token = substr(Tools::encrypt('blocklayered/index'), 0, 10);
- if (Tools::usingSecureMode())
- $domain = Tools::getShopDomainSsl(true);
- else
- $domain = Tools::getShopDomain(true);
-
- if (!Tools::file_get_contents($domain.__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;
- }
- if ($ajax && $nb_products > 0 && $cursor < $nb_products && $full)
- return '{"cursor": '.$cursor.', "count": '.($nb_products - $cursor).'}';
- else if ($ajax && $nb_products > 0 && !$full)
- return '{"cursor": '.$cursor.', "count": '.($nb_products).'}';
- else
- {
- if (version_compare(_PS_VERSION_,'1.5','>'))
- Configuration::updateGlobalValue('PS_LAYERED_INDEXED', 1);
- else
- Configuration::updateValue('PS_LAYERED_INDEXED', 1);
-
- if ($ajax)
- return '{"result": "ok"}';
- else
- return -1;
- }
- }
-
- /*
- * $cursor $cursor in order to restart indexing from the last state
- */
- private static function indexPricesUnbreakable($cursor, $full = false, $smart = false)
- {
- static $length = 100; // Nb of products to index
-
- if (is_null($cursor))
- $cursor = 0;
-
- if ($full)
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $query = '
- SELECT p.`id_product`
- FROM `'._DB_PREFIX_.'product` p
- INNER JOIN `'._DB_PREFIX_.'product_shop` ps
- ON (ps.`id_product` = p.`id_product` AND ps.`active` = 1)
- GROUP BY p.`id_product`
- ORDER BY p.`id_product` LIMIT '.(int)$cursor.','.(int)$length;
- else
- $query = '
- SELECT p.`id_product`
- FROM `'._DB_PREFIX_.'product` p
- WHERE `active` = 1
- GROUP BY p.`id_product`
- ORDER BY p.`id_product` LIMIT '.(int)$cursor.','.(int)$length;
- else
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $query = '
- SELECT p.`id_product`
- FROM `'._DB_PREFIX_.'product` p
- INNER JOIN `'._DB_PREFIX_.'product_shop` ps
- ON (ps.`id_product` = p.`id_product` AND ps.`active` = 1)
- LEFT JOIN `'._DB_PREFIX_.'layered_price_index` psi ON (psi.id_product = p.id_product)
- WHERE psi.id_product IS NULL
- GROUP BY p.`id_product`
- ORDER BY p.`id_product` LIMIT 0,'.(int)$length;
- else
- $query = '
- SELECT p.`id_product`
- FROM `'._DB_PREFIX_.'product` p
- LEFT JOIN `'._DB_PREFIX_.'layered_price_index` psi ON (psi.id_product = p.id_product)
- WHERE `active` = 1 AND psi.id_product IS NULL
- GROUP BY p.`id_product`
- ORDER BY p.`id_product` LIMIT 0,'.(int)$length;
-
- foreach (Db::getInstance()->executeS($query) as $product)
- self::indexProductPrices((int)$product['id_product'], ($smart && $full));
- return (int)($cursor + $length);
- }
-
- public static function indexProductPrices($id_product, $smart = true)
- {
- static $groups = null;
- if (is_null($groups))
- {
- $groups = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT id_group FROM `'._DB_PREFIX_.'group_reduction`');
- if (!$groups)
- $groups = array();
- }
-
- $shop_list = array();
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $shop_list = Shop::getShops(false, null, true);
- else
- $shop_list[] = 0;
-
- foreach ($shop_list as $id_shop)
- {
- static $currency_list = null;
-
- if (is_null($currency_list))
- {
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $currency_list = Currency::getCurrencies(false, 1, new Shop($id_shop));
- else
- $currency_list = Currency::getCurrencies(false, 1);
- }
-
- $min_price = array();
- $max_price = array();
-
- if ($smart)
- Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'layered_price_index` WHERE `id_product` = '.(int)$id_product.' AND `id_shop` = '.(int)$id_shop);
-
- if (Configuration::get('PS_LAYERED_FILTER_PRICE_USETAX'))
- {
- if (version_compare(_PS_VERSION_,'1.5','>'))
- $max_tax_rate = Db::getInstance()->getValue('
- SELECT max(t.rate) max_rate
- FROM `'._DB_PREFIX_.'product_shop` p
- LEFT JOIN `'._DB_PREFIX_.'tax_rules_group` trg ON (trg.id_tax_rules_group = p.id_tax_rules_group AND p.id_shop = '.(int)$shop_list.')
- LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (tr.id_tax_rules_group = trg.id_tax_rules_group)
- LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.id_tax = tr.id_tax AND t.active = 1)
- WHERE id_product = '.(int)$id_product.'
- GROUP BY id_product');
- else
- $max_tax_rate = Db::getInstance()->getValue('
- SELECT max(t.rate) max_rate
- FROM `'._DB_PREFIX_.'product` p
- LEFT JOIN `'._DB_PREFIX_.'tax_rules_group` trg ON (trg.id_tax_rules_group = p.id_tax_rules_group)
- LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (tr.id_tax_rules_group = trg.id_tax_rules_group)
- LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.id_tax = tr.id_tax AND t.active = 1)
- WHERE id_product = '.(int)$id_product.'
- GROUP BY id_product');
- }
- else
- $max_tax_rate = 0;
-
- $product_min_prices = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
- SELECT id_shop, id_currency, id_country, id_group, from_quantity
- FROM `'._DB_PREFIX_.'specific_price`
- WHERE id_product = '.(int)$id_product);
-
- // Get min price
- foreach ($currency_list as…
Large files files are truncated, but you can click here to view the full file