// Block advertising module rewritten for multistore

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@16304 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
dMetzger committed 2012-07-10 10:05:14 +00:00
1 parent 2f361c3bde
commit a37c3bfea2
3 files changed
+135 -148

No files matched your search

+31 -31
View File
@@ -146,6 +146,14 @@ class ConfigurationCore extends ObjectModel
*/ */
public static function get($key, $id_lang = null, $id_shop_group = null, $id_shop = null) 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; $id_lang = (int)$id_lang;
if ($id_shop === null) if ($id_shop === null)
$id_shop = Shop::getContextShopID(); $id_shop = Shop::getContextShopID();
@@ -155,10 +163,6 @@ class ConfigurationCore extends ObjectModel
if (!isset(self::$_CONF[$id_lang])) if (!isset(self::$_CONF[$id_lang]))
$id_lang = 0; $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)) if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop))
return self::$_CONF[$id_lang]['shop'][$id_shop][$key]; return self::$_CONF[$id_lang]['shop'][$id_shop][$key];
else if ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) 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)) if (!Validate::isConfigName($key))
return false; return false;
$sql = 'DELETE FROM `'._DB_PREFIX_.'configuration_lang` $result = Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'configuration_lang`
WHERE `id_configuration` IN ( WHERE `id_configuration` IN (
SELECT `id_configuration` SELECT `id_configuration`
FROM `'._DB_PREFIX_.'configuration` FROM `'._DB_PREFIX_.'configuration`
WHERE `name` = \''.pSQL($key).'\' WHERE `name` = "'.pSQL($key).'"
)'; )');
$result = Db::getInstance()->execute($sql);
$result2 = Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'configuration`
WHERE `name` = "'.pSQL($key).'"');
self::$_CONF = null;
$sql = 'DELETE FROM `'._DB_PREFIX_.'configuration`
WHERE `name` = \''.pSQL($key).'\'';
$result2 = Db::getInstance()->execute($sql);
return ($result && $result2); return ($result && $result2);
} }
@@ -401,29 +408,22 @@ class ConfigurationCore extends ObjectModel
public static function deleteFromContext($key) public static function deleteFromContext($key)
{ {
if (Shop::getContext() == Shop::CONTEXT_ALL) 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; return;
$id = Configuration::getIdByName($key, $id_shop_group, $id_shop); $id_shop = null;
$sql = 'DELETE FROM '._DB_PREFIX_.'configuration $id_shop_group = Shop::getContextShopGroupID();
WHERE id_configuration = '.$id; if (Shop::getContext() == Shop::CONTEXT_SHOP)
Db::getInstance()->execute($sql); $id_shop = Shop::getContextShopID();
$sql = 'DELETE FROM '._DB_PREFIX_.'configuration_lang $id = Configuration::getIdByName($key, $id_shop_group, $id_shop);
WHERE id_configuration = '.$id; Db::getInstance()->execute('
Db::getInstance()->execute($sql); 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;
} }
/** /**
+103 -116
View File
@@ -30,35 +30,23 @@ if (!defined('_PS_VERSION_'))
class BlockAdvertising extends Module class BlockAdvertising extends Module
{ {
/* Title associated to the image */
public $adv_title;
/* Link associated to the image */
public $adv_link; public $adv_link;
/** /* Name of the image without extension */
* adv_title contains string for title and alt attributes public $adv_imgname;
*
* @var mixed /* Image path with extension */
*/
public $adv_title;
/**
* adv_img contains url to the image to display.
*
* @var mixed
*/
public $adv_img; 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';
public function __construct() public function __construct()
{ {
$this->name = 'blockadvertising'; $this->name = 'blockadvertising';
$this->tab = 'advertising_marketing'; $this->tab = 'advertising_marketing';
$this->version = '0.4'; $this->version = '0.5';
$this->author = 'PrestaShop'; $this->author = 'PrestaShop';
$this->need_instance = 0; $this->need_instance = 0;
@@ -67,111 +55,109 @@ class BlockAdvertising extends Module
$this->displayName = $this->l('Block advertising'); $this->displayName = $this->l('Block advertising');
$this->description = $this->l('Adds a block to display an advertisement.'); $this->description = $this->l('Adds a block to display an advertisement.');
if (Shop::getContext() == Shop::CONTEXT_SHOP) $this->initialize();
{
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) /*
{ * Set the properties of the module, like the link to the image and the title (contextual to the current shop context)
$advert_link = Configuration::getGlobalValue('BLOCKADVERT_LINK'); */
$advert_title = Configuration::getGlobalValue('BLOCKADVERT_TITLE'); protected function initialize()
}
if (!file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT')))
{ {
$this->adv_imgname = 'advertising'; $this->adv_imgname = 'advertising';
if (!file_exists(_PS_MODULE_DIR_.$this->name.'/advertising.jpg')) if ((Shop::getContext() == Shop::CONTEXT_GROUP || Shop::getContext() == Shop::CONTEXT_SHOP)
$this->adv_imgname = ''; && file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'-g'.$this->context->shop->getContextShopGroupID().'.'.Configuration::get('BLOCKADVERT_IMG_EXT')))
else $this->adv_imgname .= '-g'.$this->context->shop->getContextShopGroupID();
Configuration::updateValue('BLOCKADVERT_IMG_EXT', 'jpg'); 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();
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_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_link = htmlentities($advert_link, ENT_QUOTES, 'UTF-8'); $this->adv_title = htmlentities(Configuration::get('BLOCKADVERT_TITLE'), ENT_QUOTES, 'UTF-8');
$this->adv_title = htmlentities($advert_title, ENT_QUOTES, 'UTF-8');
} }
public function install() public function install()
{ {
Configuration::updateValue('BLOCKADVERT_LINK', 'http://www.prestashop.com'); Configuration::updateGlobalValue('BLOCKADVERT_LINK', 'http://www.prestashop.com/');
if (!parent::install()) Configuration::updateGlobalValue('BLOCKADVERT_TITLE', 'PrestaShop');
return false; // Try to update with the extension of the image that exists in the module directory
if (!$this->registerHook('leftColumn')) foreach (scandir(_PS_MODULE_DIR_.$this->name) as $file)
return false; if (in_array($file, array('advertising.jpg', 'advertising.gif', 'advertising.png')))
return true; 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 * @return void
*/ */
private function _deleteCurrentImg() private function _deleteCurrentImg()
{ {
if ($this->adv_imgname == 'advertising') // Delete the image file
return; if ($this->adv_imgname != 'advertising' && file_exists(_PS_MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT')))
if (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')); 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 <img> and <a>
* @var string
* @return void
*/
public function postProcess() public function postProcess()
{ {
$errors = '';
if (Tools::isSubmit('submitDeleteImgConf')) if (Tools::isSubmit('submitDeleteImgConf'))
$this->_deleteCurrentImg(); $this->_deleteCurrentImg();
$errors = '';
if (Tools::isSubmit('submitAdvConf')) if (Tools::isSubmit('submitAdvConf'))
{ {
if (isset($_FILES['adv_img']) && isset($_FILES['adv_img']['tmp_name']) && !empty($_FILES['adv_img']['tmp_name'])) 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')))) if ($error = ImageManager::validateUpload($_FILES['adv_img'], Tools::convertBytes(ini_get('upload_max_filesize'))))
$errors .= $error; $errors .= $error;
elseif ($dot_pos = strrpos($_FILES['adv_img']['name'], '.'))
{
// as checkImage tell us it's a good image, we'll just copy the extension
$this->_deleteCurrentImg();
$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 else
$this->adv_imgname = $newname; {
Configuration::updateValue('BLOCKADVERT_IMG_EXT', substr($_FILES['adv_img']['name'], strrpos($_FILES['adv_img']['name'], '.') + 1));
Configuration::updateValue('BLOCKADVERT_IMG_EXT', $ext); // Set the image name with a name contextual to the shop context
$this->adv_img = Tools::getMediaServer($this->name)._MODULE_DIR_.$this->name.'/'.$this->adv_imgname.'.'.Configuration::get('BLOCKADVERT_IMG_EXT'); $this->adv_imgname = 'advertising';
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();
// 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')) if ($link = Tools::getValue('adv_link'))
{
Configuration::updateValue('BLOCKADVERT_LINK', $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')) if ($title = Tools::getValue('adv_title'))
{
Configuration::updateValue('BLOCKADVERT_TITLE', $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) if ($errors)
echo $this->displayError($errors); echo $this->displayError($errors);
@@ -180,51 +166,55 @@ class BlockAdvertising extends Module
/** /**
* getContent used to display admin module form * getContent used to display admin module form
* *
* @return void * @return string content
*/ */
public function getContent() public function getContent()
{ {
$this->postProcess(); $this->postProcess();
$output = '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data"> $output = '
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<fieldset> <fieldset>
<legend>'.$this->l('Advertising block configuration').'</legend> <legend>'.$this->l('Advertising block configuration').'</legend>';
<a href="'.$this->adv_link.'" target="_blank" title="'.$this->adv_title.'">';
if ($this->adv_img) if ($this->adv_img)
{ {
$output .= '<img src="'.$this->context->link->protocol_content.$this->adv_img.'" $output .= '
alt="'.$this->adv_title.'" title="'.$this->adv_title.'" <a href="'.$this->adv_link.'" target="_blank" title="'.$this->adv_title.'">
style="height:163px;margin-left: 100px;width:163px"/>'; <img src="'.$this->context->link->protocol_content.$this->adv_img.'" alt="'.$this->adv_title.'" title="'.$this->adv_title.'"
$output .= '<input class="button" type="submit" name="submitDeleteImgConf" style="width:155px;height:163px;margin-left:100px"/>
value="'.$this->l('Delete image').'" style=""/>'; </a>';
if ($this->adv_imgname == 'advertising')
$output .= $this->l('You cannot delete the default image (but you can change it below).');
else
$output .= '<input class="button" type="submit" name="submitDeleteImgConf" value="'.$this->l('Delete image').'" style=""/>';
} }
else else
$output .= '<div style="margin-left: 100px;width:163px;">'.$this->l('no image').'</div>'; $output .= '<div style="margin-left: 100px;width:163px;">'.$this->l('no image').'</div>';
$output .= ' $output .= '<br/><br/>
</a> <label for="adv_img">'.$this->l('Change image').'&nbsp;&nbsp;</label>
<br/> <div class="margin-form">
<br/> <input id="adv_img" type="file" name="adv_img" />
<label for="adv_img">'.$this->l('Change image').'&nbsp;&nbsp;</label><input id="adv_img" type="file" name="adv_img" /> <p>'.$this->l('Image will be displayed as 155x163.').'</p>
( '.$this->l('Image will be displayed as 155x163').' ) </div>
<br/>
<br class="clear"/> <br class="clear"/>
<label for="adv_link">'.$this->l('Image link').'&nbsp;&nbsp;</label><input id="adv_link" type="text" name="adv_link" value="'.$this->adv_link.'" /> <label for="adv_link">'.$this->l('Image link').'</label>
<div class="margin-form">
<input id="adv_link" type="text" name="adv_link" value="'.$this->adv_link.'" style="width:250px" />
</div>
<br class="clear"/> <br class="clear"/>
<br/> <label for="adv_title">'.$this->l('Title').'</label>
<label for="adv_title">'.$this->l('Title').'&nbsp;&nbsp;</label><input id="adv_title" type="text" name="adv_title" value="'.$this->adv_title.'" /> <div class="margin-form">
<input id="adv_title" type="text" name="adv_title" value="'.$this->adv_title.'" style="width:250px" />
</div>
<br class="clear"/>
<div class="margin-form">
<input class="button" type="submit" name="submitAdvConf" value="'.$this->l('Validate').'"/>
</div>
<br class="clear"/> <br class="clear"/>
<br/>
<input class="button" type="submit" name="submitAdvConf" value="'.$this->l('validate').'" style="margin-left: 200px;"/>
</fieldset> </fieldset>
</form>'; </form>';
return $output; return $output;
} }
/**
* Returns module content
*
* @param array $params Parameters
* @return string Content
*/
public function hookRightColumn($params) public function hookRightColumn($params)
{ {
$this->smarty->assign(array( $this->smarty->assign(array(
@@ -245,7 +235,4 @@ class BlockAdvertising extends Module
{ {
$this->context->controller->addCSS($this->_path.'blockadvertising.css', 'all'); $this->context->controller->addCSS($this->_path.'blockadvertising.css', 'all');
} }
} }
+1 -1
View File
@@ -2,7 +2,7 @@
<module> <module>
<name>blockadvertising</name> <name>blockadvertising</name>
<displayName><![CDATA[Block advertising]]></displayName> <displayName><![CDATA[Block advertising]]></displayName>
<version><![CDATA[0.4]]></version> <version><![CDATA[0.5]]></version>
<description><![CDATA[Adds a block to display an advertisement.]]></description> <description><![CDATA[Adds a block to display an advertisement.]]></description>
<author><![CDATA[PrestaShop]]></author> <author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[advertising_marketing]]></tab> <tab><![CDATA[advertising_marketing]]></tab>