SelectedFilters.php in Taxonomy Facets 7.3
Namespace
taxonomyFacetsFile
classes/SelectedFilters.phpView source
<?php
/**
* Get all selected filters from the url.
*
* Get the current url, taxonomy terms that are currently applied as
* filter are in the url, this function examines the url and gets
* all of the filters applied for the current page.
*
* @return array
* Array of filter arrays, each filter array has all the info about a filter:
* tid, path alias, term name, vid
*/
namespace taxonomyFacets;
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;
}
}
Classes
Name | Description |
---|---|
TaxoFacets |