1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
85ad3d82
|
Assos Assos
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* A flexible, configurable date filter.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
599a39cd
|
Assos Assos
|
/**
|
9 |
|
|
* A flexible, configurable date filter.
|
10 |
|
|
*
|
11 |
|
|
* This filter combines multiple date filters into a single filter where all
|
12 |
|
|
* fields are controlled by the same date and can be combined with either AND or
|
13 |
|
|
* OR.
|
14 |
|
|
*/
|
15 |
85ad3d82
|
Assos Assos
|
class date_views_filter_handler extends date_views_filter_handler_simple {
|
16 |
599a39cd
|
Assos Assos
|
|
17 |
|
|
/**
|
18 |
|
|
* {@inheritdoc}
|
19 |
|
|
*/
|
20 |
85ad3d82
|
Assos Assos
|
function init(&$view, &$options) {
|
21 |
|
|
parent::init($view, $options);
|
22 |
|
|
|
23 |
|
|
if (empty($this->view->date_info)) {
|
24 |
|
|
$this->view->date_info = new stdClass();
|
25 |
|
|
}
|
26 |
|
|
if (empty($this->view->date_info->date_fields)) {
|
27 |
|
|
$this->view->date_info->date_fields = array();
|
28 |
|
|
}
|
29 |
|
|
$this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, $this->options['date_fields']);
|
30 |
|
|
}
|
31 |
|
|
|
32 |
599a39cd
|
Assos Assos
|
/**
|
33 |
|
|
* {@inheritdoc}
|
34 |
|
|
*/
|
35 |
85ad3d82
|
Assos Assos
|
function option_definition() {
|
36 |
|
|
$options = parent::option_definition();
|
37 |
|
|
$options['date_fields'] = array('default' => array());
|
38 |
|
|
$options['date_method'] = array('default' => 'OR');
|
39 |
|
|
$options['date_group'] = array('default' => 'date');
|
40 |
|
|
return $options;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
599a39cd
|
Assos Assos
|
/**
|
44 |
|
|
* @todo
|
45 |
|
|
*/
|
46 |
85ad3d82
|
Assos Assos
|
function op_between($field) {
|
47 |
|
|
$this->date_combine_conditions('op_between');
|
48 |
|
|
}
|
49 |
|
|
|
50 |
599a39cd
|
Assos Assos
|
/**
|
51 |
|
|
* @todo
|
52 |
|
|
*/
|
53 |
85ad3d82
|
Assos Assos
|
function op_simple($field) {
|
54 |
|
|
$this->date_combine_conditions('op_simple');
|
55 |
|
|
}
|
56 |
|
|
|
57 |
599a39cd
|
Assos Assos
|
/**
|
58 |
|
|
* @todo
|
59 |
|
|
*/
|
60 |
85ad3d82
|
Assos Assos
|
function op_contains($field) {
|
61 |
|
|
$this->date_combine_conditions('op_contains');
|
62 |
|
|
}
|
63 |
|
|
|
64 |
599a39cd
|
Assos Assos
|
/**
|
65 |
|
|
* @todo
|
66 |
|
|
*/
|
67 |
ee46a8ed
|
Assos Assos
|
function op_empty($field) {
|
68 |
|
|
$this->get_query_fields();
|
69 |
|
|
if (empty($this->query_fields)) {
|
70 |
|
|
return;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
// Add each condition to the custom filter group.
|
74 |
|
|
foreach ((array) $this->query_fields as $query_field) {
|
75 |
|
|
$field = $query_field['field'];
|
76 |
|
|
$this->date_handler = $query_field['date_handler'];
|
77 |
|
|
|
78 |
|
|
// Respect relationships when determining the table alias.
|
79 |
|
|
if ($field['table_name'] != $this->table || !empty($this->relationship)) {
|
80 |
|
|
$this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
|
81 |
|
|
}
|
82 |
|
|
else {
|
83 |
|
|
$this->related_table_alias = NULL;
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
$table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
|
87 |
|
|
$field_name = $table_alias . '.' . $field['field_name'];
|
88 |
|
|
|
89 |
|
|
parent::op_empty($field_name);
|
90 |
|
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
85ad3d82
|
Assos Assos
|
/**
|
94 |
|
|
* Combines multiple date WHERE expressions into a single WHERE expression.
|
95 |
|
|
*
|
96 |
|
|
* @param string $function
|
97 |
|
|
* The function name to use to add individual conditions. Either 'op_simple'
|
98 |
|
|
* or 'op_between'.
|
99 |
|
|
*/
|
100 |
|
|
protected function date_combine_conditions($function) {
|
101 |
|
|
$this->get_query_fields();
|
102 |
|
|
if (empty($this->query_fields)) {
|
103 |
|
|
return;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
// Create a custom filter group for the conditions.
|
107 |
|
|
$this->query->set_where_group($this->options['date_method'], $this->options['date_group']);
|
108 |
599a39cd
|
Assos Assos
|
|
109 |
85ad3d82
|
Assos Assos
|
// Add each condition to the custom filter group.
|
110 |
|
|
foreach ((array) $this->query_fields as $query_field) {
|
111 |
|
|
$field = $query_field['field'];
|
112 |
|
|
$this->date_handler = $query_field['date_handler'];
|
113 |
|
|
|
114 |
|
|
// Respect relationships when determining the table alias.
|
115 |
|
|
if ($field['table_name'] != $this->table || !empty($this->relationship)) {
|
116 |
|
|
$this->related_table_alias = $this->query->ensure_table($field['table_name'], $this->relationship);
|
117 |
|
|
}
|
118 |
b720ea3e
|
Assos Assos
|
else {
|
119 |
1f683914
|
Assos Assos
|
$this->related_table_alias = NULL;
|
120 |
b720ea3e
|
Assos Assos
|
}
|
121 |
85ad3d82
|
Assos Assos
|
$table_alias = !empty($this->related_table_alias) ? $this->related_table_alias : $field['table_name'];
|
122 |
|
|
$field_name = $table_alias . '.' . $field['field_name'];
|
123 |
|
|
|
124 |
|
|
// Call the appropriate function, either 'op_between' or 'op_simple'.
|
125 |
|
|
parent::$function($field_name);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
// Gather all of the condition strings and their placeholders.
|
129 |
|
|
$conditions = array();
|
130 |
|
|
$placeholders = array();
|
131 |
|
|
foreach ($this->query->where[$this->options['date_group']]['conditions'] as $condition) {
|
132 |
|
|
$conditions[] = $condition['field'];
|
133 |
|
|
$placeholders += $condition['value'];
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
// Remove the conditions from the custom filter group.
|
137 |
|
|
unset($this->query->where[$this->options['date_group']]);
|
138 |
|
|
|
139 |
|
|
// Combine all of the conditions into one string.
|
140 |
|
|
$conditions = implode(' ' . $this->options['date_method'] . ' ', $conditions);
|
141 |
|
|
|
142 |
|
|
// Add it to the filter group chosen in the Views UI.
|
143 |
|
|
$this->query->add_where_expression($this->options['group'], $conditions, $placeholders);
|
144 |
|
|
}
|
145 |
|
|
|
146 |
599a39cd
|
Assos Assos
|
/**
|
147 |
|
|
* {@inheritdoc}
|
148 |
|
|
*/
|
149 |
85ad3d82
|
Assos Assos
|
function extra_options_form(&$form, &$form_state) {
|
150 |
|
|
parent::extra_options_form($form, $form_state);
|
151 |
|
|
|
152 |
|
|
$fields = date_views_fields($this->base_table);
|
153 |
|
|
$options = array();
|
154 |
|
|
foreach ($fields['name'] as $name => $field) {
|
155 |
|
|
$options[$name] = $field['label'];
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
$form['date_fields'] = array(
|
159 |
|
|
'#title' => t('Date field(s)'),
|
160 |
|
|
'#type' => 'checkboxes',
|
161 |
|
|
'#options' => $options,
|
162 |
|
|
'#default_value' => $this->options['date_fields'],
|
163 |
|
|
'#multiple' => FALSE,
|
164 |
|
|
'#description' => t('Select date field(s) to filter.'),
|
165 |
|
|
'#required' => TRUE,
|
166 |
|
|
);
|
167 |
|
|
$form['date_method'] = array(
|
168 |
|
|
'#title' => t('Method'),
|
169 |
|
|
'#type' => 'radios',
|
170 |
|
|
'#options' => array('OR' => t('OR'), 'AND' => t('AND')),
|
171 |
|
|
'#default_value' => $this->options['date_method'],
|
172 |
|
|
'#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).'),
|
173 |
|
|
);
|
174 |
|
|
}
|
175 |
|
|
|
176 |
599a39cd
|
Assos Assos
|
/**
|
177 |
|
|
* {@inheritdoc}
|
178 |
|
|
*/
|
179 |
85ad3d82
|
Assos Assos
|
function extra_options_validate($form, &$form_state) {
|
180 |
|
|
$check_fields = array_filter($form_state['values']['options']['date_fields']);
|
181 |
|
|
if (empty($check_fields)) {
|
182 |
|
|
form_error($form['date_fields'], t('You must select at least one date field for this filter.'));
|
183 |
|
|
}
|
184 |
|
|
}
|
185 |
|
|
|
186 |
599a39cd
|
Assos Assos
|
/**
|
187 |
|
|
* {@inheritdoc}
|
188 |
|
|
*/
|
189 |
85ad3d82
|
Assos Assos
|
function extra_options_submit($form, &$form_state) {
|
190 |
|
|
$form_state['values']['options']['date_fields'] = array_filter($form_state['values']['options']['date_fields']);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
599a39cd
|
Assos Assos
|
/**
|
194 |
|
|
* {@inheritdoc}
|
195 |
|
|
*/
|
196 |
85ad3d82
|
Assos Assos
|
function admin_summary() {
|
197 |
|
|
if (empty($this->options['date_fields'])) {
|
198 |
|
|
return t('Missing date fields!');
|
199 |
|
|
}
|
200 |
|
|
$handler = $this->date_handler;
|
201 |
|
|
|
202 |
|
|
$fields = date_views_fields($this->view->base_table);
|
203 |
|
|
if (!empty($this->options['date_fields'])) {
|
204 |
|
|
$output = array();
|
205 |
|
|
foreach ($this->options['date_fields'] as $field) {
|
206 |
|
|
if (array_key_exists($field, $fields['name'])) {
|
207 |
|
|
$output[] = $fields['name'][$field]['label'];
|
208 |
|
|
}
|
209 |
|
|
}
|
210 |
|
|
}
|
211 |
|
|
$field = implode(' ' . $this->options['date_method'] . ' ', $output);
|
212 |
|
|
$output = "$field " . check_plain($this->operator) . ' ';
|
213 |
|
|
$parts = $handler->date_parts();
|
214 |
|
|
$widget_options = $this->widget_options();
|
215 |
|
|
// If the filter is exposed, display the granularity.
|
216 |
|
|
if ($this->options['exposed']) {
|
217 |
|
|
return t('(@field) <strong>Exposed</strong> @widget @format', array('@field' => $field, '@format' => $parts[$handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
|
218 |
|
|
}
|
219 |
|
|
// If not exposed, display the value.
|
220 |
|
|
if (in_array($this->operator, $this->operator_values(2))) {
|
221 |
|
|
$min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
|
222 |
|
|
$max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
|
223 |
|
|
$output .= t('@min and @max', array('@min' => $min, '@max' => $max));
|
224 |
|
|
}
|
225 |
|
|
else {
|
226 |
|
|
$output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
|
227 |
|
|
}
|
228 |
|
|
return $output;
|
229 |
|
|
}
|
230 |
|
|
|
231 |
599a39cd
|
Assos Assos
|
/**
|
232 |
|
|
* @todo
|
233 |
|
|
*/
|
234 |
85ad3d82
|
Assos Assos
|
function get_query_fields() {
|
235 |
|
|
$fields = date_views_fields($this->base_table);
|
236 |
|
|
$fields = $fields['name'];
|
237 |
|
|
$this->query_fields = array();
|
238 |
|
|
foreach ((array) $this->options['date_fields'] as $delta => $name) {
|
239 |
|
|
if (array_key_exists($name, $fields) && $field = $fields[$name]) {
|
240 |
|
|
$date_handler = new date_sql_handler($field['sql_type'], date_default_timezone());
|
241 |
|
|
$date_handler->granularity = $this->options['granularity'];
|
242 |
|
|
$date_handler->db_timezone = date_get_timezone_db($field['tz_handling']);
|
243 |
|
|
$date_handler->local_timezone = date_get_timezone($field['tz_handling']);
|
244 |
|
|
$this->query_fields[] = array('field' => $field, 'date_handler' => $date_handler);
|
245 |
|
|
}
|
246 |
|
|
}
|
247 |
|
|
}
|
248 |
599a39cd
|
Assos Assos
|
|
249 |
85ad3d82
|
Assos Assos
|
} |