bebeboutik/modules/privatesales_family_menu/privatesales_family_menu.php
2017-11-21 16:17:28 +01:00

198 lines
6.7 KiB
PHP

<?php
class Privatesales_Family_Menu extends Module {
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, $site_version_front;
$id_lang = (int)$cookie->id_lang;
/**
* @Override cache file
*/
if (class_exists('CacheRedis')) {
$query_cache_name = 'sale_privatesales_family_menu_'.(int) $cookie->id_lang.'_'.$site_version_front;
if (false === ($menus = CacheRedis::getInstance()->get($query_cache_name, 'sale_privatesales_family_menu'))) {
$menus = $this->getTree($cookie->id_lang);
CacheRedis::getInstance()->set($query_cache_name, 'sale_privatesales_family_menu', $menus, CacheRedis::MEDIUM_TTL);
}
} else {
$filename = _PS_ROOT_DIR_.'/modules/privatesales_family_menu/menu_'.$id_lang.'.txt';
$expire = time() -3600;
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);
}
}
// Purchase Guide
$purchaseguide = array();
if (count($menus) > 0) {
foreach ($menus as $m) {
$purchaseguide[$m['id_category_family']] = Module::hookExec('topMenuLink',
array('id_category_family' => $m['id_category_family']));
}
}
$smarty->assign(array(
'menus' => $menus,
'purchaseguide' => $purchaseguide,
));
return $this->display(__FILE__, 'menu.tpl');
}
public function hookPrivateSales_update($params)
{
if (class_exists('CacheRedis')) {
CacheRedis::getInstance()->clear('sale*');
}
$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)
{
$this->hookPrivateSales_update($params);
}
public function hookPrivateSales_delete($params)
{
$this->hookPrivateSales_update($params);
}
public function hookCategoryUpdate($params)
{
$this->hookPrivateSales_update($params);
}
public function hookCategoryAddition($params)
{
$this->hookPrivateSales_update($params);
}
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
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]['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 CAST(p.`date_start` AS DATE) = CAST(DATE_ADD(NOW(), INTERVAL 1 DAY) AS DATE)
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;
}
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';
}
}
// 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);
}
$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'];
}
return $sales;
}
}