* @copyright 2007-2016 PrestaShop SA
* @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
{
protected $_menu = '';
protected $_html = '';
protected $user_groups;
/*
* Pattern for matching config values
*/
protected $pattern = '/^([A-Z_]*)[0-9]+/';
/*
* Name of the controller
* Used to set item selected or not in top menu
*/
protected $page_name = '';
/*
* Spaces per depth in BO
*/
protected $spacer_size = '5';
public function __construct()
{
$this->name = 'blocktopmenu';
$this->tab = 'front_office_features';
$this->version = '2.2.4';
$this->author = 'PrestaShop';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Top horizontal menu');
$this->description = $this->l('Adds a new horizontal menu to the top of your e-commerce website.');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99');
}
public function install($delete_params = true)
{
if (!parent::install() ||
!$this->registerHook('header') ||
!$this->registerHook('displayTop') ||
!$this->registerHook('actionObjectCategoryUpdateAfter') ||
!$this->registerHook('actionObjectCategoryDeleteAfter') ||
!$this->registerHook('actionObjectCategoryAddAfter') ||
!$this->registerHook('actionObjectCmsUpdateAfter') ||
!$this->registerHook('actionObjectCmsDeleteAfter') ||
!$this->registerHook('actionObjectCmsAddAfter') ||
!$this->registerHook('actionObjectSupplierUpdateAfter') ||
!$this->registerHook('actionObjectSupplierDeleteAfter') ||
!$this->registerHook('actionObjectSupplierAddAfter') ||
!$this->registerHook('actionObjectManufacturerUpdateAfter') ||
!$this->registerHook('actionObjectManufacturerDeleteAfter') ||
!$this->registerHook('actionObjectManufacturerAddAfter') ||
!$this->registerHook('actionObjectProductUpdateAfter') ||
!$this->registerHook('actionObjectProductDeleteAfter') ||
!$this->registerHook('actionObjectProductAddAfter') ||
!$this->registerHook('categoryUpdate') ||
!$this->registerHook('actionShopDataDuplication')) {
return false;
}
$this->clearMenuCache();
if ($delete_params) {
if (!$this->installDb() || !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_ITEMS', 'CAT3,CAT26') || !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_SEARCH', '1')) {
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(11) UNSIGNED NOT NULL,
`new_window` TINYINT( 1 ) 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(11) UNSIGNED NOT NULL,
`id_lang` INT(11) UNSIGNED NOT NULL,
`id_shop` INT(11) UNSIGNED NOT NULL,
`label` VARCHAR( 128 ) NOT NULL ,
`link` VARCHAR( 128 ) NOT NULL ,
INDEX ( `id_linksmenutop` , `id_lang`, `id_shop`)
) ENGINE = '._MYSQL_ENGINE_.' CHARACTER SET utf8 COLLATE utf8_general_ci;'));
}
public function uninstall($delete_params = true)
{
if (!parent::uninstall()) {
return false;
}
$this->clearMenuCache();
if ($delete_params) {
if (!$this->uninstallDB() || !Configuration::deleteByName('MOD_BLOCKTOPMENU_ITEMS') || !Configuration::deleteByName('MOD_BLOCKTOPMENU_SEARCH')) {
return false;
}
}
return true;
}
protected function uninstallDb()
{
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop`');
Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'linksmenutop_lang`');
return true;
}
public function reset()
{
if (!$this->uninstall(false)) {
return false;
}
if (!$this->install(false)) {
return false;
}
return true;
}
public function getContent()
{
$this->context->controller->addjQueryPlugin('hoverIntent');
$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();
$links_label = Tools::getValue('link') ? array_filter(Tools::getValue('link'), 'strlen') : array();
$spacer = str_repeat(' ', $this->spacer_size);
$divLangName = 'link_label';
$update_cache = false;
if (Tools::isSubmit('submitBlocktopmenu')) {
$errors_update_shops = array();
$items = Tools::getValue('items');
$shops = Shop::getContextListShopID();
foreach ($shops as $shop_id) {
$shop_group_id = Shop::getGroupFromShop($shop_id);
$updated = true;
if (count($shops) == 1) {
if (is_array($items) && count($items)) {
$updated = Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', (string)implode(',', $items), false, (int)$shop_group_id, (int)$shop_id);
} else {
$updated = Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', '', false, (int)$shop_group_id, (int)$shop_id);
}
}
$updated &= Configuration::updateValue('MOD_BLOCKTOPMENU_SEARCH', (bool)Tools::getValue('search'), false, (int)$shop_group_id, (int)$shop_id);
if (!$updated) {
$shop = new Shop($shop_id);
$errors_update_shops[] = $shop->name;
}
}
if (!count($errors_update_shops)) {
$this->_html .= $this->displayConfirmation($this->l('The settings have been updated.'));
} else {
$this->_html .= $this->displayError(sprintf($this->l('Unable to update settings for the following shop(s): %s'), implode(', ', $errors_update_shops)));
}
$update_cache = true;
} else {
if (Tools::isSubmit('submitBlocktopmenuLinks')) {
$errors_add_link = array();
foreach ($languages as $key => $val) {
$links_label[$val['id_lang']] = Tools::getValue('link_'.(int)$val['id_lang']);
$labels[$val['id_lang']] = Tools::getValue('label_'.(int)$val['id_lang']);
}
$count_links_label = count($links_label);
$count_label = count($labels);
if ($count_links_label || $count_label) {
if (!$count_links_label) {
$this->_html .= $this->displayError($this->l('Please complete the "Link" field.'));
} elseif (!$count_label) {
$this->_html .= $this->displayError($this->l('Please add a label.'));
} elseif (!isset($labels[$default_language])) {
$this->_html .= $this->displayError($this->l('Please add a label for your default language.'));
} else {
$shops = Shop::getContextListShopID();
foreach ($shops as $shop_id) {
$added = MenuTopLinks::add($links_label, $labels, Tools::getValue('new_window', 0), (int)$shop_id);
if (!$added) {
$shop = new Shop($shop_id);
$errors_add_link[] = $shop->name;
}
}
if (!count($errors_add_link)) {
$this->_html .= $this->displayConfirmation($this->l('The link has been added.'));
} else {
$this->_html .= $this->displayError(sprintf($this->l('Unable to add link for the following shop(s): %s'), implode(', ', $errors_add_link)));
}
}
}
$update_cache = true;
} elseif (Tools::isSubmit('deletelinksmenutop')) {
$errors_delete_link = array();
$id_linksmenutop = Tools::getValue('id_linksmenutop', 0);
$shops = Shop::getContextListShopID();
foreach ($shops as $shop_id) {
$deleted = MenuTopLinks::remove($id_linksmenutop, (int)$shop_id);
Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', str_replace(array('LNK'.$id_linksmenutop.',', 'LNK'.$id_linksmenutop), '', Configuration::get('MOD_BLOCKTOPMENU_ITEMS')));
if (!$deleted) {
$shop = new Shop($shop_id);
$errors_delete_link[] = $shop->name;
}
}
if (!count($errors_delete_link)) {
$this->_html .= $this->displayConfirmation($this->l('The link has been removed.'));
} else {
$this->_html .= $this->displayError(sprintf($this->l('Unable to remove link for the following shop(s): %s'), implode(', ', $errors_delete_link)));
}
$update_cache = true;
} elseif (Tools::isSubmit('updatelinksmenutop')) {
$id_linksmenutop = (int)Tools::getValue('id_linksmenutop', 0);
$id_shop = (int)Shop::getContextShopID();
if (Tools::isSubmit('updatelink')) {
$link = array();
$label = array();
$new_window = (int)Tools::getValue('new_window', 0);
foreach (Language::getLanguages(false) as $lang) {
$link[$lang['id_lang']] = Tools::getValue('link_'.(int)$lang['id_lang']);
$label[$lang['id_lang']] = Tools::getValue('label_'.(int)$lang['id_lang']);
}
MenuTopLinks::update($link, $label, $new_window, (int)$id_shop, (int)$id_linksmenutop, (int)$id_linksmenutop);
$this->_html .= $this->displayConfirmation($this->l('The link has been edited.'));
}
$update_cache = true;
}
}
if ($update_cache) {
$this->clearMenuCache();
}
$shops = Shop::getContextListShopID();
$links = array();
if (count($shops) > 1) {
$this->_html .= $this->getWarningMultishopHtml();
}
if (Shop::isFeatureActive()) {
$this->_html .= $this->getCurrentShopInfoMsg();
}
$this->_html .= $this->renderForm().$this->renderAddForm();
foreach ($shops as $shop_id) {
$links = array_merge($links, MenuTopLinks::gets((int)$id_lang, null, (int)$shop_id));
}
if (!count($links)) {
return $this->_html;
}
$this->_html .= $this->renderList();
return $this->_html;
}
protected function getWarningMultishopHtml()
{
return '
'.
$this->l('You cannot manage top menu items from a "All Shops" or a "Group Shop" context, select directly the shop you want to edit').
'
';
}
protected function getCurrentShopInfoMsg()
{
$shop_info = null;
if (Shop::getContext() == Shop::CONTEXT_SHOP) {
$shop_info = sprintf($this->l('The modifications will be applied to shop: %s'), $this->context->shop->name);
} else {
if (Shop::getContext() == Shop::CONTEXT_GROUP) {
$shop_info = sprintf($this->l('The modifications will be applied to this group: %s'), Shop::getContextShopGroup()->name);
} else {
$shop_info = $this->l('The modifications will be applied to all shops');
}
}
return '