Thibault GUILLAUME ba6a6027d7 add pack divers
2015-10-20 18:04:08 +02:00

93 lines
2.1 KiB
PHP

<?php
class CmsPack extends ObjectModel {
public $id_pack;
public $title;
public $id_cart_rule;
public $products = array();
/**
* @see ObjectModel::$definition
*/
public static $definition = array(
'table' => 'cms_pack',
'primary' => 'id_pack',
'fields' => array(
'title' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),
'id_cart_rule' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
),
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
}
public function deleteRelations() {
return (bool) Db::getInstance()->delete('cms_pack_relation', 'id_pack = '. $this->id);
}
public function addRelation($id_relation) {
return Db::getInstance()->execute('
INSERT INTO
`'._DB_PREFIX_.'cms_pack_relation`
VALUES (
'.(int)$this->id.',
'.(int)$id_relation.'
)
');
}
public function loadProducts($page_name = null) {
$this->products = $this->getRelations();
}
public function getRelations($page_name = null) {
if(!Validate::isLoadedObject($this))
return array();
$array_tmp = array();
$sql = 'SELECT `id_product`
FROM '._DB_PREFIX_.'cms_pack_relation
WHERE id_pack = '.(int)$this->id.'';
$infos = Db::getInstance()->executeS($sql);
foreach($infos as $info) {
$array_tmp[$info['id_product']] = $info['id_product'];
}
return $array_tmp;
}
public function getInfosWithValues() {
$infos = $this->getRelations();
if(count($infos) == 0)
return array();
$id_lang = Context::getContext()->language->id;
$sql = '
SELECT p.id_product as id, pl.name as content
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON p.`id_product` = pl.`id_product`
AND pl.id_lang = '.$id_lang.'
WHERE p.id_product IN ('.implode(',', $infos).')';
return Db::getInstance()->executeS($sql);
}
public function getPercentageReduction()
{
if ($this->id_cart_rule) {
$cartRule = new CartRule((int) $this->id_cart_rule);
if(isset($cartRule->reduction_percent)) {
return (int) $cartRule->reduction_percent;
}
} else {
return 0;
}
}
}