From a0921c95e9e21023c42e817d8eee6db50f70f53a Mon Sep 17 00:00:00 2001 From: fGaillard Date: Thu, 1 Mar 2012 13:39:15 +0000 Subject: [PATCH] [-] BO : #PSFV-494 : BugFix - Block top menu now display CMS categories git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@13781 b9a71923-0436-4b27-9f14-aed3839534dd --- modules/blocktopmenu/blocktopmenu.php | 462 +++++++++++++++++--------- 1 file changed, 312 insertions(+), 150 deletions(-) diff --git a/modules/blocktopmenu/blocktopmenu.php b/modules/blocktopmenu/blocktopmenu.php index abac27748..f607f3d2a 100644 --- a/modules/blocktopmenu/blocktopmenu.php +++ b/modules/blocktopmenu/blocktopmenu.php @@ -32,6 +32,22 @@ class blocktopmenu extends Module private $_menu = ''; private $_html = ''; + /* + * Pattern for matching config values + */ + private $pattern = '/^([A-Z_]*)[0-9]+/'; + + /* + * Name of the controller + * Used to set item selected or not in top menu + */ + private $page_name = ''; + + /* + * Spaces per depth in BO + */ + private $spacer_size = '5'; + public function __construct() { $this->name = 'blocktopmenu'; @@ -95,7 +111,9 @@ class blocktopmenu extends Module public function getContent() { - global $cookie; + $id_lang = Shop::getContextShopID(); + $spacer = str_repeat(' ', $this->spacer_size); + if(Tools::isSubmit('submitBlocktopmenu')) { if (Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', Tools::getValue('items'))) @@ -104,7 +122,7 @@ class blocktopmenu extends Module $this->_html .= $this->displayError($this->l('Unable to update settings')); Configuration::updateValue('MOD_BLOCKTOPMENU_SEARCH', (bool)Tools::getValue('search')); } - if(Tools::isSubmit('submitBlocktopmenuLinks')) + else if(Tools::isSubmit('submitBlocktopmenuLinks')) { if(Tools::getValue('link') == '') { @@ -116,7 +134,7 @@ class blocktopmenu extends Module $this->_html .= $this->displayConfirmation($this->l('The link has been added')); } } - if(Tools::isSubmit('submitBlocktopmenuRemove')) + else if(Tools::isSubmit('submitBlocktopmenuRemove')) { $id_linksmenutop = Tools::getValue('id_linksmenutop', 0); MenuTopLinks::remove($id_linksmenutop, (int)$this->context->shop->id); @@ -151,44 +169,43 @@ class blocktopmenu extends Module

<< '.$this->l('Add').' @@ -208,7 +225,8 @@ class blocktopmenu extends Module $("#availableItems option:selected").each(function(i){ var val = $(this).val(); var text = $(this).text(); - if(val == "PRODUCT") + text = text.replace(/(^\s*)|(\s*$)/gi,""); + if (val == "PRODUCT") { val = prompt("'.$this->l('Set ID product').'"); if(val == null || val == "" || isNaN(val)) @@ -249,10 +267,11 @@ class blocktopmenu extends Module
'; - $defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT')); - $languages = Language::getLanguages(); + $defaultLanguage = intval($this->context->language->id); + $languages = $this->context->controller->getLanguages(); $iso = Language::getIsoById($defaultLanguage); $divLangName = 'link_label'; + $this->_html .= '
'.$this->l('Add Menu Top Link').' @@ -283,6 +302,11 @@ class blocktopmenu extends Module

'; + $links = MenuTopLinks::gets($id_lang, null, Shop::getContextShopID()); + + if (!count($links)) + return $this->_html; + $this->_html .= '
'.$this->l('List Menu Top Link').' @@ -297,14 +321,13 @@ class blocktopmenu extends Module '; - $links = MenuTopLinks::gets($cookie->id_lang, null, $this->context->shop->id); foreach($links as $link) { $this->_html .= ' '.$link['id_linksmenutop'].' '.$link['label'].' - '.$link['link'].' + '.$link['link'].' '.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'
@@ -322,162 +345,301 @@ class blocktopmenu extends Module private function getMenuItems() { - // @todo why this ? - return array('global' => explode(',', Configuration::get('MOD_BLOCKTOPMENU_ITEMS'))); + return explode(',', Configuration::get('MOD_BLOCKTOPMENU_ITEMS')); } private function makeMenuOption() { - global $cookie; - foreach($this->getMenuItems() as $type => $items) - { - foreach ($items as $item) - { - $id = (int)substr($item, 3, strlen($item)); - $disabled = ((Shop::getContext() == Shop::CONTEXT_SHOP && $type == 'global') ? ' disabled="disabled"': ''); - switch(substr($item, 0, 3)) - { - case'CAT': - $this->getCategoryOption($id, $cookie->id_lang, false); - break; - case'PRD': - $product = new Product($id, true, $cookie->id_lang); - if(!is_null($product->id)) - $this->_html .= ''.PHP_EOL; - break; - case'CMS': - $cms = CMS::getLinks($cookie->id_lang, array($id)); - if(count($cms)) - $this->_html .= ''.PHP_EOL; - break; - case'MAN': - $manufacturer = new Manufacturer($id, $cookie->id_lang); - if(!is_null($manufacturer->id)) - $this->_html .= ''.PHP_EOL; - break; - case'SUP': - $supplier = new Supplier($id, $cookie->id_lang); - if(!is_null($supplier->id)) - $this->_html .= ''.PHP_EOL; - break; - case'LNK': - $link = MenuTopLinks::get($id, $cookie->id_lang, (int)$this->context->shop->id); - if(count($link)) - $this->_html .= ''.PHP_EOL; - break; - } - } - } + $menu_item = $this->getMenuItems(); + $id_lang = (int)$this->context->language->id; + $id_shop = (int)Shop::getContextShopID(); + + foreach($menu_item as $type => $item) + { + if (!$item) + continue; + + preg_match($this->pattern, $item, $values); + $id = (int)substr($item, strlen($values[1]), strlen($item)); + + switch (substr($item, 0, strlen($values[1]))) + { + case'CAT': + $category = new Category($id, $id_lang); + if(!is_null($category->id)) + $this->_html .= ''.PHP_EOL; + break; + case'PRD': + $product = new Product($id, true, $id_lang); + if(!is_null($product->id)) + $this->_html .= ''.PHP_EOL; + break; + case'CMS': + $cms = new CMS($id, $id_lang); + if (count($cms)) + $this->_html .= ''.PHP_EOL; + break; + case'CMS_CAT': + $category = new CMSCategory($id, $id_lang); + if (count($category)) + $this->_html .= ''.PHP_EOL; + break; + case'MAN': + $manufacturer = new Manufacturer($id, $id_lang); + if(!is_null($manufacturer->id)) + $this->_html .= ''.PHP_EOL; + break; + case'SUP': + $supplier = new Supplier($id, $id_lang); + if(!is_null($supplier->id)) + $this->_html .= ''.PHP_EOL; + break; + case'LNK': + $link = MenuTopLinks::get($id, $id_lang, $id_shop); + if(count($link)) + $this->_html .= ''.PHP_EOL; + break; + } + } } private function makeMenu() { - global $cookie, $page_name; - foreach($this->getMenuItems() as $type => $items) + $this->page_name = $this->context->controller->php_self; + + $menu_items = $this->getMenuItems(); + $id_lang = (int)$this->context->language->id; + $id_shop = (int)Shop::getContextShopID(); + + foreach($menu_items as $type => $item) { - foreach ($items as $item) - { - $id = (int)substr($item, 3, strlen($item)); - switch(substr($item, 0, 3)) - { - case'CAT': - $this->getCategory($id, $cookie->id_lang); - break; - case'PRD': - $selected = ($page_name == 'product' && (Tools::getValue('id_product') == $id)) ? ' class="sfHover"' : ''; - $product = new Product($id, true, $cookie->id_lang); - if(!is_null($product->id)) - $this->_menu .= ''.$product->name.''.PHP_EOL; - break; - case'CMS': - $selected = ($page_name == 'cms' && (Tools::getValue('id_cms') == $id)) ? ' class="sfHover"' : ''; - $cms = CMS::getLinks($cookie->id_lang, array($id)); - if(count($cms)) - $this->_menu .= ''.$cms[0]['meta_title'].''.PHP_EOL; - break; - case'MAN': - $selected = ($page_name == 'manufacturer' && (Tools::getValue('id_manufacturer') == $id)) ? ' class="sfHover"' : ''; - $manufacturer = new Manufacturer($id, $cookie->id_lang); - if(!is_null($manufacturer->id)) - { - if (intval(Configuration::get('PS_REWRITING_SETTINGS'))) - $manufacturer->link_rewrite = Tools::link_rewrite($manufacturer->name, false); - else - $manufacturer->link_rewrite = 0; - $link = new Link; - $this->_menu .= ''.$manufacturer->name.''.PHP_EOL; - } - break; - case'SUP': - $selected = ($page_name == 'supplier' && (Tools::getValue('id_supplier') == $id)) ? ' class="sfHover"' : ''; - $supplier = new Supplier($id, $cookie->id_lang); - if(!is_null($supplier->id)) - { - $link = new Link; - $this->_menu .= ''.$supplier->name.''.PHP_EOL; - } - break; - case'LNK': - $link = MenuTopLinks::get($id, $cookie->id_lang, (int)$this->context->shop->id); - if(count($link)) - $this->_menu .= '
  • '.$link[0]['label'].'
  • '.PHP_EOL; - break; - } - } + if (!$item) + continue; + + preg_match($this->pattern, $item, $value); + $id = (int)substr($item, strlen($value[1]), strlen($item)); + + switch(substr($item, 0, strlen($value[1]))) + { + case'CAT': + $this->getCategory($id); + break; + case'PRD': + $selected = ($this->page_name == 'product' && (Tools::getValue('id_product') == $id)) ? ' class="sfHover"' : ''; + $product = new Product($id, true, $id_lang); + if (!is_null($product->id)) + $this->_menu .= ''.$product->name.''.PHP_EOL; + break; + case'CMS': + $selected = ($this->page_name == 'cms' && (Tools::getValue('id_cms') == $id)) ? ' class="sfHover"' : ''; + $cms = CMS::getLinks($id_lang, array($id)); + if (count($cms)) + $this->_menu .= ''.$cms[0]['meta_title'].''.PHP_EOL; + break; + case'CMS_CAT': + $category = new CMSCategory($id, $id_lang); + if (count($category)) + { + $this->_menu .= '
  • '.$category->name.''; + $this->getCMSMenuItems($category->id); + $this->_menu .= '
  • '.PHP_EOL; + } + break; + case'MAN': + $selected = ($this->page_name == 'manufacturer' && (Tools::getValue('id_manufacturer') == $id)) ? ' class="sfHover"' : ''; + $manufacturer = new Manufacturer($id, $id_lang); + if (!is_null($manufacturer->id)) + { + if (intval(Configuration::get('PS_REWRITING_SETTINGS'))) + $manufacturer->link_rewrite = Tools::link_rewrite($manufacturer->name, false); + else + $manufacturer->link_rewrite = 0; + $link = new Link; + $this->_menu .= ''.$manufacturer->name.''.PHP_EOL; + } + break; + case'SUP': + $selected = ($this->page_name == 'supplier' && (Tools::getValue('id_supplier') == $id)) ? ' class="sfHover"' : ''; + $supplier = new Supplier($id, $id_lang); + if (!is_null($supplier->id)) + { + $link = new Link; + $this->_menu .= ''.$supplier->name.''.PHP_EOL; + } + break; + case'LNK': + $link = MenuTopLinks::get($id, $id_lang, $id_shop); + if (count($link)) + $this->_menu .= '
  • '.$link[0]['label'].'
  • '.PHP_EOL; + break; + } } } - private function getCategoryOption($id_category, $id_lang, $children = true) - { - $categorie = new Category($id_category, $id_lang); - if(is_null($categorie->id)) - return; - if(count(explode('.', $categorie->name)) > 1) - $name = str_replace('.', '', strstr($categorie->name, '.')); - else - $name = $categorie->name; - $this->_html .= ''; - if($children) - { - $childrens = Category::getChildren($id_category, $id_lang); - if(count($childrens)) - foreach($childrens as $children) - $this->getCategoryOption($children['id_category'], $id_lang); - } - } + private function getCategoryOption($id_category = 1, $id_lang = false, $recursive = true) + { + $id_lang = (int)($id_lang ? $id_lang : Shop::getContextShopID()); + $category = new Category($id_category, $id_lang); - private function getCategory($id_category, $id_lang) - { - global $page_name; + if (is_null($category->id)) + return; - $categorie = new Category($id_category, $id_lang); - if(is_null($categorie->id)) + if ($recursive) + { + $childrens = Category::getChildren((int)$id_category, $id_lang); + $spacer = str_repeat(' ', $this->spacer_size * (int)$category->level_depth); + } + + $this->_html .= ''; + + if (isset($childrens) && count($childrens)) + foreach($childrens as $children) + $this->getCategoryOption((int)$children['id_category'], $id_lang); + } + + private function getCategory($id_category, $id_lang = false) + { + $id_lang = $id_lang ? $id_lang : Shop::getContextShopID(); + $category = new Category($id_category, $id_lang); + + if (is_null($category->id)) return; - $selected = ($page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : ''; - $this->_menu .= ''; - if(count(explode('.', $categorie->name)) > 1) - $name = str_replace('.', '', strstr($categorie->name, '.')); - else - $name = $categorie->name; - $this->_menu .= ''.$name.''; - $childrens = Category::getChildren($id_category, $id_lang); - if(count($childrens)) + + $childrens = Category::getChildren($id_category, $id_lang); + $selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : ''; + $this->_menu .= '
  • '; + $this->_menu .= ''.$category->name.''; + + if (count($childrens)) { $this->_menu .= '
      '; + foreach($childrens as $children) $this->getCategory($children['id_category'], $id_lang); + $this->_menu .= '
    '; } $this->_menu .= '
  • '; } + private function getCMSMenuItems($parent, $depth = 1) + { + $id_lang = Context::getContext()->language->id; + + if ($depth > 3) + return; + + $categories = $this->getCMSCategories(false, $parent); + $pages = $this->getCMSPages($parent); + + if (count($categories) || count($pages)) + { + $this->_menu .= '
      '; + + foreach ($categories as $category) + { + $this->_menu .= '
    • '; + $this->_menu .= ''.$category['name'].''; + $this->getCMSMenuItems($category['id_cms_category'], $depth + 1); + $this->_menu .= '
    • '; + } + + foreach ($pages as $page) + { + $cms = new CMS($page['id_cms'], $id_lang); + $links = $cms->getLinks($id_lang, array($cms->id)); + + $selected = ($this->page_name == 'cms' && ((int)Tools::getValue('id_cms') == $page['id_cms'])) ? ' class="sfHoverForce"' : ''; + $this->_menu .= '
    • '; + $this->_menu .= ''.$cms->meta_title.''; + $this->_menu .= '
    • '; + } + + $this->_menu .= '
    '; + } + } + + private function getCMSOptions($parent = 0, $depth = 1) + { + $categories = $this->getCMSCategories(false, $parent); + $pages = $this->getCMSPages($parent); + $spacer = str_repeat(' ', $this->spacer_size * (int)$depth); + + foreach ($categories as $category) + { + $this->_html .= ''; + $this->getCMSOptions($category['id_cms_category'], $depth + 1); + } + + foreach ($pages as $page) + $this->_html .= ''; + } + public function hookTop($param) { global $smarty; + $this->makeMenu(); + $smarty->assign('MENU_SEARCH', Configuration::get('MOD_BLOCKTOPMENU_SEARCH')); $smarty->assign('MENU', $this->_menu); $smarty->assign('this_path', $this->_path); + return $this->display(__FILE__, 'blocktopmenu.tpl'); } + + private function getCMSCategories($recursive = false, $parent = 1) + { + if ($recursive === false) + { + $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite` + FROM `'._DB_PREFIX_.'cms_category` bcp + INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl + ON (bcp.`id_cms_category` = cl.`id_cms_category`) + WHERE cl.`id_lang` = '.(int)Context::getContext()->language->id.' + AND bcp.`id_parent` = '.(int)$parent; + + return Db::getInstance()->executeS($sql); + } + else + { + $sql = 'SELECT bcp.`id_cms_category`, bcp.`id_parent`, bcp.`level_depth`, bcp.`active`, bcp.`position`, cl.`name`, cl.`link_rewrite` + FROM `'._DB_PREFIX_.'cms_category` bcp + INNER JOIN `'._DB_PREFIX_.'cms_category_lang` cl + ON (bcp.`id_cms_category` = cl.`id_cms_category`) + WHERE cl.`id_lang` = '.(int)Context::getContext()->language->id.' + AND bcp.`id_parent` = '.(int)$parent; + + $results = Db::getInstance()->executeS($sql); + foreach ($results as $result) + { + $sub_categories = $this->getCMSCategories(true, $result['id_cms_category']); + if ($sub_categories && count($sub_categories) > 0) + $result['sub_categories'] = $sub_categories; + $categories[] = $result; + } + + return isset($categories) ? $categories : false; + } + + } + + private function getCMSPages($id_cms_category, $id_shop = false) + { + $id_shop = ($id_shop !== false) ? $id_shop : Context::getContext()->shop->id; + + $sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite` + FROM `'._DB_PREFIX_.'cms` c + INNER JOIN `'._DB_PREFIX_.'cms_shop` cs + ON (c.`id_cms` = cs.`id_cms`) + INNER JOIN `'._DB_PREFIX_.'cms_lang` cl + ON (c.`id_cms` = cl.`id_cms`) + WHERE c.`id_cms_category` = '.(int)$id_cms_category.' + AND cs.`id_shop` = '.(int)$id_shop.' + AND cl.`id_lang` = '.(int)Context::getContext()->language->id.' + AND c.`active` = 1 + ORDER BY `position`'; + + return Db::getInstance()->executeS($sql); + } } \ No newline at end of file