From 15335a86dc0c8c7e8a51ff7802abecf14487b35c Mon Sep 17 00:00:00 2001 From: rMalie Date: Mon, 2 Jan 2012 17:06:33 +0000 Subject: [PATCH] // Add {categories} keyword in product_rule dispatch rule #PSFV-321 --- classes/Dispatcher.php | 1 + classes/Link.php | 8 ++++++++ classes/Product.php | 31 ++++++++++++++++++++++++++++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/classes/Dispatcher.php b/classes/Dispatcher.php index 7b5199408..815a4d071 100644 --- a/classes/Dispatcher.php +++ b/classes/Dispatcher.php @@ -47,6 +47,7 @@ class DispatcherCore 'rewrite' => array('regexp' => '[a-zA-Z0-9-\pL]*'), 'ean13' => array('regexp' => '[a-zA-Z0-9-\pL]*'), 'category' => array('regexp' => '[a-zA-Z0-9-\pL]*'), + 'categories' => array('regexp' => '[/a-zA-Z0-9-\pL]*'), 'reference' => array('regexp' => '[a-zA-Z0-9-\pL]*'), 'meta_keywords' => array('regexp' => '[a-zA-Z0-9-\pL]*'), 'meta_title' => array('regexp' => '[a-zA-Z0-9-\pL]*'), diff --git a/classes/Link.php b/classes/Link.php index 84bfd1b2e..73b02493f 100644 --- a/classes/Link.php +++ b/classes/Link.php @@ -116,6 +116,14 @@ class LinkCore if ($dispatcher->hasKeyword('product_rule', 'tags')) $params['tags'] = Tools::str2url($product->getTags($id_lang)); + if ($dispatcher->hasKeyword('product_rule', 'categories')) + { + $cats = array(); + foreach ($product->getParentCategories() as $cat) + $cats[] = $cat['link_rewrite']; + $params['categories'] = implode('/', $cats); + } + if ($ipa) $anchor = $product->getAnchor($ipa); else diff --git a/classes/Product.php b/classes/Product.php index aaeb2c732..93ea878aa 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -2072,14 +2072,17 @@ class ProductCore extends ObjectModel return $ret; } - public static function getProductCategoriesFull($id_product = '', $id_lang) + public static function getProductCategoriesFull($id_product = '', $id_lang = null) { + if (!$id_lang) + $id_lang = Context::getContext()->language->id; + $ret = array(); $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT cp.`id_category`, cl.`name`, cl.`link_rewrite` FROM `'._DB_PREFIX_.'category_product` cp LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cp.`id_category` = cl.`id_category`'.Context::getContext()->shop->addSqlRestrictionOnLang('cl').') WHERE cp.`id_product` = '.(int)$id_product.' - AND cl.`id_lang` = '.(int)$id_lang + AND cl.`id_lang` = '.(int)$id_lang ); foreach ($row as $val) @@ -4696,11 +4699,33 @@ class ProductCore extends ObjectModel /** * This method allows to flush price cache * @static - * @since 1.5.0.2 + * @since 1.5.0 */ public static function flushPriceCache() { self::$_prices = array(); self::$_pricesLevel2 = array(); } + + /** + * Get list of parent categories + * + * @since 1.5.0 + * @param int $id_lang + * @return array + */ + public function getParentCategories($id_lang = null) + { + if (!$id_lang) + $id_lang = Context::getContext()->language->id; + + $interval = Category::getInterval($this->id_category_default); + $sql = new DbQuery(); + $sql->from('category', 'c'); + $sql->leftJoin('category_lang', 'cl', 'c.id_category = cl.id_category AND id_lang = '.(int)$id_lang.Context::getContext()->shop->addSqlRestrictionOnLang('cl')); + $sql->where('c.nleft <= '.(int)$interval['nleft'].' AND c.nright >= '.(int)$interval['nright']); + $sql->orderBy('c.nleft'); + + return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); + } }