views_handler_filter_date.inc

  1. 3.x handlers/views_handler_filter_date.inc
  2. 2.x handlers/views_handler_filter_date.inc

File

handlers/views_handler_filter_date.inc
View source
  1. <?php
  2. /**
  3. * Filter to handle dates stored as a timestamp.
  4. */
  5. class views_handler_filter_date extends views_handler_filter_numeric {
  6. function option_definition() {
  7. $options = parent::option_definition();
  8. // value is already set up properly, we're just adding our new field to it.
  9. $options['value']['type']['default'] = 'date';
  10. return $options;
  11. }
  12. /**
  13. * Add a type selector to the value form
  14. */
  15. function value_form(&$form, &$form_state) {
  16. if (empty($form_state['exposed'])) {
  17. $form['value']['type'] = array(
  18. '#type' => 'radios',
  19. '#title' => t('Value type'),
  20. '#options' => array(
  21. 'date' => t('A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred.'),
  22. 'offset' => t('An offset from the current time such as "!example1" or "!example2"', array('!example1' => '+1 day', '!example2' => '-2 hours -30 minutes')),
  23. ),
  24. '#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
  25. );
  26. }
  27. parent::value_form($form, $form_state);
  28. }
  29. function options_validate(&$form, &$form_state) {
  30. parent::options_validate($form, $form_state);
  31. if (!empty($form_state['values']['options']['expose']['optional'])) {
  32. // Who cares what the value is if it's exposed and optional.
  33. return;
  34. }
  35. $this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
  36. }
  37. function exposed_validate(&$form, &$form_state) {
  38. if (empty($this->options['exposed'])) {
  39. return;
  40. }
  41. if (!empty($this->options['expose']['optional'])) {
  42. // Who cares what the value is if it's exposed and optional.
  43. return;
  44. }
  45. $value = &$form_state['values'][$this->options['expose']['identifier']];
  46. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator'])) {
  47. $operator = $form_state['values'][$this->options['expose']['operator']];
  48. }
  49. else {
  50. $operator = $this->operator;
  51. }
  52. $this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
  53. }
  54. /**
  55. * Validate that the time values convert to something usable.
  56. */
  57. function validate_valid_time(&$form, $operator, $value) {
  58. $operators = $this->operators();
  59. if ($operators[$operator]['values'] == 1) {
  60. $convert = strtotime($value['value']);
  61. if (!empty($form['value']) && ($convert == -1 || $convert === FALSE)) {
  62. form_error($form['value'], t('Invalid date format.'));
  63. }
  64. }
  65. elseif ($operators[$operator]['values'] == 2) {
  66. $min = strtotime($value['min']);
  67. if ($min == -1 || $min === FALSE) {
  68. form_error($form['min'], t('Invalid date format.'));
  69. }
  70. $max = strtotime($value['max']);
  71. if ($max == -1 || $max === FALSE) {
  72. form_error($form['max'], t('Invalid date format.'));
  73. }
  74. }
  75. }
  76. function accept_exposed_input($input) {
  77. if (empty($this->options['exposed'])) {
  78. return TRUE;
  79. }
  80. // Store this because it will get overwritten.
  81. $type = $this->value['type'];
  82. $rc = parent::accept_exposed_input($input);
  83. // Don't filter if value(s) are empty.
  84. $operators = $this->operators();
  85. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator'])) {
  86. $operator = $input[$this->options['expose']['operator']];
  87. }
  88. else {
  89. $operator = $this->operator;
  90. }
  91. if ($operators[$operator]['values'] == 1) {
  92. if ($this->value['value'] == '') {
  93. return FALSE;
  94. }
  95. }
  96. else {
  97. if ($this->value['min'] == '' || $this->value['max'] == '') {
  98. return FALSE;
  99. }
  100. }
  101. // restore what got overwritten by the parent.
  102. $this->value['type'] = $type;
  103. return $rc;
  104. }
  105. function op_between($field) {
  106. if ($this->operator == 'between') {
  107. $a = intval(strtotime($this->value['min'], 0));
  108. $b = intval(strtotime($this->value['max'], 0));
  109. }
  110. else {
  111. $a = intval(strtotime($this->value['max'], 0));
  112. $b = intval(strtotime($this->value['min'], 0));
  113. }
  114. if ($this->value['type'] == 'offset') {
  115. $a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
  116. $b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign
  117. }
  118. // %s is safe here because strtotime scrubbed the input and we might
  119. // have a string if using offset.
  120. if ($this->operator == 'between') {
  121. $this->query->add_where($this->options['group'], "$field >= %s", $a);
  122. $this->query->add_where($this->options['group'], "$field <= %s", $b);
  123. }
  124. else {
  125. $this->query->add_where($this->options['group'], "$field >= %s OR $field <= %s", array($a, $b));
  126. }
  127. }
  128. function op_simple($field) {
  129. $value = intval(strtotime($this->value['value'], 0));
  130. if (!empty($this->value['type']) && $this->value['type'] == 'offset') {
  131. $value = '***CURRENT_TIME***' . sprintf('%+d', $value); // keep sign
  132. }
  133. $this->query->add_where($this->options['group'], "$field $this->operator %s", $value);
  134. }
  135. }