385 lines
16 KiB
PHP
Executable File
385 lines
16 KiB
PHP
Executable File
<?php
|
|
/*
|
|
* 2007-2011 PrestaShop
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Academic Free License (AFL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/afl-3.0.php
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@prestashop.com so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.prestashop.com for more information.
|
|
*
|
|
* @author PrestaShop SA <contact@prestashop.com>
|
|
* @copyright 2007-2011 PrestaShop SA
|
|
* @version Release: $Revision: 8783 $
|
|
* @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 BlockBestSellers extends Module
|
|
{
|
|
private $_html = '';
|
|
private $_postErrors = array();
|
|
|
|
public function __construct()
|
|
{
|
|
$this->name = 'blockbestsellers';
|
|
$this->tab = 'front_office_features';
|
|
$this->version = '1.1';
|
|
$this->author = 'PrestaShop';
|
|
$this->need_instance = 0;
|
|
|
|
parent::__construct();
|
|
|
|
$this->displayName = $this->l('Top seller block');
|
|
$this->description = $this->l('Add a block displaying the shop\'s top sellers.');
|
|
}
|
|
|
|
/**
|
|
* @see ModuleCore::install()
|
|
*/
|
|
public function install()
|
|
{
|
|
if (!parent::install() OR
|
|
!$this->registerHook('rightColumn') OR
|
|
!$this->registerHook('header') OR
|
|
!$this->registerHook('bestSaleCart') OR
|
|
!$this->registerHook('updateOrderStatus') OR
|
|
!ProductSale::fillProductSales())
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Called in administration -> module -> configure
|
|
*/
|
|
public function getContent()
|
|
{
|
|
$output = '<h2>'.$this->displayName.'</h2>';
|
|
if (Tools::isSubmit('submitBestSellers'))
|
|
{
|
|
Configuration::updateValue('PS_BLOCK_BESTSELLERS_DISPLAY', (int)(Tools::getValue('always_display')));
|
|
$output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />'.$this->l('Settings updated').'</div>';
|
|
}
|
|
return $output.$this->displayForm();
|
|
}
|
|
|
|
public function displayForm()
|
|
{
|
|
return '
|
|
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
|
|
<fieldset>
|
|
<legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
|
|
<label>'.$this->l('Always display block').'</label>
|
|
<div class="margin-form">
|
|
<input type="radio" name="always_display" id="display_on" value="1" '.(Tools::getValue('always_display', Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')) ? 'checked="checked" ' : '').'/>
|
|
<label class="t" for="display_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
|
|
<input type="radio" name="always_display" id="display_off" value="0" '.(!Tools::getValue('always_display', Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')) ? 'checked="checked" ' : '').'/>
|
|
<label class="t" for="display_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
|
|
<p class="clear">'.$this->l('Show the block even if no product is available.').'</p>
|
|
</div>
|
|
<center><input type="submit" name="submitBestSellers" value="'.$this->l('Save').'" class="button" /></center>
|
|
</fieldset>
|
|
</form>';
|
|
}
|
|
|
|
public function hookRightColumn($params)
|
|
{
|
|
if (Configuration::get('PS_CATALOG_MODE'))
|
|
return ;
|
|
|
|
global $cookie, $smarty, $cart;
|
|
$step = Tools::getValue('step');
|
|
if(!$step || ($step && $step != 3 && $step != 4)) {
|
|
$id_lang = $cookie->id_lang;
|
|
$currency = new Currency((int)($params['cookie']->id_currency));
|
|
if (class_exists('CacheRedis')) {
|
|
$query_cache_name = 'blockbestsellers_'.(int) $cookie->id_lang;
|
|
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
|
|
return;
|
|
}
|
|
$best_sellers = array();
|
|
if($bestsellers){
|
|
foreach ($bestsellers AS $bestseller){
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
}
|
|
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
|
|
}
|
|
} else {
|
|
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_'.$id_lang.'.txt';
|
|
$expire = time() -3600;
|
|
|
|
if(file_exists($filename) && filemtime($filename) > $expire) {
|
|
$data = file_get_contents($filename);
|
|
$best_sellers = json_decode($data, TRUE);
|
|
} else {
|
|
$file = fopen($filename, 'w+');
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
|
|
return;
|
|
$best_sellers = array();
|
|
|
|
if($bestsellers)
|
|
foreach ($bestsellers AS $bestseller)
|
|
{
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
|
|
fwrite($file, json_encode($best_sellers));
|
|
fclose($file);
|
|
}
|
|
}
|
|
|
|
$smarty->assign(array(
|
|
'best_sellers' => $best_sellers,
|
|
'mediumSize' => Image::getSize('medium'))
|
|
);
|
|
return $this->display(__FILE__, 'blockbestsellers_'.(int) $cookie->id_lang.'.tpl');
|
|
}
|
|
}
|
|
|
|
// Best sale on lightbox add to cart
|
|
public function hookbestSaleCart($params){
|
|
if(_PS_MOBILE_) {
|
|
return false;
|
|
}
|
|
|
|
global $smarty, $cookie;
|
|
|
|
$id_lang = $cookie->id_lang;
|
|
$currency = new Currency((int)($params['cookie']->id_currency));
|
|
|
|
if (class_exists('CacheRedis')) {
|
|
$query_cache_name = 'blockbestsellers_cart_'.(int) $cookie->id_lang;
|
|
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
|
|
return;
|
|
}
|
|
|
|
$best_sellers = array();
|
|
if($bestsellers){
|
|
foreach ($bestsellers AS $bestseller){
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
}
|
|
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
|
|
}
|
|
} else {
|
|
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_cart_'.$id_lang.'.txt';
|
|
$expire = time() -3600;
|
|
|
|
if(file_exists($filename) && filemtime($filename) > $expire) {
|
|
$data = file_get_contents($filename);
|
|
$best_sellers = json_decode($data, TRUE);
|
|
} else {
|
|
$file = fopen($filename, 'w+');
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
|
|
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
|
|
return;
|
|
$best_sellers = array();
|
|
|
|
if($bestsellers)
|
|
foreach ($bestsellers AS $bestseller)
|
|
{
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
|
|
fwrite($file, json_encode($best_sellers));
|
|
fclose($file);
|
|
}
|
|
}
|
|
$smarty->assign(
|
|
array(
|
|
'best_sellers' => $best_sellers,
|
|
)
|
|
);
|
|
|
|
return $this->display(__FILE__, 'blockbestsellers_cart_'.(int) $id_lang.'.tpl');
|
|
}
|
|
|
|
public function hookbestSaleHome($params)
|
|
{
|
|
if(_PS_MOBILE_) {
|
|
return false;
|
|
}
|
|
|
|
global $smarty;
|
|
global $cookie;
|
|
|
|
$id_lang = $cookie->id_lang;
|
|
$currency = new Currency((int)($params['cookie']->id_currency));
|
|
if (class_exists('CacheRedis')) {
|
|
$query_cache_name = 'blockbestsellers_home_'.(int) $cookie->id_lang;
|
|
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
|
|
return;
|
|
}
|
|
$best_sellers = array();
|
|
if($bestsellers){
|
|
foreach ($bestsellers AS $bestseller){
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
}
|
|
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
|
|
}
|
|
} else {
|
|
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_home_'.$id_lang.'.txt';
|
|
$expire = time() -3600;
|
|
|
|
if(file_exists($filename) && filemtime($filename) > $expire) {
|
|
$data = file_get_contents($filename);
|
|
$best_sellers = json_decode($data, TRUE);
|
|
} else {
|
|
$file = fopen($filename, 'w+');
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
|
|
return;
|
|
$best_sellers = array();
|
|
|
|
if($bestsellers)
|
|
foreach ($bestsellers AS $bestseller)
|
|
{
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
|
|
fwrite($file, json_encode($best_sellers));
|
|
fclose($file);
|
|
}
|
|
}
|
|
$smarty->assign(
|
|
array(
|
|
'best_sellers' => $best_sellers,
|
|
)
|
|
);
|
|
|
|
return $this->display(__FILE__, 'blockbestsellers_home_'.(int) $cookie->id_lang.'.tpl');
|
|
}
|
|
|
|
public function hookProductFooter($params)
|
|
{
|
|
if(_PS_MOBILE_) {
|
|
return false;
|
|
}
|
|
|
|
global $smarty;
|
|
global $cookie;
|
|
|
|
$id_lang = $cookie->id_lang;
|
|
$currency = new Currency((int)($params['cookie']->id_currency));
|
|
if (class_exists('CacheRedis')) {
|
|
$query_cache_name = 'blockbestsellers_productfooter_'.(int) $cookie->id_lang;
|
|
if (false === ($best_sellers = CacheRedis::getInstance()->get($query_cache_name, 'sale_blockbestsellers'))) {
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')){
|
|
return;
|
|
}
|
|
$best_sellers = array();
|
|
if($bestsellers){
|
|
foreach ($bestsellers AS $bestseller){
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
}
|
|
CacheRedis::getInstance()->set($query_cache_name, 'sale_blockbestsellers', $best_sellers, CacheRedis::MEDIUM_TTL);
|
|
}
|
|
} else {
|
|
$filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_productfooter_'.$id_lang.'.txt';
|
|
$expire = time() -3600;
|
|
|
|
if(file_exists($filename) && filemtime($filename) > $expire) {
|
|
$data = file_get_contents($filename);
|
|
$best_sellers = json_decode($data, TRUE);
|
|
} else {
|
|
$file = fopen($filename, 'w+');
|
|
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
|
|
return;
|
|
$best_sellers = array();
|
|
|
|
if($bestsellers)
|
|
foreach ($bestsellers AS $bestseller)
|
|
{
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
|
|
fwrite($file, json_encode($best_sellers));
|
|
fclose($file);
|
|
}
|
|
}
|
|
$smarty->assign(
|
|
array(
|
|
'best_sellers' => $best_sellers,
|
|
)
|
|
);
|
|
|
|
return $this->display(__FILE__, 'blockbestsellers_productfooter_'.(int) $cookie->id_lang.'.tpl');
|
|
}
|
|
|
|
public function hookLeftColumn($params)
|
|
{
|
|
if(_PS_MOBILE_) {
|
|
return false;
|
|
}
|
|
return $this->hookRightColumn($params);
|
|
}
|
|
|
|
public function hookHeader($params)
|
|
{
|
|
if (Configuration::get('PS_CATALOG_MODE'))
|
|
return ;
|
|
Tools::addCSS(($this->_path).'blockbestsellers.css', 'all');
|
|
}
|
|
|
|
public function hookHome($params)
|
|
{
|
|
if (Configuration::get('PS_CATALOG_MODE'))
|
|
return ;
|
|
|
|
global $smarty;
|
|
$currency = new Currency((int)($params['cookie']->id_currency));
|
|
$bestsellers = ProductSale::getBestSalesLight((int)($params['cookie']->id_lang), 0, 4);
|
|
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
|
|
return;
|
|
$best_sellers = array();
|
|
|
|
if($bestsellers)
|
|
foreach ($bestsellers AS $bestseller)
|
|
{
|
|
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
|
|
$best_sellers[] = $bestseller;
|
|
}
|
|
|
|
$smarty->assign(array(
|
|
'best_sellers' => $best_sellers,
|
|
'homeSize' => Image::getSize('home')));
|
|
return $this->display(__FILE__, 'blockbestsellers-home.tpl');
|
|
}
|
|
}
|
|
|
|
|