date_views_filter_handler.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * A flexible, configurable date filter.
  5. * This filter combines multiple date filters into a single filter
  6. * where all fields are controlled by the same date and can be combined
  7. * with either AND or OR.
  8. */
  9. // @codingStandardsIgnoreStart
  10. class date_views_filter_handler extends date_views_filter_handler_simple {
  11. function init(&$view, &$options) {
  12. parent::init($view, $options);
  13. if (empty($this->view->date_info)) {
  14. $this->view->date_info = new stdClass();
  15. }
  16. if (empty($this->view->date_info->date_fields)) {
  17. $this->view->date_info->date_fields = array();
  18. }
  19. $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
  20. }
  21. // Set default values for the date filter.
  22. function option_definition() {
  23. $options = parent::option_definition();
  24. $options['date_fields'] = array('default' => array());
  25. $options['date_method'] = array('default' => 'OR');
  26. $options['date_group'] = array('default' => 'date');
  27. return $options;
  28. }
  29. function op_between($field) {
  30. $this->date_combine_conditions('op_between');
  31. }
  32. function op_simple($field) {
  33. $this->date_combine_conditions('op_simple');
  34. }
  35. function op_contains($field) {
  36. $this->date_combine_conditions('op_contains');
  37. }
  38. /**
  39. * Combines multiple date WHERE expressions into a single WHERE expression.
  40. *
  41. * @param string $function
  42. * The function name to use to add individual conditions. Either 'op_simple'
  43. * or 'op_between'.
  44. */
  45. protected function date_combine_conditions($function) {
  46. $this->get_query_fields();
  47. if (empty($this->query_fields)) {
  48. return;
  49. }
  50. // Create a custom filter group for the conditions.
  51. $this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
  52. // Add each condition to the custom filter group.
  53. foreach ((array) $this->query_fields as $query_field) {
  54. $field = $query_field['field'];
  55. $this->date_handler = $query_field['date_handler'];
  56. // Respect relationships when determining the table alias.
  57. if ($field['table_name'] != $this->table || !empty($this->relationship)) {
  58. $this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
  59. }
  60. else {
  61. $this->related_table_alias = null;
  62. }
  63. $table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
  64. $field_name = $table_alias . '.' . $field['field_name'];
  65. // Call the appropriate function, either 'op_between' or 'op_simple'.
  66. parent::$function($field_name);
  67. }
  68. // Gather all of the condition strings and their placeholders.
  69. $conditions = array();
  70. $placeholders = array();
  71. foreach ($this->query->where[$this->options['date_group']]['conditions'] as $condition) {
  72. $conditions[] = $condition['field'];
  73. $placeholders += $condition['value'];
  74. }
  75. // Remove the conditions from the custom filter group.
  76. unset($this->query->where[$this->options['date_group']]);
  77. // Combine all of the conditions into one string.
  78. $conditions = implode(' ' . $this->options['date_method'] . ' ', $conditions);
  79. // Add it to the filter group chosen in the Views UI.
  80. $this->query->add_where_expression($this->options['group'], $conditions, $placeholders);
  81. }
  82. function extra_options_form(&$form, &$form_state) {
  83. parent::extra_options_form($form, $form_state);
  84. $fields = date_views_fields($this->base_table);
  85. $options = array();
  86. foreach ($fields['name'] as $name => $field) {
  87. $options[$name] = $field['label'];
  88. }
  89. $form['date_fields'] = array(
  90. '#title' => t('Date field(s)'),
  91. '#type' => 'checkboxes',
  92. '#options' => $options,
  93. '#default_value' => $this->options['date_fields'],
  94. '#multiple' => FALSE,
  95. '#description' => t('Select date field(s) to filter.'),
  96. '#required' => TRUE,
  97. );
  98. $form['date_method'] = array(
  99. '#title' => t('Method'),
  100. '#type' => 'radios',
  101. '#options' => array('OR' => t('OR'), 'AND' => t('AND')),
  102. '#default_value' => $this->options['date_method'],
  103. '#description' => t('Method of handling multiple date fields in the same query. Return items that have any matching date field (date = field_1 OR field_2), or only those with matches in all selected date fields (date = field_1 AND field_2).'),
  104. );
  105. }
  106. function extra_options_validate($form, &$form_state) {
  107. $check_fields = array_filter($form_state['values']['options']['date_fields']);
  108. if (empty($check_fields)) {
  109. form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
  110. }
  111. }
  112. function extra_options_submit($form, &$form_state) {
  113. $form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
  114. }
  115. // Update the summary values to provide
  116. // meaningful information for each option.
  117. function admin_summary() {
  118. if (empty($this->options['date_fields'])) {
  119. return t('Missing date fields!');
  120. }
  121. $handler = $this->date_handler;
  122. $fields = date_views_fields($this->view->base_table);
  123. if (!empty($this->options['date_fields'])) {
  124. $output = array();
  125. foreach ($this->options['date_fields'] as $field) {
  126. if (array_key_exists($field, $fields['name'])) {
  127. $output[] = $fields['name'][$field]['label'];
  128. }
  129. }
  130. }
  131. $field = implode(' ' . $this->options['date_method'] . ' ', $output);
  132. $output = "$field " . check_plain($this->operator) . ' ';
  133. $parts = $handler->date_parts();
  134. $widget_options = $this->widget_options();
  135. // If the filter is exposed, display the granularity.
  136. if ($this->options['exposed']) {
  137. return t('(@field) <strong>Exposed</strong> @widget @format', array('@field' => $field, '@format' => $parts[$handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
  138. }
  139. // If not exposed, display the value.
  140. if (in_array($this->operator, $this->operator_values(2))) {
  141. $min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
  142. $max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
  143. $output .= t('@min and @max', array('@min' => $min, '@max' => $max));
  144. }
  145. else {
  146. $output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
  147. }
  148. return $output;
  149. }
  150. function get_query_fields() {
  151. $fields = date_views_fields($this->base_table);
  152. $fields = $fields['name'];
  153. $this->query_fields = array();
  154. foreach ((array) $this->options['date_fields'] as $delta => $name) {
  155. if (array_key_exists($name, $fields) && $field = $fields[$name]) {
  156. $date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
  157. $date_handler->granularity = $this->options['granularity'];
  158. $date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
  159. $date_handler->local_timezone = date_get_timezone($field['tz_handling']);
  160. $this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
  161. }
  162. }
  163. }
  164. }
  165. // @codingStandardsIgnoreEnd