* @copyright 2007-2012 PrestaShop SA * @version Release: $Revision: 7095 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ require dirname(__FILE__).'/menutoplinks.class.php'; 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'; $this->tab = 'front_office_features'; $this->version = 1.4; $this->author = 'PrestaShop'; parent::__construct(); $this->displayName = $this->l('Top horizontal menu'); $this->description = $this->l('Add a new menu on top of your shop.'); } public function install() { if (!parent::install() || !$this->registerHook('top') || !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_ITEMS', 'CAT1,CMS1,CMS2,PRD1') || !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_SEARCH', '1') || !$this->installDB()) return false; return true; } public function installDb() { return (Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop` ( `id_linksmenutop` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, `id_shop` INT UNSIGNED NOT NULL, `new_window` TINYINT( 1 ) NOT NULL, `link` VARCHAR( 128 ) NOT NULL, INDEX (`id_shop`) ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;') && Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'linksmenutop_lang` ( `id_linksmenutop` INT NOT NULL, `id_lang` INT NOT NULL, `id_shop` INT NOT NULL, `label` VARCHAR( 128 ) NOT NULL , INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`) ) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;')); } public function uninstall() { if (!parent::uninstall() || !Configuration::deleteByName('MOD_BLOCKTOPMENU_ITEMS') || !Configuration::deleteByName('MOD_BLOCKTOPMENU_SEARCH') || !$this->uninstallDB()) return false; return true; } private function uninstallDb() { Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop`'); Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop_lang`'); return true; } public function getContent() { $id_lang = (int)Context::getContext()->language->id; $languages = $this->context->controller->getLanguages(); $default_language = (int)Configuration::get('PS_LANG_DEFAULT'); $labels = Tools::getValue('label') ? array_filter(Tools::getValue('label'), 'strlen') : array(); $spacer = str_repeat(' ', $this->spacer_size); $divLangName = 'link_label'; if (Tools::isSubmit('submitBlocktopmenu')) { if (Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', Tools::getValue('items'))) $this->_html .= $this->displayConfirmation($this->l('Settings Updated')); else $this->_html .= $this->displayError($this->l('Unable to update settings')); Configuration::updateValue('MOD_BLOCKTOPMENU_SEARCH', (bool)Tools::getValue('search')); } else if (Tools::isSubmit('submitBlocktopmenuLinks')) { if ((Tools::getValue('link') == '') && (!count($labels))) ; else if (Tools::getValue('link') == '') $this->_html .= $this->displayError($this->l('Please, fill the "Link" field')); else if (!count($labels)) $this->_html .= $this->displayError($this->l('Please add a label')); else if (!isset($labels[$default_language])) $this->_html .= $this->displayError($this->l('Please add a label for your default language')); else { MenuTopLinks::add(Tools::getValue('link'), Tools::getValue('label'), Tools::getValue('new_window', 0), (int)Shop::getContextShopID()); $this->_html .= $this->displayConfirmation($this->l('The link has been added')); } } else if (Tools::isSubmit('submitBlocktopmenuRemove')) { $id_linksmenutop = Tools::getValue('id_linksmenutop', 0); MenuTopLinks::remove($id_linksmenutop, (int)Shop::getContextShopID()); Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', str_replace(array('LNK'.$id_linksmenutop.',', 'LNK'.$id_linksmenutop), '', Configuration::get('MOD_BLOCKTOPMENU_ITEMS'))); $this->_html .= $this->displayConfirmation($this->l('The link has been removed')); } $this->_html .= '
'.$this->l('The modifications will be applied to').' '.(Shop::getContext() == Shop::CONTEXT_SHOP ? $this->l('shop:').' '.$this->context->shop->name : $this->l('all shops')).'.
'.$this->l('Settings').'
 


'.$this->l('Remove').' >>


<< '.$this->l('Add').'
 


'; $this->_html .= '
'.$this->l('Add Menu Top Link').'
'; foreach ($languages as $language) { $this->_html .= ' '; } $this->_html .= $this->displayFlags($languages, (int)$id_lang, $divLangName, 'link_label', true); $this->_html .= '


'; $links = MenuTopLinks::gets((int)$id_lang, null, (int)Shop::getContextShopID()); if (!count($links)) return $this->_html; $this->_html .= '
'.$this->l('List Menu Top Link').' '; foreach ($links as $link) { $this->_html .= ' '; } $this->_html .= '
'.$this->l('Id Link').' '.$this->l('Label').' '.$this->l('Link').' '.$this->l('New Window').' '.$this->l('Action').'
'.$link['id_linksmenutop'].' '.$link['label'].' '.$link['link'].' '.(($link['new_window']) ? $this->l('Yes') : $this->l('No')).'
'; return $this->_html; } private function getMenuItems() { return explode(',', Configuration::get('MOD_BLOCKTOPMENU_ITEMS')); } private function makeMenuOption() { $menu_item = $this->getMenuItems(); $id_lang = (int)$this->context->language->id; $id_shop = (int)Shop::getContextShopID(); foreach ($menu_item as $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((int)$id, (int)$id_lang); if (Validate::isLoadedObject($category)) $this->_html .= ''.PHP_EOL; break; case 'PRD': $product = new Product((int)$id, true, (int)$id_lang); if (Validate::isLoadedObject($product)) $this->_html .= ''.PHP_EOL; break; case 'CMS': $cms = new CMS((int)$id, (int)$id_lang); if (Validate::isLoadedObject($cms)) $this->_html .= ''.PHP_EOL; break; case 'CMS_CAT': $category = new CMSCategory((int)$id, (int)$id_lang); if (Validate::isLoadedObject($category)) $this->_html .= ''.PHP_EOL; break; case 'MAN': $manufacturer = new Manufacturer((int)$id, (int)$id_lang); if (Validate::isLoadedObject($manufacturer)) $this->_html .= ''.PHP_EOL; break; case 'SUP': $supplier = new Supplier((int)$id, (int)$id_lang); if (Validate::isLoadedObject($supplier)) $this->_html .= ''.PHP_EOL; break; case 'LNK': $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop); if (count($link)) { if (!isset($link[0]['label']) || ($link[0]['label'] == '')) { $default_language = Configuration::get('PS_LANG_DEFAULT'); $link = MenuTopLinks::get($link[0]['id_linksmenutop'], (int)$default_language, (int)Shop::getContextShopID()); } $this->_html .= ''; } break; } } } private function makeMenu() { $this->page_name = Dispatcher::getInstance()->getController(); $menu_items = $this->getMenuItems(); $id_lang = (int)$this->context->language->id; $id_shop = (int)Shop::getContextShopID(); foreach ($menu_items as $item) { 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((int)$id); break; case 'PRD': $selected = ($this->page_name == 'product' && (Tools::getValue('id_product') == $id)) ? ' class="sfHover"' : ''; $product = new Product((int)$id, true, (int)$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((int)$id_lang, array($id)); if (count($cms)) $this->_menu .= ''.$cms[0]['meta_title'].''.PHP_EOL; break; case 'CMS_CAT': $category = new CMSCategory((int)$id, (int)$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((int)$id, (int)$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((int)$id, (int)$id_lang); if (!is_null($supplier->id)) { $link = new Link; $this->_menu .= ''.$supplier->name.''.PHP_EOL; } break; case 'LNK': $link = MenuTopLinks::get((int)$id, (int)$id_lang, (int)$id_shop); if (count($link)) { if (!isset($link[0]['label']) || ($link[0]['label'] == '')) { $default_language = Configuration::get('PS_LANG_DEFAULT'); $link = MenuTopLinks::get($link[0]['id_linksmenutop'], $default_language, (int)Shop::getContextShopID()); } $this->_menu .= '
  • '.$link[0]['label'].'
  • '.PHP_EOL; } break; } } } private function getCategoryOption($id_category = 1, $id_lang = false, $id_shop = false, $recursive = true) { $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id; $category = new Category((int)$id_category, (int)$id_lang, (int)$id_shop); if (is_null($category->id)) return; if ($recursive) { $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop); $spacer = str_repeat(' ', $this->spacer_size * (int)$category->level_depth); } $shop = (object) Shop::getShop((int)$category->getShopID()); $this->_html .= ''; if (isset($children) && count($children)) foreach ($children as $child) { $this->getCategoryOption((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']); } } private function getCategory($id_category, $id_lang = false, $id_shop = false) { $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id; $category = new Category((int)$id_category, (int)$id_lang); if ($category->level_depth > 1) $category_link = $category->getLink(); else $category_link = $this->context->link->getPageLink('index'); if (is_null($category->id)) return; $children = Category::getChildren((int)$id_category, (int)$id_lang, true, (int)$id_shop); $selected = ($this->page_name == 'category' && ((int)Tools::getValue('id_category') == $id_category)) ? ' class="sfHoverForce"' : ''; $user_groups = Context::getContext()->customer->isLogged() ? Context::getContext()->customer->getGroups() : array(Configuration::get('PS_UNIDENTIFIED_GROUP')); $is_intersected = array_intersect($category->getGroups(), $user_groups); // filter the categories that the user is allowed to see and browse if (!empty($is_intersected)) { $this->_menu .= '
  • '; $this->_menu .= ''.$category->name.''; if (count($children)) { $this->_menu .= '
      '; foreach ($children as $child) $this->getCategory((int)$child['id_category'], (int)$id_lang, (int)$child['id_shop']); $this->_menu .= '
    '; } $this->_menu .= '
  • '; } } private function getCMSMenuItems($parent, $depth = 1, $id_lang = false) { $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id; if ($depth > 3) return; $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang); $pages = $this->getCMSPages((int)$parent); if (count($categories) || count($pages)) { $this->_menu .= '
      '; foreach ($categories as $category) { $this->_menu .= '
    • '; $this->_menu .= ''.$category['name'].''; $this->getCMSMenuItems($category['id_cms_category'], (int)$depth + 1); $this->_menu .= '
    • '; } foreach ($pages as $page) { $cms = new CMS($page['id_cms'], (int)$id_lang); $links = $cms->getLinks((int)$id_lang, array((int)$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, $id_lang = false) { $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id; $categories = $this->getCMSCategories(false, (int)$parent, (int)$id_lang); $pages = $this->getCMSPages((int)$parent, false, (int)$id_lang); $spacer = str_repeat(' ', $this->spacer_size * (int)$depth); foreach ($categories as $category) { $this->_html .= ''; $this->getCMSOptions($category['id_cms_category'], (int)$depth + 1, (int)$id_lang); } foreach ($pages as $page) $this->_html .= ''; } public function hookTop($param) { $this->makeMenu(); $this->smarty->assign('MENU_SEARCH', Configuration::get('MOD_BLOCKTOPMENU_SEARCH')); $this->smarty->assign('MENU', $this->_menu); $this->smarty->assign('this_path', $this->_path); $this->context->controller->addJS($this->_path.'js/hoverIntent.js'); $this->context->controller->addJS($this->_path.'js/superfish-modified.js'); $this->context->controller->addCSS($this->_path.'css/superfish-modified.css'); return $this->display(__FILE__, 'blocktopmenu.tpl'); } private function getCMSCategories($recursive = false, $parent = 1, $id_lang = false) { $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->id; 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)$id_lang.' 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)$id_lang.' AND bcp.`id_parent` = '.(int)$parent; $results = Db::getInstance()->executeS($sql); foreach ($results as $result) { $sub_categories = $this->getCMSCategories(true, $result['id_cms_category'], (int)$id_lang); 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_lang = false) { $id_shop = ($id_shop !== false) ? (int)$id_shop : (int)Context::getContext()->shop->id; $id_lang = $id_lang ? (int)$id_lang : (int)Context::getContext()->language->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)$id_lang.' AND c.`active` = 1 ORDER BY `position`'; return Db::getInstance()->executeS($sql); } }