Example #1
0
 static function getCategoriesTree($get_direct = false, $hide = false)
 {
     if (!CGlobal::$categoriesTree) {
         self::getCats();
         $categories = array();
         if ($get_direct) {
             $topcat = array();
             $subcatlv2 = array();
             $sql = 'SELECT * FROM category';
             if ($hide) {
                 $sql .= ' WHERE status != "HIDE" AND category.ref_id = 0';
             }
             $sql .= ' ORDER BY parent_id, position';
             $re = DB::query($sql);
             if ($re) {
                 while ($cat = mysql_fetch_assoc($re)) {
                     if ($cat['status'] == 'HIDE') {
                         $cat['name'] .= ' (đã ẩn)';
                     }
                     if ($cat['parent_id']) {
                         $subcatlv2[$cat['parent_id']][$cat['id']] = $cat;
                     } else {
                         $topcat[$cat['id']] = $cat;
                     }
                 }
             }
             $subcat = array();
             foreach ($topcat as $cat) {
                 if (isset($subcatlv2[$cat['id']])) {
                     foreach ($subcatlv2[$cat['id']] as $sc) {
                         $subcat[$sc['id']] = $sc;
                         //if current sub category level 2
                     }
                     $cat['childs'] = $subcat;
                     $categories[$cat['id']] = $cat;
                     $subcat = array();
                 }
             }
         } else {
             if (CGlobal::$subCategories) {
                 $subcat = CGlobal::$subCategories;
                 foreach ($subcat as $id => $sub_cat) {
                     if (isset(CGlobal::$allCategories[$id])) {
                         $cat = CGlobal::$allCategories[$id];
                         if ($cat['ref_id'] == 0) {
                             $childs = array();
                             foreach ($sub_cat as $sid => $cat2) {
                                 if ($cat2['ref_id'] == 0) {
                                     $childs[$sid] = $cat2;
                                 }
                             }
                             $cat['childs'] = $childs;
                             $categories[$id] = $cat;
                         }
                     }
                 }
             }
         }
         CGlobal::$categoriesTree = $categories;
     }
     //echo "<pre>";
     //print_r(CGlobal::$categoriesTree);
     return CGlobal::$categoriesTree;
 }