2017-07-07 20:01:01 +02:00

170 lines
5.9 KiB
PHP

<?php
class AdvSlide2 extends ObjectModel {
public $id_slide;
public $position;
public $active;
public $title;
public $subtitle;
public $label;
public $url;
public $description;
public static $definition = array(
'table' => 'advslider2',
'primary' => 'id_slide',
'multilang' => TRUE,
'fields' => array(
'id_slide' => 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_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'label' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'url' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isUrl', 'size' => 255),
'description' => array('type' => self::TYPE_HTML, 'lang' => TRUE, 'validate' => 'isCleanHtml')
)
);
public function __construct($id = NULL, $id_lang = NULL, $id_shop = NULL) {
parent::__construct($id, $id_lang, $id_shop);
$this->image_dir = _PS_IMG_DIR_ . 'slider2/';
}
public function add($null_values = false, $autodate = true)
{
$result = parent::add($null_values, $autodate);
Hook::exec('actionRefreshSlider');
return $result;
}
public function update($null_values = FALSE) {
$result = parent::update($null_values);
Hook::exec('actionRefreshSlider');
return $result;
}
public function delete($null_values = FALSE) {
$result = parent::delete($null_values);
Hook::exec('actionRefreshSlider');
return $result;
}
public static function getSlides()
{
$context = Context::getContext();
$slides = Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'advslider2` adv
JOIN `'._DB_PREFIX_.'advslider2_lang` advl ON adv.`id_slide` = advl.`id_slide` AND id_lang = '. (int)$context->language->id . '
WHERE adv.`active` = 1
ORDER BY `position` ASC
');
return $slides;
}
public function deleteImage($force_delete = false)
{
if (!$this->id) {
return false;
}
$imgToDelete = Tools::getValue('imgName', false);
if ($imgToDelete === false) {
return parent::deleteImage($force_delete = false);
}
if ($force_delete || !$this->hasMultishopEntries()) {
/* Deleting object images and thumbnails (cache) */
if ($this->image_dir) {
if (file_exists($this->image_dir.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink($this->image_dir.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
}
if (file_exists(_PS_TMP_IMG_DIR_.$this->def['table'].'_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink(_PS_TMP_IMG_DIR_.$this->def['table'].'_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
if (file_exists(_PS_TMP_IMG_DIR_.$this->def['table'].'_mini_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)
&& !unlink(_PS_TMP_IMG_DIR_.$this->def['table'].'_mini_'.$this->id.'-'.$imgToDelete.'.'.$this->image_format)) {
return false;
}
}
return true;
}
public function cleanPositions(){
return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'advslider`
SET `position`= `position` - 1
WHERE `id_slide` = '.(int)$this->id_slide.'
AND `position` > '.(int)$this->position);
}
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_slide`
FROM `'._DB_PREFIX_.'advslider2`
ORDER BY `position` ASC';
if (!$res = Db::getInstance()->executeS($sql))
return false;
foreach ($res as $row)
if ((int)$row['id_slide'] == (int)$this->id_slide)
$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_.'advslider2`
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_.'advslider2`
SET `position` = '.(int)$position.'
WHERE `id_slide`='.(int)$moved_row['id_slide']
);
$this->refreshPositions();
Hook::exec('actionRefreshSlider');
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_slide`
FROM `'._DB_PREFIX_.'advslider2`
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advslider2`
SET `position` = '.(int)$pos.'
WHERE `id_slide`='.(int)$block['id_slide']);
$pos++;
}
}
}