bebeboutik/modules/privatesales_family_menu/privatesales_family_menu.php

198 lines
6.6 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
class Privatesales_Family_Menu extends Module {
2016-01-04 12:49:26 +01:00
public function __construct() {
$this->name = 'privatesales_family_menu';
$this->tab = 'pricing_promotion';
$this->author = 'Antadis';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Menu famille vente privée');
$this->description = $this->l('Affiche un menu des familles de vente privée');
}
public function install() {
if(!Module::isInstalled('privatesales')) {
return FALSE;
}
return parent::install() && $this->registerHook('topMenu');
}
public function hooktopMenu($params) {
global $cookie, $smarty;
$id_lang = (int)$cookie->id_lang;
2017-11-20 17:12:58 +01:00
/**
* @Override cache file
*/
if (class_exists('CacheRedis')) {
$tpl_cache_name = 'sale_privatesales_family_menu_'.(int) $cookie->id_lang.'.html';
if (false === ($result = CacheRedis::getInstance()->get($tpl_cache_name, 'privatesales_family_menu'))) {
$menus = $this->getTree($cookie->id_lang);
if (!$menus) {
CacheRedis::getInstance()->set($tpl_cache_name, 'privatesales_family_menu', '', CacheRedis::MEDIUM_TTL);
return "";
}
$smarty->assign(array(
'menus' => $menus
));
$result = $this->display(__FILE__, 'menu.tpl');
CacheRedis::getInstance()->set($tpl_cache_name, 'privatesales_family_menu', $result, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt';
$expire = time() -3600;
2016-01-04 12:49:26 +01:00
2017-11-20 17:12:58 +01:00
if(file_exists($filename) && filemtime($filename) > $expire) {
$data = file_get_contents($filename);
$menus = json_decode($data, TRUE);
} else {
$file = fopen($filename, 'w+');
$menus = $this->getTree($cookie->id_lang);
fwrite($file, json_encode($menus));
fclose($file);
}
2017-11-20 17:12:58 +01:00
$smarty->assign(array(
'menus' => $menus
));
$result = $this->display(__FILE__, 'menu.tpl');
2016-01-04 12:49:26 +01:00
}
2017-11-20 17:12:58 +01:00
return $result;
2016-01-04 12:49:26 +01:00
}
public function hookPrivateSales_update($params)
{
2017-11-20 17:12:58 +01:00
if (class_exists('CacheRedis')) {
CacheRedis::getInstance()->clear('sale_privatesales*');
}
$id_langs = array(2,3);
foreach ($id_langs as $key => $id_lang) {
$filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt';
if(file_exists($filename)) {
unlink($filename);
}
}
}
public function hookPrivateSales_create($params)
{
2017-03-27 16:05:55 +02:00
$this->hookPrivateSales_update($params);
}
public function hookPrivateSales_delete($params)
{
2017-03-27 16:05:55 +02:00
$this->hookPrivateSales_update($params);
}
public function hookCategoryUpdate($params)
{
2017-03-27 16:05:55 +02:00
$this->hookPrivateSales_update($params);
}
public function hookCategoryAddition($params)
{
2017-03-27 16:05:55 +02:00
$this->hookPrivateSales_update($params);
}
2016-01-04 12:49:26 +01:00
public function getTree($id_lang) {
global $site_version_front;
include_once dirname(__FILE__).'/../categoryfamily/models/CategoryFamily.php';
$families = CategoryFamilyCore::getAll($id_lang, 0);
$families_menu = array();
foreach ($families as $key => $family) {
if($family['id_category_family'] == 13
|| $family['id_category_family'] == 43) {
unset($families[$key]);
continue;
}
$families_menu[$key] = $family;
$childrens = CategoryFamilyCore::getAll($id_lang, $family['id_category_family']);
foreach ($childrens as $key_child => $child) {
$sales = Db::getInstance()->Executes('
SELECT p.`id_sale`, p.`id_category` as category_sale, pc.`id_category`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` pv ON pv.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'privatesale_lang` pl ON pl.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'privatesale_category` pc ON pc.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'category_family_association` pcf ON pcf.`id_category` = pc.`id_category`
WHERE p.`public` = 0
2016-01-04 12:49:26 +01:00
AND pv.`version` = "'.pSql($site_version_front).'"
AND p.`enabled` = 1
AND p.`date_start` < NOW()
2016-01-04 12:49:26 +01:00
AND p.`date_end` > NOW()
AND pcf.`id_category_family` = '.(int)$child['id_category_family'].'
GROUP BY p.`id_sale`
ORDER BY p.`date_end` ASC
');
$childrens[$key_child]['sales'] = array();
$childrens[$key_child]['sales'] = $this->buildSales($sales, $id_lang);
$future_sales = Db::getInstance()->Executes('
SELECT p.`id_sale`, p.`id_category` as category_sale, pc.`id_category`
FROM `'._DB_PREFIX_.'privatesale` p
LEFT JOIN `'._DB_PREFIX_.'privatesale_site_version` pv ON pv.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'privatesale_lang` pl ON pl.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'privatesale_category` pc ON pc.`id_sale` = p.`id_sale`
LEFT JOIN `'._DB_PREFIX_.'category_family_association` pcf ON pcf.`id_category` = pc.`id_category`
WHERE p.`public` = 0
AND pv.`version` = "'.pSql($site_version_front).'"
AND p.`enabled` = 1
AND p.`date_start` > NOW()
AND p.`date_end` > NOW()
AND pcf.`id_category_family` = '.(int)$child['id_category_family'].'
GROUP BY p.`id_sale`
ORDER BY p.`date_end` ASC
');
$childrens[$key_child]['future_sales'] = array();
$childrens[$key_child]['future_sales'] = $this->buildSales($future_sales, $id_lang);
}
$families_menu[$key]['children'] = $childrens;
}
2016-01-04 12:49:26 +01:00
return $families_menu;
}
public function buildSales($sales, $id_lang) {
foreach ($sales as $key_sale => $sale) {
$info_cat = Db::getInstance()->getRow('
SELECT `name`, `link_rewrite`
FROM `'._DB_PREFIX_.'category_lang`
WHERE `id_category` = '.(int)$sale['category_sale'].
' AND id_lang ='.(int)$id_lang
);
if (!file_exists(_PS_ROOT_DIR_.'/img/menu/'.$sale['id_category'].'.jpg')) {
$img_tmp = '';
if (file_exists(_PS_ROOT_DIR_.'/img/c/'. $sale['id_category']. '.jpg')) {
$img_tmp = _PS_ROOT_DIR_.'/img/c/'. $sale['id_category']. '.jpg';
} else {
$id_parent = Category::getIdParent($sale['id_category']);
if(file_exists(_PS_ROOT_DIR_.'/img/c/'. $id_parent. '.jpg')) {
$img_tmp = _PS_ROOT_DIR_.'/img/c/'. $id_parent. '.jpg';
2016-01-04 12:49:26 +01:00
}
}
// create new format image
$image_menu = imagecreatetruecolor(260, 211);
$image_cat = imagecreatefromjpeg($img_tmp);
imagecopyresampled($image_menu, $image_cat, 0, 0, 0, 0, 260, 211, 260, 211);
imagejpeg($image_menu, _PS_ROOT_DIR_.'/img/menu/'.$sale['id_category'].'.jpg',100);
2016-01-04 12:49:26 +01:00
}
$sales[$key_sale]['img'] = '/img/menu/'.$sale['id_category'].'.jpg';
$sales[$key_sale]['link_rewrite'] = $info_cat['link_rewrite'];
$sales[$key_sale]['name'] = $info_cat['name'];
2016-01-04 12:49:26 +01:00
}
return $sales;
2016-01-04 12:49:26 +01:00
}
}