* @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 blocktopdropdownmenu 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 = 'blocktopdropdownmenu';
$this->tab = 'front_office_features';
$this->version = '1.0.5';
$this->author = 'MyPresta.eu + PrestaShop';
$this->mypresta_link = 'https://mypresta.eu/modules/front-office-features/top-horizontal-menu-with-dropdowns.html';
$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. It is an modification of default module that creates dropdown menu');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => '1.7.99.99');
$this->checkforupdates(0,0);
if (Module::isEnabled('ps_mainmenu')){
$this->context->controller->warnings[] = $this->l('Top horizontal menu with dropdowns does not work with default "Main menu" module.').' '.$this->l('To use this module you need to disable the official "Main menu" first');
if (Module::disableByName('ps_mainmenu')){
if (Module::isEnabled('blocktopdropdownmenu'))
{
$this->context->controller->success[] = $this->l('We disabled this module automatically');
}
}
}
}
public function checkforupdates($display_msg = 0, $form = 0)
{
// ---------- //
// ---------- //
// VERSION 16 //
// ---------- //
// ---------- //
$this->mkey = "nlc";
if (@file_exists('../modules/' . $this->name . '/key.php')) {
@require_once('../modules/' . $this->name . '/key.php');
} else {
if (@file_exists(dirname(__FILE__) . $this->name . '/key.php')) {
@require_once(dirname(__FILE__) . $this->name . '/key.php');
} else {
if (@file_exists('modules/' . $this->name . '/key.php')) {
@require_once('modules/' . $this->name . '/key.php');
}
}
}
if ($form == 1) {
return '
';
} else {
if (defined('_PS_ADMIN_DIR_')) {
if (Tools::isSubmit('submit_settings_updates')) {
Configuration::updateValue('mypresta_updates', Tools::getValue('mypresta_updates'));
}
if (Configuration::get('mypresta_updates') != 0 || (bool)Configuration::get('mypresta_updates') != false) {
if (Configuration::get('update_' . $this->name) < (date("U") - 259200)) {
$actual_version = blocktopdropdownmenuUpdate::verify($this->name, (isset($this->mkey) ? $this->mkey : 'nokey'), $this->version);
}
if (blocktopdropdownmenuUpdate::version($this->version) < blocktopdropdownmenuUpdate::version(Configuration::get('updatev_' . $this->name)) && Tools::getValue('ajax', 'false') == 'false') {
$this->context->controller->warnings[] = '' . $this->displayName . ': ' . $this->l('New version available, check http://MyPresta.eu for more informations') . ' ' . $this->l('More details in changelog') . '';
$this->warning = $this->context->controller->warnings[0];
}
} else {
if (Configuration::get('update_' . $this->name) < (date("U") - 259200)) {
$actual_version = blocktopdropdownmenuUpdate::verify($this->name, (isset($this->mkey) ? $this->mkey : 'nokey'), $this->version);
}
}
if ($display_msg == 1) {
if (blocktopdropdownmenuUpdate::version($this->version) < blocktopdropdownmenuUpdate::version(blocktopdropdownmenuUpdate::verify($this->name, (isset($this->mkey) ? $this->mkey : 'nokey'), $this->version))) {
return "" . $this->l('New version available!') . "";
} else {
return "" . $this->l('Module is up to date!') . "";
}
}
}
}
}
public function displayAdvert()
{
return $this->display(__file__, 'views/templates/admin/advert.tpl');
}
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_blocktopdropdownmenu_ITEMS', 'CAT3,CAT26') || !Configuration::updateGlobalValue('MOD_blocktopdropdownmenu_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 (!Configuration::deleteByName('MOD_blocktopdropdownmenu_ITEMS') || !Configuration::deleteByName('MOD_blocktopdropdownmenu_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('submitblocktopdropdownmenu')) {
$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_blocktopdropdownmenu_ITEMS', (string)implode(',', $items), false, (int)$shop_group_id, (int)$shop_id);
} else {
$updated = Configuration::updateValue('MOD_blocktopdropdownmenu_ITEMS', '', false, (int)$shop_group_id, (int)$shop_id);
}
}
$updated &= Configuration::updateValue('MOD_blocktopdropdownmenu_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('submitblocktopdropdownmenuLinks')) {
$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 = MenuTopDropdownLinks::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 = MenuTopDropdownLinks::remove($id_linksmenutop, (int)$shop_id);
Configuration::updateValue('MOD_blocktopdropdownmenu_ITEMS', str_replace(array('LNK'.$id_linksmenutop.',', 'LNK'.$id_linksmenutop), '', Configuration::get('MOD_blocktopdropdownmenu_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']);
}
MenuTopDropdownLinks::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, MenuTopDropdownLinks::gets((int)$id_lang, null, (int)$shop_id));
}
if (!count($links)) {
return $this->displayAdvert() . $this->_html . $this->checkforupdates(0,1);
}
$this->_html = $this->displayAdvert() . $this->_html . $this->renderList() . $this->checkforupdates(0,1);
return $this->_html;
}
public function inconsistency($ret = 1)
{
return true;
}
public function psversion()
{
$version = _PS_VERSION_;
$exp = $explode = explode(".", $version);
return $exp[1];
}
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 '