* @copyright 2007-2020 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
require dirname(__FILE__) . '/ps_menutoplinks.class.php';
class Ps_MainMenu extends Module implements WidgetInterface
{
/**
* @var string Name of the module running on PS 1.6.x. Used for data migration.
*/
const PS_16_EQUIVALENT_MODULE = 'blocktopmenu';
const MENU_JSON_CACHE_KEY = 'MOD_BLOCKTOPMENU_MENU_JSON';
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';
/*
* Array of files from the category image directory
*/
private $imageFiles;
public function __construct()
{
$this->name = 'ps_mainmenu';
$this->tab = 'front_office_features';
$this->version = '2.3.4';
$this->author = 'PrestaShop';
$this->imageFiles = null;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Main menu', [], 'Modules.Mainmenu.Admin');
$this->description = $this->trans('Make it easy for your visitors to find their way on your store, select the right link and turn it into a menu item.', [], 'Modules.Mainmenu.Admin');
$this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
}
public function install($delete_params = true)
{
if (!parent::install() ||
!$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('actionCategoryUpdate') ||
!$this->registerHook('actionShopDataDuplication') ||
!$this->registerHook('displayTop')) {
return false;
}
if ($delete_params) {
if ($this->uninstallPrestaShop16Module()) {
Configuration::deleteByName('MOD_BLOCKTOPMENU_SEARCH');
return true;
}
if (!$this->installDb() || !Configuration::updateGlobalValue('MOD_BLOCKTOPMENU_ITEMS', 'CAT3,CAT6,CAT9')) {
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_ . ' DEFAULT CHARSET=utf8mb4;') &&
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_ . ' DEFAULT CHARSET=utf8mb4;');
}
public function uninstall($delete_params = true)
{
if (!parent::uninstall()) {
return false;
}
$this->clearMenuCache();
if ($delete_params) {
if (!$this->uninstallDB() || !Configuration::deleteByName('MOD_BLOCKTOPMENU_ITEMS')) {
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;
}
/**
* Migrate data from 1.6 equivalent module (if applicable), then uninstall
*/
public function uninstallPrestaShop16Module()
{
if (!Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
return false;
}
$oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
if ($oldModule) {
// This closure calls the parent class to prevent data to be erased
// It allows the new module to be configured without migration
$parentUninstallClosure = function () {
return parent::uninstall();
};
$parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule));
$parentUninstallClosure();
}
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'), function ($value) {
return (bool) strlen($value);
}) : [];
$links_label = Tools::getValue('link') ? array_filter(Tools::getValue('link'), function ($value) {
return (bool) strlen($value);
}) : [];
$spacer = str_repeat(' ', $this->spacer_size);
$divLangName = 'link_label';
$update_cache = false;
if (Tools::isSubmit('submitBlocktopmenu')) {
$errors_update_shops = [];
$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);
}
}
if (!$updated) {
$shop = new Shop($shop_id);
$errors_update_shops[] = $shop->name;
}
}
if (!count($errors_update_shops)) {
$this->_html .= $this->displayConfirmation($this->trans('The settings have been updated.', [], 'Admin.Notifications.Success'));
} else {
$this->_html .= $this->displayError(sprintf($this->trans('Unable to update settings for the following shop(s): %s', [], 'Modules.Mainmenu.Admin'), implode(', ', $errors_update_shops)));
}
$update_cache = true;
} else {
if (Tools::isSubmit('submitBlocktopmenuLinks')) {
$errors_add_link = [];
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->trans('Please complete the "Link" field.', [], 'Modules.Mainmenu.Admin'));
} elseif (!$count_label) {
$this->_html .= $this->displayError($this->trans('Please add a label.', [], 'Modules.Mainmenu.Admin'));
} elseif (!isset($labels[$default_language])) {
$this->_html .= $this->displayError($this->trans('Please add a label for your default language.', [], 'Modules.Mainmenu.Admin'));
} else {
$shops = Shop::getContextListShopID();
foreach ($shops as $shop_id) {
$added = Ps_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->trans('The link has been added.', [], 'Modules.Mainmenu.Admin'));
} else {
$this->_html .= $this->displayError($this->trans('Unable to add link for the following shop(s): %s', [implode(', ', $errors_add_link)], 'Modules.Mainmenu.Admin'));
}
}
}
$update_cache = true;
} elseif (Tools::isSubmit('deletelinksmenutop')) {
$errors_delete_link = [];
$id_linksmenutop = Tools::getValue('id_linksmenutop', 0);
$shops = Shop::getContextListShopID();
foreach ($shops as $shop_id) {
$deleted = Ps_MenuTopLinks::remove($id_linksmenutop, (int) $shop_id);
Configuration::updateValue('MOD_BLOCKTOPMENU_ITEMS', str_replace(['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->trans('The link has been removed.', [], 'Modules.Mainmenu.Admin'));
} else {
$this->_html .= $this->displayError($this->trans('Unable to remove link for the following shop(s): %s', [implode(', ', $errors_delete_link)], 'Modules.Mainmenu.Admin'));
}
$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 = [];
$label = [];
$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']);
}
Ps_MenuTopLinks::update($link, $label, $new_window, (int) $id_shop, (int) $id_linksmenutop);
$this->_html .= $this->displayConfirmation($this->trans('The link has been edited.', [], 'Modules.Mainmenu.Admin'));
}
$update_cache = true;
}
}
if ($update_cache) {
$this->clearMenuCache();
}
$shops = Shop::getContextListShopID();
$links = [];
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, Ps_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->trans('You cannot manage top menu items from a "All Shops" or a "Group Shop" context, select directly the shop you want to edit', [], 'Modules.Mainmenu.Admin') .
'
';
}
protected function getCurrentShopInfoMsg()
{
$shop_info = null;
if (Shop::getContext() == Shop::CONTEXT_SHOP) {
$shop_info = $this->trans('The modifications will be applied to shop: %s', [$this->context->shop->name], 'Modules.Mainmenu.Admin');
} else {
if (Shop::getContext() == Shop::CONTEXT_GROUP) {
$shop_info = $this->trans('The modifications will be applied to this group: %s', [Shop::getContextShopGroup()->name], 'Modules.Mainmenu.Admin');
} else {
$shop_info = $this->trans('The modifications will be applied to all shops', [], 'Modules.Mainmenu.Admin');
}
}
return '