/** * Get a branch * @param int $node * @param bool $include include the wanted node itself * @return Tree */ public function branch($id, $include = FALSE) { // get descendant ids $descendants = isset($this->_map[$id]) ? $this->_map[$id]['descendants'] : array(); // get all descendants $data = array(); $map = array(); foreach ($descendants as $descendant) { $data[] = $this->_map[$descendant]['item']; $map[$descendant] = $this->_map[$descendant]; } // new root is the wanted node $root = $id; if ($include == TRUE) { // prepend items with from item if (isset($this->_map[$id]['item']) && $this->_map[$id]['item'] !== FALSE) { // when the parent is an actual element // prepend data array_unshift($data, $this->_map[$id]['item']); // new root is the node's parent $root = $this->_map[$id]['parent']; } if (isset($this->_map[$id])) { $map[$id] = $this->_map[$id]; } } // new tree with prepared map return Tree::factory($data, $map, $root); }
/** * Model_Item::_tree() * select all items and pass them to a tree * * @return void */ public function tree($reload = FALSE) { // return cached tree if that's ok if ($this->_tree !== FALSE && $reload === FALSE) { return $this->_tree; } // clear all $this->clear(); // sort items by rank if ($this->_sortable === TRUE) { $this->order_by($this->_table_name . '.rank', 'ASC'); } //get items $items = $this->find_all()->as_array(); // fill up & cache tree $this->_tree = Tree::factory($items); // return it return $this->_tree; }
/** * Creates a Tree structure using a Reader driver. * * @param RecursiveIterator $it Iterator to create tree from * @param mixed $node Internal use only * @return Tree Tree object */ public static function factory(Tree_Factory_Iterator $it, $node = null) { if (is_null($node)) { $node = new Tree(); } foreach ($it as $v) { $newNode = $node->nodes->add(new Tree_Node($v->getTag())); if ($v->hasChildren()) { Tree::factory($v->getChildren(), $newNode); } } return $node; }
function fetch() { $config =& $this->locator->get('config'); $image =& $this->locator->get('image'); $language =& $this->locator->get('language'); $url =& $this->locator->get('url'); $request =& $this->locator->get('request'); $template =& $this->locator->get('template'); $head_def =& $this->locator->get('HeaderDefinition'); $this->modelCore = $this->model->get('model_core'); if ($config->get('categorymenu_status')) { $view = $this->locator->create('template'); $categorymenu_data = array(); $list_data = array(); if (isset($_GET['path'])) { $pathlvl = $_GET['path']; } else { $pathlvl = 0; } $results = $this->modelCore->get_menucategories(); $level = explode('_', $pathlvl); $level_count = count($level); $level_path = $level_count > 1 ? array_slice($level, 0, $level_count - 1) : $level; foreach ($results as $result) { $path_count = count(explode('_', $result['path'])); $class = ''; if ($result['parent_id'] == 0) { // the main menu $class = 'menu_lvl_0'; // id of the <ul> tags and class of the <a> tags $id = 'menu_level_0'; // id of the <li> tags $type = "block"; // display:block or display:none $status = "enabled"; $cat_image = $config->get('categorymenu_catimage') ? $image->resize($result['filename'], 16, 16) : ''; } else { if (in_array($result['parent_id'], $level) && $path_count < 3) { // subcategory if selected $class = 'menu_lvl_' . ($path_count - 1); $id = 'menu_level_' . ($path_count - 1); $type = "none"; // by default. Onhover --> block --> <ul> visible $status = "disabled"; $cat_image = $config->get('categorymenu_subcatimage') ? $image->resize($result['filename'], 16, 16) : ''; } else { // subcategory if not selected $class = 'menu_lvl_' . ($path_count - 1); $id = 'menu_level_' . ($path_count - 1); $type = "none"; $status = "disabled"; $cat_image = $config->get('categorymenu_subcatimage') ? $image->resize($result['filename'], 16, 16) : ''; } } if ($request->get('path') == $result['path']) { $state = 'active'; // if the menu element is selected add new class "active" } else { $state = ''; } $products_in_category = $config->get('categorymenu_mcount') ? $this->modelCore->getPrInCat($result['category_id']) : 0; if ($class) { $categorymenu_data[$result['category_id']] = array('state' => $state, 'name' => $result['name'], 'href' => $url->href('category', false, array('path' => $result['path'])), 'class' => $class, 'id' => $id, 'type' => $type, 'level' => $path_count - 1, 'status' => $status, 'sort_order' => (int) $result['sort_order'], 'image' => $cat_image, 'products_in_category' => $products_in_category); $list_data[] = $result['path']; } } $new_categorymenu_data = array(); if ($list_data) { $rit = new Tree_Factory_List($list_data, '_'); $tree = Tree::factory($rit); $tree->nodes->traverse('setTags2', $categorymenu_data); $tree->usortNodes('cmp2'); $flatList = $tree->nodes->getFlatList(); foreach ($flatList as $node) { $tag = $node->getTag(); $new_categorymenu_data[] = $tag; } } $view->set('menus', $new_categorymenu_data); $view->set('location', $this->modelCore->module_location['categorymenu']); // Template Manager $view->set('head_def', $head_def); $template->set('head_def', $head_def); return $view->fetch('module/categorymenu.tpl'); } }
function fetch() { $config =& $this->locator->get('config'); $language =& $this->locator->get('language'); $url =& $this->locator->get('url'); $request =& $this->locator->get('request'); $template =& $this->locator->get('template'); $head_def =& $this->locator->get('HeaderDefinition'); $this->modelCore = $this->model->get('model_core'); if ($config->get('category_status')) { $language->load('extension/module/category.php'); $view = $this->locator->create('template'); $view->set('heading_title', $language->get('heading_title')); $category_data = array(); $list_data = array(); if (isset($_GET['path'])) { $pathlvl = $_GET['path']; } else { $pathlvl = 0; } $results = $this->modelCore->get_categories(); $level = explode('_', $pathlvl); $level_count = count($level); $level_path = $level_count > 1 ? array_slice($level, 0, $level_count - 1) : $level; foreach ($results as $result) { $path_count = count(explode('_', $result['path'])); $class = ''; if ($result['parent_id'] == 0) { $class = 'cat_lvl_0'; $type = "block"; $status = "enabled"; } else { if (in_array($result['parent_id'], $level)) { $class = 'cat_lvl_' . ($path_count - 1); $type = "block"; $status = "enabled"; } else { $class = 'cat_lvl_' . ($path_count - 1); $type = "none"; $status = "disabled"; } } if ($request->get('path') == $result['path']) { $state = 'active'; } else { $state = ''; } $products_in_category = $config->get('category_mcount') ? $this->modelCore->getPrInCat($result['category_id']) : 0; if ($class) { $category_data[$result['category_id']] = array('state' => $state, 'name' => $result['name'], 'href' => $url->href('category', false, array('path' => $result['path'])), 'class' => $class, 'type' => $type, 'level' => $path_count - 1, 'status' => $status, 'sort_order' => (int) $result['sort_order'], 'products_in_category' => $products_in_category); $list_data[] = $result['path']; } } $new_category_data = array(); if ($list_data) { $rit = new Tree_Factory_List($list_data, '_'); $tree = Tree::factory($rit); $tree->nodes->traverse('setTags', $category_data); $tree->usortNodes('cmp'); $flatList = $tree->nodes->getFlatList(); foreach ($flatList as $node) { $tag = $node->getTag(); $new_category_data[] = $tag; } } $view->set('categories', $new_category_data); $view->set('head_def', $head_def); $template->set('head_def', $head_def); return $view->fetch('module/category.tpl'); } }