'Use a nested list instead of using sidebar headers formatting for top-level categories'); var $default_context = LP_CONTEXT_ALL; var $description = 'Display a list of active categories'; var $active = true; var $hooks = array('sidebar'); var $nested = false; function CategoriesList(&$frontend, $args, $dummy_run=false) { $this->LightPressPlugin($frontend, $args, $dummy_run); } function run($hook, &$hook_data) { $tpl =& $this->_frontend->tpl; // cache code starts here $cache =& $this->_frontend->cache; if ($cache && $cache->active) { // try to fetch the cached snippet $result = $cache->getCache('_plugin_categorieslist',''); if ($result) { $tpl->setVar('PLUGIN_CATEGORIESLIST', $result); return; } } $tpl->setFile('plugin_categorieslist', 'plugins/categories_list' . ($this->nested ? '_nested' : '') . '.xml'); $tpl->setBlock('plugin_categorieslist', 'plugin_categorieslist_catgroup', 'PLUGIN_CATEGORIESLIST_CATGROUP'); $tpl->setBlock('plugin_categorieslist', 'plugin_categorieslist_category', 'PLUGIN_CATEGORIESLIST_CATEGORY'); $categories =& $this->_getCategories(); if (count($categories) == 0) return $this->hide(); $buffer = ''; foreach ($categories as $k=>$cat) { if (!$cat['root']) { // not a first-level category, continue continue; } $buffer .= $this->_parseCategory($tpl, $cat, $buffer); } $tpl->setVar(array( 'plugin_categorieslist_header' => $this->_frontend->messages['categories_header'], 'PLUGIN_CATEGORIESLIST_CATGROUP' => '', 'PLUGIN_CATEGORIESLIST_CATEGORY' => $buffer)); $tpl->parse('PLUGIN_CATEGORIESLIST', 'plugin_categorieslist'); if ($cache && $cache->active) $cache->setCache($tpl->_vars['PLUGIN_CATEGORIESLIST'], '_plugin_categorieslist',''); } function _parseCategory(&$tpl, &$cat) { if (count($cat['children']) > 0) { // parent, start by rendering children $buffer = ''; foreach ($cat['children'] as $k=>$child) $buffer .= $this->_parseCategory($tpl, $child); $tpl->setVar(array( 'plugin_categorieslist_category_children' => $buffer, 'plugin_categorieslist_category_permalink' => $cat['category_permalink'], 'plugin_categorieslist_category_name' => $cat['cat_name'], 'plugin_categorieslist_category_numposts' => $cat['numposts'])); return $tpl->parse('PLUGIN_CATEGORIESLIST_TMP', 'plugin_categorieslist_catgroup'); } else if ($cat['numposts'] > 0) { // normal category with posts $tpl->setVar(array( 'plugin_categorieslist_category_permalink' => $cat['category_permalink'], 'plugin_categorieslist_category_name' => $cat['cat_name'], 'plugin_categorieslist_category_numposts' => $cat['numposts'])); return $tpl->parse('PLUGIN_CATEGORIESLIST_TMP', 'plugin_categorieslist_category'); } return ''; } function &_getCategories() { // build the categories tree $categories = array(); $deferreds = array(); if (!$this->nested) { // emulate a top-level category $categories['0'] = array( 'root' => true, 'cat_name' => $this->_frontend->messages['categories_header'], 'category_permalink' => '', 'numposts' => '', 'children' => array()); } foreach ($this->_frontend->getCategories() as $cat_id=>$cat) { $parent = $cat['category_parent']; $cat['root'] = ($parent === '0'); $cat['children'] = array(); $cat['numposts'] = (int)$cat['numposts']; $categories[$cat_id] = $cat; if (!empty($parent)) { if (isset($categories[$parent])) $categories[$parent]['children'][$cat['cat_name']] =& $categories[$cat['cat_id']]; else $deferreds[$cat_id] = array($parent, $cat_id); } } foreach ($deferreds as $k) { list($parent, $cat_id) = $k; $cat =& $categories[$cat_id]; if (isset($categories[$parent])) { $categories[$parent]['children'][$cat['cat_name']] =& $categories[$cat_id]; } else { $categories[$cat['cat_id']]['category_parent'] = '0'; $categories[$cat['cat_id']]['root'] = true; } } if (!$this->nested) { // move non-parent top-level categories as children of the new top-level foreach ($categories as $k=>$v) { if ($k != '0' && $v['category_parent'] == '0' && count($v['children']) == 0) { $categories[$k]['root'] = false; $categories['0']['children'][$v['cat_name']] = $v; } } } //require_once 'classes/Var_Dump.php'; //Var_Dump::display($categories); return $categories; } } ?>