date_views_filter_handler.inc 7.1 KB

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