2017-07-06 17:41:10 +02:00

281 lines
7.0 KiB
PHP

<?php
class AdvPictoClass extends ObjectModel
{
public $id_advpicto;
public $title;
public $alt;
public $link;
public $external;
public $selfcategory_display;
public $content;
public $position;
public $active = true;
public $categories;
public static $definition = array(
'table' => 'advpicto',
'primary' => 'id_advpicto',
'multilang' => true,
'fields' => array(
'title' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => true,
'validate' => 'isString',
'required' => TRUE
),
'alt' => array(
'type' => ObjectModel :: TYPE_STRING,
'lang' => true,
'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'
),
'selfcategory_display' => array(
'type' => ObjectModel :: TYPE_INT,
'validate' => 'isBool'
),
'active' => array(
'type' => ObjectModel :: TYPE_INT,
'validate' => 'isBool'
),
'position' => array(
'type' => ObjectModel :: TYPE_INT
)
)
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
$this->image_dir = _PS_IMG_DIR_ . 'picto/';
}
public static function getAllPictos()
{
$query = '
SELECT adv.`id_advpicto` as id, advpl.`title`
FROM `' . _DB_PREFIX_ . 'advpicto` adv
LEFT JOIN `' . _DB_PREFIX_ . 'advpicto_lang` advpl ON adv.`id_advpicto` = advpl.`id_advpicto` AND advpl.`id_lang` = ' . Context::getContext()->language->id . '
';
if(Shop::isFeatureActive())
{
$query .= ' INNER JOIN `'._DB_PREFIX_.'advpicto_shop` advs ON adv.`id_advpicto` = advs.`id_advpicto` AND id_shop = '. Context::getContext()->shop->id;
}
return Db::getInstance()->executeS($query);
}
public function getCategories()
{
if(!$this->id)
{
return array();
}
if (!isset($this->categories)) {
$this->categories = array();
foreach (Db::getInstance()->executeS('
SELECT `id_category`
FROM `' . _DB_PREFIX_ . 'advpicto_category`
WHERE `id_advpicto` = ' . $this->id . '
') as $category) {
$this->categories[] = $category['id_category'];
}
}
return $this->categories;
}
public function addCategoryAssociation( $categories )
{
if(!empty($categories)) {
Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'advpicto_category`
WHERE `id_advpicto` = '.(int) $this->id.'
');
if ($categories)
{
foreach ( $categories as $category )
{
Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'advpicto_category`
(`id_advpicto`,`id_category`)
VALUES ('.(int) $this->id.','.$category.')
');
}
}
}
}
public static function getPictoName($id_picto, $id_lang)
{
return Db::getInstance()->getValue('
SELECT `title`
FROM `' . _DB_PREFIX_ . 'advpicto_lang`
WHERE `id_advpicto` = ' . $id_picto . '
AND id_lang = ' . $id_lang . '
');
}
public static function getPictosForProductList($id_product, $id_category)
{
$context = Context::getContext();
$categories = Product::getProductCategories($id_product);
if(!empty($categories))
{
$query = '
SELECT *
FROM `'._DB_PREFIX_.'advpicto` adv
INNER JOIN `'._DB_PREFIX_.'advpicto_lang` advl ON adv.`id_advpicto` = advl.`id_advpicto` AND id_lang = '. $context->language->id
;
if(Shop::isFeatureActive())
{
$query .= ' INNER JOIN `'._DB_PREFIX_.'advpicto_shop` advs ON adv.`id_advpicto` = advs.`id_advpicto` AND id_shop = '. $context->shop->id;
}
$query .= '
INNER JOIN `'._DB_PREFIX_.'advpicto_category` advc ON adv.`id_advpicto` = advc.`id_advpicto` AND advc.`id_category` IN ('. implode(',', $categories) . ')
WHERE adv.`active` = 1
GROUP BY adv.`id_advpicto`
ORDER BY adv.`position` ASC
';
$pictos = Db::getInstance()->executeS($query);
if (empty($pictos))
{
return false;
}
foreach($pictos as $key => $picto)
{
if(!$picto['selfcategory_display'])
{
$pictoCategories = Db::getInstance()->executeS('
SELECT `id_category`
FROM `' . _DB_PREFIX_ . 'advpicto_category`
WHERE `id_advpicto` = ' . $picto['id_advpicto']
);
$pictoCategories = array_map(function($elem) { return $elem['id_category']; }, $pictoCategories);
if(in_array($id_category, $pictoCategories))
{
unset($pictos[$key]);
}
}
}
return $pictos;
}
}
public static function getPictosForProductDetails($id_product = null)
{
$context = Context::getContext();
if(!empty($id_product) && is_int($id_product))
{
$query = '
SELECT DISTINCT(advp.`id_advpicto`), `title`, `alt`, `link`
FROM `'._DB_PREFIX_.'advpicto_product` advp
INNER JOIN `'._DB_PREFIX_.'advpicto_lang` advl ON advp.`id_advpicto` = advl.`id_advpicto` AND id_lang = '. $context->language->id.'
INNER JOIN `'._DB_PREFIX_.'advpicto` ap ON ap.`id_advpicto` = advp.`id_advpicto`';
if(Shop::isFeatureActive())
{
$query .= ' INNER JOIN `'._DB_PREFIX_.'advpicto_shop` advs ON advp.`id_advpicto` = advs.`id_advpicto` AND id_shop = '. $context->shop->id;
}
$query .= ' AND advp.id_product = '. (int) $id_product;
$query .= ' ORDER BY ap.position ';
$pictos = Db::getInstance()->executeS($query);
if (empty($pictos))
{
return false;
}
foreach($pictos as $key => $picto)
{
$pictos[$key]['hasImg'] = file_exists(_PS_IMG_DIR_ .'picto/'. $picto['id_advpicto'] . '.jpg');
}
return $pictos;
}
}
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_advpicto`
FROM `'._DB_PREFIX_.'advpicto`
ORDER BY `position` ASC';
$res = Db::getInstance()->executeS($sql);
if (!$res)
return false;
foreach ($res as $row)
if ((int)$row['id_advpicto'] == (int)$this->id)
$moved_row = $row;
if (!isset($moved_row) || !isset($position))
return false;
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$res = Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advpicto`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_row['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_row['position'].' AND `position` >= '.(int)$position)
)
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advpicto`
SET `position` = '.(int)$position.'
WHERE `id_advpicto`='.(int)$moved_row['id_advpicto']
);
$this->refreshPositions();
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_advpicto`
FROM `'._DB_PREFIX_.'advpicto`
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advpicto`
SET `position` = '.(int)$pos.'
WHERE `id_advpicto`='.(int)$block['id_advpicto']);
$pos++;
}
}
}