141 lines
3.6 KiB
PHP
Raw Normal View History

2015-08-17 16:38:00 +02:00
<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
include_once dirname(__FILE__).'/classes/CmsPack.php';
class Cms_Pack extends Module
{
2015-12-07 17:31:32 +01:00
public static $cache_reductionAvailable;
public static $cache_pack;
2015-08-17 16:38:00 +02:00
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()
2015-08-20 11:28:09 +02:00
|| !$this->registerHook('footer')
|| !$this->registerHook('displayBottomProductCms')
|| !$this->registerHook('displayleftPostCms')
2015-08-17 16:38:00 +02:00
)
return false;
}
public function createTables (){
return true;
}
2015-08-20 11:28:09 +02:00
public function hookdisplayFooter() {
$this->context->controller->addJS(($this->_path).'pack.js', 'all');
2015-09-28 11:11:58 +02:00
return $this->display(__FILE__, 'lightbox_footer.tpl');
2015-08-20 11:28:09 +02:00
}
public function hookdisplayleftPostCms($params) {
$pack = $this->loadPackForCMS($params['id_cmspost']);
2015-12-07 17:31:32 +01:00
2015-08-21 14:40:48 +02:00
if($pack) {
$this->smarty->assign(array(
2015-12-07 17:31:32 +01:00
'pack' => $pack,
2015-12-14 16:07:18 +01:00
'id_cms_category' => $params['id_cms_category'],
2015-12-07 17:31:32 +01:00
'reductionAvailable' => self::$cache_reductionAvailable,
2015-08-21 14:40:48 +02:00
));
return $this->display(__FILE__, 'pack_left.tpl');
}
2015-08-20 11:28:09 +02:00
}
2015-12-04 10:59:16 +01:00
public function hookdisplayBottomProductCms($params) {
2015-08-20 11:28:09 +02:00
$pack = $this->loadPackForCMS($params['id_cmspost']);
2015-08-21 14:40:48 +02:00
if ($pack) {
$this->smarty->assign(array(
2015-12-07 17:31:32 +01:00
'pack' => $pack,
2015-12-14 16:07:18 +01:00
'id_cms_category' => $params['id_cms_category'],
2015-12-07 17:31:32 +01:00
'reductionAvailable' => self::$cache_reductionAvailable,
2015-08-21 14:40:48 +02:00
));
return $this->display(__FILE__, 'pack_bottom.tpl');
}
2015-08-20 11:28:09 +02:00
}
2015-12-07 17:31:32 +01:00
public function loadPackForCMS($id_cmspost)
{
if(empty(self::$cache_pack))
{
$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;
}
2015-08-20 11:28:09 +02:00
2015-12-07 17:31:32 +01:00
$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;
}
2015-11-04 12:23:35 +01:00
}
2015-08-20 11:28:09 +02:00
}
2015-12-07 17:31:32 +01:00
return self::$cache_pack;
2015-08-20 11:28:09 +02:00
}
2015-08-17 16:38:00 +02:00
}