2
// $Id: views_handler_filter_date.inc,v 1.3 2008/11/26 19:01:44 merlinofchaos Exp $
5
* Filter to handle dates stored as a timestamp.
7
class views_handler_filter_date extends views_handler_filter_numeric {
8
function option_definition() {
9
$options = parent::option_definition();
11
// value is already set up properly, we're just adding our new field to it.
12
$options['value']['type']['default'] = 'date';
18
* Add a type selector to the value form
20
function value_form(&$form, &$form_state) {
21
if (empty($form_state['exposed'])) {
22
$form['value']['type'] = array(
24
'#title' => t('Value type'),
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"'),
29
'#default_value' => !empty($this->value['type']) ? $this->value['type'] : 'date',
32
parent::value_form($form, $form_state);
35
function options_validate(&$form, &$form_state) {
36
parent::options_validate($form, $form_state);
38
if (!empty($form_state['values']['options']['expose']['optional'])) {
39
// Who cares what the value is if it's exposed and optional.
43
$this->validate_valid_time($form['value'], $form_state['values']['options']['operator'], $form_state['values']['options']['value']);
46
function exposed_validate(&$form, &$form_state) {
47
if (empty($this->options['exposed'])) {
51
if (!empty($this->options['expose']['optional'])) {
52
// Who cares what the value is if it's exposed and optional.
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']];
61
$operator = $this->operator;
64
$this->validate_valid_time($this->options['expose']['identifier'], $operator, $value);
69
* Validate that the time values convert to something usable.
71
function validate_valid_time(&$form, $operator, $value) {
72
$operators = $this->operators();
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.'));
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.'));
85
$max = strtotime($value['max']);
86
if ($max == -1 || $max === FALSE) {
87
form_error($form['max'], t('Invalid date format.'));
92
function accept_exposed_input($input) {
93
if (empty($this->options['exposed'])) {
97
// Store this because it will get overwritten.
98
$type = $this->value['type'];
99
$rc = parent::accept_exposed_input($input);
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']];
107
$operator = $this->operator;
110
if ($operators[$operator]['values'] == 1) {
111
if ($this->value['value'] == '') {
116
if ($this->value['min'] == '' || $this->value['max'] == '') {
121
// restore what got overwritten by the parent.
122
$this->value['type'] = $type;
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));
132
$a = intval(strtotime($this->value['max'], 0));
133
$b = intval(strtotime($this->value['min'], 0));
136
if ($this->value['type'] == 'offset') {
137
$a = '***CURRENT_TIME***' . sprintf('%+d', $a); // keep sign
138
$b = '***CURRENT_TIME***' . sprintf('%+d', $b); // keep sign
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);
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
151
$this->query->add_where($this->options['group'], "$field $this->operator %s", $value);