views_handler_filter_date.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_filter_date.
  5. */
  6. /**
  7. * Filter to handle dates stored as a timestamp.
  8. *
  9. * @ingroup views_filter_handlers
  10. */
  11. class views_handler_filter_date extends views_handler_filter_numeric {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function option_definition() {
  16. $options = parent::option_definition();
  17. // Value is already set up properly, we're just adding our new field to it.
  18. $options['value']['contains']['type']['default'] = 'date';
  19. return $options;
  20. }
  21. /**
  22. * Add a type selector to the value form.
  23. */
  24. public function value_form(&$form, &$form_state) {
  25. if (empty($form_state['exposed'])) {
  26. $form['value']['type'] = array(
  27. '#type' => 'radios',
  28. '#title' => t('Value type'),
  29. '#options' => array(
  30. 'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
  31. 'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
  32. ),
  33. '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
  34. );
  35. }
  36. parent::value_form($form, $form_state);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function options_validate(&$form, &$form_state) {
  42. parent::options_validate($form, $form_state);
  43. if (!empty($this->options['exposed']) && empty($form_state['values']['options']['expose']['required'])) {
  44. // Who cares what the value is if it's exposed and non-required.
  45. return;
  46. }
  47. $this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function exposed_validate(&$form, &$form_state) {
  53. if (empty($this->options['exposed'])) {
  54. return;
  55. }
  56. if (empty($this->options['expose']['required'])) {
  57. // Who cares what the value is if it's exposed and non-required.
  58. return;
  59. }
  60. $value = &$form_state['values'][$this->options['expose']['identifier']];
  61. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
  62. $operator = $form_state['values'][$this->options['expose']['operator_id']];
  63. }
  64. else {
  65. $operator = $this->operator;
  66. }
  67. $this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
  68. }
  69. /**
  70. * Validate that the time values convert to something usable.
  71. */
  72. public function validate_valid_time(&$form, $operator, $value) {
  73. $operators = $this->operators();
  74. if ($operators[$operator]['values'] == 1) {
  75. $convert = strtotime($value['value']);
  76. if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
  77. form_error($form['value'], t('Invalid date format.'));
  78. }
  79. }
  80. elseif ($operators[$operator]['values'] == 2) {
  81. $min = strtotime($value['min']);
  82. if ($min == -1 || $min === FALSE) {
  83. form_error($form['min'], t('Invalid date format.'));
  84. }
  85. $max = strtotime($value['max']);
  86. if ($max == -1 || $max === FALSE) {
  87. form_error($form['max'], t('Invalid date format.'));
  88. }
  89. }
  90. }
  91. /**
  92. * Validate the build group options form.
  93. */
  94. public function build_group_validate($form, &$form_state) {
  95. // Special case to validate grouped date filters, this is because the
  96. // $group['value'] array contains the type of filter (date or offset)
  97. // and therefore the number of items the comparission has to be done
  98. // against 'one' instead of 'zero'.
  99. foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
  100. if (empty($group['remove'])) {
  101. // Check if the title is defined but value wasn't defined.
  102. if (!empty($group['title'])) {
  103. if ((!is_array($group['value']) && empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) == 1)) {
  104. form_error($form['group_info']['group_items'][$id]['value'], t('The value is required if title for this item is defined.'));
  105. }
  106. }
  107. // Check if the value is defined but title wasn't defined.
  108. if ((!is_array($group['value']) && !empty($group['value'])) || (is_array($group['value']) && count(array_filter($group['value'])) > 1)) {
  109. if (empty($group['title'])) {
  110. form_error($form['group_info']['group_items'][$id]['title'], t('The title is required if value for this item is defined.'));
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function accept_exposed_input($input) {
  120. if (empty($this->options['exposed'])) {
  121. return TRUE;
  122. }
  123. // Store this because it will get overwritten.
  124. $type = $this->value['type'];
  125. $rc = parent::accept_exposed_input($input);
  126. // Don't filter if value(s) are empty.
  127. $operators = $this->operators();
  128. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
  129. $operator = $input[$this->options['expose']['operator_id']];
  130. }
  131. else {
  132. $operator = $this->operator;
  133. }
  134. if ($operators[$operator]['values'] == 1) {
  135. if ($this->value['value'] == '') {
  136. return FALSE;
  137. }
  138. }
  139. elseif ($operators[$operator]['values'] == 2) {
  140. if ($this->value['min'] == '' || $this->value['max'] == '') {
  141. return FALSE;
  142. }
  143. }
  144. // Restore what got overwritten by the parent.
  145. $this->value['type'] = $type;
  146. return $rc;
  147. }
  148. /**
  149. *
  150. */
  151. public function op_between($field) {
  152. // Use the substitutions to ensure a consistent timestamp.
  153. $query_substitutions = views_views_query_substitutions($this->view);
  154. $a = intval(strtotime($this->value['min'], $query_substitutions['***CURRENT_TIME***']));
  155. $b = intval(strtotime($this->value['max'], $query_substitutions['***CURRENT_TIME***']));
  156. // This is safe because we are manually scrubbing the values. It is
  157. // necessary to do it this way because $a and $b are formulas when using an
  158. // offset.
  159. $operator = strtoupper($this->operator);
  160. $this->query->add_where_expression($this->options['group'], "$field $operator $a AND $b");
  161. }
  162. /**
  163. *
  164. */
  165. public function op_simple($field) {
  166. // Use the substitutions to ensure a consistent timestamp.
  167. $query_substitutions = views_views_query_substitutions($this->view);
  168. $value = intval(strtotime($this->value['value'], $query_substitutions['***CURRENT_TIME***']));
  169. // This is safe because we are manually scrubbing the value. It is
  170. // necessary to do it this way because $value is a formula when using an
  171. // offset.
  172. $this->query->add_where_expression($this->options['group'], "$field $this->operator $value");
  173. }
  174. }