handler_filter_date.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Views filter handler base class for handling all "normal" cases.
  4. */
  5. class SearchApiViewsHandlerFilterDate extends SearchApiViewsHandlerFilter {
  6. /**
  7. * Add a "widget type" option.
  8. */
  9. public function option_definition() {
  10. return parent::option_definition() + array(
  11. 'widget_type' => array('default' => 'default'),
  12. );
  13. }
  14. /**
  15. * If the date popup module is enabled, provide the extra option setting.
  16. */
  17. public function has_extra_options() {
  18. if (module_exists('date_popup')) {
  19. return TRUE;
  20. }
  21. return FALSE;
  22. }
  23. /**
  24. * Add extra options if we allow the date popup widget.
  25. */
  26. public function extra_options_form(&$form, &$form_state) {
  27. parent::extra_options_form($form, $form_state);
  28. if (module_exists('date_popup')) {
  29. $widget_options = array('default' => 'Default', 'date_popup' => 'Date popup');
  30. $form['widget_type'] = array(
  31. '#type' => 'radios',
  32. '#title' => t('Date selection form element'),
  33. '#default_value' => $this->options['widget_type'],
  34. '#options' => $widget_options,
  35. );
  36. }
  37. }
  38. /**
  39. * Provide a form for setting the filter value.
  40. */
  41. public function value_form(&$form, &$form_state) {
  42. parent::value_form($form, $form_state);
  43. // If we are using the date popup widget, overwrite the settings of the form
  44. // according to what date_popup expects.
  45. if ($this->options['widget_type'] == 'date_popup' && module_exists('date_popup')) {
  46. $form['value']['#type'] = 'date_popup';
  47. $form['value']['#date_format'] = 'm/d/Y';
  48. unset($form['value']['#description']);
  49. }
  50. elseif (empty($form_state['exposed'])) {
  51. $form['value']['#description'] = t('A date in any format understood by <a href="@doc-link">PHP</a>. For example, "@date1" or "@date2".', array(
  52. '@doc-link' => 'http://php.net/manual/en/function.strtotime.php',
  53. '@date1' => format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s'),
  54. '@date2' => 'now + 1 day',
  55. ));
  56. }
  57. }
  58. /**
  59. * Add this filter to the query.
  60. */
  61. public function query() {
  62. if ($this->operator === 'empty') {
  63. $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
  64. }
  65. elseif ($this->operator === 'not empty') {
  66. $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
  67. }
  68. else {
  69. while (is_array($this->value)) {
  70. $this->value = $this->value ? reset($this->value) : NULL;
  71. }
  72. $v = is_numeric($this->value) ? $this->value : strtotime($this->value, REQUEST_TIME);
  73. if ($v !== FALSE) {
  74. $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
  75. }
  76. }
  77. }
  78. }