2017-07-26 10:47:30 +02:00

135 lines
5.1 KiB
PHP

<?php
class AdvSiteReview extends ObjectModel {
public $id_comment;
public $position;
public $rank;
public $active;
public $firstname;
public $lastname;
public $content;
public $title;
public $place;
public $date;
public static $definition = array(
'table' => 'advsitereviews',
'primary' => 'id_comment',
'multilang' => TRUE,
'fields' => array(
'id_comment' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'rank' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
'active' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
// Lang fields
'firstname' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'lastname' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'lastname' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'date' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'title' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'place' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isGenericName', 'size' => 255),
'content' => array('type' => self::TYPE_STRING, 'lang' => TRUE, 'validate' => 'isCleanHtml')
)
);
public function add($null_values = false, $autodate = true)
{
$result = parent::add($null_values, $autodate);
Hook::exec('actionRefreshReviews');
return $result;
}
public function update($null_values = FALSE) {
$result = parent::update($null_values);
Hook::exec('actionRefreshReviews');
return $result;
}
public function delete($null_values = FALSE) {
$result = parent::delete($null_values);
Hook::exec('actionRefreshReviews');
return $result;
}
public static function getReviews()
{
$context = Context::getContext();
$query = 'SELECT * FROM `'._DB_PREFIX_.'advsitereviews` adv JOIN `'._DB_PREFIX_.'advsitereviews_lang` advl ON adv.`id_comment` = advl.`id_comment` AND id_lang = '. (int)$context->language->id;
if (Shop::isFeatureActive()) {
$query .= ' JOIN `'._DB_PREFIX_.'advsitereviews_shop` advs ON adv.`id_comment` = advs.`id_comment` AND id_shop = '.(int)$context->shop->id;
}
$query .= ' WHERE adv.`active` = 1 ORDER BY `position` ASC';
$reviews = Db::getInstance()->executeS($query);
return $reviews;
}
public function cleanPositions(){
return Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'advsitereviews`
SET `position`= `position` - 1
WHERE `id_comment` = '.(int)$this->id_comment.'
AND `position` > '.(int)$this->position);
}
public function updatePosition($way, $position)
{
$sql = 'SELECT `position`, `id_comment`
FROM `'._DB_PREFIX_.'advsitereviews`
ORDER BY `position` ASC';
if (!$res = Db::getInstance()->executeS($sql))
return false;
foreach ($res as $row)
if ((int)$row['id_comment'] == (int)$this->id_comment)
$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_.'advsitereviews`
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_.'advsitereviews`
SET `position` = '.(int)$position.'
WHERE `id_comment`='.(int)$moved_row['id_comment']
);
$this->refreshPositions();
Hook::exec('actionRefreshReviews');
return $res;
}
public function refreshPositions(){
$sql = 'SELECT `id_comment`
FROM `'._DB_PREFIX_.'advsitereviews`
ORDER BY `position` ASC';
if (!$blocks = Db::getInstance()->executeS($sql))
return false;
$pos=0;
foreach ($blocks as $block) {
Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'advsitereviews`
SET `position` = '.(int)$pos.'
WHERE `id_comment`='.(int)$block['id_comment']);
$pos++;
}
}
}