245 lines
8.4 KiB
PHP
Raw Normal View History

2017-07-06 17:41:10 +02:00
<?php
class AdvConstructorBlock extends ObjectModel
{
public $id_advconstructor;
public $title;
public $subtitle;
public $icon;
public $link;
public $external;
public $content;
public $skin;
public $date_from = "0000-00-00 00:00:00";
public $date_to = "0000-00-00 00:00:00";
public $active = true;
public $date_add;
public $date_upd;
public $hooks;
public $categories;
public static $definition = array(
'table' => 'advconstructor',
'primary' => 'id_advconstructor',
'multilang' => true,
'fields' => array(
'title' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => true,
'validate' => 'isString',
'required' => TRUE
),
'subtitle' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'icon' => array(
'type' => ObjectModel :: TYPE_STRING,
'validate' => 'isString'
),
'content' => array(
'type' => ObjectModel :: TYPE_HTML,
'lang' => true,
'validate' => 'isString'
),
'link' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => true,
'validate' => 'isString'
),
'external' => array(
'type' => ObjectModel :: TYPE_INT,
'validate' => 'isBool'
),
'skin' => array(
'type' => ObjectModel :: TYPE_STRING,
'validate' => 'isString'
),
'active' => array(
'type' => ObjectModel :: TYPE_INT,
'validate' => 'isBool'
),
'date_from' => array(
'type' => ObjectModel :: TYPE_DATE
),
'date_to' => array(
'type' => ObjectModel :: TYPE_DATE
),
'date_add' => array(
'type' => ObjectModel :: TYPE_DATE
),
'date_upd' => array(
'type' => ObjectModel :: TYPE_DATE
)
)
);
public function getHooks()
{
if (!isset($this->hooks)) {
$this->hooks = array();
foreach (Db::getInstance()->executeS('
SELECT `id_hook`
FROM `' . _DB_PREFIX_ . 'advconstructor_hook`
WHERE `id_advconstructor` = ' . $this->id . '
') as $hook) {
$this->hooks[] = $hook['id_hook'];
}
}
return $this->hooks;
}
public function getCategory()
{
if (!isset($this->categories)) {
$this->categories = array();
foreach (Db::getInstance()->executeS('
SELECT `id_category`
FROM `' . _DB_PREFIX_ . 'advconstructor_category`
WHERE `id_advconstructor` = ' . $this->id . '
') as $category) {
$this->categories[] = $category['id_category'];
}
}
return $this->categories;
}
public function addHooksAssociation( $hooks )
{
Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'advconstructor_hook`
WHERE `id_advconstructor` = '.(int) $this->id.'
AND id_hook NOT IN ('.implode(',',$hooks).')
');
foreach ( $hooks as $hook ) {
if ( !AdvConstructorHook::associationHookExist( $this->id, $hook ) ) {
$advconstructor_hook = new AdvConstructorHook();
$advconstructor_hook->id_advconstructor = (int) $this->id;
$advconstructor_hook->id_hook = (int) $hook;
$advconstructor_hook->add();
$advconstructor_hook->refreshPositions();
}
}
}
public function addCategoryAssociation( $categories )
{
Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'advconstructor_category`
WHERE `id_advconstructor` = '.(int) $this->id.'
');
foreach ( $categories as $category ) {
if ( !$this->associationCategoryExist( $category ) ) {
Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'advconstructor_category`
(`id_advconstructor`,`id_category`)
VALUES ('.(int) $this->id.','.$category.')
');
}
}
}
public static function getBlocksByHookID( $id_hook, $id_category )
{
$now = date('Y-m-d H:i:s');
$sql = 'SELECT id_advconstructor, position FROM `' . _DB_PREFIX_ . 'advconstructor_hook` WHERE id_hook ='.(int)$id_hook;
if ( $blocks_id = Db::getInstance()->executeS($sql) ) {
$blocks = array();
foreach ($blocks_id as $block_id) {
$blocks[] = $block_id['id_advconstructor'];
}
$collection = new Collection('AdvConstructorBlock', Context::getContext()->language->id);
$collection->Sqlwhere('a0.date_from <= IF(a0.date_from = "0000-00-00 00:00:00","0000-00-00 00:00:00","' . $now . '")' );
$collection->Sqlwhere('a0.date_to >= IF(a0.date_to = "0000-00-00 00:00:00","0000-00-00 00:00:00","' . $now . '")' );
$collection->Sqlwhere('a0.id_advconstructor IN ('.implode(',',$blocks).')' );
if (Shop::isFeatureActive()) {
$collection->Sqlwhere('a0.`id_advconstructor` IN (
SELECT sa.`id_advconstructor`
FROM `' . _DB_PREFIX_ . 'advconstructor_shop` sa
WHERE sa.id_shop IN (' . implode(', ', Shop::getContextListShopID()) . ')
)');
}
$res = $collection->getAll();
$temp = array();
foreach ( $res as $row ) {
foreach ($blocks_id as $block_id) {
if ( $block_id['id_advconstructor'] == $row->id ) {
$show = false;
if ( !self::existInOneCategory( $row->id ) ) {
$show = true;
} else {
if ( $id_category && self::testCategoryAssociation( $row->id, $id_category ) ) {
$show = true;
}
}
if ( $show ){
$temp[$block_id['position']] = $row;
}
}
}
}
ksort($temp);
return $temp;
} else {
return false;
}
}
public static function getBlocksByHookIDLite( $id_hook )
{
$sql = 'SELECT id_advconstructor FROM `' . _DB_PREFIX_ . 'advconstructor_hook` WHERE id_hook ='.(int)$id_hook.' ORDER BY position ASC';
if ( $blocks_id = Db::getInstance()->executeS($sql) ) {
$blocks = array();
foreach ($blocks_id as $block_id) {
$blocks[] = $block_id['id_advconstructor'];
}
return $blocks;
} else {
return false;
}
}
public function associationCategoryExist( $id_category )
{
$sql = 'SELECT `id_advconstructor`
FROM `'._DB_PREFIX_.'advconstructor_category`
WHERE `id_category` = '.(int) $id_category.'
AND `id_advconstructor` = '.(int) $this->id;
if (!$id = Db::getInstance()->getRow($sql)) {
return false;
} else {
return true;
}
}
public static function existInOneCategory( $id_advconstructor ){
$sql = 'SELECT `id_advconstructor`
FROM `'._DB_PREFIX_.'advconstructor_category`
WHERE `id_advconstructor` = '.(int) $id_advconstructor;
if (!$id = Db::getInstance()->getRow($sql)) {
return false;
} else {
return true;
}
}
public static function testCategoryAssociation( $id_advconstructor, $id_category ){
$sql = 'SELECT `id_advconstructor`
FROM `'._DB_PREFIX_.'advconstructor_category`
WHERE `id_category` = '.$id_category.' AND
`id_advconstructor` = '.(int) $id_advconstructor;
if (!$id = Db::getInstance()->getRow($sql)) {
return false;
} else {
return true;
}
}
}