diff --git a/modules/privatesales/controllers/front/home.php b/modules/privatesales/controllers/front/home.php index cf98a0b2..2016f784 100644 --- a/modules/privatesales/controllers/front/home.php +++ b/modules/privatesales/controllers/front/home.php @@ -50,7 +50,7 @@ class privatesaleshomeModuleFrontController extends ModuleFrontController { $this->setTemplate('privatesales-home.tpl'); // 12613 - Optimisation - $sale_cache = new SaleCache($this->context, ($news ? "home_".$news : 'home')); + $sale_cache = new SaleCache($this->context, "home", 7200, $news); // 2 Hours if ($sale_cache->isCached($this->template)) { $this->setCacheHtml($sale_cache->getCache()); return; diff --git a/modules/privatesales/controllers/front/homecat.php b/modules/privatesales/controllers/front/homecat.php index a3d72fbd..4f1e3837 100644 --- a/modules/privatesales/controllers/front/homecat.php +++ b/modules/privatesales/controllers/front/homecat.php @@ -62,7 +62,7 @@ class privatesaleshomecatModuleFrontController extends ModuleFrontController { $this->setTemplate('privatesales-home.tpl'); // 12613 - Optimisation - $sale_cache = new SaleCache($this->context, 'homecat'); + $sale_cache = new SaleCache($this->context, 'homecat', 7200); // 2 Hours if ($sale_cache->isCached($this->template)) { $this->setCacheHtml($sale_cache->getCache()); return; diff --git a/modules/privatesales/models/SaleCache.php b/modules/privatesales/models/SaleCache.php index e19210fb..1df562a8 100644 --- a/modules/privatesales/models/SaleCache.php +++ b/modules/privatesales/models/SaleCache.php @@ -8,7 +8,7 @@ class SaleCache private $template_path = ''; private $path_cache_file = ''; - function __construct(Context $context, $cache_prefix, $lifetime = 7200) // 2 Hours + function __construct(Context $context, $cache_prefix, $lifetime, $is_sales_news = null) { $d = new DateTime(); @@ -21,11 +21,16 @@ class SaleCache $cache_prefix .= implode('_',$customer->getGroups()); } - $unformat_cache_id = "private_sales_".$cache_prefix."_"; - $unformat_cache_id .= $this->addCachePrefixSlider($d); - $unformat_cache_id .= "_".$d->format('Ymd'); + $mobileDetect = new Mobile_Detect(); + if($mobileDetect->isMobile()) { + $cache_prefix .= '_mobile_'; + } + + $cache_prefix .= $this->addCachePrefixSlider($d); + $cache_prefix .= $this->addCachePrefixSales($d, $is_sales_news); + + $this->cache_id = md5("private_sales_".$cache_prefix."_".$d->format('Ymd')); - $this->cache_id = md5($unformat_cache_id); $this->max_filemtime = $d->getTimestamp() - $lifetime; $this->path_cache_file = __DIR__.'/../cache/'.$this->cache_id; $this->smarty = $context->smarty; @@ -92,4 +97,29 @@ class SaleCache return implode(';',$slider); } + function addCachePrefixSales(DateTime $date_now, $news) + { + $tmp = ''; + + if (Configuration::get('PRIVATESALES_FLASH')) { + $tmp .= implode(',', SaleCore::getAllSalesId("current",0,TRUE,FALSE,TRUE)); + } + + if (Configuration::get('PRIVATESALES_HOTSPOT')) { + $tmp .= implode(',', SaleCore::getAllSalesId("hotspot")); + } + + if (Configuration::get('PRIVATESALES_PASTSALES')) { + $tmp .= implode(',', SaleCore::getAllSalesId("past",Configuration::get('PRIVATESALES_PASTLIMIT'))); + } + + if (Configuration::get('PRIVATESALES_FUTURESALES')) { + $tmp .= implode(',', SaleCore::getAllSalesId("future",Configuration::get('PRIVATESALES_FUTURELIMIT'), TRUE, FALSE)); + } + + $tmp .= implode(',', SaleCore::getAllSalesId("current",0,TRUE,FALSE,FALSE,$news)); + + return $tmp; + } + } diff --git a/modules/privatesales/models/SaleCore.php b/modules/privatesales/models/SaleCore.php index 2fb5b9a1..c7419f98 100644 --- a/modules/privatesales/models/SaleCore.php +++ b/modules/privatesales/models/SaleCore.php @@ -274,6 +274,74 @@ class SaleCore extends ObjectModel{ return $update; } + + public static function getAllSalesId($type = "all", $date_limit = 0, $active = TRUE, $archived = FALSE, $flashsale = FALSE, $newssale = NULL) + { + $date_now = date('Y-m-d H:i:s'); + + $sql = 'SELECT `id_privatesales` FROM `'._DB_PREFIX_.'privatesales` WHERE 0=0 '; + + + if ($type == self::STATE_CURRENT) { + $sql .= ' AND `date_start` <= \''.$date_now.'\' + AND `date_end` > \''.$date_now.'\' + AND `date_end_hotspot` < \''.$date_now.'\' + '; + + }elseif ($type == self::STATE_FUTURE) { + $sql .= ' AND `date_start` > \''.$date_now.'\' '; + if ($date_limit > 0) { + $sql .= ' AND `date_start` <= DATE_ADD(NOW(), INTERVAL ' . (int)$date_limit . ' DAY) '; + } + }elseif ($type == self::STATE_PAST) { + $sql .= ' AND `date_end` < \''.$date_now.'\' + '; + if($date_limit > 0){ + $sql .= ' AND `date_end` >= DATE_SUB(NOW(), INTERVAL ' . (int)$date_limit . ' DAY) '; + } + } + elseif ($type == self::STATE_ARCHIVED) { + $sql .= ' AND `archived` = 1 '; + } + elseif ($type == self::STATE_HOTSPOT) { + $sql .= ' AND `date_start_hotspot` <= \''.$date_now.'\' + AND `date_end_hotspot` > \''.$date_now.'\' + '; + } + + if ($active !== null) { + $sql .= ' AND `active` = 1'; + } + + if ($archived !== null){ + $sql .= ' AND `archived` = 0'; + } + + if ($flashsale) { + $sql .= ' AND `flash` = 1'; + } + else { + $sql .= ' AND `flash` = 0'; + } + + if ( $newssale !== null ) { + if ( $newssale == 1 ) { + $sql .= 'AND `news` = 1'; + } + elseif ( $newssale == 2 ) { + $sql .= 'AND `news` = 0'; + } + } + + $result = Db::getInstance()->executeS($sql); + + if (!is_array($result)) { + return array(); + } + + return array_column($result, 'id_privatesales'); + } + /** * */ @@ -293,7 +361,7 @@ class SaleCore extends ObjectModel{ $collection->Sqlwhere('a0.`date_start` <= DATE_ADD(NOW(), INTERVAL ' . (int)$date_limit . ' DAY)'); } }else if($type == self::STATE_PAST){ - $collection->where('date_end', '<', date('Y-m-d H:i:s')); + $collection->where('date_end', '<', $date_now); if($date_limit > 0){ $collection->Sqlwhere('a0.`date_end` >= DATE_SUB(NOW(), INTERVAL ' . (int)$date_limit . ' DAY)'); }