diff --git a/classes/Configuration.php b/classes/Configuration.php index 013ee482f..a3b79824a 100644 --- a/classes/Configuration.php +++ b/classes/Configuration.php @@ -146,6 +146,14 @@ class ConfigurationCore extends ObjectModel */ public static function get($key, $id_lang = null, $id_shop_group = null, $id_shop = null) { + // If conf if not initialized, try manual query + if (!self::$_CONF) + { + Configuration::loadConfiguration(); + if (!self::$_CONF) + return Db::getInstance()->getValue('SELECT `value` FROM `'._DB_PREFIX_.'configuration` WHERE `name` = "'.pSQL($key).'"'); + } + $id_lang = (int)$id_lang; if ($id_shop === null) $id_shop = Shop::getContextShopID(); @@ -154,11 +162,7 @@ class ConfigurationCore extends ObjectModel if (!isset(self::$_CONF[$id_lang])) $id_lang = 0; - - // If conf if not initialized, try manual query - if (!self::$_CONF) - return Db::getInstance()->getValue('SELECT `value` FROM '._DB_PREFIX_.'configuration WHERE `name` = \''.pSQL($key).'\''); - + if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop)) return self::$_CONF[$id_lang]['shop'][$id_shop][$key]; else if ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) @@ -379,17 +383,20 @@ class ConfigurationCore extends ObjectModel if (!Validate::isConfigName($key)) return false; - $sql = 'DELETE FROM `'._DB_PREFIX_.'configuration_lang` - WHERE `id_configuration` IN ( - SELECT `id_configuration` - FROM `'._DB_PREFIX_.'configuration` - WHERE `name` = \''.pSQL($key).'\' - )'; - $result = Db::getInstance()->execute($sql); + $result = Db::getInstance()->execute(' + DELETE FROM `'._DB_PREFIX_.'configuration_lang` + WHERE `id_configuration` IN ( + SELECT `id_configuration` + FROM `'._DB_PREFIX_.'configuration` + WHERE `name` = "'.pSQL($key).'" + )'); - $sql = 'DELETE FROM `'._DB_PREFIX_.'configuration` - WHERE `name` = \''.pSQL($key).'\''; - $result2 = Db::getInstance()->execute($sql); + $result2 = Db::getInstance()->execute(' + DELETE FROM `'._DB_PREFIX_.'configuration` + WHERE `name` = "'.pSQL($key).'"'); + + self::$_CONF = null; + return ($result && $result2); } @@ -401,29 +408,22 @@ class ConfigurationCore extends ObjectModel public static function deleteFromContext($key) { if (Shop::getContext() == Shop::CONTEXT_ALL) - $id_shop = $id_shop_group = null; - else if (Shop::getContext() == Shop::CONTEXT_GROUP) - { - $id_shop_group = Shop::getContextShopGroupID(); - $id_shop = null; - } - else - { - $id_shop_group = Shop::getContextShopGroupID(); - $id_shop = Shop::getContextShopID(); - } - - if (!$id_shop && !$id_shop_group) return; + + $id_shop = null; + $id_shop_group = Shop::getContextShopGroupID(); + if (Shop::getContext() == Shop::CONTEXT_SHOP) + $id_shop = Shop::getContextShopID(); $id = Configuration::getIdByName($key, $id_shop_group, $id_shop); - $sql = 'DELETE FROM '._DB_PREFIX_.'configuration - WHERE id_configuration = '.$id; - Db::getInstance()->execute($sql); - - $sql = 'DELETE FROM '._DB_PREFIX_.'configuration_lang - WHERE id_configuration = '.$id; - Db::getInstance()->execute($sql); + Db::getInstance()->execute(' + DELETE FROM '._DB_PREFIX_.'configuration + WHERE id_configuration = '.(int)$id); + Db::getInstance()->execute(' + DELETE FROM '._DB_PREFIX_.'configuration_lang + WHERE id_configuration = '.(int)$id); + + self::$_CONF = null; } /** diff --git a/modules/blockadvertising/blockadvertising.php b/modules/blockadvertising/blockadvertising.php index c62afe8cb..180e9454c 100644 --- a/modules/blockadvertising/blockadvertising.php +++ b/modules/blockadvertising/blockadvertising.php @@ -30,35 +30,23 @@ if (!defined('_PS_VERSION_')) class BlockAdvertising extends Module { - public $adv_link; - - /** - * adv_title contains string for title and alt attributes - * - * @var mixed - */ + /* Title associated to the image */ public $adv_title; - /** - * adv_img contains url to the image to display. - * - * @var mixed - */ - public $adv_img; - public $use_global = true; - - /** - * adv_imgname is the filename of the image to display - * @TODO make it configurable for SEO, but need a function to clean filename - * @var string - */ - public $adv_imgname = 'advertising_custom'; + /* Link associated to the image */ + public $adv_link; + + /* Name of the image without extension */ + public $adv_imgname; + + /* Image path with extension */ + public $adv_img; public function __construct() { $this->name = 'blockadvertising'; $this->tab = 'advertising_marketing'; - $this->version = '0.4'; + $this->version = '0.5'; $this->author = 'PrestaShop'; $this->need_instance = 0; @@ -67,111 +55,109 @@ class BlockAdvertising extends Module $this->displayName = $this->l('Block advertising'); $this->description = $this->l('Adds a block to display an advertisement.'); - if (Shop::getContext() == Shop::CONTEXT_SHOP) - { - if (defined('_PS_ADMIN_DIR_') || file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'-'.(int)$this->context->shop->id.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) - { - $this->adv_imgname .= '-'.(int)$this->context->shop->id; - $this->use_global = false; - $advert_link = Configuration::get('BLOCKADVERT_LINK'); - $advert_title = Configuration::get('BLOCKADVERT_TITLE'); - } - else - $this->use_global = true; - } - - if ($this->use_global) - { - $advert_link = Configuration::getGlobalValue('BLOCKADVERT_LINK'); - $advert_title = Configuration::getGlobalValue('BLOCKADVERT_TITLE'); - } - - if (!file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) - { - $this->adv_imgname = 'advertising'; - if (!file_exists(_PS_MODULE_DIR_.$this->name.'/advertising.jpg')) - $this->adv_imgname = ''; - else - Configuration::updateValue('BLOCKADVERT_IMG_EXT', 'jpg'); - } - - if (!empty($this->adv_imgname)) - $this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'); - - $this->adv_link = htmlentities($advert_link, ENT_QUOTES, 'UTF-8'); - $this->adv_title = htmlentities($advert_title, ENT_QUOTES, 'UTF-8'); + $this->initialize(); } + /* + * Set the properties of the module, like the link to the image and the title (contextual to the current shop context) + */ + protected function initialize() + { + $this->adv_imgname = 'advertising'; + if ((Shop::getContext() == Shop::CONTEXT_GROUP || Shop::getContext() == Shop::CONTEXT_SHOP) + && file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'-g'.$this->context->shop->getContextShopGroupID().'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) + $this->adv_imgname .= '-g'.$this->context->shop->getContextShopGroupID(); + if (Shop::getContext() == Shop::CONTEXT_SHOP + && file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'-s'.$this->context->shop->getContextShopID().'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) + $this->adv_imgname .= '-s'.$this->context->shop->getContextShopID(); + $this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'); + $this->adv_link = htmlentities(Configuration::get('BLOCKADVERT_LINK'), ENT_QUOTES, 'UTF-8'); + $this->adv_title = htmlentities(Configuration::get('BLOCKADVERT_TITLE'), ENT_QUOTES, 'UTF-8'); + } + public function install() { - Configuration::updateValue('BLOCKADVERT_LINK', 'http://www.prestashop.com'); - if (!parent::install()) - return false; - if (!$this->registerHook('leftColumn')) - return false; - return true; + Configuration::updateGlobalValue('BLOCKADVERT_LINK', 'http://www.prestashop.com/'); + Configuration::updateGlobalValue('BLOCKADVERT_TITLE', 'PrestaShop'); + // Try to update with the extension of the image that exists in the module directory + foreach (scandir(_PS_MODULE_DIR_.$this->name) as $file) + if (in_array($file, array('advertising.jpg', 'advertising.gif', 'advertising.png'))) + Configuration::updateGlobalValue('BLOCKADVERT_IMG_EXT', substr($file, strrpos($file, '.') + 1)); + + return (parent::install() && $this->registerHook('leftColumn')); + } + + public function uninstall() + { + Configuration::deleteByName('BLOCKADVERT_LINK'); + Configuration::deleteByName('BLOCKADVERT_TITLE'); + Configuration::deleteByName('BLOCKADVERT_IMG_EXT'); + return (parent::uninstall()); } /** - * _deleteCurrentImg delete current image, (so this will use default image) + * delete the contextual image (it is not allowed to delete the default image) * * @return void */ private function _deleteCurrentImg() { - if ($this->adv_imgname == 'advertising') - return; - if (file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) + // Delete the image file + if ($this->adv_imgname != 'advertising' && file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) unlink(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT')); - $this->adv_imgname = $this->adv_imgname == 'advertising_custom'?'advertising':''; + + // Update the extension to the global value or the shop group value if available + Configuration::deleteFromContext('BLOCKADVERT_IMG_EXT'); + Configuration::updateValue('BLOCKADVERT_IMG_EXT', Configuration::get('BLOCKADVERT_IMG_EXT')); + + // Reset the properties of the module + $this->initialize(); } - /** - * postProcess update configuration - * @TODO adding alt and title attributes for and - * @var string - * @return void - */ + public function postProcess() { - $errors = ''; if (Tools::isSubmit('submitDeleteImgConf')) $this->_deleteCurrentImg(); + $errors = ''; if (Tools::isSubmit('submitAdvConf')) { if (isset($_FILES['adv_img']) && isset($_FILES['adv_img']['tmp_name']) && !empty($_FILES['adv_img']['tmp_name'])) { if ($error = ImageManager::validateUpload($_FILES['adv_img'], Tools::convertBytes(ini_get('upload_max_filesize')))) $errors .= $error; - elseif ($dot_pos = strrpos($_FILES['adv_img']['name'], '.')) + else { - // as checkImage tell us it's a good image, we'll just copy the extension + Configuration::updateValue('BLOCKADVERT_IMG_EXT', substr($_FILES['adv_img']['name'], strrpos($_FILES['adv_img']['name'], '.') + 1)); - $this->_deleteCurrentImg(); + // Set the image name with a name contextual to the shop context $this->adv_imgname = 'advertising'; - $ext = substr($_FILES['adv_img']['name'], $dot_pos + 1); - $newname = 'advertising_custom'.'-'.(int)$this->context->shop->id; - if (!move_uploaded_file($_FILES['adv_img']['tmp_name'], _PS_MODULE_DIR_.$this->name.'/'.$newname.'.'.$ext)) - $errors .= $this->l('Error move uploaded file'); - else - $this->adv_imgname = $newname; + if (Shop::getContext() == Shop::CONTEXT_GROUP) + $this->adv_imgname = 'advertising'.'-g'.(int)$this->context->shop->getContextShopGroupID(); + elseif (Shop::getContext() == Shop::CONTEXT_SHOP) + $this->adv_imgname = 'advertising'.'-s'.(int)$this->context->shop->getContextShopID(); - Configuration::updateValue('BLOCKADVERT_IMG_EXT', $ext); - $this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'); + // Copy the image in the module directory with its new name + if (!move_uploaded_file($_FILES['adv_img']['tmp_name'], _PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'))) + $errors .= $this->l('Error move uploaded file'); } } + + // If the link is not set, then delete it in order to use the next default value (either the global value or the group value) if ($link = Tools::getValue('adv_link')) - { Configuration::updateValue('BLOCKADVERT_LINK', $link); - $this->adv_link = htmlentities($link, ENT_QUOTES, 'UTF-8'); - } - + elseif (Shop::getContext() == Shop::CONTEXT_SHOP || Shop::getContext() == Shop::CONTEXT_GROUP) + Configuration::deleteFromContext('BLOCKADVERT_LINK'); + + // If the title is not set, then delete it in order to use the next default value (either the global value or the group value) if ($title = Tools::getValue('adv_title')) - { Configuration::updateValue('BLOCKADVERT_TITLE', $title); - $this->adv_title = htmlentities($title, ENT_QUOTES, 'UTF-8'); - } + elseif (Shop::getContext() == Shop::CONTEXT_SHOP || Shop::getContext() == Shop::CONTEXT_GROUP) + Configuration::deleteFromContext('BLOCKADVERT_TITLE'); + + // Reset the module properties + $this->initialize(); } if ($errors) echo $this->displayError($errors); @@ -180,57 +166,61 @@ class BlockAdvertising extends Module /** * getContent used to display admin module form * - * @return void + * @return string content */ public function getContent() { $this->postProcess(); - $output = '
-
- '.$this->l('Advertising block configuration').' - '; - if ($this->adv_img) - { - $output .= ''.$this->adv_title.''; - $output .= ''; - } - else - $output .= '
'.$this->l('no image').'
'; - $output .= ' -
-
-
- - ( '.$this->l('Image will be displayed as 155x163').' ) -
-
- -
-
- -
-
- -
-
'; + $output = ' +
+
+ '.$this->l('Advertising block configuration').''; + if ($this->adv_img) + { + $output .= ' + + '.$this->adv_title.' + '; + if ($this->adv_imgname == 'advertising') + $output .= $this->l('You cannot delete the default image (but you can change it below).'); + else + $output .= ''; + } + else + $output .= '
'.$this->l('no image').'
'; + $output .= '

+ +
+ +

'.$this->l('Image will be displayed as 155x163.').'

+
+
+ +
+ +
+
+ +
+ +
+
+
+ +
+
+
+
'; return $output; } - /** - * Returns module content - * - * @param array $params Parameters - * @return string Content - */ public function hookRightColumn($params) { $this->smarty->assign(array( - 'image' => $this->context->link->protocol_content.$this->adv_img, - 'adv_link' => $this->adv_link, - 'adv_title' => $this->adv_title, + 'image' => $this->context->link->protocol_content.$this->adv_img, + 'adv_link' => $this->adv_link, + 'adv_title' => $this->adv_title, )); return $this->display(__FILE__, 'blockadvertising.tpl'); @@ -245,7 +235,4 @@ class BlockAdvertising extends Module { $this->context->controller->addCSS($this->_path.'blockadvertising.css', 'all'); } - -} - - +} \ No newline at end of file diff --git a/modules/blockadvertising/config.xml b/modules/blockadvertising/config.xml index 42a5703ca..9830b4ea6 100755 --- a/modules/blockadvertising/config.xml +++ b/modules/blockadvertising/config.xml @@ -2,7 +2,7 @@ blockadvertising - +