You are here

MenuTree.php in Taxonomy Facets 7.3

Namespace

taxonomyFacets

File

classes/MenuTree.php
View source
<?php

/**
 * Created by PhpStorm.
 * User: darko
 * Date: 26/07/2017
 * Time: 13:21
 */
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;

    // Get fully loaded terms for all applied filters.
    $this->filtersObject = TaxoFacets::getInstance();

    // Get the term id of the filter that belongs to given vocabulary.
    $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);

    // Count items, we need this so that leaf object can display right class
    $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)) {

        // Check if the current term in this loop has any children.
        $children = taxonomy_get_children($leaf->tid, $this->vid);
        if ($children) {
          $leafClass = 'collapsed';
        }

        // Check if selected filer is somewhere in the children leafs.
        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);

      // remove curent filter from the url
      $headItem->linkUrl = str_replace('/' . $headItem->urlAlias, "", $headItem->linkUrl);
      $this->menuTree[] = theme('taxonomy_facets_removefilter_template', array(
        'menuHed' => $headItem,
      ));
    }
  }

  /**
   * When building menu tree we check if we want to display a menu item
   * depending on various user preferences
   * @param $term
   * @param $children
   * @param $terms
   * @param $tid_selected
   * @return bool
   *  true if to display, false if not to display
   */
  private function displayMenuItem($term) {
    $display_item = TRUE;
    $filter_applied = FALSE;
    $children = taxonomy_get_children($term->tid, $this->vid);

    // Get user preferences.
    $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 user preference is to NOT display link if empty, i.e no nodes underneath.
    if ($do_not_display_if_empty) {

      // check if it has nodes underneath
      $has_nodes = taxonomy_facets_get_subnodes($this->vid, $term->tid);
      if (!$has_nodes) {

        // if no nodes do not display
        $display_item = FALSE;
      }
    }

    // User preference is to NOT display link if selection of filters have
    // no nodes underneath.
    if ($do_not_display_if_intersection_empty) {

      // Check if this item is already used as filter applied, if yes we display
      // item anyhow.
      if ($this->tidSelected == $term->tid) {
        $filter_applied = TRUE;
      }

      // Do this check only if item is last leaf
      // and if no filter applied.
      $applied_filters = $this->filtersObject
        ->getAppliedFilters();
      if ($applied_filters && empty($children) && !$filter_applied) {

        // Remove filter from this vocabulary, if any.
        $new_terms_arr = array();
        foreach ($applied_filters as $t) {
          if ($this->vid != $t->vid) {
            $new_terms_arr[] = $t;
          }
        }

        // Add current item to filters.
        $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;
  }

  /**
   * Check if selected filer is somewhere in the children leafs.
   */
  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;
  }

}

Classes

Namesort descending Description
MenuTree