Merge branch 'fix-bestSellerCache' into develop

This commit is contained in:
Marion Muszynski 2017-03-29 10:56:13 +02:00
commit 7174dc2f25

View File

@ -1,6 +1,6 @@
<?php <?php
/* /*
* 2007-2011 PrestaShop * 2007-2011 PrestaShop
* *
* NOTICE OF LICENSE * NOTICE OF LICENSE
* *
@ -26,282 +26,290 @@
*/ */
if (!defined('_PS_VERSION_')) if (!defined('_PS_VERSION_'))
exit; exit;
class BlockBestSellers extends Module class BlockBestSellers extends Module
{ {
private $_html = ''; private $_html = '';
private $_postErrors = array(); private $_postErrors = array();
public function __construct() public function __construct()
{ {
$this->name = 'blockbestsellers'; $this->name = 'blockbestsellers';
$this->tab = 'front_office_features'; $this->tab = 'front_office_features';
$this->version = '1.1'; $this->version = '1.1';
$this->author = 'PrestaShop'; $this->author = 'PrestaShop';
$this->need_instance = 0; $this->need_instance = 0;
parent::__construct(); parent::__construct();
$this->displayName = $this->l('Top seller block'); $this->displayName = $this->l('Top seller block');
$this->description = $this->l('Add a block displaying the shop\'s top sellers.'); $this->description = $this->l('Add a block displaying the shop\'s top sellers.');
} }
/** /**
* @see ModuleCore::install() * @see ModuleCore::install()
*/ */
public function install() public function install()
{ {
if (!parent::install() OR if (!parent::install() OR
!$this->registerHook('rightColumn') OR !$this->registerHook('rightColumn') OR
!$this->registerHook('header') OR !$this->registerHook('header') OR
!$this->registerHook('bestSaleCart') OR !$this->registerHook('bestSaleCart') OR
!$this->registerHook('updateOrderStatus') OR !$this->registerHook('updateOrderStatus') OR
!ProductSale::fillProductSales()) !ProductSale::fillProductSales())
return false; return false;
return true; return true;
} }
/** /**
* Called in administration -> module -> configure * Called in administration -> module -> configure
*/ */
public function getContent() public function getContent()
{ {
$output = '<h2>'.$this->displayName.'</h2>'; $output = '<h2>'.$this->displayName.'</h2>';
if (Tools::isSubmit('submitBestSellers')) if (Tools::isSubmit('submitBestSellers'))
{ {
Configuration::updateValue('PS_BLOCK_BESTSELLERS_DISPLAY', (int)(Tools::getValue('always_display'))); 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>'; $output .= '<div class="conf confirm"><img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />'.$this->l('Settings updated').'</div>';
} }
return $output.$this->displayForm(); return $output.$this->displayForm();
} }
public function displayForm() public function displayForm()
{ {
return ' return '
<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post"> <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
<fieldset> <fieldset>
<legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend> <legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
<label>'.$this->l('Always display block').'</label> <label>'.$this->l('Always display block').'</label>
<div class="margin-form"> <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" ' : '').'/> <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> <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" ' : '').'/> <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> <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> <p class="clear">'.$this->l('Show the block even if no product is available.').'</p>
</div> </div>
<center><input type="submit" name="submitBestSellers" value="'.$this->l('Save').'" class="button" /></center> <center><input type="submit" name="submitBestSellers" value="'.$this->l('Save').'" class="button" /></center>
</fieldset> </fieldset>
</form>'; </form>';
} }
public function hookRightColumn($params)
{
if (Configuration::get('PS_CATALOG_MODE'))
return ;
global $cookie, $smarty, $cart; public function hookRightColumn($params)
$step = Tools::getValue('step'); {
if (Configuration::get('PS_CATALOG_MODE'))
return ;
global $cookie, $smarty, $cart;
$step = Tools::getValue('step');
if(!$step || ($step && $step != 3 && $step != 4)) { if(!$step || ($step && $step != 3 && $step != 4)) {
$id_lang = $cookie->id_lang; $id_lang = $cookie->id_lang;
Tools::enableCache(); $id_lang = $cookie->id_lang;
$smarty->cache_lifetime = 3600; $filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_'.$id_lang.'.txt';
$cache_id = "blockbestsellers".$id_lang; $expire = time() -3600;
if(!$this->isCached('blockbestsellers_'.(int) $cookie->id_lang.'.tpl', $cache_id)) { if(file_exists($filename) && filemtime($filename) > $expire) {
$currency = new Currency((int)($params['cookie']->id_currency)); $data = file_get_contents($filename);
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10); $best_sellers = json_decode($data, TRUE);
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')) } else {
return; $file = fopen($filename, 'w+');
$best_sellers = array(); $currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 5, NULL, NULL, 10);
if($bestsellers) if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
foreach ($bestsellers AS $bestseller) return;
{ $best_sellers = array();
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
$smarty->assign(array(
'best_sellers' => $best_sellers,
'mediumSize' => Image::getSize('medium')));
}
$display = $this->display(__FILE__, 'blockbestsellers_'.(int) $cookie->id_lang.'.tpl', $cache_id); if($bestsellers)
Tools::restoreCacheSettings(); foreach ($bestsellers AS $bestseller)
$smarty->cache_lifetime = -1; {
return $display; $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
} $best_sellers[] = $bestseller;
} }
// Best sale on lightbox add to cart fwrite($file, json_encode($best_sellers));
public function hookbestSaleCart($params){ fclose($file);
if(_PS_MOBILE_) { }
return false;
}
global $smarty; $smarty->assign(array(
global $cookie; 'best_sellers' => $best_sellers,
'mediumSize' => Image::getSize('medium'))
);
return $this->display(__FILE__, 'blockbestsellers_'.(int) $cookie->id_lang.'.tpl');
}
}
$id_lang = $cookie->id_lang; // Best sale on lightbox add to cart
public function hookbestSaleCart($params){
if(_PS_MOBILE_) {
return false;
}
Tools::enableCache(); global $smarty;
$smarty->cache_lifetime = 3600; global $cookie;
$cache_id = "blockbestsellers_cart".$id_lang;
if(!$this->isCached('blockbestsellers_cart_'.(int) $id_lang.'.tpl', $cache_id)) { $id_lang = $cookie->id_lang;
$currency = new Currency((int)($params['cookie']->id_currency)); $filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_cart_'.$id_lang.'.txt';
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10); $expire = time() -3600;
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
return;
$best_sellers = array();
if($bestsellers) if(file_exists($filename) && filemtime($filename) > $expire) {
foreach ($bestsellers AS $bestseller) $data = file_get_contents($filename);
{ $best_sellers = json_decode($data, TRUE);
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency); } else {
$best_sellers[] = $bestseller; $file = fopen($filename, 'w+');
} $currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10);
$smarty->assign(
array(
'best_sellers' => $best_sellers,
)
);
}
$display = $this->display(__FILE__, 'blockbestsellers_cart_'.(int) $id_lang.'.tpl' , $cache_id); if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
Tools::restoreCacheSettings(); return;
$smarty->cache_lifetime = -1; $best_sellers = array();
return $display;
}
public function hookbestSaleHome($params) if($bestsellers)
{ foreach ($bestsellers AS $bestseller)
if(_PS_MOBILE_) { {
return false; $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
} $best_sellers[] = $bestseller;
}
global $smarty; fwrite($file, json_encode($best_sellers));
global $cookie; fclose($file);
}
$smarty->assign(
array(
'best_sellers' => $best_sellers,
)
);
$id_lang = $cookie->id_lang; return $this->display(__FILE__, 'blockbestsellers_cart_'.(int) $id_lang.'.tpl');
}
Tools::enableCache(); public function hookbestSaleHome($params)
$smarty->cache_lifetime = 3600; {
$cache_id = "blockbestsellers_home".$id_lang; if(_PS_MOBILE_) {
return false;
}
if(!$this->isCached('blockbestsellers_home_'.(int) $cookie->id_lang.'.tpl', $cache_id)) { global $smarty;
$currency = new Currency((int)($params['cookie']->id_currency)); global $cookie;
$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) $id_lang = $cookie->id_lang;
foreach ($bestsellers AS $bestseller) $filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_home_'.$id_lang.'.txt';
{ $expire = time() -3600;
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller;
}
$smarty->assign(
array(
'best_sellers' => $best_sellers,
)
);
}
$display = $this->display(__FILE__, 'blockbestsellers_home_'.(int) $cookie->id_lang.'.tpl' , $cache_id); if(file_exists($filename) && filemtime($filename) > $expire) {
Tools::restoreCacheSettings(); $data = file_get_contents($filename);
$smarty->cache_lifetime = -1; $best_sellers = json_decode($data, TRUE);
return $display; } else {
} $file = fopen($filename, 'w+');
$currency = new Currency((int)($params['cookie']->id_currency));
$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();
public function hookProductFooter($params) if($bestsellers)
{ foreach ($bestsellers AS $bestseller)
if(_PS_MOBILE_) { {
return false; $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
} $best_sellers[] = $bestseller;
}
global $smarty; fwrite($file, json_encode($best_sellers));
global $cookie; fclose($file);
}
$smarty->assign(
array(
'best_sellers' => $best_sellers,
)
);
$id_lang = $cookie->id_lang; return $this->display(__FILE__, 'blockbestsellers_home_'.(int) $cookie->id_lang.'.tpl');
}
Tools::enableCache(); public function hookProductFooter($params)
$smarty->cache_lifetime = 3600; {
$cache_id = "blockbestsellers_productfooter".$id_lang; if(_PS_MOBILE_) {
return false;
}
if(!$this->isCached('blockbestsellers_productfooter_'.(int) $cookie->id_lang.'.tpl', $cache_id)) { global $smarty;
$currency = new Currency((int)($params['cookie']->id_currency)); global $cookie;
$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) $id_lang = $cookie->id_lang;
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,
)
);
}
$display = $this->display(__FILE__, 'blockbestsellers_productfooter_'.(int) $cookie->id_lang.'.tpl' , $cache_id); $filename = _PS_ROOT_DIR_.'/modules/blockbestsellers/blockbestsellers_productfooter_'.$id_lang.'.txt';
Tools::restoreCacheSettings(); $expire = time() -3600;
$smarty->cache_lifetime = -1;
return $display;
}
public function hookLeftColumn($params)
{
if(_PS_MOBILE_) {
return false;
}
return $this->hookRightColumn($params);
}
public function hookHeader($params) if(file_exists($filename) && filemtime($filename) > $expire) {
{ $data = file_get_contents($filename);
if (Configuration::get('PS_CATALOG_MODE')) $best_sellers = json_decode($data, TRUE);
return ; } else {
Tools::addCSS(($this->_path).'blockbestsellers.css', 'all'); $file = fopen($filename, 'w+');
} $currency = new Currency((int)($params['cookie']->id_currency));
$bestsellers = ProductSale::getBestSalesVp((int)($params['cookie']->id_lang), 0, 6, NULL, NULL, 10);
public function hookHome($params) if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
{ return;
if (Configuration::get('PS_CATALOG_MODE')) $best_sellers = array();
return ;
global $smarty; if($bestsellers)
$currency = new Currency((int)($params['cookie']->id_currency)); foreach ($bestsellers AS $bestseller)
$bestsellers = ProductSale::getBestSalesLight((int)($params['cookie']->id_lang), 0, 4); {
if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY')) $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
return; $best_sellers[] = $bestseller;
$best_sellers = array(); }
if($bestsellers) fwrite($file, json_encode($best_sellers));
foreach ($bestsellers AS $bestseller) fclose($file);
{ }
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$best_sellers[] = $bestseller; $smarty->assign(
} array(
'best_sellers' => $best_sellers,
$smarty->assign(array( )
'best_sellers' => $best_sellers, );
'homeSize' => Image::getSize('home')));
return $this->display(__FILE__, 'blockbestsellers-home.tpl'); 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');
}
} }