139 lines
4.8 KiB
PHP
139 lines
4.8 KiB
PHP
<?php
|
|
|
|
class GuidePost extends ObjectModel
|
|
{
|
|
public $id;
|
|
public $id_guide_post;
|
|
public $id_guide_category;
|
|
public $position;
|
|
public $active;
|
|
public $meta_title;
|
|
public $meta_description;
|
|
public $meta_keywords;
|
|
public $content;
|
|
public $link_rewrite;
|
|
|
|
protected $fieldsValidate = array('id_guide_category' => 'isUnsignedInt');
|
|
protected $fieldsRequiredLang = array('meta_title', 'link_rewrite');
|
|
protected $fieldsSizeLang = array('meta_description' => 255, 'meta_keywords' => 255, 'meta_title' => 128, 'link_rewrite' => 128, 'content' => 3999999999999);
|
|
protected $fieldsValidateLang = array('meta_description' => 'isGenericName', 'meta_keywords' => 'isGenericName', 'meta_title' => 'isGenericName', 'link_rewrite' => 'isLinkRewrite', 'content' => 'isString');
|
|
|
|
protected $table = 'guide_post';
|
|
protected $identifier = 'id_guide_post';
|
|
|
|
protected $webserviceParameters = array(
|
|
'objectNodeName' => 'content',
|
|
'objectsNodeName' => 'content_management_system',
|
|
);
|
|
|
|
public function getFields()
|
|
{
|
|
parent::validateFields();
|
|
$fields['id_guide_post'] = (int)($this->id);
|
|
$fields['id_guide_category'] = (int)($this->id_guide_category);
|
|
$fields['position'] = (int)($this->position);
|
|
$fields['active'] = (int)($this->active);
|
|
return $fields;
|
|
}
|
|
|
|
public function getTranslationsFieldsChild()
|
|
{
|
|
parent::validateFieldsLang();
|
|
|
|
$fieldsArray = array('meta_title', 'meta_description', 'meta_keywords', 'link_rewrite');
|
|
$fields = array();
|
|
$languages = Language::getLanguages(false);
|
|
$defaultLanguage = (int)(Configuration::get('PS_LANG_DEFAULT'));
|
|
foreach ($languages as $language)
|
|
{
|
|
$fields[$language['id_lang']]['id_lang'] = (int)($language['id_lang']);
|
|
$fields[$language['id_lang']][$this->identifier] = (int)($this->id);
|
|
$fields[$language['id_lang']]['content'] = (isset($this->content[$language['id_lang']])) ? pSQL($this->content[$language['id_lang']], true) : '';
|
|
foreach ($fieldsArray as $field)
|
|
{
|
|
if (!Validate::isTableOrIdentifier($field))
|
|
die(Tools::displayError());
|
|
if (isset($this->{$field}[$language['id_lang']]) AND !empty($this->{$field}[$language['id_lang']]))
|
|
$fields[$language['id_lang']][$field] = pSQL($this->{$field}[$language['id_lang']]);
|
|
elseif (in_array($field, $this->fieldsRequiredLang))
|
|
$fields[$language['id_lang']][$field] = pSQL($this->{$field}[$defaultLanguage]);
|
|
else
|
|
$fields[$language['id_lang']][$field] = '';
|
|
}
|
|
}
|
|
return $fields;
|
|
}
|
|
|
|
public function updatePosition($way, $position)
|
|
{
|
|
if (!$res = Db::getInstance()->ExecuteS('
|
|
SELECT cp.`id_guide_post`, cp.`position`, cp.`id_guide_category`
|
|
FROM `'._DB_PREFIX_.'guide_post` cp
|
|
WHERE cp.`id_guide_category` = '.(int)$this->id_guide_category.'
|
|
ORDER BY cp.`position` ASC'
|
|
))
|
|
return false;
|
|
|
|
foreach ($res AS $guide_post)
|
|
if ((int)($guide_post['id_guide_post']) == (int)($this->id))
|
|
$movedPost = $guide_post;
|
|
|
|
if (!isset($movedPost) || !isset($position))
|
|
return false;
|
|
|
|
// < and > statements rather than BETWEEN operator
|
|
// since BETWEEN is treated differently according to databases
|
|
return (Db::getInstance()->Execute('
|
|
UPDATE `'._DB_PREFIX_.'guide_post`
|
|
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
|
|
WHERE `position`
|
|
'.($way
|
|
? '> '.(int)($movedPost['position']).' AND `position` <= '.(int)($position)
|
|
: '< '.(int)($movedPost['position']).' AND `position` >= '.(int)($position)).'
|
|
AND `id_guide_category`='.(int)($movedPost['id_guide_category']))
|
|
AND Db::getInstance()->Execute('
|
|
UPDATE `'._DB_PREFIX_.'guide_post`
|
|
SET `position` = '.(int)($position).'
|
|
WHERE `id_guide_post` = '.(int)($movedPost['id_guide_post']).'
|
|
AND `id_guide_category`='.(int)($movedPost['id_guide_category'])));
|
|
}
|
|
|
|
public function add($autodate = true, $nullValues = false)
|
|
{
|
|
$this->position = self::getLastPosition((int)$this->id_guide_category);
|
|
return parent::add($autodate, $nullValues);
|
|
}
|
|
|
|
public function update($autodate = true, $nullValues = false)
|
|
{
|
|
$this->position = self::cleanupPositions((int)$this->id_guide_category);
|
|
return parent::update($autodate, $nullValues);
|
|
}
|
|
|
|
public static function getLastPosition($id_guide_category)
|
|
{
|
|
$position = Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'guide_post` WHERE `id_guide_category` = '.(int)($id_guide_category));
|
|
return $position?:1;
|
|
}
|
|
|
|
private function cleanupPositions($id_guide_category)
|
|
{
|
|
$sql = 'SELECT `id_guide_post`
|
|
FROM `'._DB_PREFIX_.'guide_post`
|
|
WHERE `id_guide_category` = '.intval($id_guide_category).'
|
|
ORDER BY `position` ASC';
|
|
$rows = Db::getInstance()->ExecuteS($sql);
|
|
|
|
$position = 1;
|
|
foreach($rows as $row) {
|
|
$sql = 'UPDATE `'._DB_PREFIX_.'guide_post`
|
|
SET `position` = '.$position.'
|
|
WHERE `id_guide_post` = '.$row['id_guide_post'];
|
|
Db::getInstance()->Execute($sql);
|
|
$position++;
|
|
}
|
|
return $position;
|
|
}
|
|
}
|
|
|