toutpratique/modules/cms_pack/cms_pack.php
2017-05-26 15:18:35 +02:00

150 lines
3.9 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
include_once dirname(__FILE__).'/classes/CmsPack.php';
class Cms_Pack extends Module
{
public static $cache_reductionAvailable;
public static $cache_pack;
public function __construct()
{
$this->name = 'cms_pack';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Product pack for CMS ANTADIS');
$this->description = $this->l('Manage your product pack in CMS Antadis');
$this->secure_key = Tools::encrypt($this->name);
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install()
|| !$this->createTables()
|| !$this->registerHook('footer')
|| !$this->registerHook('displayBottomProductCms')
|| !$this->registerHook('displayleftPostCms')
)
return false;
}
public function createTables (){
return true;
}
public function hookdisplayFooter() {
// $this->context->controller->addJS(($this->_path).'pack.js', 'all');
return $this->display(__FILE__, 'lightbox_footer.tpl');
}
public function hookdisplayleftPostCms($params) {
$fromEdito = isset($params['from_edito']);
$pack = $this->loadPackForCMS($params['id_cmspost'], $fromEdito);
if($pack) {
$this->smarty->assign(array(
'pack' => $pack,
'id_cms_category' => $params['id_cms_category'],
'reductionAvailable' => self::$cache_reductionAvailable,
));
return $this->display(__FILE__, 'pack_left.tpl');
}
}
public function hookdisplayBottomProductCms($params) {
$pack = $this->loadPackForCMS($params['id_cmspost']);
if ($pack) {
$this->smarty->assign(array(
'pack' => $pack,
'id_cms_category' => $params['id_cms_category'],
'reductionAvailable' => self::$cache_reductionAvailable,
));
return $this->display(__FILE__, 'pack_bottom.tpl');
}
}
public function loadPackForCMS($id_cmspost, $fromEdito = false)
{
if(empty(self::$cache_pack))
{
if($fromEdito) {
$id_pack = Db::getInstance()->getValue('
SELECT `id_pack`
FROM `'._DB_PREFIX_.'cmsps_editos_relation_pack`
WHERE `id_edito` = '.(int)$id_cmspost
);
} else {
$id_pack = Db::getInstance()->getValue('
SELECT `id_pack`
FROM `'._DB_PREFIX_.'cmsps_posts_relation_pack`
WHERE `id_post` = '.(int)$id_cmspost
);
}
$result = array();
$id_lang = Context::getContext()->language->id;
if ($id_pack) {
$pack = new CmsPack((int) $id_pack);
if (!Validate::isLoadedObject($pack)) {
return false;
}
$pack->loadProducts();
$collection_product = new Collection('Product', $id_lang);
$collection_product->where('id_product', 'IN', $pack->products);
$collection_product->where('active', '=', 1);
$products = $collection_product->getResults();
$products = array_map(function($product) {
return $product->loadReductionInfo();
}, $products);
$result['products'] = $products;
$price = array_map(function($product) {
return floatval($product->price);
}, $result['products']);
$result['price_total'] = array_sum($price);
$result['pack'] = $pack->title;
if ($pack->id_cart_rule) {
$result['remise'] = true;
$result['remise_percent'] = $pack->getPercentageReduction();
} else {
$result['remise']= false;
}
}
self::$cache_pack = $result;
}
// On check si tous les produits sont en stock pour afficher ou non la réduction
if(empty(self::$cache_reductionAvailable) && isset(self::$cache_pack['products']))
{
self::$cache_reductionAvailable = true;
foreach(self::$cache_pack['products'] as $product)
{
if($product->quantity == 0)
{
self::$cache_reductionAvailable = false;
continue;
}
}
}
return self::$cache_pack;
}
}