handler_filter_date.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * Contains SearchApiViewsHandlerFilterDate.
  5. */
  6. /**
  7. * Views filter handler base class for handling date fields.
  8. */
  9. class SearchApiViewsHandlerFilterDate extends SearchApiViewsHandlerFilterNumeric {
  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. 'date_popup_format' => array('default' => 'm/d/Y'),
  17. 'year_range' => array('default' => '-3:+3'),
  18. );
  19. }
  20. /**
  21. * If the date popup module is enabled, provide the extra option setting.
  22. */
  23. public function has_extra_options() {
  24. if (module_exists('date_popup')) {
  25. return TRUE;
  26. }
  27. return FALSE;
  28. }
  29. /**
  30. * Add extra options if we allow the date popup widget.
  31. */
  32. public function extra_options_form(&$form, &$form_state) {
  33. parent::extra_options_form($form, $form_state);
  34. if (module_exists('date_popup')) {
  35. $widget_options = array(
  36. 'default' => 'Default',
  37. 'date_popup' => 'Date popup',
  38. );
  39. $form['widget_type'] = array(
  40. '#type' => 'radios',
  41. '#title' => t('Date selection form element'),
  42. '#default_value' => $this->options['widget_type'],
  43. '#options' => $widget_options,
  44. );
  45. $form['date_popup_format'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Date format'),
  48. '#default_value' => $this->options['date_popup_format'],
  49. '#description' => t('A date in any format understood by <a href="@doc-link">PHP</a>. For example, "Y-m-d" or "m/d/Y".', array(
  50. '@doc-link' => 'http://php.net/manual/en/function.date.php'
  51. )),
  52. '#states' => array(
  53. 'visible' => array(
  54. ':input[name="options[widget_type]"]' => array('value' => 'date_popup'),
  55. ),
  56. ),
  57. );
  58. }
  59. if (module_exists('date_api')) {
  60. $form['year_range'] = array(
  61. '#type' => 'date_year_range',
  62. '#default_value' => $this->options['year_range'],
  63. );
  64. }
  65. }
  66. /**
  67. * Validate extra options.
  68. */
  69. public function extra_options_validate($form, &$form_state) {
  70. if (isset($form_state['values']['options']['year_range'])) {
  71. if (!preg_match('/^(?:\-[0-9]{1,4}|[0-9]{4}):(?:[\+|\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
  72. form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9'));
  73. }
  74. }
  75. }
  76. /**
  77. * Provide a form for setting the filter value.
  78. */
  79. public function value_form(&$form, &$form_state) {
  80. parent::value_form($form, $form_state);
  81. $is_date_popup = ($this->options['widget_type'] == 'date_popup' && module_exists('date_popup'));
  82. // If the operator is between
  83. if ($this->operator == 'between') {
  84. if ($is_date_popup) {
  85. $form['value']['min']['#type'] = 'date_popup';
  86. $form['value']['min']['#date_format'] = $this->options['date_popup_format'];
  87. $form['value']['min']['#date_year_range'] = $this->options['year_range'];
  88. $form['value']['max']['#type'] = 'date_popup';
  89. $form['value']['max']['#date_format'] = $this->options['date_popup_format'];
  90. $form['value']['max']['#date_year_range'] = $this->options['year_range'];
  91. }
  92. }
  93. // If we are using the date popup widget, overwrite the settings of the form
  94. // according to what date_popup expects.
  95. elseif ($is_date_popup) {
  96. $form['value']['#type'] = 'date_popup';
  97. $form['value']['#date_format'] = $this->options['date_popup_format'];
  98. $form['value']['#date_year_range'] = $this->options['year_range'];
  99. unset($form['value']['#description']);
  100. }
  101. elseif (empty($form_state['exposed'])) {
  102. $form['value']['#description'] = t('A date in any format understood by <a href="@doc-link">PHP</a>. For example, "@date1" or "@date2".', array(
  103. '@doc-link' => 'http://php.net/manual/en/function.strtotime.php',
  104. '@date1' => format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s'),
  105. '@date2' => 'now + 1 day',
  106. ));
  107. }
  108. }
  109. /**
  110. * Add this filter to the query.
  111. */
  112. public function query() {
  113. $this->normalizeValue();
  114. if ($this->operator === 'empty') {
  115. $this->query->condition($this->real_field, NULL, '=', $this->options['group']);
  116. }
  117. elseif ($this->operator === 'not empty') {
  118. $this->query->condition($this->real_field, NULL, '<>', $this->options['group']);
  119. }
  120. elseif (in_array($this->operator, array('between', 'not between'), TRUE)) {
  121. $min = $this->value['min'];
  122. if ($min !== '') {
  123. $min = is_numeric($min) ? $min : strtotime($min, REQUEST_TIME);
  124. }
  125. $max = $this->value['max'];
  126. if ($max !== '') {
  127. $max = is_numeric($max) ? $max : strtotime($max, REQUEST_TIME);
  128. }
  129. if (is_numeric($min) && is_numeric($max)) {
  130. $this->query->condition($this->real_field, array($min, $max), strtoupper($this->operator), $this->options['group']);
  131. }
  132. elseif (is_numeric($min)) {
  133. $operator = $this->operator === 'between' ? '>=' : '<';
  134. $this->query->condition($this->real_field, $min, $operator, $this->options['group']);
  135. }
  136. elseif (is_numeric($max)) {
  137. $operator = $this->operator === 'between' ? '<=' : '>';
  138. $this->query->condition($this->real_field, $min, $operator, $this->options['group']);
  139. }
  140. }
  141. else {
  142. $v = is_numeric($this->value['value']) ? $this->value['value'] : strtotime($this->value['value'], REQUEST_TIME);
  143. if ($v !== FALSE) {
  144. $this->query->condition($this->real_field, $v, $this->operator, $this->options['group']);
  145. }
  146. }
  147. }
  148. }