PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/date/date_views/includes/date_views_filter_handler.inc

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