Skip to content

Instantly share code, notes, and snippets.

@andergmartins
Created January 29, 2013 18:55
Show Gist options
  • Save andergmartins/4666617 to your computer and use it in GitHub Desktop.
Save andergmartins/4666617 to your computer and use it in GitHub Desktop.
K2 getCategoryTree function - Categorias recursivas
function getCategoryTree($categories){
$mainframe = &JFactory::getApplication();
$db = &JFactory::getDBO();
$user = &JFactory::getUser();
$aid = (int) $user->get('aid');
if(!is_array($categories)){
$categories = (array)$categories;
}
JArrayHelper::toInteger($categories);
$categories = array_unique($categories);
sort($categories);
$key = implode('|', $categories);
$clientID = $mainframe->getClientId();
static $K2CategoryTreeInstances = array();
if(isset($K2CategoryTreeInstances[$clientID]) && array_key_exists($key, $K2CategoryTreeInstances[$clientID])){
return $K2CategoryTreeInstances[$clientID][$key];
}
$array = $categories;
while(count($array)){
$query = "SELECT id
FROM #__k2_categories
WHERE parent IN (".implode(',', $array).")
AND id NOT IN (".implode(',', $array).") ";
if($mainframe->isSite()){
$query.="
AND published=1
AND trash=0";
if(K2_JVERSION=='16'){
$query.= " AND access IN(".implode(',', $user->authorisedLevels()).")";
if($mainframe->getLanguageFilter()) {
$query.= " AND language IN(".$db->Quote(JFactory::getLanguage()->getTag()).", ".$db->Quote('*').")";
}
}
else{
$query.=" AND access<={$aid}";
}
}
$db->setQuery($query);
$array = $db->loadResultArray();
$categories = array_merge($categories, $array);
}
JArrayHelper::toInteger($categories);
$categories = array_unique($categories);
$K2CategoryTreeInstances[$clientID][$key] = $categories;
return $categories;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment