* @copyright 2007-2013 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ /** * @since 1.5.0 * @version 1.2 (2012-03-14) */ if (!defined('_PS_VERSION_')) exit; include_once(_PS_MODULE_DIR_.'devspelivredor/livredorItem.php'); include_once(_PS_MODULE_DIR_.'devspelivredor/livredorTheme.php'); class devspeLivredor extends Module { private $_html = ''; public function __construct() { $this->name = 'devspelivredor'; $this->version = '1'; $this->need_instance = 0; $this->secure_key = Tools::encrypt($this->name); parent::__construct(); $this->displayName = $this->l('Gestion de la livre d\'or'); $this->description = $this->l('Administrer la livre d\'or'); } /** * @see Module::install() */ public function install() { /* Adds Module */ if (parent::install() && $this->registerHook('top') && $this->registerHook('actionShopDataDuplication')) { $res = $this->createTables(); return $res; } return false; } /** * @see Module::uninstall() */ public function uninstall() { /* Deletes Module */ if (parent::uninstall()) { /* Deletes tables */ $res = $this->deleteTables(); /* Unsets configuration */ // $res &= Configuration::deleteByName('devspelivredor_WIDTH'); // $res &= Configuration::deleteByName('devspelivredor_HEIGHT'); // $res &= Configuration::deleteByName('devspelivredor_SPEED'); // $res &= Configuration::deleteByName('devspelivredor_PAUSE'); // $res &= Configuration::deleteByName('devspelivredor_LOOP'); return $res; } return false; } /** * Creates tables */ protected function createTables() { /* livredors */ $res = (bool)Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devspelivredor` ( `id_livredor_item` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_shop` int(10) unsigned NOT NULL, PRIMARY KEY (`id_livredor_item`, `id_shop`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); /* livredors configuration */ $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devspelivredor_item` ( `id_livredor_item` int(10) unsigned NOT NULL AUTO_INCREMENT, `position` int(10) unsigned NOT NULL DEFAULT \'0\', `id_theme` int(11) unsigned NOT NULL DEFAULT \'0\', `age` int(11) unsigned NOT NULL DEFAULT \'0\', `note` int(11) unsigned NOT NULL DEFAULT \'0\', `active` tinyint(1) unsigned NOT NULL DEFAULT \'0\', `en_avant` tinyint(1) unsigned NOT NULL DEFAULT \'0\', PRIMARY KEY (`id_livredor_item`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); /* livredors lang configuration */ $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devspelivredor_item_lang` ( `id_livredor_item` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `prenom` varchar(255) NOT NULL, `nom` varchar(255) NOT NULL, `departement` varchar(255) NOT NULL, `ville` varchar(255) NOT NULL, `titre` varchar(255) NOT NULL, `phrase` text NOT NULL, PRIMARY KEY (`id_livredor_item`,`id_lang`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); /* livredors configuration */ $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devspelivredor_theme` ( `id_livredor_theme` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_livredor_parent` int(10) unsigned NOT NULL, `position` int(10) unsigned NOT NULL DEFAULT \'0\', `active` tinyint(1) unsigned NOT NULL DEFAULT \'0\', PRIMARY KEY (`id_livredor_theme`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); /* livredors lang configuration */ $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'devspelivredor_theme_lang` ( `id_livredor_theme` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `title` varchar(255) NOT NULL, PRIMARY KEY (`id_livredor_theme`,`id_lang`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8; '); return $res; } /** * deletes tables */ protected function deleteTables() { $livredors = $this->getItemslivredor(); foreach ($livredors as $livredor) { $to_del = new livredorItem($livredor['id_livredor']); $to_del->delete(); } return Db::getInstance()->execute(' DROP TABLE IF EXISTS `'._DB_PREFIX_.'devspelivredor`, `'._DB_PREFIX_.'devspelivredor_item`, `'._DB_PREFIX_.'devspelivredor_item_lang`, `'._DB_PREFIX_.'devspelivredor_theme`, `'._DB_PREFIX_.'devspelivredor_theme_lang`; '); } public function getContent() { $this->_html .= $this->headerHTML(); $this->_html .= '

'.$this->displayName.'.

'; /* Validate & process */ if (Tools::isSubmit('submitlivredor') || Tools::isSubmit('delete_id_livredor') || Tools::isSubmit('submittheme') || Tools::isSubmit('delete_id_theme') || Tools::isSubmit('changeStatus') || Tools::isSubmit('changeStatusEnavant')) { if ($this->_postValidation()) $this->_postProcess(); $this->_displayForm(); } elseif (Tools::isSubmit('addlivredor') || (Tools::isSubmit('id_livredor') && $this->livredorExists((int)Tools::getValue('id_livredor')))) $this->_displayAddItemForm(); elseif (Tools::isSubmit('addtheme') || (Tools::isSubmit('id_theme') && $this->livredorThemeExists((int)Tools::getValue('id_theme')))) $this->_displayAddThemeForm(); else $this->_displayForm(); return $this->_html; } private function _displayForm() { /* THEME livredor */ $livredors = $this->getItemsTheme(); $this->_html .= '

'; /* Begin fieldset livredors */ $this->_html .= '
'.$this->l('Gestion des thèmes').' '.$this->l('Ajouter un thème').' '; /* Display notice if there are no livredors yet */ if (!$livredors) $this->_html .= '

'.$this->l('Aucun thème créé').'

'; else /* Display livredors */ { $this->_html .= '
'; } // End fieldset $this->_html .= '
'; /* ITEMS livredor */ /* Gets livredors */ $livredors = $this->getItemslivredor(); $this->_html .= '

'; /* Begin fieldset livredors */ $this->_html .= '
'.$this->l('Gestion des messages du livre d\'or').' '.$this->l('Ajouter un message').' '; /* Display notice if there are no livredors yet */ if (!$livredors) $this->_html .= '

'.$this->l('Aucun message créé').'

'; else /* Display livredors */ { $this->_html .= '
'; } // End fieldset $this->_html .= '
'; } private function _displayAddItemForm() { /* Sets livredor : depends if edited or added */ $livredor = null; if (Tools::isSubmit('id_livredor') && $this->livredorExists((int)Tools::getValue('id_livredor'))) $livredor = new livredorItem((int)Tools::getValue('id_livredor')); /* Checks if directory is writable */ // if (!is_writable('.')) // $this->adminDisplayWarning(sprintf($this->l('Modules %s must be writable (CHMOD 755 / 777)'), $this->name)); /* Gets languages and sets which div requires translations */ $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT'); $languages = Language::getLanguages(false); $divLangName = 'ville¤titre¤nom¤prenom¤departement¤phrase'; $this->_html .= ''; /* Form */ $this->_html .= '
'; /* Fieldset Upload */ // $this->_html .= ' //
// 1 - '.$this->l('Téléchargez votre image').''; // /* Image */ // $this->_html .= '
'; // foreach ($languages as $language) // { // $this->_html .= '
'; // $this->_html .= ''; // /* Sets image as hidden in case it does not change */ // if ($livredor && $livredor->image[$language['id_lang']]) // $this->_html .= ''; // /* Display image */ // if ($livredor && $livredor->image[$language['id_lang']]) // $this->_html .= ''; // $this->_html .= '
'; // } // $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'image', true); // /* End Fieldset Upload */ // $this->_html .= '


'; /* Fieldset edit/add */ $this->_html .= '
'; if (Tools::isSubmit('addlivredor')) /* Configure legend */ $this->_html .= ' 1 - '.$this->l('Ajouter un message dans le livre d\'or').''; elseif (Tools::isSubmit('id_livredor')) /* Edit legend */ $this->_html .= '1 - '.$this->l('Modifier le message').''; $themes=$this->getItemsTheme(null); // print_r($themes); $les_themes=''; /* theme parent */ $this->_html .= '
'; $this->_html .= '
'.$les_themes.'
'; $this->_html .= '


'; /* titre */ $this->_html .= '
'; $this->_html .= '
note) && $livredor->note==1 ? 'checked="checked"' : '').'>1 note) && $livredor->note==2 ? 'checked="checked"' : '').'>2 note) && $livredor->note==3 ? 'checked="checked"' : '').'>3 note) && $livredor->note==4 ? 'checked="checked"' : '').'>4 note) && $livredor->note==5 ? 'checked="checked"' : '').'>5
'; $this->_html .= '


'; /* titre */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'titre', true); $this->_html .= '


'; /* phrase */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'phrase', true); $this->_html .= '

'; $this->_html .= '
'; /* Form content */ /* Form content */ $this->_html .= '

'; $this->_html .= ' 2 - '.$this->l('Informations personnelles').''; /* Sets id livredor as hidden */ if ($livredor && Tools::getValue('id_livredor')) $this->_html .= ''; /* Sets position as hidden */ $this->_html .= ''; /* Prenom */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'prenom', true); $this->_html .= '


'; /* nom */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'nom', true); $this->_html .= '


'; /* Age */ $this->_html .= '
'; $this->_html .= '
'; $this->_html .= '


'; /* Département */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'departement', true); $this->_html .= '


'; /* ville */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'ville', true); $this->_html .= '


'; $this->_html .= '
'; /* Form content */ /* Form content */ $this->_html .= '

'; $this->_html .= ' 3 - '.$this->l('Sauvegarder').''; /* Active */ $this->_html .= '
Yes active) && (int)$livredor->active == 0)) ? '' : 'checked="checked" ').' value="1" /> No active) && (int)$livredor->active == 0)) ? 'checked="checked" ' : '').' value="0" />
'; /* Active */ $this->_html .= '
Yes en_avant) && (int)$livredor->en_avant == 0)) ? '' : 'checked="checked" ').' value="1" /> No en_avant) && (int)$livredor->en_avant == 0)) ? 'checked="checked" ' : '').' value="0" />
'; /* Save */ $this->_html .= '

'.$this->l('Annuler').'

'; /* End of fieldset & form */ $this->_html .= '

*'.$this->l('Champs obligatoire').'

'; } private function _displayAddThemeForm() { /* Sets livredor : depends if edited or added */ $livredor = null; if (Tools::isSubmit('id_theme') && $this->livredorThemeExists((int)Tools::getValue('id_theme'))) $livredor = new livredorTheme((int)Tools::getValue('id_theme')); // print_r($livredor); /* Checks if directory is writable */ // if (!is_writable('.')) // $this->adminDisplayWarning(sprintf($this->l('Modules %s must be writable (CHMOD 755 / 777)'), $this->name)); /* Gets languages and sets which div requires translations */ $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT'); $languages = Language::getLanguages(false); $divLangName = 'title'; $this->_html .= ''; /* Form */ $this->_html .= '
'; /* Fieldset edit/add */ $this->_html .= '
'; if (Tools::getValue('addtheme')) /* Configure legend */ $this->_html .= ' '.$this->l('Ajouter un thème').''; elseif (Tools::getValue('id_theme')) /* Edit legend */ $this->_html .= ' '.$this->l('Modifier le thème').''; /* Sets id livredor as hidden */ $id_livredor=0; if ($livredor && Tools::getValue('id_theme')){ $id_livredor=$livredor->id; $this->_html .= ''; } /* Sets position as hidden */ $this->_html .= ''; /* Form content */ $themes=$this->getItemsTheme(null,true); // print_r($themes); $les_themes=''; /* theme parent */ $this->_html .= '

'; $this->_html .= '
'.$les_themes.'
'; $this->_html .= '


'; /* Form content */ /* Title */ $this->_html .= '
'; foreach ($languages as $language) { $this->_html .= '
'; } $this->_html .= $this->displayFlags($languages, $id_lang_default, $divLangName, 'title', true); $this->_html .= '


'; /* Active */ $this->_html .= '
Yes active) && (int)$livredor->active == 0)) ? '' : 'checked="checked" ').' value="1" /> No active) && (int)$livredor->active == 0)) ? 'checked="checked" ' : '').' value="0" />
'; /* Save */ $this->_html .= '

'.$this->l('Annuler').'

'; /* End of fieldset & form */ $this->_html .= '

*'.$this->l('Champs obligatoire').'

'; } private function _postValidation() { $errors = array(); if (Tools::isSubmit('changeStatus')) { if (!Validate::isInt(Tools::getValue('id_livredor'))) $errors[] = $this->l('Invalid livredor'); } /* Validation for livredor */ elseif (Tools::isSubmit('changeStatusEnavant')) { if (!Validate::isInt(Tools::getValue('id_livredor'))) $errors[] = $this->l('Invalid livredor'); } /* Validation for livredor */ elseif (Tools::isSubmit('submitlivredor')) { /* Checks state (active) */ if (!Validate::isInt(Tools::getValue('active_livredor')) || (Tools::getValue('active_livredor') != 0 && Tools::getValue('active_livredor') != 1)) $errors[] = $this->l('Invalid "active" state'); if (!Validate::isInt(Tools::getValue('en_avant_livredor')) || (Tools::getValue('en_avant_livredor') != 0 && Tools::getValue('en_avant_livredor') != 1)) $errors[] = $this->l('Invalid "en avant" state'); /* Checks position */ if (!Validate::isInt(Tools::getValue('position')) || (Tools::getValue('position') < 0)) $errors[] = $this->l('Invalid livredor position'); /* If edit : checks id_livredor */ if (Tools::isSubmit('id_livredor')) { if (!Validate::isInt(Tools::getValue('id_livredor')) && !$this->livredorExists(Tools::getValue('id_livredor'))) $errors[] = $this->l('Invalid id_livredor'); } /* Checks title/url/legend/phrase/image */ $languages = Language::getLanguages(false); foreach ($languages as $language) { if (Tools::strlen(Tools::getValue('phrase_'.$language['id_lang'])) > 255) $errors[] = $this->l('La phrase est trop longue'); // if (Tools::strlen(Tools::getValue('phrase_'.$language['id_lang'])) > 4000) // $errors[] = $this->l('The phrase is too long.'); // if (Tools::getValue('image_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_'.$language['id_lang']))) // $errors[] = $this->l('Invalid filename'); // if (Tools::getValue('image_old_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_old_'.$language['id_lang']))) // $errors[] = $this->l('Invalid filename'); } /* Checks title/url/legend/phrase for default lang */ $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT'); if (Tools::strlen(Tools::getValue('titre_'.$id_lang_default)) == 0) $errors[] = $this->l('Le titre est obligatoire'); if (Tools::strlen(Tools::getValue('phrase_'.$id_lang_default)) == 0) $errors[] = $this->l('La phrase obligatoire'); if (Tools::strlen(Tools::getValue('prenom_'.$id_lang_default)) == 0) $errors[] = $this->l('Le prénom obligatoire'); if (Tools::strlen(Tools::getValue('nom_'.$id_lang_default)) == 0) $errors[] = $this->l('Le nom obligatoire'); if (Tools::strlen(Tools::getValue('note')) == 0) $errors[] = $this->l('La note obligatoire'); // if (!Tools::isSubmit('has_picture') && (!isset($_FILES['image_'.$id_lang_default]) || empty($_FILES['image_'.$id_lang_default]['tmp_name']))) // $errors[] = $this->l('The image is not set.'); // if (Tools::getValue('image_old_'.$id_lang_default) && !Validate::isFileName(Tools::getValue('image_old_'.$id_lang_default))) // $errors[] = $this->l('The image is not set.'); } /* Validation for deletion */ elseif (Tools::isSubmit('delete_id_livredor') && (!Validate::isInt(Tools::getValue('delete_id_livredor')) || !$this->livredorExists((int)Tools::getValue('delete_id_livredor')))) $errors[] = $this->l('Invalid id_livredor'); elseif (Tools::isSubmit('submittheme')) { /* Checks state (active) */ if (!Validate::isInt(Tools::getValue('active_theme')) || (Tools::getValue('active_theme') != 0 && Tools::getValue('active_theme') != 1)) $errors[] = $this->l('Invalid theme state'); /* Checks position */ if (!Validate::isInt(Tools::getValue('position')) || (Tools::getValue('position') < 0)) $errors[] = $this->l('Invalid theme position'); /* If edit : checks id_theme */ if (Tools::isSubmit('id_theme')) { if (!Validate::isInt(Tools::getValue('id_theme')) && !$this->livredorExists(Tools::getValue('id_theme'))) $errors[] = $this->l('Invalid id_theme'); } /* Checks title/url/legend/phrase/image */ $languages = Language::getLanguages(false); foreach ($languages as $language) { // if (Tools::strlen(Tools::getValue('title_'.$language['id_lang'])) > 255) // $errors[] = $this->l('The title is too long.'); // if (Tools::strlen(Tools::getValue('phrase_'.$language['id_lang'])) > 4000) // $errors[] = $this->l('The phrase is too long.'); // if (Tools::getValue('image_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_'.$language['id_lang']))) // $errors[] = $this->l('Invalid filename'); // if (Tools::getValue('image_old_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_old_'.$language['id_lang']))) // $errors[] = $this->l('Invalid filename'); } /* Checks title/url/legend/phrase for default lang */ $id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT'); if (Tools::strlen(Tools::getValue('title_'.$id_lang_default)) == 0) $errors[] = $this->l('Nom du thème obligatoire'); } /* Validation for deletion */ elseif (Tools::isSubmit('delete_id_theme') && (!Validate::isInt(Tools::getValue('delete_id_theme')) || !$this->livredorExists((int)Tools::getValue('delete_id_theme')))) $errors[] = $this->l('Invalid id_theme'); /* Display errors if needed */ if (count($errors)) { $this->_html .= $this->displayError(implode('
', $errors)); return false; } /* Returns if validation is ok */ return true; } private function _postProcess() { $errors = array(); if (Tools::isSubmit('changeStatusEnavant') && Tools::isSubmit('id_livredor')) { $livredor = new livredorItem((int)Tools::getValue('id_livredor')); if ($livredor->en_avant == 0){ Db::getInstance()->execute('update '._DB_PREFIX_.'devspelivredor_item set en_avant=0'); $livredor->en_avant = 1; }else $livredor->en_avant = 0; $res = $livredor->update(); $this->clearCache(); $this->_html .= ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.'))); } /* Processes livredor */ elseif (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_livredor')) { $livredor = new livredorItem((int)Tools::getValue('id_livredor')); if ($livredor->active == 0) $livredor->active = 1; else $livredor->active = 0; $res = $livredor->update(); $this->clearCache(); $this->_html .= ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.'))); } /* Processes livredor */ elseif (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_theme')) { $livredor = new livredorTheme((int)Tools::getValue('id_theme')); if ($livredor->active == 0) $livredor->active = 1; else $livredor->active = 0; $res = $livredor->update(); $this->clearCache(); $this->_html .= ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.'))); } /* Processes livredor */ elseif (Tools::isSubmit('submitlivredor')) { /* Sets ID if needed */ if (Tools::getValue('id_livredor')) { $livredor = new livredorItem((int)Tools::getValue('id_livredor')); if (!Validate::isLoadedObject($livredor)) { $this->_html .= $this->displayError($this->l('Invalid id_livredor')); return; } } else $livredor = new livredorItem(); /* Sets position */ $livredor->position = (int)Tools::getValue('position'); $livredor->id_theme = (int)Tools::getValue('id_theme'); /* Sets active */ $livredor->active = (int)Tools::getValue('active_livredor'); $livredor->en_avant = (int)Tools::getValue('en_avant_livredor'); /* Sets each langue fields */ $languages = Language::getLanguages(false); foreach ($languages as $language) { $livredor->prenom[$language['id_lang']] = Tools::getValue('prenom_'.$language['id_lang']); $livredor->nom[$language['id_lang']] = Tools::getValue('nom_'.$language['id_lang']); $livredor->departement[$language['id_lang']] = Tools::getValue('departement_'.$language['id_lang']); $livredor->ville[$language['id_lang']] = Tools::getValue('ville_'.$language['id_lang']); $livredor->titre[$language['id_lang']] = Tools::getValue('titre_'.$language['id_lang']); $livredor->phrase[$language['id_lang']] = Tools::getValue('phrase_'.$language['id_lang']); $livredor->note = Tools::getValue('note'); $livredor->age = Tools::getValue('age'); // /* Uploads image and sets livredor */ // $type = strtolower(substr(strrchr($_FILES['image_'.$language['id_lang']]['name'], '.'), 1)); // $imagesize = array(); // $imagesize = @getimagesize($_FILES['image_'.$language['id_lang']]['tmp_name']); // if (isset($_FILES['image_'.$language['id_lang']]) && // isset($_FILES['image_'.$language['id_lang']]['tmp_name']) && // !empty($_FILES['image_'.$language['id_lang']]['tmp_name']) && // !empty($imagesize) && // in_array(strtolower(substr(strrchr($imagesize['mime'], '/'), 1)), array('jpg', 'gif', 'jpeg', 'png')) && // in_array($type, array('jpg', 'gif', 'jpeg', 'png'))) // { // $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS'); // $salt = sha1(microtime()); // if ($error = ImageManager::validateUpload($_FILES['image_'.$language['id_lang']])) // $errors[] = $error; // elseif (!$temp_name || !move_uploaded_file($_FILES['image_'.$language['id_lang']]['tmp_name'], $temp_name)) // return false; // elseif (!ImageManager::resize($temp_name, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name'].$salt).'.'.$type, null, null, $type)) // $errors[] = $this->displayError($this->l('An error occurred during the image upload process.')); // if (isset($temp_name)) // @unlink($temp_name); // $livredor->image[$language['id_lang']] = Tools::encrypt($_FILES['image_'.($language['id_lang'])]['name'].$salt).'.'.$type; // } // elseif (Tools::getValue('image_old_'.$language['id_lang']) != '') // $livredor->image[$language['id_lang']] = Tools::getValue('image_old_'.$language['id_lang']); } /* Processes if no errors */ if (!$errors) { /* Adds */ if($livredor->en_avant==1) Db::getInstance()->execute('update '._DB_PREFIX_.'devspelivredor_item set en_avant=0'); if (!Tools::getValue('id_livredor')) { if (!$livredor->add()) $errors[] = $this->displayError($this->l('The livredor could not be added.')); } /* Update */ elseif (!$livredor->update()) $errors[] = $this->displayError($this->l('The livredor could not be updated.')); $this->clearCache(); } } /* Deletes */ elseif (Tools::isSubmit('delete_id_livredor')) { $livredor = new livredorItem((int)Tools::getValue('delete_id_livredor')); $res = $livredor->delete(); $this->clearCache(); if (!$res) $this->_html .= $this->displayError('Could not delete'); else $this->_html .= $this->displayConfirmation($this->l('livredor deleted')); } elseif (Tools::isSubmit('submittheme')) { /* Sets ID if needed */ if (Tools::getValue('id_theme')) { $livredor = new livredorTheme((int)Tools::getValue('id_theme')); if (!Validate::isLoadedObject($livredor)) { $this->_html .= $this->displayError($this->l('Invalid id_theme')); return; } } else $livredor = new livredorTheme(); /* Sets position */ $livredor->id_livredor_parent = (int)Tools::getValue('id_livredor_parent'); $livredor->position = (int)Tools::getValue('position'); /* Sets active */ $livredor->active = (int)Tools::getValue('active_theme'); /* Sets each langue fields */ $languages = Language::getLanguages(false); foreach ($languages as $language) { $livredor->title[$language['id_lang']] = Tools::getValue('title_'.$language['id_lang']); } /* Processes if no errors */ if (!$errors) { /* Adds */ if (!Tools::getValue('id_theme')) { $livredor->add(); // $errors[] = $this->displayError($this->l('Le thème ne peut être créé')); } /* Update */ elseif (!$livredor->update()) $errors[] = $this->displayError($this->l('Le thème n\'a pas été modifié')); $this->clearCache(); } } /* Deletes */ elseif (Tools::isSubmit('delete_id_theme')) { $livredor = new livredorTheme((int)Tools::getValue('delete_id_theme')); $res = $livredor->delete(); $this->clearCache(); if (!$res) $this->_html .= $this->displayError('Could not delete'); else $this->_html .= $this->displayConfirmation($this->l('Thème supprimé')); } /* Display errors if needed */ if (count($errors)) $this->_html .= $this->displayError(implode('
', $errors)); elseif (Tools::isSubmit('submittheme') && Tools::getValue('id_theme')) $this->_html .= $this->displayConfirmation($this->l('Thème updated')); elseif (Tools::isSubmit('submittheme')) $this->_html .= $this->displayConfirmation($this->l('Thème added')); } private function _prepareHook() { if (!$this->isCached('devspelivredor.tpl', $this->getCacheId())) { // $livredorr = array( // 'width' => Configuration::get('devspelivredor_WIDTH'), // 'height' => Configuration::get('devspelivredor_HEIGHT'), // 'speed' => Configuration::get('devspelivredor_SPEED'), // 'pause' => Configuration::get('devspelivredor_PAUSE'), // 'loop' => Configuration::get('devspelivredor_LOOP'), // ); $livredors = $this->getItemslivredor(true); if (!$livredors) return false; $this->smarty->assign('devspelivredor_item', $livredors); // $this->smarty->assign('devspelivredor', $livredorr); } return true; } public function hookTop() { // if(!$this->_prepareHook()) // return; // Check if not a mobile theme /* if ($this->context->getMobileDevice() != false) return false; // $this->context->controller->addJS($this->_path.'js/jquery.bxlivredorr.min.js'); $this->context->controller->addCSS($this->_path.'devspelivredor.css'); $this->context->controller->addJS($this->_path.'js/devspelivredor.js'); return $this->display(__FILE__, 'devspelivredor.tpl', $this->getCacheId()); */ } public function clearCache() { $this->_clearCache('devspelivredor.tpl'); } public function hookActionShopDataDuplication($params) { Db::getInstance()->execute(' INSERT IGNORE INTO '._DB_PREFIX_.'devspelivredor (id_livredor_item, id_shop) SELECT id_livredor_item, '.(int)$params['new_id_shop'].' FROM '._DB_PREFIX_.'devspelivredor WHERE id_shop = '.(int)$params['old_id_shop']); $this->clearCache(); } public function headerHTML() { if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name) return; $this->context->controller->addJqueryUI('ui.sortable'); /* Style & js for fieldset 'livredors configuration' */ $html = ' '; return $html; } public function getNextPosition() { $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT MAX(hss.`position`) AS `next_position` FROM `'._DB_PREFIX_.'devspelivredor_item` hss, `'._DB_PREFIX_.'devspelivredor` hs WHERE hss.`id_livredor_item` = hs.`id_livredor_item` AND hs.`id_shop` = '.(int)$this->context->shop->id ); return (++$row['next_position']); } public function getNextPositionTheme() { $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' SELECT MAX(hss.`position`) AS `next_position` FROM `'._DB_PREFIX_.'devspelivredor_theme` hss' ); return (++$row['next_position']); } public function getItemslivredor($active = null) { $this->context = Context::getContext(); $id_shop = $this->context->shop->id; $id_lang = $this->context->language->id; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT hs.`id_livredor_item` as id_livredor, hss.`position`, hss.`id_theme`, hss.`active`, hss.`en_avant`, hss.`note`, hss.`age`, hssl.`prenom`, hssl.`nom`, hssl.`nom`, hssl.`departement`, hssl.`ville`, hssl.`titre`, hssl.`phrase` FROM '._DB_PREFIX_.'devspelivredor hs LEFT JOIN '._DB_PREFIX_.'devspelivredor_item hss ON (hs.id_livredor_item = hss.id_livredor_item) LEFT JOIN '._DB_PREFIX_.'devspelivredor_item_lang hssl ON (hss.id_livredor_item = hssl.id_livredor_item) WHERE (id_shop = '.(int)$id_shop.') AND hssl.id_lang = '.(int)$id_lang. ($active ? ' AND hss.`active` = 1' : ' ').' ORDER BY hss.position'); } public function getItemsTheme($active = null,$zero=null) { $this->context = Context::getContext(); $id_shop = $this->context->shop->id; $id_lang = $this->context->language->id; return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT hss.`id_livredor_theme` as id_theme, hss.`position`, hss.`active`, hss.`id_livredor_parent`, hssl.`title` FROM '._DB_PREFIX_.'devspelivredor_theme hss LEFT JOIN '._DB_PREFIX_.'devspelivredor_theme_lang hssl ON (hss.id_livredor_theme = hssl.id_livredor_theme) WHERE hssl.id_lang = '.(int)$id_lang. ($active ? ' AND hss.`active` = 1' : ' '). ($zero ? ' AND hss.`id_livredor_parent` = 0' : ' ').' ORDER BY hss.position'); } public function displayStatusEnavant($id_livredor, $en_avant) { $title = ((int)$en_avant == 0 ? $this->l('Mettre ce message en avant') : $this->l('Ce message est mis en avant')); $img = ((int)$en_avant == 0 ? 'en_avant_off.jpg' : 'en_avant_on.jpg'); $html = ''.$title.''; return $html; } public function displayStatus($id_livredor, $active) { $title = ((int)$active == 0 ? $this->l('Disabled') : $this->l('Enabled')); $img = ((int)$active == 0 ? 'disabled.gif' : 'enabled.gif'); $html = ''; return $html; } public function livredorExists($id_livredor) { $req = 'SELECT hs.`id_livredor_item` as id_livredor FROM `'._DB_PREFIX_.'devspelivredor` hs WHERE hs.`id_livredor_item` = '.(int)$id_livredor; $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req); return ($row); } public function livredorThemeExists($id_theme) { $req = 'SELECT hs.`id_livredor_theme` as id_theme FROM `'._DB_PREFIX_.'devspelivredor_theme` hs WHERE hs.`id_livredor_theme` = '.(int)$id_theme; $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req); return ($row); } }