* @copyright 2022 ECSoft * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of ECSoft */ require_once dirname(__FILE__) . '/EcsGtmProTools.php'; class EcsGtmProInstaller { protected $module = null; protected $customHooksErrors = array(); protected function getTabs() { return array( array( 'class' => 'AdminEcsGtmProOrders', 'name' => 'GTM Orders', 'parent' => 'AdminParentOrders' ), array( 'class' => 'AdminEcsGtmProHooks', 'name' => 'GTM Pro', 'parent' => -1 ) ); } protected $hooks = array( 'header', 'displayAfterTitleTag', 'displayAfterBodyOpeningTag', 'displayBeforeBodyClosingTag', 'displayProductPriceBlock', 'actionObjectOrderDetailUpdateAfter', 'displayBackOfficeHeader', 'updateOrderStatus', 'displayOrderConfirmation', 'actionCustomerAccountAdd', 'actionAuthentication', 'actionOrderGridDefinitionModifier', 'actionOrderGridQueryBuilderModifier' ); public function getCustomHooks() { if ($this->module->is17() || $this->module->is8x()) { return array( 'displayAfterTitleTag' => array( 'regExp' => '/(<\/title>)/is', 'template' => _PS_THEME_DIR_ . 'templates/_partials/head.tpl' ), 'displayAfterBodyOpeningTag' => array( 'regExp' => '/()/is', 'template' => _PS_THEME_DIR_ . 'templates/layouts/layout-both-columns.tpl' ), 'displayBeforeBodyClosingTag' => array( 'regExp' => '/(<\/body>)/is', 'template' => _PS_THEME_DIR_ . 'templates/layouts/layout-both-columns.tpl' ) ); } else { return array( 'displayAfterTitleTag' => array( 'regExp' => '/(<\/title>)/is', 'template' => _PS_THEME_DIR_ . 'header.tpl' ), 'displayAfterBodyOpeningTag' => array( 'regExp' => '/()/is', 'template' => _PS_THEME_DIR_ . 'header.tpl' ), 'displayBeforeBodyClosingTag' => array( 'regExp' => '/(<\/body>)/is', 'template' => _PS_THEME_DIR_ . 'footer.tpl' ) ); } } public function __construct($module) { $this->setModule($module); } public function setModule($module) { $this->module = $module; } public function getModule() { return $this->module; } public function install() { Configuration::updateValue('ARGTMPRO_INSTALL_TS', time()); $res = $this->installHook() && $this->installTabs() && $this->installDB() && $this->installDefaults() && $this->installOverrides() && $this->module->clearGlobalCache(); $this->installCustomHooks(); return $res; } public function getCustomHooksErrors() { return $this->customHooksErrors; } public function addCustomHookError($hook, $error) { if (isset($this->customHooksErrors[$hook])) { $this->customHooksErrors[$hook][] = $error; } else { $this->customHooksErrors[$hook] = array( $error ); } } public function installCustomHooks() { $success = true; $this->customHooksErrors = array(); foreach ($this->getCustomHooks() as $hook => $params) { $fileName = $params['template']; $regExp = $params['regExp']; if (!EcsGtmProTools::isFileContainString($fileName, '{hook h="' . $hook . '"}')) { $fileContent = Tools::file_get_contents($fileName); if (!empty($fileContent)) { $matches = preg_split($regExp, $fileContent, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); if (count($matches) == 3) { $newContent = $matches[0] . $matches[1] . PHP_EOL . "{hook h=\"{$hook}\"}" . $matches[2]; if (is_writable($fileName)) { if (!file_put_contents($fileName, $newContent)) { $this->addCustomHookError($hook, $this->module->l(sprintf('Can\'t write to file "%s"', $fileName))); $success = false; } } else { $success = false; $this->addCustomHookError($hook, $this->module->l(sprintf('File "%s" is not writeable', $fileName))); } } else { $success = false; } } else { $success = false; } } $this->module->unregisterHook($hook); $this->module->registerHook($hook); } return $success; } public function uninstall() { return $this->uninstallDB() && $this->uninstallDefaults() && $this->unistallTabs(); } public function unistallTabs() { foreach ($this->getTabs() as $tab) { if ($id_tab = Tab::getIdFromClassName($tab['class'])) { $tab = new Tab($id_tab); $tab->delete(); } } return true; } public function uninstallDB() { return EcsGtmProOrder::uninstallTable(); } public function installTabs() { foreach ($this->getTabs() as $tab) { $this->addTab($tab['class'], $tab['name'], $tab['parent']); } return true; } public function addTab($className, $name, $parent) { $tab = new Tab(); $tab->active = 1; $tab->class_name = $className; $tab->name = array(); $tab->name[(int)(Configuration::get('PS_LANG_DEFAULT'))] = $this->module->l($name); $tab->module = $this->module->name; $tab->id_parent = ($parent == -1) ? -1 : (int) Tab::getIdFromClassName($parent); return $tab->add(); } public function installHook() { $res = true; foreach ($this->hooks as $hook) { $res = $res && $this->module->registerHook($hook); } return $res; } public function installDB() { return EcsGtmProOrder::installTable(); } public function uninstallDefaults() { foreach ($this->module->getForms() as $model) { $model->clearConfig(); } return true; } public function installDefaults() { foreach ($this->module->getForms() as $model) { $model->loadDefaults(); $model->saveToConfig(false); } return true; } public function installOverrides() { return true; } }