Бортовой журнал Ктулху

Добавление нового хука к модулю Theme Configurator для Prestashop 1.6

Theme Configurator - это модуль Prestashop 1.6 который позволяет добавлять баннеры на страницы и редактировать опции темы. В некоторых случаях его функционала недостаточно и я напишу как добавить новый хук.

 

Я создаю новый хук в котором будут отображаться два баннера с возможностью редактирования через админку. Чтобы как можно меньше вмешиваться в код модуля, скопировать файл  /theme_name/themeconfigurator/themeconfigurator.php в /override/modules/themeconfigurator и убрать лишнее или создать там пустой.

Содержимое файла:

class ThemeConfiguratorOverride extends ThemeConfigurator
{
public function install()
 {
 $themes_colors = array(
 'theme1',
 'theme2',
 'theme3',
 'theme4',
 'theme5',
 'theme6',
 'theme7',
 'theme8',
 'theme9'
 );
 $themes_fonts = array(
 'font1' => 'Open Sans',
 'font2' => 'Josefin Slab',
 'font3' => 'Arvo',
 'font4' => 'Lato',
 'font5' => 'Volkorn',
 'font6' => 'Abril Fatface',
 'font7' => 'Ubuntu',
 'font8' => 'PT Sans',
 'font9' => 'Old Standard TT',
 'font10' => 'Droid Sans'
 );
if (!parent::install()
 || !$this->installDB()
 || !$this->installFixtures(Language::getLanguages(true)) ||
 !$this->registerHook('displayHeader') ||
 !$this->registerHook('displayTop') ||
 !$this->registerHook('displayDiscount') ||
 !$this->registerHook('displayLeftColumn') ||
 !$this->registerHook('displayRightColumn') ||
 !$this->registerHook('displayHome') ||
 !$this->registerHook('displayFooter') ||
 !$this->registerHook('displayBackOfficeHeader') ||
 !$this->registerHook('actionObjectLanguageAddAfter') ||
 !Configuration::updateValue('PS_TC_THEMES', serialize($themes_colors)) ||
 !Configuration::updateValue('PS_TC_FONTS', serialize($themes_fonts)) ||
 !Configuration::updateValue('PS_TC_THEME', '') ||
 !Configuration::updateValue('PS_TC_FONT', '') ||
 !Configuration::updateValue('PS_TC_ACTIVE', 1) ||
 !Configuration::updateValue('PS_SET_DISPLAY_SUBCATEGORIES', 1) ||
 !$this->createAjaxController()
 )
 return false;
return true;
 }
private function installDB()
 {
 return (
 Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'themeconfigurator`') &&
 Db::getInstance()->Execute('
 CREATE TABLE `'._DB_PREFIX_.'themeconfigurator` (
 `id_item` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `id_shop` int(10) unsigned NOT NULL,
 `id_lang` int(10) unsigned NOT NULL,
 `item_order` int(10) unsigned NOT NULL,
 `title` VARCHAR(100),
 `title_use` tinyint(1) unsigned NOT NULL DEFAULT \'0\',
 `hook` VARCHAR(100),
 `url` TEXT,
 `target` tinyint(1) unsigned NOT NULL DEFAULT \'0\',
 `image` VARCHAR(100),
 `image_w` VARCHAR(10),
 `image_h` VARCHAR(10),
 `html` TEXT,
 `active` tinyint(1) unsigned NOT NULL DEFAULT \'1\',
 PRIMARY KEY (`id_item`)
 ) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;')
 );
}
public function hookdisplayDiscount()
 {
 $this->context->smarty->assign(array(
 'htmlitems' => $this->getItemsFromHook('discount'),
 'hook' => 'discount'
 ));
return $this->display(__FILE__, 'hook.tpl');
 }
protected function renderThemeConfiguratorForm()
 {
 $id_shop = (int)$this->context->shop->id;
 $items = array();
 $hooks = array();
$this->context->smarty->assign('htmlcontent', array(
 'admin_tpl_path' => $this->admin_tpl_path,
 'hooks_tpl_path' => $this->hooks_tpl_path,
'info' => array(
 'module' => $this->name,
 'name' => $this->displayName,
 'version' => $this->version,
 'psVersion' => _PS_VERSION_,
 'context' => (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') == 0) ? 1 : ($this->context->shop->getTotalShops() != 1) ? $this->context->shop->getContext() : 1
 )
 ));
foreach ($this->languages as $language)
 {
 $hooks[$language['id_lang']] = array(
 'home',
 'top',
 'discount',
 'left',
 'right',
 'footer'
 );
foreach ($hooks[$language['id_lang']] as $hook)
 $items[$language['id_lang']][$hook] = Db::getInstance()->ExecuteS('
 SELECT * FROM `'._DB_PREFIX_.'themeconfigurator`
 WHERE id_shop = '.(int)$id_shop.'
 AND id_lang = '.(int)$language['id_lang'].'
 AND hook = \''.pSQL($hook).'\'
 ORDER BY item_order ASC'
 );
 }
$this->context->smarty->assign('htmlitems', array(
 'items' => $items,
 'theme_url' => $this->context->link->getAdminLink('AdminThemeConfigurator'),
 'lang' => array(
 'default' => $this->default_language,
 'all' => $this->languages,
 'lang_dir' => _THEME_LANG_DIR_,
 'user' => $this->context->language->id
 ),
 'postAction' => 'index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&tab_module=other&module_name='.$this->name.'',
 'id_shop' => $id_shop
 ));
$this->context->controller->addJqueryUI('ui.sortable');
 return $this->display(__FILE__, 'views/templates/admin/admin.tpl');
 }
}

Добавил в themeconfigurator.php:

  • !$this->registerHook('displayDiscount') || в метод install()
  • метод hookdisplayDiscount()
  • 'discount', в метод renderThemeConfiguratorForm()

Внес изменения в шаблоны админки:

items.tpl

<option value="discount"{if $hItem.hook == 'discount'} selected="selected"{/if}>discount</option>

new.tpl

<option value="discount">discount</option>

В месте вывода баннеров располагаем хук:

{hook h='displayDiscount'}