'advreassurance4', 'primary' => 'id_reassurance', 'multilang' => TRUE, 'fields' => array( 'id_reassurance' => 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_ . 'reassurance4/'; } public function add($null_values = false, $autodate = true) { $result = parent::add($null_values, $autodate); Hook::exec('actionRefreshreassurance4'); return $result; } public function update($null_values = FALSE) { $result = parent::update($null_values); Hook::exec('actionRefreshreassurance4'); return $result; } public function delete($null_values = FALSE) { $result = parent::delete($null_values); Hook::exec('actionRefreshreassurance4'); return $result; } public static function getBlocks() { $context = Context::getContext(); $reassurance4 = Db::getInstance()->executeS(' SELECT * FROM `'._DB_PREFIX_.'advreassurance4` adv JOIN `'._DB_PREFIX_.'advreassurance4_lang` advl ON adv.`id_reassurance` = advl.`id_reassurance` AND id_lang = '. (int)$context->language->id . ' WHERE adv.`active` = 1 ORDER BY `position` ASC '); return $reassurance4; } public function cleanPositions(){ return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'advreassurance4` SET `position`= `position` - 1 WHERE `id_reassurance` = '.(int)$this->id_reassurance.' AND `position` > '.(int)$this->position); } public function updatePosition($way, $position) { $sql = 'SELECT `position`, `id_reassurance` FROM `'._DB_PREFIX_.'advreassurance4` ORDER BY `position` ASC'; if (!$res = Db::getInstance()->executeS($sql)) return false; foreach ($res as $row) if ((int)$row['id_reassurance'] == (int)$this->id_reassurance) $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_.'advreassurance4` 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_.'advreassurance4` SET `position` = '.(int)$position.' WHERE `id_reassurance`='.(int)$moved_row['id_reassurance'] ); $this->refreshPositions(); Hook::exec('actionRefreshreassurance4'); return $res; } public function refreshPositions(){ $sql = 'SELECT `id_reassurance` FROM `'._DB_PREFIX_.'advreassurance4` ORDER BY `position` ASC'; if (!$blocks = Db::getInstance()->executeS($sql)) return false; $pos=0; foreach ($blocks as $block) { Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'advreassurance4` SET `position` = '.(int)$pos.' WHERE `id_reassurance`='.(int)$block['id_reassurance']); $pos++; } } }