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