From 7d61be252fcca9520a09c92ead0369565302926f Mon Sep 17 00:00:00 2001 From: Jerome Nadaud Date: Tue, 3 Sep 2013 15:27:58 +0200 Subject: [PATCH] [+] BO : Get categories in nested array --- classes/Category.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/classes/Category.php b/classes/Category.php index a5fea0fbd..ee0bd87c4 100644 --- a/classes/Category.php +++ b/classes/Category.php @@ -105,8 +105,6 @@ class CategoryCore extends ObjectModel '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' => 64), 'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 64), 'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'), @@ -479,6 +477,39 @@ class CategoryCore extends ObjectModel return $categories; } + public static function getNestedCategories($id_lang = false, $active = true, $sql_filter = '', $sql_sort = '', $sql_limit = '') + { + if (!Validate::isBool($active)) + die(Tools::displayError()); + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT * + FROM `'._DB_PREFIX_.'category` c + '.Shop::addSqlAssociation('category', 'c').' + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').' + WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)$id_lang : '').' + '.($active ? 'AND `active` = 1' : '').' + '.(!$id_lang ? 'GROUP BY c.id_category' : '').' + '.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, category_shop.`position` ASC').' + '.($sql_limit != '' ? $sql_limit : '') + ); + + $categories = array(); + $buff = array(); + + foreach ($result as $row) + { + $current = &$buff[$row['id_category']]; + $current = $row; + + if ($row['id_parent'] == 0) + $categories[$row['id_category']] = &$current; + else + $buff[$row['id_parent']]['children'][$row['id_category']] = &$current; + } + + return $categories; + } + public static function getSimpleCategories($id_lang) { return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('