name = 'cyb_head'; $this->tab = ''; $this->version = '1.0'; $this->bootstrap = true; $this->ps_versions_compliancy = array( 'min' => '1.7', 'max' => _PS_VERSION_, ); // Name of the module $this->displayName = $this->l('Module Custom Head'); // Confirm if uninstall module $this->confirmUninstall = $this->l('Etes vous sûr de vouloir supprimer le module Custom Head ?'); parent::__construct(); } /** * @return bool if install * @since 1.0 */ public function install() { if (! parent::install() || ! Configuration::updateValue('cyb_title', '') || ! Configuration::updateValue('cyb_color_title', '') || ! Configuration::updateValue('cyb_file', '') || ! $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_slider ( id_cyb_slides int(10) unsigned NOT NULL AUTO_INCREMENT, file_cyb_slide varchar(255) DEFAULT NULL, color_cyb_slides varchar(255) DEFAULT NULL, title_cyb_slide varchar(255) DEFAULT NULL, PRIMARY KEY (id_cyb_slides) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); } /** * @return bool if uninstall * @since 1.0 */ public function uninstall() { if (! parent::uninstall() || ! Configuration::deleteByName('cyb_title') || ! Configuration::deleteByName('cyb_color_title') || ! Configuration::deleteByName('cyb_file') || ! $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_slider`; '); } /** * Get content function * @since 1.0 */ public function getContent() { $this->_html .= $this->headerHTML(); /* Validate & process */ if (Tools::isSubmit('deletecyb_head') || Tools::isSubmit('submitConfCustomHead')) { // Submit form or delete $this->postProcess(); $this->_html .= $this->renderList(); } else if (Tools::isSubmit('addcyb_head') || (Tools::isSubmit('updatecyb_head') && Pictures::pictureExists((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'); } /** * @return string * @since 1.0 */ public function renderList() { // global $smarty; $pictures = $this->getPictures(); $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 ), 'title' => array( 'title' => 'Nom', 'type' => 'string', 'search' => false, ), 'color' => array( 'title' => 'Couleur', 'type' => 'string', 'search' => false, ), 'url' => array( 'title' => 'Image', 'type' => 'image', '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($pictures); $helper->show_toolbar = true; $helper->identifier = 'id'; $helper->table = 'cyb_head'; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; return $helper->generateList($pictures, $fields_list); // $smarty->assign( // array( // 'link' => $this->context->link, // 'pictures' => $pictures, // ) // ); // // return $this->display(__FILE__, 'list.tpl'); } /** * @since 1.0 */ public function postProcess() { $errors = array(); if (Tools::isSubmit('submitConfCustomHead')) { if (Tools::getValue('id')) { $obj = new Pictures((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 Pictures(); } $obj->setTitle(Tools::getValue('title')); $obj->setColor(Tools::getValue('color_title')); if (isset($_FILES['file'], $_FILES['file']['name']) && $_FILES['file']['name']) { if ($error = ImageManager::validateUpload($_FILES['file'], 4000000)) { $errors[] = $error; } else { $ext = substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.') + 1); $file_name = md5($_FILES['file']['name']) . '.' . $ext; if (! move_uploaded_file($_FILES['file']['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->setUrl($file_name); } } } /* Adds */ if (! Tools::getValue('id')) { $res = $obj->add(); if (! $res) { $errors[] = $this->displayError("Une erreur est survenue lors de l'ajout de l'image"); } else { $this->_html .= $this->displayConfirmation('Image ajoutée'); } } else { $res = $obj->update(); if (! $res) { $errors[] = $this->displayError("Une erreur est survenue lors de la modification de l'image"); } else { $this->_html .= $this->displayConfirmation('Image mise à jour'); } } } else if (Tools::isSubmit('deletecyb_head')) { $obj = new Pictures((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('submitConfCustomHead') && 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('submitConfCustomHead')) { 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 = 'submitConfCustomHead'; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->module = $this; $fields_form = array( 'form' => array( 'legend' => array( 'title' => "Paramètre du visuel", 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'text', 'label' => 'Titre', 'name' => 'title', 'required' => true, ), array( 'type' => 'file', 'label' => 'Image', 'name' => 'file', 'required' => true, 'display_image' => true, ), array( 'type' => 'color', 'label' => 'Couleur du titre', 'name' => 'color_title', ), ), 'submit' => array( 'title' => 'Valider' ) ), ); if (Tools::isSubmit('id') && Pictures::pictureExists((int)Tools::getValue('id'))) { $obj = new Pictures((int)Tools::getValue('id')); $fields_form['form']['input'][] = array( 'type' => 'hidden', 'name' => 'id' ); $fields_form['form']['images'] = $obj->getUrl(); } // 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 Pictures(); if (Tools::isSubmit('updatecyb_head') && Pictures::pictureExists((int)Tools::getValue('id'))) { $obj = new Pictures((int)Tools::getValue('id')); $fields['id'] = (int)Tools::getValue('id', $obj->id); } $fields['title'] = Tools::getValue('title', $obj->getTitle()); $fields['color_title'] = Tools::getValue('color_title', $obj->getColor()); $fields['file'] = Tools::getValue('file', $obj->getUrl()); return $fields; } /** * @param $params * @since 1.0 */ public function hookHeader() { $this->context->controller->registerJavascript('js-custom-head', 'modules/' . $this->name. '/views/js/app.js', [ 'media' => 'all', 'position' => 'bottom', ] ); $this->context->controller->registerStylesheet('css-custom-head', 'modules/' . $this->name. '/views/css/styles.css', [ 'media' => 'all', 'priority' => 0, ] ); } /** * Hook display displayCustomHeader * @return string template * @since 1.0 */ public function hookdisplayFooterBefore() { global $smarty; // Get picture $a_pictures = $this->getPictures(); // $index = array_rand($a_pictures); // if (isset($a_pictures[$index])) { $smarty->assign('liste_picture', $a_pictures); // } // die('erreur'); return $this->display(__FILE__, 'display_custom_head.tpl'); } public function getPictures() { $pictures = Db::getInstance(_PS_USE_SQL_SLAVE_)->executes(' SELECT id_cyb_slides, color_cyb_slides, title_cyb_slide, file_cyb_slide FROM '._DB_PREFIX_.'cyb_slider ORDER BY id_cyb_slides' ); $res = array(); foreach ($pictures as $pict) { $res[] = array( 'id' => $pict['id_cyb_slides'], 'url' => $this->context->link->getMediaLink(_MODULE_DIR_.'cyb_head/views/img/'.$pict['file_cyb_slide']), 'title' => $pict['title_cyb_slide'], 'color' => $pict['color_cyb_slides'], ); } return $res; } }