2017-07-12 15:05:18 +02:00

137 lines
6.3 KiB
PHP

<?php
class Category extends CategoryCore
{
const CATEGORY_HOME = 2;
public $home;
public $description_univers;
public $long_description;
public $liste_icons_univers;
public static $definition = array(
'table' => 'category',
'primary' => 'id_category',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
'nleft' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'nright' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'level_depth' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
'home' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
'id_parent' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'is_root_category' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'position' => array('type' => self::TYPE_INT),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
/* Lang fields */
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'liste_icons_univers' => array('type' => self::TYPE_STRING, 'lang' => true, 'size' => 128),
'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'long_description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'description_univers' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
),
);
public static function getChildrenIds($id_parent, $id_lang, $active = true, $id_shop = false)
{
if (!Validate::isBool($active))
die(Tools::displayError());
$cache_id = 'Category::getChildren_'.(int)$id_parent.'-'.(int)$id_lang.'-'.(bool)$active.'-'.(int)$id_shop;
if (!Cache::isStored($cache_id))
{
$query = '
SELECT c.`id_category`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').')
'.Shop::addSqlAssociation('category', 'c').'
WHERE `id_lang` = '.(int)$id_lang.'
AND c.`id_parent` = '.(int)$id_parent.'
'.($active ? 'AND `active` = 1' : '').'
GROUP BY c.`id_category`
ORDER BY category_shop.`position` ASC
';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
$result = array_column($result, 'id_category');
Cache::store($cache_id, $result);
}
return Cache::retrieve($cache_id);
}
public function recurseLiteCategTree($max_depth = 3, $current_depth = 0, $id_lang = null, $excluded_ids_array = null)
{
$id_lang = is_null($id_lang) ? Context::getContext()->language->id : (int)$id_lang;
$children = array();
$subcats = $this->getSubCategories($id_lang, true);
if (($max_depth == 0 || $current_depth < $max_depth) && $subcats && count($subcats)) {
foreach ($subcats as &$subcat) {
if (!$subcat['id_category']) {
break;
} elseif (!is_array($excluded_ids_array) || !in_array($subcat['id_category'], $excluded_ids_array)) {
$categ = new Category($subcat['id_category'], $id_lang);
$children[] = $categ->recurseLiteCategTree($max_depth, $current_depth + 1, $id_lang, $excluded_ids_array);
}
}
}
// if (is_array($this->description)) {
// foreach ($this->description as $lang => $description) {
// $this->description[$lang] = Category::getDescriptionClean($description);
// }
// } else {
// $this->description = Category::getDescriptionClean($this->description);
// }
return array(
'id' => (int)$this->id,
'link' => Context::getContext()->link->getCategoryLink($this->id, $this->link_rewrite),
'name' => $this->name,
'desc'=> $this->description,
'description_univers'=> $this->description_univers,
'children' => $children
);
}
public function getChildrenCategories($limit = null)
{
$context = Context::getContext();
$query = '
SELECT DISTINCT c.*, cl.*
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON cl.`id_category` = c.`id_category`
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON cs.`id_category` = c.`id_category`
WHERE c.`id_parent` = '.$this->id.'
AND cl.`id_lang` = '.$context->language->id.'
AND cs.`id_shop` = '.$context->shop->id.'
AND cl.`id_shop` = '.$context->shop->id.'
AND c.`active` = 1
ORDER BY cs.`position` ASC
';
if($limit)
{
$query .= 'LIMIT 0, '.$limit;
}
$categories = Db::getInstance()->executeS($query);
return !$categories ? false : $categories;
}
}