You are here

class MenuLeaf in Taxonomy Facets 7.3

Hierarchy

Expanded class hierarchy of MenuLeaf

File

classes/MenuLeaf.php, line 13

Namespace

taxonomyFacets
View source
class MenuLeaf {
  private $vid = NULL;
  public $tid = NULL;
  public $termName = NULL;
  public $urlAlias = NULL;
  public $leafClass = NULL;
  public $linkClass = NULL;
  public $linkUrl = null;
  private $filtersObject = null;
  public function __construct($leaf, $numberOfItems = 1, $itemNumber = 1, $leafClass = 'first') {

    // Get fully loaded terms for all applied filters.
    $this->filtersObject = TaxoFacets::getInstance();
    $this->termName = check_plain($leaf->name);
    $this->tid = $leaf->tid;
    $this->vid = $leaf->vid;
    $this
      ->getTermUrlAlias();
    $this
      ->calculateLeafClass($numberOfItems, $itemNumber, $leafClass);
    $this
      ->buildLinkUrl();
  }

  /**
   * Get taxonomy term url alias from term id.
   *
   * @param integer $tid
   *   The term id
   *
   * @return string
   *   Return url alias
   */
  private function getTermUrlAlias() {
    $query = db_select('url_alias', 'u')
      ->fields('u', array(
      'alias',
    ))
      ->condition('source', 'taxonomy/term/' . $this->tid)
      ->execute();
    $this->urlAlias = current($query
      ->fetchAll())->alias;
  }
  private function calculateLeafClass($numberOfItems, $itemNumber, $leafClass) {
    switch ($itemNumber) {
      case 1:
        $this->leafClass = 'first';
        break;
      case $numberOfItems:
        $this->leafClass = 'last';
        break;
    }
    $this->leafClass .= ' ' . $leafClass;
  }
  private function buildLinkUrl() {
    $url = array();
    $noTermFromCurrentVocabularyFound = true;
    if ($filters = $this->filtersObject
      ->getAppliedFilters()) {

      // Loop trough applied filters.
      foreach ($filters as $filter) {

        // if filter is from current vocabulary than apply this leaf url alias
        // instead of already applied filter
        if ($filter->vid == $this->vid) {
          $obj = new \stdClass();
          $obj->vid = $this->vid;
          $obj->url = $this->urlAlias;
          $url[] = $obj;
          $noTermFromCurrentVocabularyFound = false;
        }
        else {
          $obj = new \stdClass();
          $obj->vid = $filter->vid;
          $obj->url = $filter->term_path_alias;
          $url[] = $obj;
        }
      }
    }

    // If filters from this vocabulary were not in the applied filters than
    // also apply the alias from the current leaf
    if ($noTermFromCurrentVocabularyFound == true) {
      $obj = new \stdClass();
      $obj->vid = $this->vid;
      $obj->url = $this->urlAlias;
      $url[] = $obj;
    }
    $this->linkUrl = self::taxonomy_facets_prepend_language_prefix() . '/' . variable_get('taxonomy_facets_first_argument', 'items_list');

    // Now order url aliases (filters) by vocabulary id so that we preserve
    // order, so we don't end up with duplicate pages for same filter
    // combinations.
    usort($url, 'taxonomy_facets_sort_by_vid');
    foreach ($url as $u) {
      $this->linkUrl .= '/' . $u->url;
    }
  }

  /**
   * Some sites will opt out to not prepend language prefix to the url produced with this module
   * https://www.drupal.org/node/2379617
   *
   * @return string
   *  language alias or empty string
   */
  public static function taxonomy_facets_prepend_language_prefix() {
    global $language;
    $alias = '';

    // Prepend language prefix to the path, but only if user did not
    // choose to ignore it in the prefrences.
    $lang_ignore = variable_get('taxonomy_facets_ignore_language_prefix', FALSE);
    if ($language->prefix != NULL && $lang_ignore == NULL) {
      $alias = '/' . $language->prefix;
    }
    return $alias;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuLeaf::$filtersObject private property
MenuLeaf::$leafClass public property
MenuLeaf::$linkClass public property
MenuLeaf::$linkUrl public property
MenuLeaf::$termName public property
MenuLeaf::$tid public property
MenuLeaf::$urlAlias public property
MenuLeaf::$vid private property
MenuLeaf::buildLinkUrl private function
MenuLeaf::calculateLeafClass private function
MenuLeaf::getTermUrlAlias private function Get taxonomy term url alias from term id.
MenuLeaf::taxonomy_facets_prepend_language_prefix public static function Some sites will opt out to not prepend language prefix to the url produced with this module https://www.drupal.org/node/2379617
MenuLeaf::__construct public function