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