* @copyright 2007-2015 Orange35 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/ */ class AdvancedHotspotSlide extends ObjectModel { public $title; public $url; public $image; public $active; public $position; /** * @see ObjectModel::$definition */ public static $definition = array( 'table' => 'o35_ahslider_slides', 'primary' => 'id_homeslider_slides', 'multilang' => true, 'fields' => array( 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true), 'position' => array('type' => self::TYPE_INT, 'validate' => 'isunsignedInt', 'required' => true), 'title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 255), /* Lang fields*/ 'url' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isUrl', 'size' => 255), /* Lang fields*/ 'image' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 255), /* Lang fields*/ ) ); public $table_alias = ''; public $table_name = ''; public $slides_table_name = ''; public $slides_lang_table_name = ''; public function __construct($table_alias, $id_slide = null, $id_lang = null, $id_shop = null) { $this->initTables($table_alias); parent::__construct($id_slide, $id_lang, $id_shop); } protected function initTables($table_alias) { $this->table_alias = $table_alias; $this->table_name = _DB_PREFIX_.$this->table_alias; $this->slides_table_name = $this->table_name.'_slides'; $this->slides_lang_table_name = $this->table_name.'_lang'; return $this; } public function add($autodate = true, $null_values = false) { $context = Context::getContext(); $id_shop = $context->shop->id; $res = parent::add($autodate, $null_values); $res &= Db::getInstance()->execute(' INSERT INTO `'.$this->table_name.'` (`id_shop`, `id_homeslider_slides`) VALUES('.(int)$id_shop.', '.(int)$this->id.')' ); return $res; } public function delete() { $res = true; $images = $this->image; foreach ($images as $image) { if (preg_match('/sample/', $image) === 0) if ($image && file_exists(dirname(__FILE__).'/../views/img/slides/'.$image)) $res &= @unlink(dirname(__FILE__).'/../views/img/slides/'.$image); } $res &= $this->reOrderPositions(); $res &= Db::getInstance()->execute(' DELETE FROM `'.$this->table_name.'` WHERE `id_homeslider_slides` = '.(int)$this->id ); $res &= parent::delete(); return $res; } public function reOrderPositions() { $id_slide = $this->id; $context = Context::getContext(); $id_shop = $context->shop->id; $max = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT MAX(hss.`position`) as position FROM `'.$this->slides_table_name.'` hss, `'.$this->table_name.'` hs WHERE hss.`id_homeslider_slides` = hs.`id_homeslider_slides` AND hs.`id_shop` = '.(int)$id_shop ); if ((int)$max == (int)$id_slide) return true; $rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT hss.`position` as position, hss.`id_homeslider_slides` as id_slide FROM `'.$this->slides_table_name.'` hss LEFT JOIN `'.$this->table_name.'` hs ON (hss.`id_homeslider_slides` = hs.`id_homeslider_slides`) WHERE hs.`id_shop` = '.(int)$id_shop.' AND hss.`position` > '.(int)$this->position ); foreach ($rows as $row) { $current_slide = new AdvancedHotspotSlide($this->table_alias, $row['id_slide']); --$current_slide->position; $current_slide->update(); unset($current_slide); } return true; } }