// 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
2012-07-10 10:05:14 +00:00
parent 2f361c3bde
commit a37c3bfea2
3 changed files with 159 additions and 172 deletions
+35 -35
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)
{
// 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;
}
/**
+123 -136
View File
@@ -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 <img> and <a>
* @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 = '<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<fieldset>
<legend>'.$this->l('Advertising block configuration').'</legend>
<a href="'.$this->adv_link.'" target="_blank" title="'.$this->adv_title.'">';
if ($this->adv_img)
{
$output .= '<img src="'.$this->context->link->protocol_content.$this->adv_img.'"
alt="'.$this->adv_title.'" title="'.$this->adv_title.'"
style="height:163px;margin-left: 100px;width:163px"/>';
$output .= '<input class="button" type="submit" name="submitDeleteImgConf"
value="'.$this->l('Delete image').'" style=""/>';
}
else
$output .= '<div style="margin-left: 100px;width:163px;">'.$this->l('no image').'</div>';
$output .= '
</a>
<br/>
<br/>
<label for="adv_img">'.$this->l('Change image').'&nbsp;&nbsp;</label><input id="adv_img" type="file" name="adv_img" />
( '.$this->l('Image will be displayed as 155x163').' )
<br/>
<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.'" />
<br class="clear"/>
<br/>
<label for="adv_title">'.$this->l('Title').'&nbsp;&nbsp;</label><input id="adv_title" type="text" name="adv_title" value="'.$this->adv_title.'" />
<br class="clear"/>
<br/>
<input class="button" type="submit" name="submitAdvConf" value="'.$this->l('validate').'" style="margin-left: 200px;"/>
</fieldset>
</form>';
$output = '
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post" enctype="multipart/form-data">
<fieldset>
<legend>'.$this->l('Advertising block configuration').'</legend>';
if ($this->adv_img)
{
$output .= '
<a href="'.$this->adv_link.'" target="_blank" title="'.$this->adv_title.'">
<img src="'.$this->context->link->protocol_content.$this->adv_img.'" alt="'.$this->adv_title.'" title="'.$this->adv_title.'"
style="width:155px;height:163px;margin-left:100px"/>
</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
$output .= '<div style="margin-left: 100px;width:163px;">'.$this->l('no image').'</div>';
$output .= '<br/><br/>
<label for="adv_img">'.$this->l('Change image').'&nbsp;&nbsp;</label>
<div class="margin-form">
<input id="adv_img" type="file" name="adv_img" />
<p>'.$this->l('Image will be displayed as 155x163.').'</p>
</div>
<br class="clear"/>
<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"/>
<label for="adv_title">'.$this->l('Title').'</label>
<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"/>
</fieldset>
</form>';
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');
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@
<module>
<name>blockadvertising</name>
<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>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[advertising_marketing]]></tab>