/**
  * Utility Function:
  * Create the globalcats category tree, the result of this function is cached
  *
  * @access private
  * @return array
  */
 static function getCategoriesTree()
 {
     global $globalcats;
     $db = JFactory::getDBO();
     $ROOT_CATEGORY_ID = 1;
     $_nowDate = 'UTC_TIMESTAMP()';
     $nullDate = $db->getNullDate();
     // get the category tree and append the ancestors to each node
     $query = 'SELECT c.id, c.parent_id, c.published, c.access, c.title, c.level, c.lft, c.rgt, c.language,' . '  CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END AS slug,' . '  COUNT(rel.itemid) AS numitems' . ' FROM #__categories as c' . ' LEFT JOIN #__flexicontent_cats_item_relations AS rel ON c.id=rel.catid' . ' LEFT JOIN #__content AS i ON rel.itemid=i.id ' . '  AND i.state IN (1,-5) ' . '  AND ( i.publish_up = ' . $db->Quote($nullDate) . ' OR i.publish_up <= ' . $_nowDate . ' )' . '  AND ( i.publish_down = ' . $db->Quote($nullDate) . ' OR i.publish_down >= ' . $_nowDate . ' )' . " WHERE c.extension='" . FLEXI_CAT_EXTENSION . "' AND c.lft > '" . FLEXI_LFT_CATEGORY . "' AND c.rgt < '" . FLEXI_RGT_CATEGORY . "'" . ' GROUP BY c.id' . ' ORDER BY c.parent_id, c.lft';
     $db->setQuery($query);
     $cats = $db->loadObjectList();
     //establish the hierarchy of the categories
     $children = array();
     $parents = array();
     //set depth limit
     $levellimit = 30;
     foreach ($cats as $child) {
         $parent = $child->parent_id;
         if ($parent) {
             $parents[] = $parent;
         }
         $list = @$children[$parent] ? $children[$parent] : array();
         array_push($list, $child);
         $children[$parent] = $list;
     }
     $parents = array_unique($parents);
     //get list of the items
     $globalcats = plgSystemFlexisystem::_getCatAncestors($ROOT_CATEGORY_ID, '', array(), $children, true, max(0, $levellimit - 1));
     foreach ($globalcats as $cat) {
         $cat->ancestorsonlyarray = $cat->ancestors;
         $cat->ancestorsonly = implode(',', $cat->ancestors);
         $cat->ancestors[] = $cat->id;
         $cat->ancestorsarray = $cat->ancestors;
         $cat->ancestors = implode(',', $cat->ancestors);
         $cat->descendantsarray = plgSystemFlexisystem::_getDescendants($cat);
         $cat->totalitems = plgSystemFlexisystem::_getItemCounts($cat);
         $cat->descendants = implode(',', $cat->descendantsarray);
         $cat->language = isset($cat->language) ? $cat->language : '';
     }
     return $globalcats;
 }
	/**
	 * Utility Function:
	 * Create the globalcats category tree, the result of this function is cached
	 *
	 * @access private
	 * @return array
	 */
	static function getCategoriesTree()
	{
		global $globalcats;
		$db = JFactory::getDBO();
		$ROOT_CATEGORY_ID = FLEXI_J16GE ? 1 : 0;

		// get the category tree and append the ancestors to each node
		if (FLEXI_J16GE) {
			$query	= 'SELECT id, parent_id, published, access, title, level, lft, rgt, language,'
				. ' CASE WHEN CHAR_LENGTH(alias) THEN CONCAT_WS(\':\', id, alias) ELSE id END as slug'
				. ' FROM #__categories as c'
				. ' WHERE c.extension="'.FLEXI_CAT_EXTENSION.'" AND lft > ' . FLEXI_LFT_CATEGORY . ' AND rgt < ' . FLEXI_RGT_CATEGORY
				. ' ORDER BY parent_id, lft'
				;
		} else {
			$query	= 'SELECT id, parent_id, published, access, title,'
				. ' CASE WHEN CHAR_LENGTH(alias) THEN CONCAT_WS(\':\', id, alias) ELSE id END as slug'
				. ' FROM #__categories'
				. ' WHERE section = ' . FLEXI_SECTION
				. ' ORDER BY parent_id, ordering'
				;
		}
		$db->setQuery($query);
		$cats = $db->loadObjectList();

		//establish the hierarchy of the categories
		$children = array();
		$parents = array();
		
		//set depth limit
		$levellimit = 30;
		
		foreach ($cats as $child) {
			$parent = $child->parent_id;
			if ($parent) $parents[] = $parent;
			$list 	= @$children[$parent] ? $children[$parent] : array();
			array_push($list, $child);
			$children[$parent] = $list;
		}
		
		$parents = array_unique($parents);

		//get list of the items
		$globalcats = plgSystemFlexisystem::_getCatAncestors($ROOT_CATEGORY_ID, '', array(), $children, true, max(0, $levellimit-1));

		foreach ($globalcats as $cat) {
			$cat->ancestorsonlyarray = $cat->ancestors;
			$cat->ancestorsonly      = implode(',', $cat->ancestors);
			$cat->ancestors[]        = $cat->id;
			$cat->ancestorsarray     = $cat->ancestors;
			$cat->ancestors          = implode(',', $cat->ancestors);
			$cat->descendantsarray   = plgSystemFlexisystem::_getDescendants($cat);
			$cat->descendants        = implode(',', $cat->descendantsarray);
			$cat->language           = isset($cat->language) ? $cat->language : '';
		}
		
		return $globalcats;
	}