You are here

class TaxoFacets in Taxonomy Facets 7.3

Hierarchy

Expanded class hierarchy of TaxoFacets

File

classes/SelectedFilters.php, line 17

Namespace

taxonomyFacets
View source
class TaxoFacets {
  protected static $_instance = NULL;
  protected $filters = null;

  // Terms, array of term objects, id, name etc
  protected $terms = array();

  // Term ids.
  protected $tids = array();

  // Array of url aliases, i.e filter names
  protected $term_names = NULL;

  // Protected constructor, a Singleton Design Pattern.
  protected function __construct() {
    $url = drupal_encode_path($_GET['q']);
    $this->term_names = explode('/', $url);

    // Chop off first term as it is just word used for page callback
    // and its not an actual term, we will need it for checks later on so put it in to
    // the variable.
    $first_url_argument = array_shift($this->term_names);

    // If first argument was not reserved word as per settings it means we are on the node body page, so get filters
    // from the url category parameter, i.e the format is:
    // http://mysite.dev/content/dell-tower-2025/?categories=croatia/dell
    if ($first_url_argument != variable_get('taxonomy_facets_first_argument', 'items_list')) {
      if (array_key_exists('categories', $_GET)) {
        if ($url = drupal_encode_path(check_url($_GET['categories']))) {
          $this->term_names = explode('/', $url);
        }
      }
    }

    // Set filters
    foreach ($this->term_names as $term_name) {
      if ($tid = self::getTermIdFromUrlAlias($term_name)) {
        $term = taxonomy_term_load($tid);
        $term->term_path_alias = $term_name;
        $this->tids[] = $tid;
        $this->terms[] = $term;
      }
    }

    // Sort by vocabulary id so we alwys get same order of filters, to avoid
    // duplicate urls for same pages.
    if ($this->term_names) {
      usort($this->terms, 'taxonomy_facets_sort_by_vid');
    }
  }

  // Singleton pattern, make sure only one instance of this class exists.
  public static function getInstance() {
    if (!self::$_instance instanceof self) {
      self::$_instance = new self();
    }
    return self::$_instance;
  }

  /**
   * Get term id.
   *
   * For a given taxonomy term name return the term id.
   * @todo deal with duplicate term names, i.e same name in 2 vocabularies.
   *
   * @param integer $term_name
   *   Taxonomy term name, or rather its clean url interpretation.
   *
   * @return integer
   *   Return the term id. return null if no term with this name found.
   */
  static function getTermIdFromUrlAlias($term_name) {
    if ($path = drupal_lookup_path('source', $term_name)) {
      $path = explode('/', $path);
      return $path[2];
    }

    // If there were no records, i.e no term with this name, we return null.
    return NULL;
  }
  public function getAppliedFilters() {
    return $this->terms;
  }
  public function getAppliedFilterNames() {
    return $this->term_names;
  }
  public function getAppliedFilterTids() {
    return $this->tids;
  }

  // We store fey filters in the object, each belongs to a different vocabulary.
  // Get the term id of the filter that belongs to a given vocabulary.
  public function getSelectedFilterForVocabulary($vid) {
    foreach ($this->terms as $term) {
      if ($term->vid == $vid) {
        return $term->tid;
      }
    }
    return null;
  }
  public function getSelectedFilterTermForVocabulary($vid) {
    foreach ($this->terms as $term) {
      if ($term->vid == $vid) {
        return $term;
      }
    }
    return null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TaxoFacets::$filters protected property
TaxoFacets::$terms protected property
TaxoFacets::$term_names protected property
TaxoFacets::$tids protected property
TaxoFacets::$_instance protected static property
TaxoFacets::getAppliedFilterNames public function
TaxoFacets::getAppliedFilters public function
TaxoFacets::getAppliedFilterTids public function
TaxoFacets::getInstance public static function
TaxoFacets::getSelectedFilterForVocabulary public function
TaxoFacets::getSelectedFilterTermForVocabulary public function
TaxoFacets::getTermIdFromUrlAlias static function Get term id.
TaxoFacets::__construct protected function