name = 'advconstructor'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = 'Antadis'; $this->need_instance = 0; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Générateur de bloc'); $this->description = $this->l('Générer n\'importe quel type de bloc'); } /***********************/ /******* Install *******/ /***********************/ public function install() { $this->installDb(); $this->createTab(); $this->createFolder(); if ( !parent::install() ) { $this->uninstallDb(); $this->deleteTab(); $this->uninstall(); return false; } Configuration::updateValue('ADVCONSTRUCTOR_HOOKS', ''); Configuration::updateValue('ADVCONSTRUCTOR_TYPE', 'alert,edito,footer,nav,seo'); Configuration::updateValue('ADVCONSTRUCTOR_ICON', 'loupe,message,rouage'); return true; } public function uninstall() { if ( !parent::uninstall() ) { return false; } Configuration::deleteByName('ADVCONSTRUCTOR_HOOKS'); Configuration::deleteByName('ADVCONSTRUCTOR_TYPE'); Configuration::deleteByName('ADVCONSTRUCTOR_ICON'); $this->uninstallDb(); $this->deleteTab(); return true; } public function installDb() { $sql = array(); $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor` ( `id_advconstructor` int(10) unsigned NOT NULL auto_increment, `skin` varchar(255) NOT NULL, `active` int(11), `icon` varchar(255) NOT NULL, `external` int(10) unsigned NOT NULL, `date_from` datetime NOT NULL, `date_to` datetime NOT NULL, `date_add` datetime NOT NULL DEFAULT "0000-00-00 00:00:00", `date_upd` datetime NOT NULL DEFAULT "0000-00-00 00:00:00", PRIMARY KEY (`id_advconstructor`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor_lang` ( `id_advconstructor` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `title` varchar(255) NOT NULL, `subtitle` varchar(255) NOT NULL, `link` varchar(255), `content` text, PRIMARY KEY (`id_advconstructor`,`id_lang`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor_shop` ( `id_advconstructor` int(10) unsigned NOT NULL, `id_shop` int(10) unsigned NOT NULL, `type` varchar(255) NOT NULL, PRIMARY KEY (`id_advconstructor`,`id_shop`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor_category` ( `id_advconstructor` int(10) unsigned NOT NULL, `id_category` int(10) unsigned NOT NULL, PRIMARY KEY (`id_advconstructor`,`id_category`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor_product` ( `id_advconstructor` int(10) unsigned NOT NULL, `id_product` int(10) unsigned NOT NULL, PRIMARY KEY (`id_advconstructor`,`id_product`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advconstructor_hook` ( `id_advconstructor_hook` int(10) unsigned NOT NULL auto_increment, `id_advconstructor` int(10) unsigned NOT NULL, `id_hook` int(10) unsigned NOT NULL, `id_category` int(10) unsigned, `position` int(10) unsigned NOT NULL, PRIMARY KEY (`id_advconstructor_hook`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8'; return $this->uninstallDb() && $this->updateDb($sql); } public function uninstallDb() { $sql = array(); $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor` ;'; $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor_lang` ;'; $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor_shop` ;'; $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor_category` ;'; $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor_product` ;'; $sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advconstructor_hook` ;'; return $this->updateDb($sql); } public function updateDb( $sqls ) { foreach ( $sqls as $sql ) { if ( !Db::getInstance()->execute($sql) ) { return false; } } return true; } public function createFolder() { $img_dir = _PS_IMG_DIR_ . 'constructor'; $folder = is_dir($img_dir); if (!$folder) { $folder = mkdir($img_dir, 0755, true); } return $folder; } public function createTab() { $languages = Language::getLanguages(); $new_tab = new Tab(); $new_tab->class_name = 'AdminAdvConstructor'; $new_tab->id_parent = Tab::getCurrentParentId(); $new_tab->module = $this->name; foreach ( $languages as $language ) { $new_tab->name[$language['id_lang']] = 'Générateur de bloc'; } $new_tab->add(); $id_tab_parent = $new_tab->id; $new_tab = new Tab(); $new_tab->class_name = 'AdminAdvConstructorPosition'; $new_tab->id_parent = $id_tab_parent; $new_tab->module = $this->name; foreach ( $languages as $language ) { $new_tab->name[$language['id_lang']] = 'Positions des blocs'; } $new_tab->add(); } public function deleteTab() { $idTab = Tab::getIdFromClassName('AdminAdvConstructor'); if ( $idTab ) { $tab = new Tab($idTab); $tab->delete(); } $idTab = Tab::getIdFromClassName('AdminAdvConstructorPosition'); if ( $idTab ) { $tab = new Tab($idTab); $tab->delete(); } } /************************/ /********* HOOK *********/ /************************/ public function __call( $name, $args = array() ) { $hookName = str_replace('hook', '', $name); $id_hook = (int)Hook::getIdByName($hookName); $hooks_id = explode( ",", Configuration::get('ADVCONSTRUCTOR_HOOKS') ); if (in_array($id_hook, $hooks_id)) { $html = ''; $id_category = false; if (Tools::getValue('controller') == 'category') { $id_category = (int) Tools::getValue('id_category'); } if ( $blocks = AdvConstructorBlock::getBlocksByHookID($id_hook, $id_category) ) { $i = 0; foreach ($blocks as $block) { $i++; $block->rang = $i; $this->smarty->assign( array( 'block' => $block, 'linkimg' =>'modules/advconstructor/img/', ) ); $html .= $this->display(__FILE__, trim($block->skin).'.tpl'); } } return $html; } return NULL; } /*********************************/ /********* CONFIGURATION *********/ /*********************************/ public function getContent() { $this->_postProcess(); $this->_html = ''; $this->_html .= $this->renderForm(); return $this->_html; } protected function _postProcess() { if ( Tools::isSubmit('submitConfiguration') ) { $this->unHook(); $hooks = Tools::getValue('hookBox'); foreach ($hooks as $hook) { $this->registerHook(Hook::getNameById($hook)); } $res = Configuration::updateValue('ADVCONSTRUCTOR_TYPE', Tools::getValue('ADVCONSTRUCTOR_TYPE')); $res &= Configuration::updateValue('ADVCONSTRUCTOR_ICON', Tools::getValue('ADVCONSTRUCTOR_ICON')); $res &= Configuration::updateValue('ADVCONSTRUCTOR_HOOKS', implode($hooks,',')); if (!$res) { $errors[] = $this->displayError($this->l('The configuration could not be updated.')); } else { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true).'&conf=6&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name); } } } public function renderForm() { $hooks = array(); $hooks_configuration = explode( ",", Configuration::get('ADVCONSTRUCTOR_HOOKS') ); foreach ( Hook::getHooks() as $hook ) { $hook['selected'] = (bool) in_array($hook['id_hook'], $hooks_configuration); $hooks[] = $hook; } $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->l('Configuration'), 'icon' => 'icon-plus-sign-alt' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Types de blocs'), 'name' => 'ADVCONSTRUCTOR_TYPE', 'value' => Configuration::get('ADVCONSTRUCTOR_TYPE') ), array( 'type' => 'text', 'label' => $this->l('Icones de blocs'), 'name' => 'ADVCONSTRUCTOR_ICON', 'value' => Configuration::get('ADVCONSTRUCTOR_ICON') ), array( 'type' => 'hook', 'label' => $this->l('Hooks à afficher'), 'name' => 'hookBox', 'values' => $hooks, 'col' => '9' ) ), 'submit' => array( 'title' => $this->l('Sauvegarder'), ) ), ); $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $this->fields_form = array(); $helper->module = $this; $helper->identifier = $this->identifier; $helper->submit_action = 'submitConfiguration'; $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->tpl_vars = array( 'fields_value' => $this->getConfigFieldsValues() ); $helper->override_folder = '/'; return $helper->generateForm(array($fields_form)); } public function getConfigFieldsValues() { $id_shop_group = Shop::getContextShopGroupID(); $id_shop = Shop::getContextShopID(); return array( 'ADVCONSTRUCTOR_TYPE' => Configuration::get('ADVCONSTRUCTOR_TYPE'), 'ADVCONSTRUCTOR_ICON' => Configuration::get('ADVCONSTRUCTOR_ICON') ); } protected function unHook() { $sql = 'SELECT `id_hook` FROM `'._DB_PREFIX_.'hook_module` WHERE `id_module` = '.(int)$this->id; $result = Db::getInstance()->executeS($sql); foreach ($result as $row) { $this->unregisterHook((int)$row['id_hook']); $this->unregisterExceptions((int)$row['id_hook']); } } }