query_type_date.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * @file
  4. * Date query type plugin for the Search API adapter.
  5. */
  6. /**
  7. * Plugin for "date" query types.
  8. */
  9. class SearchApiFacetapiDate extends SearchApiFacetapiTerm implements FacetapiQueryTypeInterface {
  10. /**
  11. * Loads the include file containing the date API functions.
  12. */
  13. public function __construct(FacetapiAdapter $adapter, array $facet) {
  14. module_load_include('date.inc', 'facetapi');
  15. parent::__construct($adapter, $facet);
  16. }
  17. /**
  18. * Returns the query type associated with the plugin.
  19. *
  20. * @return string
  21. * The query type.
  22. */
  23. static public function getType() {
  24. return 'date';
  25. }
  26. /**
  27. * Adds the filter to the query object.
  28. *
  29. * @param $query
  30. * An object containing the query in the backend's native API.
  31. */
  32. public function execute($query) {
  33. // Return terms for this facet.
  34. $this->adapter->addFacet($this->facet, $query);
  35. $settings = $this->adapter->getFacet($this->facet)->getSettings()->settings;
  36. // First check if the facet is enabled for this search.
  37. $default_true = isset($settings['default_true']) ? $settings['default_true'] : TRUE;
  38. $facet_search_ids = isset($settings['facet_search_ids']) ? $settings['facet_search_ids'] : array();
  39. if ($default_true != empty($facet_search_ids[$query->getOption('search id')])) {
  40. // Facet is not enabled for this search ID.
  41. return;
  42. }
  43. // Change limit to "unlimited" (-1).
  44. $options = &$query->getOptions();
  45. if (!empty($options['search_api_facets'][$this->facet['name']])) {
  46. $options['search_api_facets'][$this->facet['name']]['limit'] = -1;
  47. }
  48. if ($active = $this->adapter->getActiveItems($this->facet)) {
  49. $item = end($active);
  50. $field = $this->facet['field'];
  51. $regex = str_replace(array('^', '$'), '', FACETAPI_REGEX_DATE);
  52. $filter = preg_replace_callback($regex, array($this, 'replaceDateString'), $item['value']);
  53. $this->addFacetFilter($query, $field, $filter);
  54. }
  55. }
  56. /**
  57. * Replacement callback for replacing ISO dates with timestamps.
  58. */
  59. public function replaceDateString($matches) {
  60. return strtotime($matches[0]);
  61. }
  62. /**
  63. * Initializes the facet's build array.
  64. *
  65. * @return array
  66. * The initialized render array.
  67. */
  68. public function build() {
  69. $facet = $this->adapter->getFacet($this->facet);
  70. $search_ids = drupal_static('search_api_facetapi_active_facets', array());
  71. if (empty($search_ids[$facet['name']]) || !search_api_current_search($search_ids[$facet['name']])) {
  72. return array();
  73. }
  74. $search_id = $search_ids[$facet['name']];
  75. $build = array();
  76. $search = search_api_current_search($search_id);
  77. $results = $search[1];
  78. if (!$results['result count']) {
  79. return array();
  80. }
  81. // Gets total number of documents matched in search.
  82. $total = $results['result count'];
  83. // Most of the code below is copied from search_facetapi's implementation of
  84. // this method.
  85. // Executes query, iterates over results.
  86. if (isset($results['search_api_facets']) && isset($results['search_api_facets'][$this->facet['name']])) {
  87. $values = $results['search_api_facets'][$this->facet['name']];
  88. foreach ($values as $value) {
  89. if ($value['count']) {
  90. $filter = $value['filter'];
  91. // We only process single values further. The "missing" filter and
  92. // range filters will be passed on unchanged.
  93. if ($filter == '!') {
  94. $build[$filter]['#count'] = $value['count'];
  95. }
  96. elseif ($filter[0] == '"') {
  97. $filter = substr($value['filter'], 1, -1);
  98. if ($filter) {
  99. $raw_values[$filter] = $value['count'];
  100. }
  101. }
  102. else {
  103. $filter = substr($value['filter'], 1, -1);
  104. $pos = strpos($filter, ' ');
  105. if ($pos !== FALSE) {
  106. $lower = facetapi_isodate(substr($filter, 0, $pos), FACETAPI_DATE_DAY);
  107. $upper = facetapi_isodate(substr($filter, $pos + 1), FACETAPI_DATE_DAY);
  108. $filter = '[' . $lower . ' TO ' . $upper . ']';
  109. }
  110. $build[$filter]['#count'] = $value['count'];
  111. }
  112. }
  113. }
  114. }
  115. // Get the finest level of detail we're allowed to drill down to.
  116. $settings = $facet->getSettings()->settings;
  117. $granularity = isset($settings['date_granularity']) ? $settings['date_granularity'] : FACETAPI_DATE_MINUTE;
  118. // Gets active facets, starts building hierarchy.
  119. $parent = $gap = NULL;
  120. $active_items = $this->adapter->getActiveItems($this->facet);
  121. foreach ($active_items as $value => $item) {
  122. // If the item is active, the count is the result set count.
  123. $build[$value] = array('#count' => $total);
  124. // Gets next "gap" increment.
  125. if ($value[0] != '[' || $value[strlen($value) - 1] != ']' || !($pos = strpos($value, ' TO '))) {
  126. continue;
  127. }
  128. $start = substr($value, 1, $pos);
  129. $end = substr($value, $pos + 4, -1);
  130. $date_gap = facetapi_get_date_gap($start, $end);
  131. $gap = facetapi_get_next_date_gap($date_gap, $granularity);
  132. // If there is a previous item, there is a parent, uses a reference so the
  133. // arrays are populated when they are updated.
  134. if (NULL !== $parent) {
  135. $build[$parent]['#item_children'][$value] = &$build[$value];
  136. $build[$value]['#item_parents'][$parent] = $parent;
  137. }
  138. // Stores the last value iterated over.
  139. $parent = $value;
  140. }
  141. if (empty($raw_values)) {
  142. return $build;
  143. }
  144. ksort($raw_values);
  145. // Mind the gap! Calculates gap from min and max timestamps.
  146. $timestamps = array_keys($raw_values);
  147. if (NULL === $parent) {
  148. if (count($raw_values) > 1) {
  149. $gap = facetapi_get_timestamp_gap(min($timestamps), max($timestamps));
  150. // Array of numbers used to determine whether the next gap is smaller than
  151. // the minimum gap allowed in the drilldown.
  152. $gap_numbers = array(
  153. FACETAPI_DATE_YEAR => 6,
  154. FACETAPI_DATE_MONTH => 5,
  155. FACETAPI_DATE_DAY => 4,
  156. FACETAPI_DATE_HOUR => 3,
  157. FACETAPI_DATE_MINUTE => 2,
  158. FACETAPI_DATE_SECOND => 1,
  159. );
  160. // Gets gap numbers for both the gap and minimum gap, checks if the gap
  161. // is within the limit set by the $granularity parameter.
  162. if ($gap_numbers[$gap] < $gap_numbers[$granularity]) {
  163. $gap = $granularity;
  164. }
  165. }
  166. else {
  167. $gap = $granularity;
  168. }
  169. }
  170. // Converts all timestamps to dates in ISO 8601 format.
  171. $dates = array();
  172. foreach ($timestamps as $timestamp) {
  173. $dates[$timestamp] = facetapi_isodate($timestamp, $gap);
  174. }
  175. // Treat each date as the range start and next date as the range end.
  176. $range_end = array();
  177. $previous = NULL;
  178. foreach (array_unique($dates) as $date) {
  179. if (NULL !== $previous) {
  180. $range_end[$previous] = facetapi_get_next_date_increment($previous, $gap);
  181. }
  182. $previous = $date;
  183. }
  184. $range_end[$previous] = facetapi_get_next_date_increment($previous, $gap);
  185. // Groups dates by the range they belong to, builds the $build array
  186. // with the facet counts and formatted range values.
  187. foreach ($raw_values as $value => $count) {
  188. $new_value = '[' . $dates[$value] . ' TO ' . $range_end[$dates[$value]] . ']';
  189. if (!isset($build[$new_value])) {
  190. $build[$new_value] = array('#count' => $count);
  191. }
  192. // Active items already have their value set because it's the current
  193. // result count.
  194. elseif (!isset($active_items[$new_value])) {
  195. $build[$new_value]['#count'] += $count;
  196. }
  197. // Adds parent information if not already set.
  198. if (NULL !== $parent && $parent != $new_value) {
  199. $build[$parent]['#item_children'][$new_value] = &$build[$new_value];
  200. $build[$new_value]['#item_parents'][$parent] = $parent;
  201. }
  202. }
  203. return $build;
  204. }
  205. }