* @copyright 2007-2012 PrestaShop SA * @version Release: $Revision: 6844 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class Blockreinsurance extends Module { public function __construct() { $this->name = 'blockreinsurance'; if (version_compare(_PS_VERSION_, '1.4.0.0') >= 0) $this->tab = 'front_office_features'; else $this->tab = 'Blocks'; $this->version = '1.0'; parent::__construct(); $this->displayName = $this->l('Bloc reinsurance'); $this->description = $this->l('Add a block to display more infos to reassure your customers'); } public function install() { $return = (parent::install() && $this->installDB() && Configuration::updateValue('blockreinsurance_nbblocks', 5) && $this->registerHook('footer')); $return &= $this->installFixtures(); return $return; } public function installDB() { return Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'reinsurance` ( `id_contactinfos` INT UNSIGNED NOT NULL AUTO_INCREMENT, `id_shop` int(10) unsigned NOT NULL , `filename` VARCHAR(100) NOT NULL, `text` VARCHAR(300) NOT NULL, PRIMARY KEY (`id_contactinfos`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'); } public function uninstall() { // Delete configuration return (Configuration::deleteByName('blockreinsurance_nbblocks') && $this->uninstallDB() && parent::uninstall()); } public function uninstallDB() { return Db::getInstance()->execute(' DROP TABLE IF EXISTS `'._DB_PREFIX_.'reinsurance`'); } public function addToDB() { if (isset($_POST['nbblocks'])) { for ($i = 1; $i <= (int)$_POST['nbblocks']; $i++) { $filename = explode('.', $_FILES['info'.$i.'_file']['name']); if (isset($_FILES['info'.$i.'_file']) && isset($_FILES['info'.$i.'_file']['tmp_name']) && !empty($_FILES['info'.$i.'_file']['tmp_name'])) { if ($error = ImageManager::validateUpload($_FILES['info'.$i.'_file'])) return false; elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['info'.$i.'_file']['tmp_name'], $tmpName)) return false; elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/'.$filename[0].'.jpg')) return false; unlink($tmpName); } Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'reinsurance` (`filename`,`text`) VALUES ("'.((isset($filename[0]) && $filename[0] != '') ? pSQL($filename[0]) : ''). '", "'.((isset($_POST['info'.$i.'_text']) && $_POST['info'.$i.'_text'] != '') ? pSQL($_POST['info'.$i.'_text']) : '').'")'); } return true; } else return false; } public function removeFromDB() { $dir = opendir(dirname(__FILE__).'/img'); while (false !== ($file = readdir($dir))) { $path = dirname(__FILE__).'/img/'.$file; if ($file != '..' && $file != '.' && !is_dir($file)) unlink($path); } closedir($dir); return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'reinsurance`'); } public function getAllFromDB() { return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'reinsurance`'); } public function getContent() { // If we try to update the settings $output = ''; if (isset($_POST['submitModule'])) { Configuration::updateValue('blockreinsurance_nbblocks', ((isset($_POST['nbblocks']) && $_POST['nbblocks'] != '') ? (int)$_POST['nbblocks'] : '')); if ($this->removeFromDB() && $this->addToDB()) $output = '
'.$this->l('Configuration updated').'
'; else $output = '
'.$this->l('An error occurred during the save').'
'; } $nb_blocks = Configuration::get('blockreinsurance_nbblocks'); $infos = $this->getAllFromDB(); $content = '

'.$this->displayName.'

'.$output.'
 
'; // Show by default 5 blocks maximum for ($i = 1; $i <= 5; $i++) { $content .= '

'.$this->l('Block number').' '.$i.'

'. ((!empty($infos[$i - 1]) && $infos[$i - 1]['filename'] != '') ? '' : ''). '
 

'; } $content .= '
 

'; return $content; } public function hookFooter($params) { global $smarty; $this->context->controller->addCSS($this->_path.'style.css', 'all'); $infos = $this->getAllFromDB(); $smarty->assign(array( 'nbblocks' => Configuration::get('blockreinsurance_nbblocks'), 'infos' => $infos )); return $this->display(__FILE__, 'blockreinsurance.tpl'); } public function installFixtures() { $nb_blocks = Configuration::get('blockreinsurance_nbblocks'); $tab_texts = array( $this->l('Money back'), $this->l('Exchange in-store'), $this->l('Payment for shipping'), $this->l('Free Shipping'), $this->l('100% secured payment'), ); $success = true; for ($a = 1; $a <= $nb_blocks; $a++) { $success &= Db::getInstance()->execute(' INSERT INTO `'._DB_PREFIX_.'reinsurance` (`filename`, `text`, `id_shop`) VALUES ("reassurance'.$a.'", "'.$tab_texts[($a-1)].'", '.(int)$this->context->shop->id.')'); } return $success; } }