Révision 599a39cd
Ajouté par Assos Assos il y a environ 3 ans
drupal7/sites/all/modules/date/date_views/includes/date_views_filter_handler_simple.inc | ||
---|---|---|
1 | 1 |
<?php |
2 |
|
|
2 | 3 |
/** |
3 | 4 |
* @file |
4 |
* A standard Views filter for a single date field, |
|
5 |
* using Date API form selectors and sql handling. |
|
5 |
* A standard Views filter for a single date field. |
|
6 | 6 |
*/ |
7 | 7 |
|
8 |
// @codingStandardsIgnoreStart |
|
8 |
/** |
|
9 |
* A standard Views filter for a single date field. |
|
10 |
*/ |
|
9 | 11 |
class date_views_filter_handler_simple extends views_handler_filter_date { |
10 |
var $date_handler = NULL; |
|
11 |
var $offset = NULL; |
|
12 | 12 |
|
13 |
/** |
|
14 |
* @var object |
|
15 |
*/ |
|
16 |
public $date_handler = NULL; |
|
17 |
|
|
18 |
/** |
|
19 |
* @var int |
|
20 |
*/ |
|
21 |
public $offset = NULL; |
|
22 |
|
|
23 |
/** |
|
24 |
* {@inheritdoc} |
|
25 |
*/ |
|
13 | 26 |
function init(&$view, &$options) { |
14 | 27 |
parent::init($view, $options); |
15 | 28 |
module_load_include('inc', 'date_api', 'date_api_sql'); |
... | ... | |
26 | 39 |
$this->date_handler->granularity = isset($options['granularity']) ? $options['granularity'] : 'day'; |
27 | 40 |
$this->format = $this->date_handler->views_formats($this->options['granularity'], 'sql'); |
28 | 41 |
|
29 |
// Identify the base table for this field. |
|
30 |
// It will be used to call for the right query field options.
|
|
42 |
// Identify the base table for this field. It will be used to call for the
|
|
43 |
// right query field options. |
|
31 | 44 |
$this->base_table = $this->table; |
32 |
|
|
33 | 45 |
} |
34 | 46 |
|
35 |
// Set default values for the date filter. |
|
47 |
/** |
|
48 |
* {@inheritdoc} |
|
49 |
*/ |
|
36 | 50 |
function option_definition() { |
37 | 51 |
$options = parent::option_definition(); |
38 | 52 |
$options['granularity'] = array('default' => 'day'); |
... | ... | |
44 | 58 |
return $options; |
45 | 59 |
} |
46 | 60 |
|
61 |
/** |
|
62 |
* {@inheritdoc} |
|
63 |
*/ |
|
47 | 64 |
function operators() { |
65 |
// Cache the operators that are being added. |
|
66 |
static $new_operators; |
|
67 |
if (!isset($new_operators)) { |
|
68 |
$new_operators = array( |
|
69 |
'title' => t('Contains'), |
|
70 |
'method' => 'op_contains', |
|
71 |
'short' => t('contains'), |
|
72 |
'values' => 1, |
|
73 |
); |
|
74 |
} |
|
75 |
|
|
76 |
// Add the new 'contains' operators. |
|
48 | 77 |
$operators = parent::operators(); |
49 |
$operators['contains'] = array( |
|
50 |
'title' => t('Contains'), |
|
51 |
'method' => 'op_contains', |
|
52 |
'short' => t('contains'), |
|
53 |
'values' => 1, |
|
54 |
); |
|
78 |
$operators['contains'] = $new_operators; |
|
55 | 79 |
return $operators; |
56 | 80 |
} |
57 | 81 |
|
... | ... | |
91 | 115 |
/** |
92 | 116 |
* Helper function to see if we need to swap in the default value. |
93 | 117 |
* |
94 |
* Views exposed filters treat everything as submitted, so if it's an empty value we have to
|
|
95 |
* see if anything actually was submitted. If nothing has really been submitted, we need
|
|
96 |
* to swap in our default value logic. |
|
118 |
* Views exposed filters treat everything as submitted, so if it's an empty |
|
119 |
* value we have to see if anything actually was submitted. If nothing has
|
|
120 |
* really been submitted, we need to swap in our default value logic.
|
|
97 | 121 |
*/ |
98 | 122 |
function get_filter_value($prefix, $input) { |
99 | 123 |
// All our date widgets provide datetime values but we use ISO in our SQL |
... | ... | |
111 | 135 |
return str_replace(' ', 'T', $input); |
112 | 136 |
} |
113 | 137 |
|
138 |
/** |
|
139 |
* {@inheritdoc} |
|
140 |
*/ |
|
114 | 141 |
function accept_exposed_input($input) { |
115 | 142 |
if (!empty($this->options['exposed'])) { |
116 | 143 |
$element_input = $input[$this->options['expose']['identifier']]; |
... | ... | |
127 | 154 |
$input[$this->options['expose']['identifier']] = $element_input; |
128 | 155 |
} |
129 | 156 |
return parent::accept_exposed_input($input); |
130 |
|
|
131 | 157 |
} |
132 | 158 |
|
159 |
/** |
|
160 |
* @todo |
|
161 |
*/ |
|
133 | 162 |
function op_between($field) { |
134 |
|
|
135 |
// Add the delta field to the view so we can later find the value that matched our query.
|
|
163 |
// Add the delta field to the view so we can later find the value that |
|
164 |
// matched our query. |
|
136 | 165 |
list($table_name, $field_name) = explode('.', $field); |
137 | 166 |
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) { |
138 | 167 |
$this->query->add_field($table_name, 'delta'); |
... | ... | |
160 | 189 |
} |
161 | 190 |
} |
162 | 191 |
|
192 |
/** |
|
193 |
* @todo |
|
194 |
*/ |
|
163 | 195 |
function op_simple($field) { |
164 |
|
|
165 |
// Add the delta field to the view so we can later find the value that matched our query.
|
|
196 |
// Add the delta field to the view so we can later find the value that |
|
197 |
// matched our query. |
|
166 | 198 |
list($table_name, $field_name) = explode('.', $field); |
167 | 199 |
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) { |
168 | 200 |
$this->query->add_field($table_name, 'delta'); |
... | ... | |
180 | 212 |
$this->query->add_where_expression($group, "$field $this->operator $placeholder", array($placeholder => $value)); |
181 | 213 |
} |
182 | 214 |
|
215 |
/** |
|
216 |
* @todo |
|
217 |
*/ |
|
183 | 218 |
function op_contains($field) { |
184 |
|
|
185 |
// Add the delta field to the view so we can later find the value that matched our query.
|
|
219 |
// Add the delta field to the view so we can later find the value that |
|
220 |
// matched our query. |
|
186 | 221 |
list($table_name, $field_name) = explode('.', $field); |
187 | 222 |
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) { |
188 | 223 |
$this->query->add_field($table_name, 'delta'); |
... | ... | |
204 | 239 |
} |
205 | 240 |
|
206 | 241 |
/** |
207 |
* Set the granularity of the date parts to use in the filter. |
|
208 |
*/ |
|
209 |
function has_extra_options() { return TRUE; } |
|
242 |
* {@inheritdoc} |
|
243 |
*/ |
|
244 |
function has_extra_options() { |
|
245 |
return TRUE; |
|
246 |
} |
|
210 | 247 |
|
211 | 248 |
/** |
212 |
* Date selection options.
|
|
249 |
* {@inheritdoc}
|
|
213 | 250 |
*/ |
214 | 251 |
function widget_options() { |
215 | 252 |
$options = array( |
... | ... | |
223 | 260 |
return $options; |
224 | 261 |
} |
225 | 262 |
|
263 |
/** |
|
264 |
* @todo |
|
265 |
*/ |
|
226 | 266 |
function year_range() { |
227 | 267 |
$year_range = explode(':', $this->options['year_range']); |
228 | 268 |
if (substr($this->options['year_range'], 0, 1) == '-' || $year_range[0] < 0) { |
... | ... | |
233 | 273 |
return $year_range; |
234 | 274 |
} |
235 | 275 |
|
276 |
/** |
|
277 |
* {@inheritdoc} |
|
278 |
*/ |
|
236 | 279 |
function extra_options_form(&$form, &$form_state) { |
237 | 280 |
parent::extra_options_form($form, $form_state); |
238 | 281 |
$form['form_type'] = array( |
... | ... | |
264 | 307 |
); |
265 | 308 |
} |
266 | 309 |
|
310 |
/** |
|
311 |
* {@inheritdoc} |
|
312 |
*/ |
|
267 | 313 |
function extra_options_validate($form, &$form_state) { |
268 | 314 |
if (!preg_match('/^(?:[\+\-][0-9]{1,4}|[0-9]{4}):(?:[\+\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) { |
269 | 315 |
form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9')); |
... | ... | |
271 | 317 |
} |
272 | 318 |
|
273 | 319 |
/** |
274 |
* Add the selectors to the value form using the date handler.
|
|
320 |
* {@inheritdoc}
|
|
275 | 321 |
*/ |
276 | 322 |
function value_form(&$form, &$form_state) { |
277 |
// We use different values than the parent form, so we must |
|
278 |
// construct our own form element.
|
|
323 |
// We use different values than the parent form, so we must construct our
|
|
324 |
// own form element. |
|
279 | 325 |
$form['value'] = array(); |
280 | 326 |
$form['value']['#tree'] = TRUE; |
327 |
$form['value']['#id'] = 'date_views_exposed_filter-' . bin2hex(drupal_random_bytes(16)); |
|
328 |
$form['value']['#type'] = 'container'; |
|
329 |
if (module_exists('ctools')) { |
|
330 |
$form['value']['#pre_render'] = array('ctools_dependent_pre_render'); |
|
331 |
} |
|
281 | 332 |
|
282 | 333 |
// Below section copied from views_handler_filter_numeric.inc. |
283 | 334 |
$which = 'all'; |
... | ... | |
288 | 339 |
|
289 | 340 |
$identifier = $this->options['expose']['identifier']; |
290 | 341 |
if (!empty($form_state['exposed'])) { |
291 |
|
|
292 | 342 |
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) { |
293 |
// exposed and locked.
|
|
343 |
// Exposed and locked.
|
|
294 | 344 |
$which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value'; |
295 | 345 |
} |
296 | 346 |
else { |
... | ... | |
307 | 357 |
$form['value'] += $this->date_parts_form($form_state, 'max', $source, $which, $this->operator_values(2), $identifier, 'default_to_date'); |
308 | 358 |
} |
309 | 359 |
|
310 |
// Add some extra validation for the select widget to be sure that |
|
311 |
// the user inputs all parts of the date.
|
|
360 |
// Add some extra validation for the select widget to be sure that the user
|
|
361 |
// inputs all parts of the date. |
|
312 | 362 |
if ($this->options['form_type'] == 'date_select') { |
313 | 363 |
$form['value']['#element_validate'] = array('date_views_select_validate'); |
314 | 364 |
} |
... | ... | |
341 | 391 |
$label = t('Start date'); |
342 | 392 |
$relative_label = t('Relative start date'); |
343 | 393 |
break; |
394 |
|
|
344 | 395 |
case 'max': |
345 | 396 |
$label = t('End date'); |
346 | 397 |
$relative_label = t('Relative end date'); |
347 | 398 |
break; |
399 |
|
|
348 | 400 |
default: |
349 | 401 |
$label = ''; |
350 | 402 |
$relative_label = t('Relative date'); |
351 |
break; |
|
352 | 403 |
} |
353 | 404 |
|
354 | 405 |
$type = $this->options['form_type']; |
... | ... | |
362 | 413 |
|
363 | 414 |
if (!empty($form_state['exposed'])) { |
364 | 415 |
// UI when the date selector is exposed. |
416 |
$label = $this->options['expose']['label']; |
|
365 | 417 |
$default_date = $this->date_default_value($prefix); |
366 | 418 |
$id = 'edit-' . str_replace('_', '-', $this->field) . '-' . $prefix; |
367 | 419 |
$form[$prefix] = array( |
368 | 420 |
'#title' => check_plain($label), |
421 |
'#title_display' => 'invisible', |
|
369 | 422 |
'#type' => $type, |
370 | 423 |
'#size' => 20, |
371 | 424 |
'#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date, |
... | ... | |
380 | 433 |
$form[$prefix]['#pre_render'][] = 'ctools_dependent_pre_render'; |
381 | 434 |
$form[$prefix]['#dependency'] = array($source => $operator_values); |
382 | 435 |
} |
436 |
|
|
437 |
// Validate the input value; set form state input array. |
|
438 |
$input = isset($form_state['input'][$identifier][$prefix]) ? $form_state['input'][$identifier][$prefix] : array(); |
|
439 |
$form_state['input'][$identifier][$prefix] = $this->input_validate($form[$prefix], $input); |
|
440 |
|
|
383 | 441 |
if (!isset($form_state['input'][$identifier][$prefix])) { |
442 |
// Handle bogus input from the query string to prevent fatal errors. |
|
443 |
if (isset($form_state['input'][$identifier]) && !is_array($form_state['input'][$identifier])) { |
|
444 |
$form_state['input'][$identifier] = array(); |
|
445 |
} |
|
384 | 446 |
// Ensure these exist. |
385 | 447 |
foreach ($granularity as $key) { |
386 | 448 |
$form_state['input'][$identifier][$prefix][$key] = NULL; |
... | ... | |
438 | 500 |
return $form; |
439 | 501 |
} |
440 | 502 |
|
503 |
/** |
|
504 |
* Returns form state input array appropriate to missing, valid, and invalid input. |
|
505 |
*/ |
|
506 |
function input_validate($element, $input) { |
|
507 |
if (!$input) { |
|
508 |
// No input value (e.g. initial form load), set value to NULL so Form API |
|
509 |
// calls the element value_callback routine with FALSE to set the '#value' |
|
510 |
// property from the '#default_value' property. |
|
511 |
// See _form_builder_handle_input_element(). |
|
512 |
if ($element['#type'] != 'date_select') { |
|
513 |
return array('date' => NULL); |
|
514 |
} |
|
515 |
$granularity = date_format_order($element['#date_format']); |
|
516 |
if ($key = array_search('timezone', $granularity)) { |
|
517 |
unset($granularity[$key]); |
|
518 |
} |
|
519 |
return array_fill_keys($granularity, NULL); |
|
520 |
} |
|
521 |
|
|
522 |
// Include element defaults to avoid notices in the xxx_input_date routines. |
|
523 |
// Copied from form_builder() in form.inc. |
|
524 |
if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = element_info($element['#type']))) { |
|
525 |
// Overlay $info onto $element, retaining preexisting keys in $element. |
|
526 |
$element += $info; |
|
527 |
$element['#defaults_loaded'] = TRUE; |
|
528 |
} |
|
529 |
|
|
530 |
$date = NULL; |
|
531 |
$function = "{$element['#type']}_input_date"; |
|
532 |
if (($date = $function($element, $input)) && date_is_date($date)) { |
|
533 |
// The input is valid including being in the expected format. |
|
534 |
return $input; |
|
535 |
} |
|
536 |
|
|
537 |
if (is_object($date)) { |
|
538 |
// Input may not meet the granularity and format but is recognizable. |
|
539 |
return $input; |
|
540 |
} |
|
541 |
|
|
542 |
// Input is bogus, return empty array in granularity format. |
|
543 |
if ($element['#type'] != 'date_select') { |
|
544 |
return array('date' => ''); |
|
545 |
} |
|
546 |
|
|
547 |
$granularity = date_format_order($element['#date_format']); |
|
548 |
if ($key = array_search('timezone', $granularity)) { |
|
549 |
unset($granularity[$key]); |
|
550 |
} |
|
551 |
return array_fill_keys($granularity, ''); |
|
552 |
} |
|
553 |
|
|
441 | 554 |
/** |
442 | 555 |
* Value validation. |
443 | 556 |
* |
444 |
* TODO add in more validation.
|
|
557 |
* @todo Add in more validation.
|
|
445 | 558 |
* |
446 |
* We are setting an extra option using a value form |
|
447 |
* because it makes more sense to set it there. |
|
448 |
* That's not the normal method, so we have to manually |
|
559 |
* We are setting an extra option using a value form because it makes more |
|
560 |
* sense to set it there. That's not the normal method, so we have to manually |
|
449 | 561 |
* transfer the selected value back to the option. |
450 | 562 |
*/ |
451 | 563 |
function value_validate($form, &$form_state) { |
452 |
|
|
453 | 564 |
$options = &$form_state['values']['options']; |
454 | 565 |
|
455 | 566 |
if ($options['operator'] == 'between' || $options['operator'] == 'not between') { |
... | ... | |
459 | 570 |
} |
460 | 571 |
else { |
461 | 572 |
$this->options['default_date'] = $options['value']['min_group']['default_date']; |
462 |
// NULL out the value field, user wanted the relative value to take hold. |
|
573 |
// NULL out the value field, user wanted the relative value to take |
|
574 |
// hold. |
|
463 | 575 |
$options['value']['min_group']['min'] = NULL; |
464 | 576 |
} |
465 | 577 |
} |
... | ... | |
473 | 585 |
} |
474 | 586 |
else { |
475 | 587 |
$this->options['default_to_date'] = $options['value']['max_group']['default_to_date']; |
476 |
// NULL out the value field, user wanted the relative value to take hold. |
|
588 |
// NULL out the value field, user wanted the relative value to take |
|
589 |
// hold. |
|
477 | 590 |
$options['value']['max_group']['max'] = NULL; |
478 | 591 |
} |
479 | 592 |
} |
... | ... | |
489 | 602 |
} |
490 | 603 |
else { |
491 | 604 |
$this->options['default_date'] = $options['value']['value_group']['default_date']; |
492 |
// NULL out the value field, user wanted the relative value to take hold. |
|
605 |
// NULL out the value field, user wanted the relative value to take |
|
606 |
// hold. |
|
493 | 607 |
$options['value']['value_group']['value'] = NULL; |
494 | 608 |
} |
495 | 609 |
} |
... | ... | |
512 | 626 |
// Our date widgets do their own validation. |
513 | 627 |
} |
514 | 628 |
|
515 |
// Update the summary values to provide |
|
516 |
// meaningful information for each option. |
|
629 |
/** |
|
630 |
* {@inheritdoc} |
|
631 |
*/ |
|
517 | 632 |
function admin_summary() { |
518 | 633 |
$parts = $this->date_handler->date_parts(); |
519 | 634 |
$widget_options = $this->widget_options(); |
... | ... | |
535 | 650 |
} |
536 | 651 |
|
537 | 652 |
} |
538 |
// @codingStandardsIgnoreEnd |
Formats disponibles : Unified diff
Weekly update of contrib modules