* @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 = 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->_html .= '
';
$links = MenuTopLinks::gets($id_lang, null, Shop::getContextShopID());
if (!count($links))
return $this->_html;
$this->_html .= '
';
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 $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))
{
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->_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 $type => $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($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 .= '