Files
ws_customhtml/src/Module/Install.php
T

240 lines
8.1 KiB
PHP

<?php
namespace Websmid\CustomHtml\Module;
use Websmid\CustomHtml\Config\Config;
use Context;
use Db;
use Hook;
use Language;
use Tab;
trait Install
{
public function install()
{
if (!parent::install()) {
error_log('[ws_customhtml] parent::install() failed');
return false;
}
if (!$this->createTables()) {
error_log('[ws_customhtml] createTables() failed — DB error: ' . Db::getInstance()->getMsgError());
return false;
}
if (!$this->registerTabs(Config::TABS)) {
error_log('[ws_customhtml] registerTabs() failed');
return false;
}
if (!$this->registerHooks()) {
error_log('[ws_customhtml] registerHooks() failed');
return false;
}
if (!$this->registerGroupHooks()) {
error_log('[ws_customhtml] registerGroupHooks() failed');
return false;
}
return true;
}
private function createTables(): bool
{
$sqlToExec = [];
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_group (
`id_ws_customhtml_group` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`hook_name` VARCHAR(64) NOT NULL,
`custom_class` VARCHAR(128) NOT NULL DEFAULT \'\',
`default_block_class` VARCHAR(128) NOT NULL DEFAULT \'\',
`active` TINYINT(1) NOT NULL DEFAULT 1,
`ordering` INT(11) NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_group_lang (
`id_ws_customhtml_group` INT(11) NOT NULL,
`id_lang` INT(11) NOT NULL,
`title` VARCHAR(256) NOT NULL DEFAULT \'\',
PRIMARY KEY (`id_ws_customhtml_group`, `id_lang`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml (
`id_ws_customhtml` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_ws_customhtml_group` INT(11) NOT NULL,
`type` VARCHAR(32) NOT NULL DEFAULT \'html\',
`custom_class` VARCHAR(128) NOT NULL DEFAULT \'\',
`image` VARCHAR(255) NOT NULL DEFAULT \'\',
`alert_type` VARCHAR(16) NOT NULL DEFAULT \'info\',
`timeout` INT(11) NOT NULL DEFAULT 0,
`slider_mode` VARCHAR(16) NOT NULL DEFAULT \'always\',
`slides_per_view_mobile` DECIMAL(4,2) NOT NULL DEFAULT 1,
`slides_per_view_tablet` DECIMAL(4,2) NOT NULL DEFAULT 2,
`slides_per_view_desktop` DECIMAL(4,2) NOT NULL DEFAULT 3,
`autoplay_delay` INT(11) NOT NULL DEFAULT 0,
`active` TINYINT(1) NOT NULL DEFAULT 1,
`show_title` TINYINT(1) NOT NULL DEFAULT 0,
`ordering` INT(11) NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
KEY `id_ws_customhtml_group` (`id_ws_customhtml_group`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_lang (
`id_ws_customhtml` INT(11) NOT NULL,
`id_lang` INT(11) NOT NULL,
`title` VARCHAR(256) NOT NULL DEFAULT \'\',
`content` LONGTEXT,
`description` TEXT,
`extra_description` TEXT,
`link` VARCHAR(512) NOT NULL DEFAULT \'\',
`link_text` VARCHAR(128) NOT NULL DEFAULT \'\',
PRIMARY KEY (`id_ws_customhtml`, `id_lang`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_faq (
`id_ws_customhtml_faq` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_ws_customhtml` INT(11) NOT NULL,
`ordering` INT(11) NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
KEY `id_ws_customhtml` (`id_ws_customhtml`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_faq_lang (
`id_ws_customhtml_faq` INT(11) NOT NULL,
`id_lang` INT(11) NOT NULL,
`question` VARCHAR(512) NOT NULL DEFAULT \'\',
`answer` TEXT,
PRIMARY KEY (`id_ws_customhtml_faq`, `id_lang`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_slide (
`id_ws_customhtml_slide` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`id_ws_customhtml` INT(11) NOT NULL,
`image` VARCHAR(255) NOT NULL DEFAULT \'\',
`ordering` INT(11) NOT NULL DEFAULT 0,
`date_add` DATETIME NOT NULL,
`date_upd` DATETIME NOT NULL,
KEY `id_ws_customhtml` (`id_ws_customhtml`)
);
';
$sqlToExec[] = '
CREATE TABLE IF NOT EXISTS ' . _DB_PREFIX_ . 'ws_customhtml_slide_lang (
`id_ws_customhtml_slide` INT(11) NOT NULL,
`id_lang` INT(11) NOT NULL,
`title` VARCHAR(256) NOT NULL DEFAULT \'\',
`description` TEXT,
`extra_description` TEXT,
`link` VARCHAR(512) NOT NULL DEFAULT \'\',
`link_text` VARCHAR(128) NOT NULL DEFAULT \'\',
PRIMARY KEY (`id_ws_customhtml_slide`, `id_lang`)
);
';
foreach ($sqlToExec as $sqlBlock) {
Db::getInstance()->execute(trim($sqlBlock));
}
return true;
}
private function registerHooks(): bool
{
foreach (Config::HOOKS as $hook) {
if (!$this->registerHook($hook)) {
return false;
}
}
return true;
}
private function registerGroupHooks(): bool
{
$hookNames = Db::getInstance()->executeS(
'SELECT DISTINCT `hook_name` FROM `' . _DB_PREFIX_ . 'ws_customhtml_group`'
);
$idShop = (int) Context::getContext()->shop->id;
foreach ($hookNames as $row) {
$hookName = $row['hook_name'];
if (Hook::isModuleRegisteredOnHook($this, $hookName, $idShop)) {
continue;
}
if (!$this->registerHook($hookName)) {
return false;
}
}
return true;
}
private function registerTabs(array $moduleTabs): bool
{
$languages = Language::getLanguages(false);
foreach ($moduleTabs as $moduleTab) {
if (Tab::getIdFromClassName($moduleTab['class_name'])) {
continue;
}
$tab = new Tab();
$tab->class_name = $moduleTab['class_name'];
$tab->module = $moduleTab['module'];
$tab->active = 1;
$tab->route_name = $moduleTab['route_name'];
if (isset($moduleTab['icon'])) {
$tab->icon = $moduleTab['icon'];
}
foreach ($languages as $language) {
if (is_array($moduleTab['name'])) {
$tab->name[$language['id_lang']] = $moduleTab['name'][$language['iso_code']]
?? $moduleTab['name']['en'];
} else {
$tab->name[$language['id_lang']] = $moduleTab['name'];
}
}
if (isset($moduleTab['parent_class_name'])) {
$tab->id_parent = (int) Tab::getIdFromClassName($moduleTab['parent_class_name']);
} elseif (isset($moduleTab['id_parent'])) {
$tab->id_parent = (int) $moduleTab['id_parent'];
} else {
$tab->id_parent = -1;
}
$tab->add();
if (!$tab->id) {
return false;
}
}
return true;
}
}