handler_filter_date.inc 2.7 KB

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