1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
599a39cd
|
Assos Assos
|
|
3 |
85ad3d82
|
Assos Assos
|
/**
|
4 |
|
|
* @file
|
5 |
599a39cd
|
Assos Assos
|
* A standard Views filter for a single date field.
|
6 |
85ad3d82
|
Assos Assos
|
*/
|
7 |
|
|
|
8 |
599a39cd
|
Assos Assos
|
/**
|
9 |
|
|
* A standard Views filter for a single date field.
|
10 |
|
|
*/
|
11 |
85ad3d82
|
Assos Assos
|
class date_views_filter_handler_simple extends views_handler_filter_date {
|
12 |
|
|
|
13 |
599a39cd
|
Assos Assos
|
/**
|
14 |
|
|
* @var object
|
15 |
|
|
*/
|
16 |
|
|
public $date_handler = NULL;
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
|
|
* @var int
|
20 |
|
|
*/
|
21 |
|
|
public $offset = NULL;
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* {@inheritdoc}
|
25 |
|
|
*/
|
26 |
85ad3d82
|
Assos Assos
|
function init(&$view, &$options) {
|
27 |
|
|
parent::init($view, $options);
|
28 |
|
|
module_load_include('inc', 'date_api', 'date_api_sql');
|
29 |
|
|
$this->date_handler = new date_sql_handler(DATE_UNIX);
|
30 |
|
|
if (!empty($this->definition['field_name'])) {
|
31 |
|
|
$field = field_info_field($this->definition['field_name']);
|
32 |
|
|
if (!empty($field) && !empty($field['type'])) {
|
33 |
|
|
$this->date_handler->date_type = $field['type'];
|
34 |
|
|
}
|
35 |
|
|
$this->date_handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
|
36 |
|
|
$this->date_handler->local_timezone = date_get_timezone($field['settings']['tz_handling']);
|
37 |
|
|
}
|
38 |
|
|
$this->form_submitted = FALSE;
|
39 |
|
|
$this->date_handler->granularity = isset($options['granularity']) ? $options['granularity'] : 'day';
|
40 |
|
|
$this->format = $this->date_handler->views_formats($this->options['granularity'], 'sql');
|
41 |
|
|
|
42 |
599a39cd
|
Assos Assos
|
// Identify the base table for this field. It will be used to call for the
|
43 |
|
|
// right query field options.
|
44 |
85ad3d82
|
Assos Assos
|
$this->base_table = $this->table;
|
45 |
|
|
}
|
46 |
|
|
|
47 |
599a39cd
|
Assos Assos
|
/**
|
48 |
|
|
* {@inheritdoc}
|
49 |
|
|
*/
|
50 |
85ad3d82
|
Assos Assos
|
function option_definition() {
|
51 |
|
|
$options = parent::option_definition();
|
52 |
|
|
$options['granularity'] = array('default' => 'day');
|
53 |
|
|
$options['form_type'] = array('default' => 'date_select');
|
54 |
|
|
$options['default_date'] = array('default' => '');
|
55 |
|
|
$options['default_to_date'] = array('default' => '');
|
56 |
|
|
$options['year_range'] = array('default' => '-3:+3');
|
57 |
|
|
$options['add_delta'] = array('default' => '');
|
58 |
|
|
return $options;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
599a39cd
|
Assos Assos
|
/**
|
62 |
|
|
* {@inheritdoc}
|
63 |
|
|
*/
|
64 |
85ad3d82
|
Assos Assos
|
function operators() {
|
65 |
599a39cd
|
Assos Assos
|
// Cache the operators that are being added.
|
66 |
|
|
static $new_operators;
|
67 |
|
|
if (!isset($new_operators)) {
|
68 |
|
|
$new_operators = array(
|
69 |
|
|
'title' => t('Contains'),
|
70 |
|
|
'method' => 'op_contains',
|
71 |
|
|
'short' => t('contains'),
|
72 |
|
|
'values' => 1,
|
73 |
|
|
);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
// Add the new 'contains' operators.
|
77 |
85ad3d82
|
Assos Assos
|
$operators = parent::operators();
|
78 |
599a39cd
|
Assos Assos
|
$operators['contains'] = $new_operators;
|
79 |
85ad3d82
|
Assos Assos
|
return $operators;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Helper function to find a default value.
|
84 |
|
|
*/
|
85 |
|
|
function date_default_value($prefix, $options = NULL) {
|
86 |
|
|
$default_date = '';
|
87 |
|
|
if (empty($options)) {
|
88 |
|
|
$options = $this->options;
|
89 |
|
|
}
|
90 |
|
|
// If this is a remembered value, use the value from the SESSION.
|
91 |
|
|
if (!empty($this->options['expose']['remember'])) {
|
92 |
|
|
$display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
|
93 |
|
|
if (!empty($_SESSION['views'][$this->view->name][$display_id][$this->options['expose']['identifier']][$prefix])) {
|
94 |
|
|
return $_SESSION['views'][$this->view->name][$display_id][$this->options['expose']['identifier']][$prefix];
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
// This is a date that needs to be constructed from options like 'now' .
|
99 |
|
|
$default_option = $prefix == 'max' ? $options['default_to_date'] : $options['default_date'];
|
100 |
|
|
if (!empty($default_option)) {
|
101 |
|
|
str_replace('now', 'today', $default_option);
|
102 |
|
|
$date = date_create($default_option, date_default_timezone_object());
|
103 |
|
|
$default_date = !empty($date) ? $date->format($this->format) : '';
|
104 |
|
|
|
105 |
|
|
// The format for our filter is in ISO format, but the widget will need it in datetime format.
|
106 |
|
|
$default_date = str_replace('T', ' ', $default_date);
|
107 |
|
|
}
|
108 |
|
|
// This a fixed date.
|
109 |
|
|
else {
|
110 |
|
|
$default_date = $options['value'][$prefix];
|
111 |
|
|
}
|
112 |
|
|
return $default_date;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Helper function to see if we need to swap in the default value.
|
117 |
|
|
*
|
118 |
599a39cd
|
Assos Assos
|
* Views exposed filters treat everything as submitted, so if it's an empty
|
119 |
|
|
* value we have to see if anything actually was submitted. If nothing has
|
120 |
|
|
* really been submitted, we need to swap in our default value logic.
|
121 |
85ad3d82
|
Assos Assos
|
*/
|
122 |
|
|
function get_filter_value($prefix, $input) {
|
123 |
|
|
// All our date widgets provide datetime values but we use ISO in our SQL
|
124 |
|
|
// for consistency between the way filters and arguments work (arguments
|
125 |
|
|
// cannot contain spaces).
|
126 |
|
|
if (empty($input)) {
|
127 |
|
|
if (empty($this->options['exposed'])) {
|
128 |
|
|
return str_replace(' ', 'T', $this->date_default_value($prefix));
|
129 |
|
|
}
|
130 |
|
|
elseif (isset($this->options['expose']['identifier']) && !isset($_GET[$this->options['expose']['identifier']])) {
|
131 |
|
|
return str_replace(' ', 'T', $this->date_default_value($prefix));
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
return str_replace(' ', 'T', $input);
|
136 |
|
|
}
|
137 |
|
|
|
138 |
599a39cd
|
Assos Assos
|
/**
|
139 |
|
|
* {@inheritdoc}
|
140 |
|
|
*/
|
141 |
85ad3d82
|
Assos Assos
|
function accept_exposed_input($input) {
|
142 |
|
|
if (!empty($this->options['exposed'])) {
|
143 |
|
|
$element_input = $input[$this->options['expose']['identifier']];
|
144 |
|
|
$element_input['value'] = $this->get_filter_value('value', !empty($element_input['value']) ? $element_input['value'] : '');
|
145 |
|
|
$element_input['min'] = $this->get_filter_value('min', !empty($element_input['min']) ? $element_input['min'] : '');
|
146 |
|
|
$element_input['max'] = $this->get_filter_value('max', !empty($element_input['max']) ? $element_input['max'] : '');
|
147 |
|
|
if (is_array($element_input) && isset($element_input['default_date'])) {
|
148 |
|
|
unset($element_input['default_date']);
|
149 |
|
|
}
|
150 |
|
|
if (is_array($element_input) && isset($element_input['default_to_date'])) {
|
151 |
|
|
unset($element_input['default_to_date']);
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
$input[$this->options['expose']['identifier']] = $element_input;
|
155 |
|
|
}
|
156 |
|
|
return parent::accept_exposed_input($input);
|
157 |
|
|
}
|
158 |
|
|
|
159 |
599a39cd
|
Assos Assos
|
/**
|
160 |
|
|
* @todo
|
161 |
|
|
*/
|
162 |
85ad3d82
|
Assos Assos
|
function op_between($field) {
|
163 |
599a39cd
|
Assos Assos
|
// Add the delta field to the view so we can later find the value that
|
164 |
|
|
// matched our query.
|
165 |
85ad3d82
|
Assos Assos
|
list($table_name, $field_name) = explode('.', $field);
|
166 |
|
|
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
|
167 |
|
|
$this->query->add_field($table_name, 'delta');
|
168 |
|
|
$real_field_name = str_replace(array('_value', '_value2'), '', $this->real_field);
|
169 |
|
|
$this->query->add_field($table_name, 'entity_id', 'date_id_' . $real_field_name);
|
170 |
|
|
$this->query->add_field($table_name, 'delta', 'date_delta_' . $real_field_name);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
$min_value = $this->get_filter_value('min', $this->value['min']);
|
174 |
|
|
$min_comp_date = new DateObject($min_value, date_default_timezone(), $this->format);
|
175 |
|
|
$max_value = $this->get_filter_value('max', $this->value['max']);
|
176 |
|
|
$max_comp_date = new DateObject($max_value, date_default_timezone(), $this->format);
|
177 |
|
|
$field_min = $this->date_handler->sql_field($field, NULL, $min_comp_date);
|
178 |
|
|
$field_min = $this->date_handler->sql_format($this->format, $field_min);
|
179 |
|
|
$field_max = $this->date_handler->sql_field($field, NULL, $max_comp_date);
|
180 |
|
|
$field_max = $this->date_handler->sql_format($this->format, $field_max);
|
181 |
|
|
$placeholder_min = $this->placeholder();
|
182 |
|
|
$placeholder_max = $this->placeholder();
|
183 |
|
|
$group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
|
184 |
|
|
if ($this->operator == 'between') {
|
185 |
|
|
$this->query->add_where_expression($group, "$field_min >= $placeholder_min AND $field_max <= $placeholder_max", array($placeholder_min => $min_value, $placeholder_max => $max_value));
|
186 |
|
|
}
|
187 |
|
|
else {
|
188 |
|
|
$this->query->add_where_expression($group, "$field_min < $placeholder_min OR $field_max > $placeholder_max", array($placeholder_min => $min_value, $placeholder_max => $max_value));
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
|
192 |
599a39cd
|
Assos Assos
|
/**
|
193 |
|
|
* @todo
|
194 |
|
|
*/
|
195 |
85ad3d82
|
Assos Assos
|
function op_simple($field) {
|
196 |
599a39cd
|
Assos Assos
|
// Add the delta field to the view so we can later find the value that
|
197 |
|
|
// matched our query.
|
198 |
85ad3d82
|
Assos Assos
|
list($table_name, $field_name) = explode('.', $field);
|
199 |
|
|
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
|
200 |
|
|
$this->query->add_field($table_name, 'delta');
|
201 |
|
|
$real_field_name = str_replace(array('_value', '_value2'), '', $this->real_field);
|
202 |
|
|
$this->query->add_field($table_name, 'entity_id', 'date_id_' . $real_field_name);
|
203 |
|
|
$this->query->add_field($table_name, 'delta', 'date_delta_' . $real_field_name);
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
$value = $this->get_filter_value('value', $this->value['value']);
|
207 |
|
|
$comp_date = new DateObject($value, date_default_timezone(), $this->format);
|
208 |
|
|
$field = $this->date_handler->sql_field($field, NULL, $comp_date);
|
209 |
|
|
$field = $this->date_handler->sql_format($this->format, $field);
|
210 |
|
|
$placeholder = $this->placeholder();
|
211 |
|
|
$group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
|
212 |
|
|
$this->query->add_where_expression($group, "$field $this->operator $placeholder", array($placeholder => $value));
|
213 |
|
|
}
|
214 |
|
|
|
215 |
599a39cd
|
Assos Assos
|
/**
|
216 |
|
|
* @todo
|
217 |
|
|
*/
|
218 |
85ad3d82
|
Assos Assos
|
function op_contains($field) {
|
219 |
599a39cd
|
Assos Assos
|
// Add the delta field to the view so we can later find the value that
|
220 |
|
|
// matched our query.
|
221 |
85ad3d82
|
Assos Assos
|
list($table_name, $field_name) = explode('.', $field);
|
222 |
|
|
if (!empty($this->options['add_delta']) && (substr($field_name, -6) == '_value' || substr($field_name, -7) == '_value2')) {
|
223 |
|
|
$this->query->add_field($table_name, 'delta');
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
$value = $this->get_filter_value('value', $this->value['value']);
|
227 |
|
|
$comp_date = new DateObject($value, date_default_timezone(), $this->format);
|
228 |
|
|
$fields = date_views_fields($this->base_table);
|
229 |
|
|
$fields = $fields['name'];
|
230 |
|
|
$fromto = $fields[$field]['fromto'];
|
231 |
|
|
$field_min = $this->date_handler->sql_field($fromto[0], NULL, $comp_date);
|
232 |
|
|
$field_min = $this->date_handler->sql_format($this->format, $field_min);
|
233 |
|
|
$field_max = $this->date_handler->sql_field($fromto[1], NULL, $comp_date);
|
234 |
|
|
$field_max = $this->date_handler->sql_format($this->format, $field_max);
|
235 |
|
|
$placeholder_min = $this->placeholder();
|
236 |
|
|
$placeholder_max = $this->placeholder();
|
237 |
|
|
$group = !empty($this->options['date_group']) ? $this->options['date_group'] : $this->options['group'];
|
238 |
|
|
$this->query->add_where_expression($group, "$field_max >= $placeholder_min AND $field_min <= $placeholder_max", array($placeholder_min => $value, $placeholder_max => $value));
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/**
|
242 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
243 |
|
|
*/
|
244 |
|
|
function has_extra_options() {
|
245 |
|
|
return TRUE;
|
246 |
|
|
}
|
247 |
85ad3d82
|
Assos Assos
|
|
248 |
|
|
/**
|
249 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
250 |
85ad3d82
|
Assos Assos
|
*/
|
251 |
|
|
function widget_options() {
|
252 |
|
|
$options = array(
|
253 |
|
|
'date_select' => t('Select'),
|
254 |
|
|
'date_text' => t('Text'),
|
255 |
|
|
'date_popup' => t('Popup'),
|
256 |
|
|
);
|
257 |
|
|
if (!module_exists('date_popup')) {
|
258 |
|
|
unset($options['date_popup']);
|
259 |
|
|
}
|
260 |
|
|
return $options;
|
261 |
|
|
}
|
262 |
|
|
|
263 |
599a39cd
|
Assos Assos
|
/**
|
264 |
|
|
* @todo
|
265 |
|
|
*/
|
266 |
85ad3d82
|
Assos Assos
|
function year_range() {
|
267 |
|
|
$year_range = explode(':', $this->options['year_range']);
|
268 |
|
|
if (substr($this->options['year_range'], 0, 1) == '-' || $year_range[0] < 0) {
|
269 |
|
|
$this_year = date_format(date_now(), 'Y');
|
270 |
|
|
$year_range[0] = $this_year + $year_range[0];
|
271 |
|
|
$year_range[1] = $this_year + $year_range[1];
|
272 |
|
|
}
|
273 |
|
|
return $year_range;
|
274 |
|
|
}
|
275 |
|
|
|
276 |
599a39cd
|
Assos Assos
|
/**
|
277 |
|
|
* {@inheritdoc}
|
278 |
|
|
*/
|
279 |
85ad3d82
|
Assos Assos
|
function extra_options_form(&$form, &$form_state) {
|
280 |
|
|
parent::extra_options_form($form, $form_state);
|
281 |
|
|
$form['form_type'] = array(
|
282 |
|
|
'#type' => 'radios',
|
283 |
|
|
'#title' => t('Date selection form element'),
|
284 |
|
|
'#default_value' => $this->options['form_type'],
|
285 |
|
|
'#options' => $this->widget_options(),
|
286 |
|
|
);
|
287 |
|
|
|
288 |
|
|
$form['granularity'] = $this->date_handler->granularity_form($this->options['granularity']);
|
289 |
|
|
$form['granularity']['#title'] = t('Filter granularity');
|
290 |
|
|
|
291 |
|
|
$form['year_range'] = array(
|
292 |
|
|
'#type' => 'date_year_range',
|
293 |
|
|
'#default_value' => $this->options['year_range'],
|
294 |
|
|
);
|
295 |
|
|
|
296 |
|
|
if (!empty($this->definition['field_name'])) {
|
297 |
|
|
$field = field_info_field($this->definition['field_name']);
|
298 |
|
|
}
|
299 |
|
|
$form['add_delta'] = array(
|
300 |
|
|
'#type' => 'radios',
|
301 |
|
|
'#title' => t('Add multiple value identifier'),
|
302 |
|
|
'#default_value' => $this->options['add_delta'],
|
303 |
|
|
'#options' => array('' => t('No'), 'yes' => t('Yes')),
|
304 |
|
|
'#description' => t('Add an identifier to the view to show which multiple value date fields meet the filter criteria. Note: This option may introduce duplicate values into the view. Required when using multiple value fields in a Calendar or any time you want the node view of multiple value dates to display only the values that match the view filters.'),
|
305 |
|
|
// Only let mere mortals tweak this setting for multi-value fields
|
306 |
|
|
'#access' => !empty($field) ? $field['cardinality'] != 1 : 0,
|
307 |
|
|
);
|
308 |
|
|
}
|
309 |
|
|
|
310 |
599a39cd
|
Assos Assos
|
/**
|
311 |
|
|
* {@inheritdoc}
|
312 |
|
|
*/
|
313 |
85ad3d82
|
Assos Assos
|
function extra_options_validate($form, &$form_state) {
|
314 |
b720ea3e
|
Assos Assos
|
if (!preg_match('/^(?:[\+\-][0-9]{1,4}|[0-9]{4}):(?:[\+\-][0-9]{1,4}|[0-9]{4})$/', $form_state['values']['options']['year_range'])) {
|
315 |
85ad3d82
|
Assos Assos
|
form_error($form['year_range'], t('Date year range must be in the format -9:+9, 2005:2010, -9:2010, or 2005:+9'));
|
316 |
|
|
}
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
/**
|
320 |
599a39cd
|
Assos Assos
|
* {@inheritdoc}
|
321 |
85ad3d82
|
Assos Assos
|
*/
|
322 |
|
|
function value_form(&$form, &$form_state) {
|
323 |
599a39cd
|
Assos Assos
|
// We use different values than the parent form, so we must construct our
|
324 |
|
|
// own form element.
|
325 |
85ad3d82
|
Assos Assos
|
$form['value'] = array();
|
326 |
|
|
$form['value']['#tree'] = TRUE;
|
327 |
599a39cd
|
Assos Assos
|
$form['value']['#id'] = 'date_views_exposed_filter-' . bin2hex(drupal_random_bytes(16));
|
328 |
|
|
$form['value']['#type'] = 'container';
|
329 |
|
|
if (module_exists('ctools')) {
|
330 |
|
|
$form['value']['#pre_render'] = array('ctools_dependent_pre_render');
|
331 |
|
|
}
|
332 |
85ad3d82
|
Assos Assos
|
|
333 |
|
|
// Below section copied from views_handler_filter_numeric.inc.
|
334 |
|
|
$which = 'all';
|
335 |
|
|
$source = '';
|
336 |
|
|
if (!empty($form['operator'])) {
|
337 |
|
|
$source = ($form['operator']['#type'] == 'radios') ? 'radio:options[operator]' : 'edit-options-operator';
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
$identifier = $this->options['expose']['identifier'];
|
341 |
|
|
if (!empty($form_state['exposed'])) {
|
342 |
|
|
if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
|
343 |
599a39cd
|
Assos Assos
|
// Exposed and locked.
|
344 |
85ad3d82
|
Assos Assos
|
$which = in_array($this->operator, $this->operator_values(2)) ? 'minmax' : 'value';
|
345 |
|
|
}
|
346 |
|
|
else {
|
347 |
|
|
$source = 'edit-' . drupal_html_id($this->options['expose']['operator_id']);
|
348 |
|
|
}
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
if ($which == 'all' || $which == 'value') {
|
352 |
|
|
$form['value'] += $this->date_parts_form($form_state, 'value', $source, $which, $this->operator_values(1), $identifier, 'default_date');
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
if ($which == 'all' || $which == 'minmax') {
|
356 |
|
|
$form['value'] += $this->date_parts_form($form_state, 'min', $source, $which, $this->operator_values(2), $identifier, 'default_date');
|
357 |
|
|
$form['value'] += $this->date_parts_form($form_state, 'max', $source, $which, $this->operator_values(2), $identifier, 'default_to_date');
|
358 |
|
|
}
|
359 |
|
|
|
360 |
599a39cd
|
Assos Assos
|
// Add some extra validation for the select widget to be sure that the user
|
361 |
|
|
// inputs all parts of the date.
|
362 |
85ad3d82
|
Assos Assos
|
if ($this->options['form_type'] == 'date_select') {
|
363 |
|
|
$form['value']['#element_validate'] = array('date_views_select_validate');
|
364 |
|
|
}
|
365 |
|
|
|
366 |
|
|
}
|
367 |
|
|
|
368 |
|
|
/**
|
369 |
|
|
* A form element to select date part values.
|
370 |
|
|
*
|
371 |
|
|
* @param string $prefix
|
372 |
|
|
* A prefix for the date values, 'value', 'min', or 'max' .
|
373 |
|
|
* @param string $source
|
374 |
|
|
* The operator for this element.
|
375 |
|
|
* @param string $which
|
376 |
|
|
* Which element to provide, 'all', 'value', or 'minmax' .
|
377 |
|
|
* @param array $operator_values
|
378 |
|
|
* An array of the allowed operators for this element.
|
379 |
|
|
* @param array $identifier
|
380 |
|
|
* Identifier of the exposed element.
|
381 |
|
|
* @param array $relative_id
|
382 |
|
|
* Form element id to use for the relative date field.
|
383 |
|
|
*
|
384 |
|
|
* @return
|
385 |
|
|
* The form date part element for this instance.
|
386 |
|
|
*/
|
387 |
b720ea3e
|
Assos Assos
|
function date_parts_form(&$form_state, $prefix, $source, $which, $operator_values, $identifier, $relative_id) {
|
388 |
85ad3d82
|
Assos Assos
|
module_load_include('inc', 'date_api', 'date_api_elements');
|
389 |
|
|
switch ($prefix) {
|
390 |
|
|
case 'min':
|
391 |
|
|
$label = t('Start date');
|
392 |
|
|
$relative_label = t('Relative start date');
|
393 |
|
|
break;
|
394 |
599a39cd
|
Assos Assos
|
|
395 |
85ad3d82
|
Assos Assos
|
case 'max':
|
396 |
|
|
$label = t('End date');
|
397 |
|
|
$relative_label = t('Relative end date');
|
398 |
|
|
break;
|
399 |
599a39cd
|
Assos Assos
|
|
400 |
85ad3d82
|
Assos Assos
|
default:
|
401 |
|
|
$label = '';
|
402 |
|
|
$relative_label = t('Relative date');
|
403 |
|
|
}
|
404 |
|
|
|
405 |
|
|
$type = $this->options['form_type'];
|
406 |
|
|
if ($type == 'date_popup' && !module_exists('date_popup')) {
|
407 |
|
|
$type = 'date_text';
|
408 |
|
|
}
|
409 |
|
|
|
410 |
|
|
$format = $this->date_handler->views_formats($this->options['granularity'], 'display');
|
411 |
|
|
$granularity = array_keys($this->date_handler->date_parts($this->options['granularity']));
|
412 |
|
|
$relative_value = ($prefix == 'max' ? $this->options['default_to_date'] : $this->options['default_date']);
|
413 |
|
|
|
414 |
|
|
if (!empty($form_state['exposed'])) {
|
415 |
|
|
// UI when the date selector is exposed.
|
416 |
599a39cd
|
Assos Assos
|
$label = $this->options['expose']['label'];
|
417 |
85ad3d82
|
Assos Assos
|
$default_date = $this->date_default_value($prefix);
|
418 |
|
|
$id = 'edit-' . str_replace('_', '-', $this->field) . '-' . $prefix;
|
419 |
|
|
$form[$prefix] = array(
|
420 |
|
|
'#title' => check_plain($label),
|
421 |
599a39cd
|
Assos Assos
|
'#title_display' => 'invisible',
|
422 |
85ad3d82
|
Assos Assos
|
'#type' => $type,
|
423 |
|
|
'#size' => 20,
|
424 |
|
|
'#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
|
425 |
|
|
'#date_format' => date_limit_format($format, $granularity),
|
426 |
|
|
'#date_label_position' => 'within',
|
427 |
|
|
'#date_year_range' => $this->options['year_range'],
|
428 |
|
|
'#process' => array($type . '_element_process'),
|
429 |
db9ffd17
|
Assos Assos
|
'#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '-inside-wrapper">',
|
430 |
85ad3d82
|
Assos Assos
|
'#suffix' => '</div></div>',
|
431 |
|
|
);
|
432 |
|
|
if ($which == 'all') {
|
433 |
|
|
$form[$prefix]['#pre_render'][] = 'ctools_dependent_pre_render';
|
434 |
|
|
$form[$prefix]['#dependency'] = array($source => $operator_values);
|
435 |
|
|
}
|
436 |
599a39cd
|
Assos Assos
|
|
437 |
|
|
// Validate the input value; set form state input array.
|
438 |
|
|
$input = isset($form_state['input'][$identifier][$prefix]) ? $form_state['input'][$identifier][$prefix] : array();
|
439 |
|
|
$form_state['input'][$identifier][$prefix] = $this->input_validate($form[$prefix], $input);
|
440 |
|
|
|
441 |
85ad3d82
|
Assos Assos
|
if (!isset($form_state['input'][$identifier][$prefix])) {
|
442 |
599a39cd
|
Assos Assos
|
// Handle bogus input from the query string to prevent fatal errors.
|
443 |
|
|
if (isset($form_state['input'][$identifier]) && !is_array($form_state['input'][$identifier])) {
|
444 |
|
|
$form_state['input'][$identifier] = array();
|
445 |
|
|
}
|
446 |
b720ea3e
|
Assos Assos
|
// Ensure these exist.
|
447 |
|
|
foreach ($granularity as $key) {
|
448 |
|
|
$form_state['input'][$identifier][$prefix][$key] = NULL;
|
449 |
|
|
}
|
450 |
85ad3d82
|
Assos Assos
|
}
|
451 |
|
|
}
|
452 |
|
|
else {
|
453 |
|
|
// UI when the date selector is on the views configuration screen.
|
454 |
|
|
$default_date = '';
|
455 |
|
|
$id = 'edit-options-value-' . $prefix;
|
456 |
|
|
$form[$prefix . '_group'] = array(
|
457 |
|
|
'#type' => 'fieldset',
|
458 |
|
|
'#attributes' => array('class' => array('date-views-filter-fieldset')),
|
459 |
|
|
);
|
460 |
|
|
$form[$prefix . '_group'][$prefix . '_choose_input_type'] = array(
|
461 |
|
|
'#title' => check_plain($label),
|
462 |
|
|
'#type' => 'select',
|
463 |
|
|
'#options' => array('date' => t('Select a date'), 'relative' => ('Enter a relative date')),
|
464 |
|
|
'#attributes' => array('class' => array($prefix . '-choose-input-type')),
|
465 |
|
|
'#default_value' => !empty($relative_value) ? 'relative' : 'date',
|
466 |
|
|
);
|
467 |
|
|
$form[$prefix . '_group'][$prefix] = array(
|
468 |
|
|
'#title' => t('Select a date'),
|
469 |
|
|
'#type' => $type,
|
470 |
|
|
'#size' => 20,
|
471 |
|
|
'#default_value' => !empty($this->value[$prefix]) ? $this->value[$prefix] : $default_date,
|
472 |
|
|
'#date_format' => date_limit_format($format, $granularity),
|
473 |
|
|
'#date_label_position' => 'within',
|
474 |
|
|
'#date_year_range' => $this->options['year_range'],
|
475 |
|
|
'#process' => array($type . '_element_process'),
|
476 |
db9ffd17
|
Assos Assos
|
'#prefix' => '<div id="' . $id . '-wrapper"><div id="' . $id . '-inside-wrapper">',
|
477 |
85ad3d82
|
Assos Assos
|
'#suffix' => '</div></div>',
|
478 |
|
|
'#states' => array(
|
479 |
|
|
'visible' => array(
|
480 |
|
|
":input.{$prefix}-choose-input-type" => array('value' => 'date'),
|
481 |
|
|
),
|
482 |
|
|
),
|
483 |
|
|
);
|
484 |
|
|
$form[$prefix . '_group'][$relative_id] = array(
|
485 |
|
|
'#type' => 'textfield',
|
486 |
|
|
'#title' => check_plain($relative_label),
|
487 |
|
|
'#default_value' => $relative_value,
|
488 |
|
|
'#description' => t("Relative dates are computed when the view is displayed. Examples: now, now +1 day, 12AM today, Monday next week. <a href=\"@relative_format\">More examples of relative date formats in the PHP documentation</a>.", array('@relative_format' => 'http://www.php.net/manual/en/datetime.formats.relative.php')),
|
489 |
|
|
'#states' => array(
|
490 |
|
|
'visible' => array(
|
491 |
|
|
":input.{$prefix}-choose-input-type" => array('value' => 'relative'),
|
492 |
|
|
),
|
493 |
|
|
),
|
494 |
|
|
);
|
495 |
|
|
if ($which == 'all') {
|
496 |
|
|
$form[$prefix . '_group']['#pre_render'][] = 'ctools_dependent_pre_render';
|
497 |
|
|
$form[$prefix . '_group']['#dependency'] = array($source => $operator_values);
|
498 |
|
|
}
|
499 |
|
|
}
|
500 |
|
|
return $form;
|
501 |
|
|
}
|
502 |
|
|
|
503 |
599a39cd
|
Assos Assos
|
/**
|
504 |
|
|
* Returns form state input array appropriate to missing, valid, and invalid input.
|
505 |
|
|
*/
|
506 |
|
|
function input_validate($element, $input) {
|
507 |
|
|
if (!$input) {
|
508 |
|
|
// No input value (e.g. initial form load), set value to NULL so Form API
|
509 |
|
|
// calls the element value_callback routine with FALSE to set the '#value'
|
510 |
|
|
// property from the '#default_value' property.
|
511 |
|
|
// See _form_builder_handle_input_element().
|
512 |
|
|
if ($element['#type'] != 'date_select') {
|
513 |
|
|
return array('date' => NULL);
|
514 |
|
|
}
|
515 |
|
|
$granularity = date_format_order($element['#date_format']);
|
516 |
|
|
if ($key = array_search('timezone', $granularity)) {
|
517 |
|
|
unset($granularity[$key]);
|
518 |
|
|
}
|
519 |
|
|
return array_fill_keys($granularity, NULL);
|
520 |
|
|
}
|
521 |
|
|
|
522 |
|
|
// Include element defaults to avoid notices in the xxx_input_date routines.
|
523 |
|
|
// Copied from form_builder() in form.inc.
|
524 |
|
|
if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = element_info($element['#type']))) {
|
525 |
|
|
// Overlay $info onto $element, retaining preexisting keys in $element.
|
526 |
|
|
$element += $info;
|
527 |
|
|
$element['#defaults_loaded'] = TRUE;
|
528 |
|
|
}
|
529 |
|
|
|
530 |
|
|
$date = NULL;
|
531 |
|
|
$function = "{$element['#type']}_input_date";
|
532 |
|
|
if (($date = $function($element, $input)) && date_is_date($date)) {
|
533 |
|
|
// The input is valid including being in the expected format.
|
534 |
|
|
return $input;
|
535 |
|
|
}
|
536 |
|
|
|
537 |
|
|
if (is_object($date)) {
|
538 |
|
|
// Input may not meet the granularity and format but is recognizable.
|
539 |
|
|
return $input;
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
// Input is bogus, return empty array in granularity format.
|
543 |
|
|
if ($element['#type'] != 'date_select') {
|
544 |
|
|
return array('date' => '');
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
$granularity = date_format_order($element['#date_format']);
|
548 |
|
|
if ($key = array_search('timezone', $granularity)) {
|
549 |
|
|
unset($granularity[$key]);
|
550 |
|
|
}
|
551 |
|
|
return array_fill_keys($granularity, '');
|
552 |
|
|
}
|
553 |
|
|
|
554 |
85ad3d82
|
Assos Assos
|
/**
|
555 |
|
|
* Value validation.
|
556 |
|
|
*
|
557 |
599a39cd
|
Assos Assos
|
* @todo Add in more validation.
|
558 |
85ad3d82
|
Assos Assos
|
*
|
559 |
599a39cd
|
Assos Assos
|
* We are setting an extra option using a value form because it makes more
|
560 |
|
|
* sense to set it there. That's not the normal method, so we have to manually
|
561 |
85ad3d82
|
Assos Assos
|
* transfer the selected value back to the option.
|
562 |
|
|
*/
|
563 |
|
|
function value_validate($form, &$form_state) {
|
564 |
|
|
$options = &$form_state['values']['options'];
|
565 |
|
|
|
566 |
|
|
if ($options['operator'] == 'between' || $options['operator'] == 'not between') {
|
567 |
|
|
if ($options['value']['min_group']['min_choose_input_type'] == 'relative') {
|
568 |
|
|
if (empty($options['value']['min_group']['default_date'])) {
|
569 |
|
|
form_set_error('options][value][min_group][default_date', t('Relative start date not specified.'));
|
570 |
|
|
}
|
571 |
|
|
else {
|
572 |
|
|
$this->options['default_date'] = $options['value']['min_group']['default_date'];
|
573 |
599a39cd
|
Assos Assos
|
// NULL out the value field, user wanted the relative value to take
|
574 |
|
|
// hold.
|
575 |
85ad3d82
|
Assos Assos
|
$options['value']['min_group']['min'] = NULL;
|
576 |
|
|
}
|
577 |
|
|
}
|
578 |
|
|
// If an absolute date was used, be sure to wipe the relative date.
|
579 |
|
|
else {
|
580 |
|
|
$this->options['default_date'] = '';
|
581 |
|
|
}
|
582 |
|
|
if ($options['value']['max_group']['max_choose_input_type'] == 'relative') {
|
583 |
|
|
if (empty($options['value']['max_group']['default_to_date'])) {
|
584 |
|
|
form_set_error('options][value][max_group][default_to_date', t('Relative end date not specified.'));
|
585 |
|
|
}
|
586 |
|
|
else {
|
587 |
|
|
$this->options['default_to_date'] = $options['value']['max_group']['default_to_date'];
|
588 |
599a39cd
|
Assos Assos
|
// NULL out the value field, user wanted the relative value to take
|
589 |
|
|
// hold.
|
590 |
85ad3d82
|
Assos Assos
|
$options['value']['max_group']['max'] = NULL;
|
591 |
|
|
}
|
592 |
|
|
}
|
593 |
|
|
// If an absolute date was used, be sure to wipe the relative date.
|
594 |
|
|
else {
|
595 |
|
|
$this->options['default_to_date'] = '';
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
elseif (in_array($options['operator'], array('<', '<=', '=', '!=', '>=', '>'))) {
|
599 |
|
|
if ($options['value']['value_group']['value_choose_input_type'] == 'relative') {
|
600 |
|
|
if (empty($options['value']['value_group']['default_date'])) {
|
601 |
|
|
form_set_error('options][value][value_group][default_date', t('Relative date not specified.'));
|
602 |
|
|
}
|
603 |
|
|
else {
|
604 |
|
|
$this->options['default_date'] = $options['value']['value_group']['default_date'];
|
605 |
599a39cd
|
Assos Assos
|
// NULL out the value field, user wanted the relative value to take
|
606 |
|
|
// hold.
|
607 |
85ad3d82
|
Assos Assos
|
$options['value']['value_group']['value'] = NULL;
|
608 |
|
|
}
|
609 |
|
|
}
|
610 |
|
|
// If an absolute date was used, be sure to wipe the relative date.
|
611 |
|
|
else {
|
612 |
|
|
$this->options['default_date'] = '';
|
613 |
|
|
}
|
614 |
|
|
}
|
615 |
|
|
// Flatten the form structure for views, so the values can be saved.
|
616 |
|
|
foreach (array('value', 'min', 'max') as $key) {
|
617 |
|
|
$options['value'][$key] = $options['value'][$key . '_group'][$key];
|
618 |
|
|
}
|
619 |
|
|
}
|
620 |
|
|
|
621 |
|
|
/**
|
622 |
|
|
* Validate that the time values convert to something usable.
|
623 |
|
|
*/
|
624 |
|
|
function validate_valid_time(&$form, $operator, $value) {
|
625 |
|
|
// Override the core date filter validation.
|
626 |
|
|
// Our date widgets do their own validation.
|
627 |
|
|
}
|
628 |
|
|
|
629 |
599a39cd
|
Assos Assos
|
/**
|
630 |
|
|
* {@inheritdoc}
|
631 |
|
|
*/
|
632 |
85ad3d82
|
Assos Assos
|
function admin_summary() {
|
633 |
|
|
$parts = $this->date_handler->date_parts();
|
634 |
|
|
$widget_options = $this->widget_options();
|
635 |
|
|
// If the filter is exposed, display the granularity.
|
636 |
|
|
if ($this->options['exposed']) {
|
637 |
|
|
return t('<strong>Exposed</strong> @widget @format', array('@format' => $parts[$this->date_handler->granularity], '@widget' => $widget_options[$this->options['form_type']]));
|
638 |
|
|
}
|
639 |
|
|
// If not exposed, display the value.
|
640 |
|
|
$output = '';
|
641 |
|
|
if (in_array($this->operator, $this->operator_values(2))) {
|
642 |
|
|
$min = check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['min']);
|
643 |
|
|
$max = check_plain(!empty($this->options['default_to_date']) ? $this->options['default_to_date'] : $this->options['value']['max']);
|
644 |
|
|
$output .= t('@min and @max', array('@min' => $min, '@max' => $max));
|
645 |
|
|
}
|
646 |
|
|
else {
|
647 |
|
|
$output .= check_plain(!empty($this->options['default_date']) ? $this->options['default_date'] : $this->options['value']['value']);
|
648 |
|
|
}
|
649 |
|
|
return $output;
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
} |