* @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 */ if (!defined('_PS_VERSION_')) exit; class BlockSpecials extends Module { private $_html = ''; private $_postErrors = array(); function __construct() { $this->name = 'blockspecials'; $this->tab = 'pricing_promotion'; $this->version = '0.9'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Specials block'); $this->description = $this->l('Adds a block displaying current product specials.'); } public function install() { if (!Configuration::get('BLOCKSPECIALS_NB_CACHES')) Configuration::updateValue('BLOCKSPECIALS_NB_CACHES', 20); $this->_clearCache('blockspecials.tpl'); return (parent::install() && $this->registerHook('rightColumn') && $this->registerHook('header') && $this->registerHook('addproduct') && $this->registerHook('updateproduct') && $this->registerHook('deleteproduct') ); } public function uninstall() { $this->_clearCache('blockspecials.tpl'); return parent::uninstall(); } public function getContent() { $output = '

'.$this->displayName.'

'; if (Tools::isSubmit('submitSpecials')) { Configuration::updateValue('PS_BLOCK_SPECIALS_DISPLAY', (int)Tools::getValue('always_display')); Configuration::updateValue('BLOCKSPECIALS_NB_CACHES', (int)Tools::getValue('BLOCKSPECIALS_NB_CACHES')); $output .= '
'.$this->l('Settings updated').'
'; } return $output.$this->displayForm(); } public function displayForm() { return '
'.$this->l('Settings').'

'.$this->l('Show the block even if no product is available.').'

'.$this->l('Specials are displayed randomly on the front end, but since it takes a lot of ressources, it is better to cache the results. Cache is reset everyday. 0 will disable the cache.').'

'; } public function hookRightColumn($params) { if (Configuration::get('PS_CATALOG_MODE')) return; // We need to create multiple caches because the products are sorted randomly $random = date('Ymd').'|'.round(rand(1, max(Configuration::get('BLOCKSPECIALS_NB_CACHES'), 1))); if (!Configuration::get('BLOCKSPECIALS_NB_CACHES') || !$this->isCached('blockspecials.tpl', $this->getCacheId('blockspecials|'.$random))) { if (!($special = Product::getRandomSpecial((int)$params['cookie']->id_lang)) && !Configuration::get('PS_BLOCK_SPECIALS_DISPLAY')) return; $this->smarty->assign(array( 'special' => $special, 'priceWithoutReduction_tax_excl' => Tools::ps_round($special['price_without_reduction'], 2), 'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')), )); } return $this->display(__FILE__, 'blockspecials.tpl', $this->getCacheId('blockspecials|'.$random)); } public function hookLeftColumn($params) { return $this->hookRightColumn($params); } public function hookHeader($params) { if (Configuration::get('PS_CATALOG_MODE')) return ; $this->context->controller->addCSS(($this->_path).'blockspecials.css', 'all'); } public function hookAddProduct($params) { $this->_clearCache('blockspecials.tpl'); } public function hookUpdateProduct($params) { $this->_clearCache('blockspecials.tpl'); } public function hookDeleteProduct($params) { $this->_clearCache('blockspecials.tpl'); } }