From 224b55c3c2ff6e934766b7acb8f3ad9dbd761079 Mon Sep 17 00:00:00 2001 From: rMalie Date: Wed, 14 Sep 2011 16:13:24 +0000 Subject: [PATCH] // Merge -> revision 8589 git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8596 b9a71923-0436-4b27-9f14-aed3839534dd --- controllers/CategoryController.php | 1 + index.php | 2 +- modules/blocklayered/blocklayered.js | 27 ++- modules/blocklayered/blocklayered.php | 227 ++++++++++---------------- 4 files changed, 108 insertions(+), 149 deletions(-) diff --git a/controllers/CategoryController.php b/controllers/CategoryController.php index 978e466e6..3b50ff69a 100644 --- a/controllers/CategoryController.php +++ b/controllers/CategoryController.php @@ -171,6 +171,7 @@ class CategoryControllerCore extends FrontController Module::hookExec('productListAssign', array('nbProducts' => &$this->nbProducts, 'catProducts' => &$this->cat_products, 'hookExecuted' => &$hookExecuted)); if (!$hookExecuted) { + self::$smarty->assign('categoryNameComplement', ''); $this->nbProducts = $this->category->getProducts(NULL, NULL, NULL, $this->orderBy, $this->orderWay, true); $this->pagination((int)$this->nbProducts); $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay); diff --git a/index.php b/index.php index bb4a9a351..cc5ce8475 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,6 @@ ').attr('type', 'hidden').attr('name', $(this).attr('name')).val($(this).attr('rel')).appendTo('#layered_form'); else $('\'input[name='+$(this).attr('name')+']:hidden\'').remove(); + window.location = '#'+$(this).next().children(1).attr('href').replace(/https?:\/\/.*\/\d+-[^\/]+/, ''); reloadContent(); }); + // Click on checkbox $('#layered_form input[type=checkbox]').live('click', function() { + window.location = '#'+$(this).next().children(1).attr('href').replace(/https?:\/\/.*\/\d+-[^\/]+/, ''); reloadContent(); }); + // Click on label $("label a").live({ click: function() { + if($(this).parent().parent().find('input').attr('disabled') == '') + { + window.location = '#'+this.href.replace(/https?:\/\/.*\/\d+-[^\/]+/, ''); $(this).parent().parent().find('input').click(); reloadContent(); + } + return false; } }); paginationButton(); initLayered(); - reloadContent(); }); function addSlider(type, data, unit) @@ -81,7 +90,14 @@ function initSliders() function initLayered() { initSliders(); - var params = document.location.toString(); + if (window.location.href.split('#').length == 2) + { + var params = window.location.href.split('#')[1]; + reloadContent('&selected_filters='+params, true); + } + else + { + var params = window.location.href; params = friendlyUrl(params, 'long'); params = params.split('#'); params.shift(); @@ -99,6 +115,8 @@ function initLayered() $('#'+val.split('=')[0]).click(); } }); + reloadContent(); +} } function updatelink(link) @@ -216,15 +234,14 @@ function openCloseFilter() }); } -function reloadContent(params_plus) +function reloadContent(params_plus, force) { - if (typeof(allowReload) == 'undefined') + if (typeof(allowReload) == 'undefined' && typeof(force) != 'undefined' && force != true) { allowReload = true; return; } - window.location = updatelink( window.location.href); for(i = 0; i < ajaxQueries.length; i++) ajaxQueries[i].abort(); ajaxQueries = new Array(); diff --git a/modules/blocklayered/blocklayered.php b/modules/blocklayered/blocklayered.php index 000452c53..f81b4e384 100644 --- a/modules/blocklayered/blocklayered.php +++ b/modules/blocklayered/blocklayered.php @@ -66,6 +66,9 @@ class BlockLayered extends Module self::_installFriendlyUrlTable(); self::_installIndexableAttributeTable(); + $this->indexUrl(); + self::fullIndexProcess(); + return $result; } @@ -75,12 +78,12 @@ class BlockLayered extends Module Configuration::deleteByName('PS_LAYERED_HIDE_0_VALUES'); Configuration::deleteByName('PS_LAYERED_SHOW_QTIES'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'price_static_index'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'layered_friendly_url'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'layered_indexable_attribute_group'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'layered_indexable_feature'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'layered_category'); - Db::getInstance()->Execute('DROP TABLE '._DB_PREFIX_.'layered_filter'); + Db::getInstance()->Execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'price_static_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_category'); + Db::getInstance()->Execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'layered_filter'); return parent::uninstall(); } @@ -138,20 +141,15 @@ class BlockLayered extends Module /* * Url indexation */ - public function indexUrl($cursor = array(), $ajax = false, $truncate = true) + public function indexUrl($ajax = false, $truncate = true) { if($truncate) Db::getInstance()->execute('TRUNCATE '._DB_PREFIX_.'layered_friendly_url'); - if(!empty($idCategory)) - $categorySubQuery = ' AND lc.id_category = '.(int)$idCategory; - else - $categorySubQuery = ''; - $attributeValues = 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'.$categorySubQuery.') + INNER JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = lc.id_category AND lc.id_category <> 1 ) ORDER BY position ASC'); if (!$filters) return; @@ -177,7 +175,7 @@ class BlockLayered extends Module if (!isset($attributeValues[$attribute['id_lang']][$filter['id_category']]['c'.$attribute['id_name']])) $attributeValues[$attribute['id_lang']][$filter['id_category']]['c'.$attribute['id_name']] = array(); $attributeValues[$attribute['id_lang']][$filter['id_category']]['c'.$attribute['id_name']][] = array('name' => $attribute['name'], - 'id_name' => 'c'.$attribute['id_name'], 'value' => $attribute['value'], 'id_value' => $attribute['id_value'].'_'.$attribute['id_name'], + 'id_name' => 'c'.$attribute['id_name'], 'value' => $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; @@ -209,8 +207,9 @@ class BlockLayered extends Module 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 nleft > (SELECT nleft FROM '._DB_PREFIX_.'category WHERE id_category = 2) - AND nright < (SELECT nright FROM '._DB_PREFIX_.'category WHERE id_category = 2) + WHERE nleft > (SELECT nleft FROM '._DB_PREFIX_.'category WHERE id_category = '.$filter['id_category'].') + AND nright < (SELECT nright FROM '._DB_PREFIX_.'category WHERE id_category = '.$filter['id_category'].') + AND cl.id_lang = '.(int)$filter['id_lang'].' '); foreach($categories as $category) { @@ -230,6 +229,7 @@ class BlockLayered extends Module $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) @@ -264,107 +264,23 @@ class BlockLayered extends Module break; } - $maxExecutionTime = ini_get('max_execution_time') * 0.9; // 90% of safety margin - if ($maxExecutionTime > 5) - $maxExecutionTime = 5; - $startTime = microtime(true); - $memoryLimit = (Tools::getMemoryLimit() * 0.9); - - if (empty($cursor)) - { - $cursor = array( - 'attribute_values' => 0, - 'tmp1' => 0, - 'possibility' => 0, - 'total' => 0 - ); - - if ($ajax) - { - // Calculation, to eval progression - $nbPosibilities = 0; - foreach ($attributeValues as $idLang => $tmp1) - foreach ($tmp1 as $key => $tmp2) - { - $nbPosibilitiesTmp = 1; - foreach ($tmp2 as $name) - { - $count = count($name)+1; - $nbPosibilitiesTmp *= $count; - } - $nbPosibilities += $nbPosibilitiesTmp; - } - $cursor['nb_posibilities'] = $nbPosibilities; - } - } - - // Foreach langs $attributeValuesKeys = array_keys($attributeValues); - for (;$cursor['attribute_values'] < count($attributeValuesKeys); $cursor['attribute_values']++) + foreach($attributeValues as $id_lang => $attributesByCategoriesByLang) { - $id_lang = $attributeValuesKeys[$cursor['attribute_values']]; - $tmp1 = &$attributeValues[$id_lang]; - // Foreach categories - $tmp1Keys = array_keys($tmp1); - for(; $cursor['tmp1'] < count($tmp1Keys); $cursor['tmp1']++) + foreach($attributesByCategoriesByLang as $id_category => $attributesByCategory) { - $id_category = $tmp1Keys[$cursor['tmp1']]; - $tmp2 = &$tmp1[$id_category]; - - $cursors = array_fill(0, count($tmp2), 0); - $limits = array(); - $nbName = count($tmp2); - $nbPosibilities = 1; - $tmp2 = array_values($tmp2); - - // fill limits and calculate the number of combination possible - foreach ($tmp2 as $name) + foreach($attributesByCategory as $attribute) { - $count = count($name)+1; - $limits[] = $count; - $nbPosibilities *= $count; - } - - // Loop all url posibilities - for (; $cursor['possibility'] < $nbPosibilities; $cursor['possibility']++) + foreach($attribute as $param) { - if ($memoryLimit < memory_get_peak_usage() OR (microtime(true) - $startTime) > $maxExecutionTime) - { - if ($ajax) - return Tools::jsonEncode(array('cursor' => Tools::jsonEncode($cursor))); - Tools::file_get_contents(Tools::getProtocol().Tools::getHttpHost().'/modules/blocklayered/blocklayered-url-indexer.php'.'?token='.substr(Tools::encrypt('blocklayered/index'), 0, 10).'&cursor='.Tools::jsonEncode($cursor)); - return 1; - } - - $cursor['total']++; - // generate all parameters posibilities - $a = 1; - foreach ($cursors as $position => $cursorP) - { - // Get all possible combination (one by group) - // 0 means no combinations selected in the group - $cursors[$position] = (($cursor['possibility'] / ($a)) % $limits[$position]); - $a *= $limits[$position]; - } - $link = ''; - $selectedFilters = array('category' => array()); - - // Generate url with selected filters - foreach ($cursors as $position => $cursorP) - { - if ($cursorP != 0) - { - $link .= '/'.Tools::link_rewrite($tmp2[$position][$cursorP-1]['name'].'-'.$tmp2[$position][$cursorP-1]['value']); - if (!isset($selectedFilters[$tmp2[$position][$cursorP-1]['type']])) - $selectedFilters[$tmp2[$position][$cursorP-1]['type']] = array(); - if (!isset($tmp2[$position][$cursorP-1]['id_id_value'])) - $tmp2[$position][$cursorP-1]['id_id_value'] = $tmp2[$position][$cursorP-1]['id_value']; - $selectedFilters[$tmp2[$position][$cursorP-1]['type']][$tmp2[$position][$cursorP-1]['id_id_value']] = $tmp2[$position][$cursorP-1]['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.'\''); if ($idLayeredFriendlyUrl == false) @@ -373,9 +289,8 @@ class BlockLayered extends Module $idLayeredFriendlyUrl = Db::getInstance()->Insert_ID(); } } - $cursor['possibility'] = 0; } - $cursor['tmp1'] = 0; + } } if ($ajax) return '{"result": 1}'; @@ -545,7 +460,7 @@ class BlockLayered extends Module WHERE `active` = 1 AND psi.id_product IS NULL'); $maxExecutionTime = ini_get('max_execution_time') * 0.9; // 90% of safety margin - if ($maxExecutionTime > 5) + if ($maxExecutionTime > 5 || $maxExecutionTime <= 0) $maxExecutionTime = 5; $startTime = microtime(true); @@ -949,9 +864,6 @@ class BlockLayered extends Module '.$this->l('A nightly rebuild is recommended.').'