name = 'cyb_reassurance'; $this->tab = ''; $this->version = '1.0'; $this->author = 'Cyberscope Bastien'; $this->bootstrap = true; $this->ps_versions_compliancy = array( 'min' => '1.7', 'max' => _PS_VERSION_, ); // Name of the module $this->displayName = $this->l('Module Cyber Réassurance'); // Confirm if uninstall module $this->confirmUninstall = $this->l('Etes vous sûr de vouloir supprimer le module Cyber Réassurance ?'); // $this->_orderBy = 'position'; parent::__construct(); } /** * @return bool if install * @since 1.0 */ public function install() { if (! parent::install() || ! Configuration::updateValue('cyb_titre', '') || ! Configuration::updateValue('cyb_lien', '') || ! Configuration::updateValue('cyb_fichier', '') || ! $this->registerHook('displayHeaderReassurance') || ! $this->registerHook('displayFooterBefore') || ! $this->registerHook('header') || ! $this->createTables()) { return false; } return true; } /** * Creates tables * @since 1.0 */ protected function createTables() { return (bool)Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'cyb_reassurance ( id_cyb int(10) unsigned NOT NULL AUTO_INCREMENT, titre_cyb varchar(255) DEFAULT NULL, lien_cyb varchar(255) DEFAULT NULL, fichier_cyb varchar(255) DEFAULT NULL, PRIMARY KEY (id_cyb) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); } /** * @return bool if uninstall * @since 1.0 */ public function uninstall() { if (! parent::uninstall() || ! Configuration::deleteByName('cyb_titre') || ! Configuration::deleteByName('cyb_lien') || ! Configuration::deleteByName('cyb_fichier') || ! $this->deleteTables()) { return false; } return true; } /** * deletes tables * @since 1.0 */ protected function deleteTables() { return Db::getInstance()->execute(' DROP TABLE IF EXISTS `'._DB_PREFIX_.'cyb_reassurance`; '); } /** * Get content function * @since 1.0 */ public function getContent() { $this->_html .= $this->headerHTML(); /* Validate & process */ if (Tools::isSubmit('deletecyb_reassurance') || Tools::isSubmit('submitConfCybReassurance')) { // Submit form or delete $this->postProcess(); $this->_html .= $this->renderList(); } else if (Tools::isSubmit('addcyb_reassurance') || (Tools::isSubmit('updatecyb_reassurance') && Reassurances::reassuranceExists((int)Tools::getValue('id')))) { // Add or Edit $this->_html .= $this->renderForm(); } else { $this->_html .= $this->renderList(); } return $this->_html; } public function headerHTML() { if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name) { return; } $this->context->controller->addJqueryUI('ui.sortable'); // $this->context->controller->addJqueryUI(array( // 'ui.draggable', // 'ui.droppable' //)); } /** * @return string * @since 1.0 */ public function renderList() { // global $smarty; $reassurances = $this->getReassurances(); $fields_list = array( 'id' => array( 'title' => 'Id', 'align' => 'center', // Align Col (not header) 'width' => 50, // header search : width 'type' => 'int', 'class' => 'fixed-width-xs', // class css 'search' => false, // header search : display 'orderby' => true, // header search : order ), 'titre' => array( 'title' => 'Nom', 'type' => 'string', 'search' => false, ), 'lien' => array( 'title' => 'Lien', 'type' => 'string', 'search' => false, ), 'fichier' => array( 'title' => 'Image', 'type' => 'image', 'search' => false, ), // 'position' => array( // 'title' => 'Position', // 'filter_key' => 'a!position', // 'align' => 'center', // 'class' => 'fixed-width-sm', //// 'position'=>'position', // 'search' => false, // ), ); // Init toolbar $this->initToolbar(); $helper = new HelperList(); $helper->shopLinkType = ''; $helper->actions = array('edit', 'delete'); $helper->module = $this; $helper->toolbar_btn = $this->toolbar_btn; $helper->listTotal = count($reassurances); $helper->show_toolbar = true; $helper->identifier = 'id'; $helper->table = 'cyb_reassurance'; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; return $helper->generateList($reassurances, $fields_list); } /** * @since 1.0 */ public function postProcess() { $errors = array(); if (Tools::isSubmit('submitConfCybReassurance')) { if (Tools::getValue('id')) { $obj = new Reassurances((int)Tools::getValue('id')); if (! $obj->getId()) { $this->_html .= $this->displayError($this->getTranslator()->trans('Invalid slide ID', array(), 'Modules.Imageslider.Admin')); return false; } } else { $obj = new Reassurances(); } $obj->setTitre(Tools::getValue('titre')); $obj->setLien(Tools::getValue('lien')); if (isset($_FILES['fichier'], $_FILES['fichier']['name']) && $_FILES['fichier']['name']) { if ($error = ImageManager::validateUpload($_FILES['fichier'], 4000000)) { $errors[] = $error; } else { $ext = substr($_FILES['fichier']['name'], strrpos($_FILES['fichier']['name'], '.') + 1); $file_name = md5($_FILES['fichier']['name']) . '.' . $ext; if (! move_uploaded_file($_FILES['fichier']['tmp_name'], dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views/img' . DIRECTORY_SEPARATOR . $file_name) ) { return $this->displayError($this->trans('An error occurred while attempting to upload the file.', array(), 'Admin.Notifications.Error')); } else { $obj->setFichier($file_name); } } } /* Adds */ if (! Tools::getValue('id')) { $res = $obj->add(); if (! $res) { $errors[] = $this->displayError("Une erreur est survenue lors de l'ajout du bloc réassurance"); } else { $this->_html .= $this->displayConfirmation('Bloc réassurance ajouté'); } } else { $res = $obj->update(); if (! $res) { $errors[] = $this->displayError("Une erreur est survenue lors de la modification du bloc réassurance"); } else { $this->_html .= $this->displayConfirmation('Bloc réassurance mis à jour'); } } } else if (Tools::isSubmit('deletecyb_reassurance')) { $obj = new Reassurances((int)Tools::getValue('id')); $res = $obj->delete(); if (! $res) { $this->_html .= $this->displayError('Could not delete.'); } else { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=1&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } } if (count($errors)) { $this->_html .= $this->displayError(implode('
', $errors)); } elseif (Tools::isSubmit('submitConfCybReassurance') && Tools::getValue('id')) { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=4&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } elseif (Tools::isSubmit('submitConfCybReassurance')) { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=3&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } } /** * View form * @return string * @since 1.0 */ public function renderForm() { $helper = new HelperForm(); $helper->submit_action = 'submitConfCybReassurance'; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->module = $this; $fields_form = array( 'form' => array( 'legend' => array( 'title' => "Paramètre du bloc réassurance", 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'textarea', 'label' => 'Titre', 'name' => 'titre', 'required' => true, 'autoload_rte' => true, ), array( 'type' => 'text', 'label' => 'Lien', 'name' => 'lien', 'required' => false, ), array( 'type' => 'file', 'label' => 'Image', 'name' => 'fichier', 'required' => true, 'display_image' => true, ), ), 'submit' => array( 'title' => 'Valider' ) ), ); if (Tools::isSubmit('id') && Reassurances::reassuranceExists((int)Tools::getValue('id'))) { $obj = new Reassurances((int)Tools::getValue('id')); $fields_form['form']['input'][] = array( 'type' => 'hidden', 'name' => 'id' ); $fields_form['form']['images'] = $obj->getFichier(); } // Set var to helper $helper->tpl_vars = array( 'fields_value' => $this->getAddFieldsValues(), 'image_baseurl' => $this->_path.'views/img/', 'base_url' => $this->context->shop->getBaseURL(), ); return $helper->generateForm(array($fields_form)); } /** * Toolbar */ public function initToolbar() { $this->toolbar_btn['new'] = array( 'short' => 'lorem ipsum', 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&add'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'), 'desc' => 'Ajouter' ); } public function getAddFieldsValues() { $fields = array(); $obj = new Reassurances(); if (Tools::isSubmit('updatecyb_reassurance') && Reassurances::reassuranceExists((int)Tools::getValue('id'))) { $obj = new Reassurances((int)Tools::getValue('id')); $fields['id'] = (int)Tools::getValue('id', $obj->id); } $fields['titre'] = Tools::getValue('titre', $obj->getTitre()); $fields['lien'] = Tools::getValue('lien', $obj->getLien()); $fields['fichier'] = Tools::getValue('fichier', $obj->getFichier()); return $fields; } /** * @param $params * @since 1.0 */ public function hookHeader() { $this->context->controller->registerJavascript('js-cyb-reassurance', 'modules/' . $this->name. '/views/js/app.js', [ 'media' => 'all', 'position' => 'bottom', ] ); $this->context->controller->registerStylesheet('css-cyb-reassurance', 'modules/' . $this->name. '/views/css/styles.css', [ 'media' => 'all', 'priority' => 0, ] ); } /** * Hook display FooterBefore * @return string template * @since 1.0 */ public function hookdisplayFooterBefore() { global $smarty; // Get reassurance $a_reassurances = $this->getReassurances(); $smarty->assign('liste_reassurances', $a_reassurances); return $this->display(__FILE__, 'cyb_reassurance.tpl'); } public function hookdisplayHeaderReassurance() { global $smarty; // Get reassurance $a_reassurances = $this->getReassurances(); $smarty->assign('liste_reassurances', $a_reassurances); return $this->display(__FILE__, 'cyb_reassurance.tpl'); } public function getReassurances() { $reassurances = Db::getInstance(_PS_USE_SQL_SLAVE_)->executes(' SELECT id_cyb, lien_cyb, titre_cyb, fichier_cyb FROM '._DB_PREFIX_.'cyb_reassurance ORDER BY id_cyb' ); // + position $res = array(); foreach ($reassurances as $rea) { // var_dump($rea['fichier_cyb']); $res[] = array( 'id' => $rea['id_cyb'], 'fichier' => $rea['fichier_cyb'] ? $this->context->link->getMediaLink(_MODULE_DIR_.$this->name.'/views/img/'.$rea['fichier_cyb']) : '', 'titre' => $rea['titre_cyb'], 'lien' => $rea['lien_cyb'], // 'ordre' => $rea['position'], ); } return $res; } }