* @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 BlockNewProducts extends Module { public function __construct() { $this->name = 'blocknewproducts'; $this->tab = 'front_office_features'; $this->version = '1.4'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('New products block'); $this->description = $this->l('Displays a block featuring your store\'s newest products.'); } public function install() { if (!parent::install() || !$this->registerHook('rightColumn') || !$this->registerHook('header') || !$this->registerHook('addproduct') || !$this->registerHook('updateproduct') || !$this->registerHook('deleteproduct') || !Configuration::updateValue('NEW_PRODUCTS_NBR', 5) ) return false; $this->_clearCache('blocknewproducts.tpl'); return true; } public function uninstall() { $this->_clearCache('blocknewproducts.tpl'); return parent::uninstall(); } public function getContent() { $output = '

'.$this->displayName.'

'; if (Tools::isSubmit('submitBlockNewProducts')) { if (!($productNbr = Tools::getValue('productNbr')) || empty($productNbr)) $output .= '
'.$this->l('Please complete the "products to display" field.').'
'; elseif ((int)($productNbr) == 0) $output .= '
'.$this->l('Invalid number.').'
'; else { Configuration::updateValue('PS_BLOCK_NEWPRODUCTS_DISPLAY', (int)(Tools::getValue('always_display'))); Configuration::updateValue('NEW_PRODUCTS_NBR', (int)($productNbr)); $output .= '
'.$this->l('Settings updated').'
'; } } return $output.$this->displayForm(); } public function displayForm() { $output = '
'.$this->l('Settings').'

'.$this->l('Define the number of products to be displayed in this block.').'

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

'; return $output; } public function hookRightColumn($params) { if (!$this->isCached('blocknewproducts.tpl', $this->getCacheId())) { if (!Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')) return; $newProducts = Product::getNewProducts((int) $params['cookie']->id_lang, 0, (int) Configuration::get('NEW_PRODUCTS_NBR')); if (!$newProducts) return; $this->smarty->assign(array( 'new_products' => $newProducts, 'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')), )); } return $this->display(__FILE__, 'blocknewproducts.tpl', $this->getCacheId()); } protected function getCacheId($name = null) { return parent::getCacheId('blocknewproducts|'.date('Ymd')); } public function hookLeftColumn($params) { return $this->hookRightColumn($params); } public function hookHome($params) { return $this->hookRightColumn($params); } public function hookHeader($params) { $this->context->controller->addCSS(($this->_path).'blocknewproducts.css', 'all'); } public function hookAddProduct($params) { $this->_clearCache('blocknewproducts.tpl'); } public function hookUpdateProduct($params) { $this->_clearCache('blocknewproducts.tpl'); } public function hookDeleteProduct($params) { $this->_clearCache('blocknewproducts.tpl'); } }