* @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 BlockViewed extends Module { private $_html = ''; private $_postErrors = array(); public function __construct() { $this->name = 'blockviewed'; $this->tab = 'front_office_features'; $this->version = 0.9; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Viewed products block.'); $this->description = $this->l('Adds a block displaying recently viewed products.'); } public function install() { if (!parent::install() || !$this->registerHook('leftColumn') || !$this->registerHook('header') || !Configuration::updateValue('PRODUCTS_VIEWED_NBR', 2)) return false; return true; } public function getContent() { $output = '

'.$this->displayName.'

'; if (Tools::isSubmit('submitBlockViewed')) { if (!($productNbr = Tools::getValue('productNbr')) || empty($productNbr)) $output .= '
'.$this->l('You must fill in the \'Products displayed\' field.').'
'; elseif ((int)($productNbr) == 0) $output .= '
'.$this->l('Invalid number.').'
'; else { Configuration::updateValue('PRODUCTS_VIEWED_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 displayed in this block.').'

'; return $output; } public function hookRightColumn($params) { $id_product = (int)Tools::getValue('id_product'); $productsViewed = (isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) ? array_slice(array_reverse(explode(',', $params['cookie']->viewed)), 0, Configuration::get('PRODUCTS_VIEWED_NBR')) : array(); if ($id_product && !in_array($id_product, $productsViewed)) { if(isset($params['cookie']->viewed) && !empty($params['cookie']->viewed)) $params['cookie']->viewed .= ',' . (int)$id_product; else $params['cookie']->viewed = (int)$id_product; } if (count($productsViewed)) { $defaultCover = Language::getIsoById($params['cookie']->id_lang).'-default'; $productIds = implode(',', $productsViewed); $productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT MAX(image_shop.id_image) id_image, p.id_product, il.legend, product_shop.active, pl.name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite FROM '._DB_PREFIX_.'product p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').') LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').' LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image) LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').') WHERE p.id_product IN ('.$productIds.') AND pl.id_lang = '.(int)($params['cookie']->id_lang).' AND cl.id_lang = '.(int)($params['cookie']->id_lang).' GROUP BY product_shop.id_product' ); $productsImagesArray = array(); foreach ($productsImages as $pi) $productsImagesArray[$pi['id_product']] = $pi; $productsViewedObj = array(); foreach ($productsViewed as $productViewed) { $obj = (object)'Product'; if (!isset($productsImagesArray[$productViewed]) || (!$obj->active = $productsImagesArray[$productViewed]['active'])) continue; else { $obj->id = (int)($productsImagesArray[$productViewed]['id_product']); $obj->id_image = (int)$productsImagesArray[$productViewed]['id_image']; $obj->cover = (int)($productsImagesArray[$productViewed]['id_product']).'-'.(int)($productsImagesArray[$productViewed]['id_image']); $obj->legend = $productsImagesArray[$productViewed]['legend']; $obj->name = $productsImagesArray[$productViewed]['name']; $obj->description_short = $productsImagesArray[$productViewed]['description_short']; $obj->link_rewrite = $productsImagesArray[$productViewed]['link_rewrite']; $obj->category_rewrite = $productsImagesArray[$productViewed]['category_rewrite']; // $obj is not a real product so it cannot be used as argument for getProductLink() $obj->product_link = $this->context->link->getProductLink($obj->id, $obj->link_rewrite, $obj->category_rewrite); if (!isset($obj->cover) || !$productsImagesArray[$productViewed]['id_image']) { $obj->cover = $defaultCover; $obj->legend = ''; } $productsViewedObj[] = $obj; } } if ($id_product && !in_array($id_product, $productsViewed)) { // Check if the user to the right of access to this product $product = new Product((int)$id_product); if ($product->checkAccess((int)$this->context->customer->id)) array_unshift($productsViewed, $id_product); } if (!count($productsViewedObj)) return; $this->smarty->assign(array( 'productsViewedObj' => $productsViewedObj, 'mediumSize' => Image::getSize('medium'))); return $this->display(__FILE__, 'blockviewed.tpl'); } return; } public function hookLeftColumn($params) { return $this->hookRightColumn($params); } public function hookHeader($params) { $this->context->controller->addCSS(($this->_path).'blockviewed.css', 'all'); } }