Michael RICOIS 006edad609 Breadcrumb
2017-11-14 11:25:17 +01:00

384 lines
10 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 'img/purchaseguide';
}
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
));
}
}
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;
}
public function findByCategoryFamily($id, $id_lang)
{
$sql = "SELECT gc.*, gcl.name FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_category_family=".$id;
$row = Db::getInstance()->getRow($sql);
return $row;
}
public function getLinkRewrite($id_lang)
{
if ($this->id) {
return $this->id.'-'.$this->link_rewrite[$id_lang];
}
return '';
}
public function getTree($id_lang, $active = 1)
{
return $this->recurseData(0, $id_lang, $active);
}
protected function recurseData($id_category, $id_lang, $active = 1)
{
$data = array();
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_parent=".$id_category." ORDER BY position ASC";
$result = Db::getInstance()->ExecuteS($sql);
if (count($result) > 0) {
foreach ($result as $c) {
$rewrite = 'guide/';
if ($c['id_parent'] != 0) {
$parent = $this->getParent($c['id_guide_category'], $id_lang);
$rewrite.= $parent['link_rewrite'].'/';
}
$rewrite.= $c['id_guide_category'].'-'.$c['link_rewrite'];
$item = array(
'id_guide_category' => $c['id_guide_category'],
'name' => $c['name'],
'rewrite' => $rewrite,
'children' => $this->recurseData($c['id_guide_category'], $id_lang),
);
$data[] = $item;
}
}
return $data;
}
public function getParent($id_category, $id_lang)
{
$sql = "SELECT gc.*, gcl.name, gcl.link_rewrite
FROM `"._DB_PREFIX_."guide_category` gc, `"._DB_PREFIX_."guide_category_lang` gcl
WHERE gc.id_guide_category=gcl.id_guide_category AND gcl.id_lang=".$id_lang.
" AND gc.id_guide_category=".$id_category;
$result = Db::getInstance()->getRow($sql);
return $result;
}
public static function getRoot($id_lang)
{
$sql = "SELECT gc.id_guide_category, gcl.name, gcl.link_rewrite, gcl.meta_title,
gcl.meta_description, gcl.meta_keywords
FROM ps_guide_category gc, ps_guide_category_lang gcl
WHERE gc.active=1 AND gc.id_guide_category=gcl.id_guide_category
AND gc.id_parent=0 AND gcl.id_lang=".$id_lang;
$result = Db::getInstance()->ExecuteS($sql);
return $result;
}
}