Issue #1984490 by Nick_vh: Added support for OR logic in requirements.
[project/facetapi.git] / plugins / facetapi / url_processor_standard.inc
1 <?php
2
3 /**
4 * @file
5 * The standard url processor class.
6 */
7
8 /**
9 * Url processor plugin that retrieves facet data from the query string.
10 *
11 * This plugin retrieves facet data from $_GET, and stored all information in
12 * the "f" query string variable by default.
13 */
14 class FacetapiUrlProcessorStandard extends FacetapiUrlProcessor {
15
16 /**
17 * Implements FacetapiUrlProcessor::fetchParams().
18 *
19 * Use $_GET as the source for facet data.
20 */
21 public function fetchParams() {
22 return $_GET;
23 }
24
25 /**
26 * Implements FacetapiUrlProcessor::normalizeParams().
27 *
28 * Strips the "q" and "page" variables from the params array.
29 */
30 public function normalizeParams(array $params, $filter_key = 'f') {
31 return drupal_get_query_parameters($params, array('q', 'page'));
32 }
33
34 /**
35 * Implements FacetapiUrlProcessor::getQueryString().
36 */
37 public function getQueryString(array $facet, array $values, $active) {
38 $qstring = $this->params;
39 $active_items = $this->adapter->getActiveItems($facet);
40
41 // Appends to qstring if inactive, removes if active.
42 foreach ($values as $value) {
43 if ($active && isset($active_items[$value])) {
44 unset($qstring[$this->filterKey][$active_items[$value]['pos']]);
45 }
46 elseif (!$active) {
47 $field_alias = rawurlencode($facet['field alias']);
48 $qstring[$this->filterKey][] = $field_alias . ':' . $value;
49 }
50 }
51
52 // Removes duplicates, resets array keys and returns query string.
53 // @see http://drupal.org/node/1340528
54 $qstring[$this->filterKey] = array_values(array_unique($qstring[$this->filterKey]));
55 return array_filter($qstring);
56 }
57
58 /**
59 * Implements FacetapiUrlProcessor::setBreadcrumb().
60 */
61 public function setBreadcrumb() {
62 $breadcrumb = drupal_get_breadcrumb();
63
64 // Gets search keys and active items form the adapter.
65 $keys = $this->adapter->getSearchKeys();
66 $active_items = $this->adapter->getAllActiveItems();
67
68 $item = menu_get_item();
69
70 // Initializes base breadcrumb query.
71 $query = $this->params;
72 unset($query[$this->filterKey]);
73
74 // Adds the current search to the query.
75 if ($keys) {
76 // The last item should be text, not a link.
77 $breadcrumb[] = $active_items ? l($keys, current_path(), array('query' => $query)) : check_plain($keys);
78 }
79
80 // Adds filters to the breadcrumb trail.
81 $last = end($active_items);
82 foreach ($active_items as $item) {
83 $query[$this->filterKey][] = rawurlencode($item['field alias']) . ':' . $item['value'];
84
85 // Replaces with the mapped value.
86 $value = $this->adapter->getMappedValue($item['facets'][0], $item['value']);
87
88 // The last item should be text, not a link.
89 if ($last == $item) {
90 $breadcrumb[] = !empty($value['#html']) ? $value['#markup'] : check_plain($value['#markup']);
91 }
92 else {
93 // Appends the filter to the breadcrumb trail.
94 $breadcrumb[] = l($value['#markup'], current_path(), array('query' => $query, 'html' => !empty($value['#html'])));
95 }
96 }
97
98 // Sets the breadcrumb trail with the keys and filters.
99 drupal_set_breadcrumb($breadcrumb);
100 }
101 }