You are here

function _taxo_faceted_navigation_get_menu_tree in Taxonomy Facets 7

Helper function for taxo_faceted_navigation_get_menu_tree.

Helper function that returns menu tree, it is called recursively. There is no limit on the number of menu items or the depth of the tree.

Parameters

integer $vid: Vocabulary id

array $tree: Taxonomy tree, but only one level, either first level if called for the first time, or first level children of the current level.

array $terms: Selected filters.

integer $do_not_display_if_empty: User preferences

integer $do_not_display_if_intersection_empty: User preferences

integer $tid_selected: Selected term in the menu

bolen $first_level: (optional) Defaults to True.

Return value

string Themed menu item.

1 call to _taxo_faceted_navigation_get_menu_tree()
taxo_faceted_navigation_get_menu_tree in ./taxo_faceted_navigation.module
Print out menu tree for each vocab selected to be taxo faceted filter.

File

./taxo_faceted_navigation.module, line 420
Taxo Faceted Navigation module code.

Code

function _taxo_faceted_navigation_get_menu_tree($vid, $tree, $terms, $do_not_display_if_empty, $do_not_display_if_intersection_empty, $tid_selected, $first_level = TRUE) {
  $term_name = taxo_faceted_navigation_get_term_name_from_id($tid_selected);
  $menu = '';
  if ($first_level && $tid_selected) {
    $menu = '';
    $menu_record = array();
    $menu_record['tid'] = '';
    $menu_record['term name'] = $term_name['name'];
    $menu_record['url alias'] = taxo_faceted_navigation_build_url_alias($vid, $terms, NULL);
    $menu_record['menu item class'] = 'first leaf';
    $menu_record['active'] = '';
    $menu .= theme('taxo_faceted_navigation_removefilter_template', array(
      'taxo_menu_item' => $menu_record,
    ));
  }

  // We need this so we know when we hit the last item.
  $number_of_items = count($tree);
  $item_number = 1;

  // Loop through current level of terms and format as links.
  foreach ($tree as $term) {

    // Check if the current term in this loop has any children.
    $children = array();
    $children = taxonomy_get_children($term->tid, $vid);

    // Work out if we will display this item or not.
    $display_item = TRUE;

    // User preference is to NOT display link if empty, i.e no nodes underneath.
    if ($do_not_display_if_empty) {
      $has_nodes = taxo_faceted_navigation_get_subnodes($vid, $term->tid);
      if (!$has_nodes) {
        $display_item = FALSE;
      }
    }

    // Now check for other box values.
    // 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 has filter already applied, if yes we display item
      // anyhow.
      $filter_applied = FALSE;
      if ($tid_selected == $term->tid) {
        $filter_applied = TRUE;
      }

      // Do this check only if item is last leaf
      // and if no filter applied.
      if ($terms && empty($children) && !$filter_applied) {

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

        // Add current item to filters.
        $curr_term['tid'] = $term->tid;
        $curr_term['term_path_alias'] = '';
        $curr_term['term_name'] = '';
        $curr_term['vid'] = '';
        $new_terms_arr[] = $curr_term;
        $nodes = taxo_faceted_navigation_get_nodes_based_on_intersect_of_terms($new_terms_arr);
        if (empty($nodes)) {
          $display_item = FALSE;
        }
      }
    }
    if ($display_item) {
      $menu_record = array();
      $menu_record['tid'] = $term->tid;
      $menu_record['term name'] = check_plain($term->name);
      $menu_record['url alias'] = taxo_faceted_navigation_build_url_alias($vid, $terms, $term->tid);
      $curent_term_in_children = FALSE;

      // If it has a filter applied then check that one of the children or
      // any sub children is filter current selected term
      // also set menu class as expandable as there are children and menu can
      // be expanded.
      if (!empty($children)) {

        // Get all children and subchildren.
        $all_children = taxonomy_get_tree($vid, $term->tid);
        foreach ($all_children as $child) {
          if ($tid_selected == $child->tid) {
            $curent_term_in_children = TRUE;
          }
        }
      }

      // Sort out menu item class for menu item in this loop.
      $menu_record['menu item class'] = '';

      // If first element in current level.
      if ($item_number == 1) {
        $menu_record['menu item class'] = 'first ';
      }
      else {
        if ($item_number == $number_of_items) {
          $menu_record['menu item class'] = 'last ';
        }
      }
      $menu_record['active'] = '';

      // This is selected menu item.
      if ($term->tid == $tid_selected) {
        $menu_record['active'] = 'class="active"';
        if (empty($children)) {
          $menu_record['menu item class'] .= 'leaf';
        }
        else {
          $menu_record['menu item class'] .= 'expanded';
        }
      }
      else {
        if (empty($children)) {
          $menu_record['menu item class'] .= 'leaf';
        }
        else {
          $menu_record['menu item class'] .= 'collapsed';
        }
      }

      // Add menu items from this level to menu string.
      $menu .= theme('taxo_faceted_navigation_menu_template', array(
        'taxo_menu_item' => $menu_record,
      ));

      // Print sub menu if current term is one of the children of tid in loop,
      // or if tid in loop is actually the selected one.
      if ($curent_term_in_children || $tid_selected == $term->tid) {
        $menu .= _taxo_faceted_navigation_get_menu_tree($vid, $children, $terms, $do_not_display_if_empty, $do_not_display_if_intersection_empty, $tid_selected, FALSE);
      }
      $item_number++;
    }
  }
  return theme('taxo_faceted_navigation_ul_wrapper_template', array(
    'ul_sub_menu' => $menu,
  ));
}