303 lines
7.7 KiB
PHP
303 lines
7.7 KiB
PHP
<?php
|
|
|
|
class GuideCategory extends ObjectModel
|
|
{
|
|
public $id;
|
|
public $id_guide_category;
|
|
public $name;
|
|
public $active = 1;
|
|
public $description;
|
|
public $id_parent;
|
|
public $id_category_family;
|
|
public $position;
|
|
public $level_depth;
|
|
public $link_rewrite;
|
|
public $meta_title;
|
|
public $meta_keywords;
|
|
public $meta_description;
|
|
|
|
protected $table = 'guide_category';
|
|
protected $identifier = 'id_guide_category';
|
|
|
|
protected $tables = array ('guide_category', 'guide_category_lang');
|
|
|
|
protected $fieldsRequired = array('id_parent', 'active');
|
|
protected $fieldsSize = array('id_parent' => 10, 'active' => 1);
|
|
protected $fieldsValidate = array('active' => 'isBool', 'id_parent' => 'isUnsignedInt');
|
|
protected $fieldsRequiredLang = array('name', 'link_rewrite');
|
|
protected $fieldsSizeLang = array(
|
|
'name' => 64,
|
|
'link_rewrite' => 64,
|
|
'meta_title' => 128,
|
|
'meta_description' => 255,
|
|
'meta_keywords' => 255
|
|
);
|
|
protected $fieldsValidateLang = array(
|
|
'name' => 'isCatalogName',
|
|
'link_rewrite' => 'isLinkRewrite',
|
|
'description' => 'isCleanHtml',
|
|
'meta_title' => 'isGenericName',
|
|
'meta_description' => 'isGenericName',
|
|
'meta_keywords' => 'isGenericName'
|
|
);
|
|
|
|
private $error;
|
|
|
|
public function getFields()
|
|
{
|
|
parent::validateFields();
|
|
if (isset($this->id))
|
|
$fields['id_guide_category'] = (int)($this->id);
|
|
$fields['id_category_family'] = (int)($this->id_category_family);
|
|
$fields['active'] = (int)($this->active);
|
|
$fields['id_parent'] = (int)($this->id_parent);
|
|
$fields['position'] = (int)($this->position);
|
|
$fields['level_depth'] = (int)($this->level_depth);
|
|
return $fields;
|
|
}
|
|
|
|
public static function getSubPath()
|
|
{
|
|
return 'modules/purchaseguide/img';
|
|
}
|
|
|
|
public function getImageFilePath()
|
|
{
|
|
if ($this->id) {
|
|
return _PS_ROOT_DIR_.'/'.self::getSubPath().'/'.$this->id.'.jpg';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public function getImageFileUrl()
|
|
{
|
|
if ($this->id) {
|
|
return __PS_BASE_URI__.self::getSubPath().'/'.$this->id.'.jpg';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public static function findCategoriesTree($id_lang, $exclude_id=0)
|
|
{
|
|
$sql = "SELECT DISTINCT a.`id_guide_category` as id_guide_category, `name`
|
|
FROM `"._DB_PREFIX_."guide_category` a
|
|
JOIN `"._DB_PREFIX_."guide_category_lang` b
|
|
ON a.`id_guide_category` = b.`id_guide_category`
|
|
WHERE b.id_lang = '".intval($id_lang)."'
|
|
AND a.`id_parent` = '%s'
|
|
";
|
|
if ($exclude_id!='0') {
|
|
$sql .= " AND a.`id_guide_category` <> '".intval($exclude_id)."'";
|
|
}
|
|
$rows = array();
|
|
self::findRecursiveData(
|
|
$rows,
|
|
$sql,
|
|
array(
|
|
'id_parent' => 0,
|
|
'identifier' => 'id_guide_category',
|
|
'level' => -1
|
|
)
|
|
);
|
|
return $rows;
|
|
}
|
|
|
|
public static function findFamiliesTree($id_lang, $exclude_id=0)
|
|
{
|
|
$sql = "SELECT DISTINCT a.`id_category_family` as id_category_family, `name`
|
|
FROM `"._DB_PREFIX_."category_family` a
|
|
JOIN `"._DB_PREFIX_."category_family_lang` b
|
|
ON a.`id_category_family` = b.`id_category_family`
|
|
WHERE b.id_lang = '".intval($id_lang)."'
|
|
AND a.`id_parent` = '%d'
|
|
";
|
|
if ($exclude_id!='0') {
|
|
$sql .= " AND a.`id_category_family` <> '".intval($exclude_id)."'";
|
|
}
|
|
$rows = array();
|
|
self::findRecursiveData(
|
|
$rows,
|
|
$sql,
|
|
array(
|
|
'id_parent' => 0,
|
|
'identifier' => 'id_category_family',
|
|
'level' => -1
|
|
)
|
|
);
|
|
return $rows;
|
|
}
|
|
|
|
|
|
private static function findRecursiveData(
|
|
&$rows,
|
|
$sql,
|
|
array $params
|
|
)
|
|
{
|
|
$level = $params['level'];
|
|
$level++;
|
|
|
|
$resuts = Db::getInstance()->ExecuteS(sprintf($sql, intval($params['id_parent'])));
|
|
foreach($resuts as &$row) {
|
|
$row['level'] = $level;
|
|
$rows[] = $row;
|
|
self::findRecursiveData(
|
|
$rows,
|
|
$sql,
|
|
array(
|
|
'id_parent' => $row[$params['identifier']],
|
|
'identifier' => $params['identifier'],
|
|
'level' => $level
|
|
)
|
|
);
|
|
}
|
|
}
|
|
|
|
/////
|
|
// Check then return multilingual fields for database interaction
|
|
//
|
|
// @return array Multilingual fields
|
|
///
|
|
public function getTranslationsFieldsChild()
|
|
{
|
|
parent::validateFieldsLang();
|
|
return parent::getTranslationsFields(array('name', 'description', 'link_rewrite', 'meta_title', 'meta_keywords', 'meta_description'));
|
|
}
|
|
|
|
public function add($autodate = true, $nullValues = false)
|
|
{
|
|
$this->position = self::getLastPosition((int)$this->id_parent);
|
|
return parent::add($autodate, $nullValues);
|
|
}
|
|
|
|
public function update($autodate = true, $nullValues = false)
|
|
{
|
|
$this->position = self::cleanupPositions((int)$this->id_parent);
|
|
return parent::update($autodate, $nullValues);
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
if ($this->getNbSubCategories($this->id)) {
|
|
$this->error = 1;
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
if ($this->getNbPosts($this->id)) {
|
|
$this->error = 2;
|
|
return FALSE;
|
|
}
|
|
|
|
if (!parent::delete()) {
|
|
$this->error = 0;
|
|
return false;
|
|
}
|
|
|
|
@unlink($this->getImageFilePath());
|
|
|
|
$this->cleanupPositions($this->id_parent);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function deleteSelection($selection)
|
|
{
|
|
if (!is_array($selection) OR !Validate::isTableOrIdentifier($this->identifier) OR !Validate::isTableOrIdentifier($this->table))
|
|
die(Tools::displayError());
|
|
$result = true;
|
|
foreach ($selection AS $id)
|
|
{
|
|
$this->id = (int)($id);
|
|
$result2 = $this->delete();
|
|
$result = $result && $this->delete();
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function getDeleteError()
|
|
{
|
|
return $this->error;
|
|
}
|
|
|
|
public function updatePosition($way, $position)
|
|
{
|
|
if (!$res = Db::getInstance()->ExecuteS('
|
|
SELECT cp.`id_guide_category`, cp.`position`, cp.`id_parent`
|
|
FROM `'._DB_PREFIX_.'guide_category` cp
|
|
WHERE cp.`id_parent` = '.(int)$this->id_parent.'
|
|
ORDER BY cp.`position` ASC'
|
|
))
|
|
return false;
|
|
|
|
foreach ($res AS $category)
|
|
if ((int)($category['id_guide_category']) == (int)($this->id))
|
|
$movedCategory = $category;
|
|
|
|
if (!isset($movedCategory) || !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_category`
|
|
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
|
|
WHERE `position`
|
|
'.($way
|
|
? '> '.(int)($movedCategory['position']).' AND `position` <= '.(int)($position)
|
|
: '< '.(int)($movedCategory['position']).' AND `position` >= '.(int)($position)).'
|
|
AND `id_parent`='.(int)($movedCategory['id_parent']))
|
|
AND Db::getInstance()->Execute('
|
|
UPDATE `'._DB_PREFIX_.'guide_category`
|
|
SET `position` = '.(int)($position).'
|
|
WHERE `id_parent` = '.(int)($movedCategory['id_parent']).'
|
|
AND `id_guide_category`='.(int)($movedCategory['id_guide_category'])));
|
|
}
|
|
|
|
|
|
public static function getLastPosition($id_parent)
|
|
{
|
|
return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'guide_category` WHERE `id_parent` = '.(int)($id_parent)));
|
|
}
|
|
|
|
|
|
private function getNbSubCategories($id_guide_category)
|
|
{
|
|
$sql = 'SELECT COUNT(*)
|
|
FROM `'._DB_PREFIX_.'guide_category`
|
|
WHERE `id_parent` = '.intval($id_guide_category);
|
|
return Db::getInstance()->getValue($sql);
|
|
}
|
|
|
|
|
|
private function getNbPosts($id_guide_category)
|
|
{
|
|
$sql = 'SELECT COUNT(*)
|
|
FROM `'._DB_PREFIX_.'guide_post`
|
|
WHERE `id_guide_category` = '.intval($id_guide_category);
|
|
return Db::getInstance()->getValue($sql);
|
|
}
|
|
|
|
|
|
private function cleanupPositions($id_parent)
|
|
{
|
|
$sql = 'SELECT `id_guide_category`
|
|
FROM `'._DB_PREFIX_.'guide_category`
|
|
WHERE `id_parent` = '.intval($id_parent).'
|
|
ORDER BY `position` ASC';
|
|
$rows = Db::getInstance()->ExecuteS($sql);
|
|
|
|
$position = 1;
|
|
foreach($rows as $row) {
|
|
$sql = 'UPDATE `'._DB_PREFIX_.'guide_category`
|
|
SET `position` = '.$position.'
|
|
WHERE `id_guide_category` = '.$row['id_guide_category'];
|
|
Db::getInstance()->Execute($sql);
|
|
$position++;
|
|
}
|
|
return $position;
|
|
}
|
|
|
|
}
|
|
|
|
|