139 lines
4.7 KiB
PHP
Raw Normal View History

2017-07-06 17:41:10 +02:00
<?php
class AdvRea2 extends ObjectModel {
public $id_reassurance2;
public $position;
public $active;
public $title;
public $subtitle;
public $url;
public static $definition = array(
'table' => 'advreassurance2',
'primary' => 'id_reassurance2',
'multilang' => TRUE,
'fields' => array(
'id_reassurance2' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
// Lang fields
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'subtitle' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isCleanHtml'),
'url' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isUrl', 'size' => 255)
)
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
$this->image_dir = _PS_IMG_DIR_ . 'reassurance2/';
}
public function add($null_values = false, $autodate = true)
{
$result = parent::add($null_values, $autodate);
Hook::exec('actionRefreshReassurance2');
return $result;
}
public function update($null_values = FALSE) {
$result = parent::update($null_values);
Hook::exec('actionRefreshReassurance2');
return $result;
}
public function delete($null_values = FALSE) {
$result = parent::delete($null_values);
Hook::exec('actionRefreshReassurance2');
return $result;
}
public static function getBlocks()
{
$context = Context::getContext();
$query = '
2017-07-06 17:41:10 +02:00
SELECT *
FROM `'._DB_PREFIX_.'advreassurance2` adv
JOIN `'._DB_PREFIX_.'advreassurance2_lang` advl ON adv.`id_reassurance2` = advl.`id_reassurance2` AND id_lang = '. (int)$context->language->id;
if (Shop::isFeatureActive()) {
$query .= ' JOIN `'._DB_PREFIX_.'advreassurance2_shop` advs ON adv.`id_reassurance2` = advs.`id_reassurance2` AND id_shop = '. (int)$context->shop->id;
}
$query .= '
2017-07-06 17:41:10 +02:00
WHERE adv.`active` = 1
ORDER BY `position` ASC
';
$reassurances = Db::getInstance()->executeS($query);
return $reassurances;
2017-07-06 17:41:10 +02:00
}
public function cleanPositions(){
return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'advreassurance2`
SET `position`= `position` - 1
WHERE `id_reassurance2` = '.(int)$this->id_reassurance2.'
AND `position` > '.(int)$this->position);
}
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_reassurance2`
FROM `'._DB_PREFIX_.'advreassurance2`
ORDER BY `position` ASC';
if (!$res = Db::getInstance()->executeS($sql))
return false;
foreach ($res as $row)
if ((int)$row['id_reassurance2'] == (int)$this->id_reassurance2)
$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_.'advreassurance2`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
AND `position`
'.($way
? '> '.(int)$moved_row['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_row['position'].' AND `position` >= '.(int)$position)
)
&& Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advreassurance2`
SET `position` = '.(int)$position.'
WHERE `id_reassurance2`='.(int)$moved_row['id_reassurance2']
);
$this->refreshPositions();
Hook::exec('actionRefreshReassurance2');
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_reassurance2`
FROM `'._DB_PREFIX_.'advreassurance2`
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advreassurance2`
SET `position` = '.(int)$pos.'
WHERE `id_reassurance2`='.(int)$block['id_reassurance2']);
$pos++;
}
}
}