MenuTree.php in Taxonomy Facets 7.3
File
classes/MenuTree.php
View source
<?php
namespace taxonomyFacets;
include_once 'SelectedFilters.php';
include_once 'MenuLeaf.php';
class MenuTree {
private $menuTree = array();
private $vid = NULL;
private $filtersObject = NULL;
private $tidSelected = NULL;
private $termSelected = null;
public function __construct($vid) {
$this->vid = $vid;
$this->filtersObject = TaxoFacets::getInstance();
$this->tidSelected = $this->filtersObject
->getSelectedFilterForVocabulary($vid);
$this->termSelected = $this->filtersObject
->getSelectedFilterTermForVocabulary($vid);
$this
->buildMenyTreeHeader();
$this
->buildMenuTreeRecursively(0);
}
public function getMenuTree() {
return $this->menuTree;
}
private function buildMenuTreeRecursively($parent) {
$tree = taxonomy_get_tree($this->vid, $parent, 1, FALSE);
$item_number = 1;
$number_of_items = count($tree);
$this->menuTree[] = theme('taxonomy_facets_ul_wrapper_begin_template');
$leafClass = 'leaf';
foreach ($tree as $leaf) {
if ($this
->displayMenuItem($leaf)) {
$children = taxonomy_get_children($leaf->tid, $this->vid);
if ($children) {
$leafClass = 'collapsed';
}
if ($selectedFilterInChildrenLeafs = $this
->selectedFilterInChildrenLeafs($leaf->tid)) {
$leafClass = 'expanded';
}
$this->menuTree[] = new MenuLeaf($leaf, $number_of_items, $item_number, $leafClass);
$item_number++;
if ($selectedFilterInChildrenLeafs || $this
->menuItemIsFilterApplied($leaf->tid)) {
$this
->buildMenuTreeRecursively($leaf->tid);
}
}
}
$this->menuTree[] = theme('taxonomy_facets_ul_wrapper_end_template');
}
private function buildMenyTreeHeader() {
if ($this->termSelected) {
$headItem = new MenuLeaf($this->termSelected);
$headItem->linkUrl = str_replace('/' . $headItem->urlAlias, "", $headItem->linkUrl);
$this->menuTree[] = theme('taxonomy_facets_removefilter_template', array(
'menuHed' => $headItem,
));
}
}
private function displayMenuItem($term) {
$display_item = TRUE;
$filter_applied = FALSE;
$children = taxonomy_get_children($term->tid, $this->vid);
$do_not_display_if_empty = variable_get('taxonomy_facets_display_link_if_empty', FALSE);
$do_not_display_if_intersection_empty = variable_get('taxonomy_facets_display_link_if_intersection_empty', FALSE);
if ($do_not_display_if_empty) {
$has_nodes = taxonomy_facets_get_subnodes($this->vid, $term->tid);
if (!$has_nodes) {
$display_item = FALSE;
}
}
if ($do_not_display_if_intersection_empty) {
if ($this->tidSelected == $term->tid) {
$filter_applied = TRUE;
}
$applied_filters = $this->filtersObject
->getAppliedFilters();
if ($applied_filters && empty($children) && !$filter_applied) {
$new_terms_arr = array();
foreach ($applied_filters as $t) {
if ($this->vid != $t->vid) {
$new_terms_arr[] = $t;
}
}
$curr_term = new \stdClass();
$curr_term->tid = $term->tid;
$new_terms_arr[] = $curr_term;
$nodes = taxonomy_facets_get_nodes_based_on_intersect_of_terms($new_terms_arr);
if (empty($nodes)) {
$display_item = FALSE;
}
}
}
return $display_item;
}
private function selectedFilterInChildrenLeafs($tid) {
$all_children = taxonomy_get_tree($this->vid, $tid);
foreach ($all_children as $child) {
if ($this->tidSelected == $child->tid) {
return true;
}
}
return false;
}
private function menuItemIsFilterApplied($tid) {
if ($this->tidSelected == $tid) {
return true;
}
return false;
}
}