1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Date API elements themes and validation.
|
6
|
* This file is only included during the edit process to reduce memory usage.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Implements hook_element_info().
|
11
|
*
|
12
|
* Parameters for date form elements, designed to have sane defaults so any
|
13
|
* or all can be omitted.
|
14
|
*
|
15
|
* Fill the element #default_value with a date in datetime format,
|
16
|
* (YYYY-MM-DD HH:MM:SS), adjusted to the proper local timezone.
|
17
|
*
|
18
|
* NOTE - Converting a date stored in the database from UTC to the local zone
|
19
|
* and converting it back to UTC before storing it is not handled by this
|
20
|
* element and must be done in pre-form and post-form processing!!
|
21
|
*
|
22
|
* The date_select element will create a collection of form elements, with a
|
23
|
* separate select or textfield for each date part. The whole collection will
|
24
|
* get reformatted back to a date value of the requested type during validation.
|
25
|
*
|
26
|
* The date_text element will create a textfield that can contain a whole
|
27
|
* date or any part of a date as text. The user input value will be re-formatted
|
28
|
* back into a date value of the requested type during validation.
|
29
|
*
|
30
|
* The date_timezone element will create a drop-down selector to pick a
|
31
|
* timezone name.
|
32
|
*
|
33
|
* The date_year_range element will create two textfields (for users with
|
34
|
* JavaScript enabled they will appear as drop-down selectors with an option
|
35
|
* for custom text entry) to pick a range of years that will be passed to form
|
36
|
* submit handlers as a single string (e.g., -3:+3).
|
37
|
*
|
38
|
* #date_timezone
|
39
|
* The local timezone to be used to create this date.
|
40
|
*
|
41
|
* #date_format
|
42
|
* A format string that describes the format and order of date parts to
|
43
|
* display in the edit form for this element. This makes it possible
|
44
|
* to show date parts in a custom order, or to leave some of them out.
|
45
|
* Be sure to add 'A' or 'a' to get an am/pm selector. Defaults to the
|
46
|
* short site default format.
|
47
|
*
|
48
|
* #date_label_position
|
49
|
* Handling option for date part labels, like 'Year', 'Month', and 'Day',
|
50
|
* can be 'above' the date part, 'within' it, or 'none', default is 'above' .
|
51
|
* The 'within' option shows the label as the first option in a select list
|
52
|
* or the default value for an empty textfield, taking up less screen space.
|
53
|
*
|
54
|
* #date_increment
|
55
|
* Increment minutes and seconds by this amount, default is 1.
|
56
|
*
|
57
|
* #date_year_range
|
58
|
* The number of years to go back and forward in a year selector,
|
59
|
* default is -3:+3 (3 back and 3 forward).
|
60
|
*
|
61
|
* #date_text_parts
|
62
|
* Array of date parts that should use textfields instead of selects
|
63
|
* i.e. array('year') will format the year as a textfield and other
|
64
|
* date parts as drop-down selects.
|
65
|
*/
|
66
|
function _date_api_element_info() {
|
67
|
$date_base = array(
|
68
|
'#input' => TRUE, '#tree' => TRUE,
|
69
|
'#date_timezone' => date_default_timezone(),
|
70
|
'#date_flexible' => 0,
|
71
|
'#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
|
72
|
'#date_text_parts' => array(),
|
73
|
'#date_increment' => 1,
|
74
|
'#date_year_range' => '-3:+3',
|
75
|
'#date_label_position' => 'above',
|
76
|
);
|
77
|
if (module_exists('ctools')) {
|
78
|
$date_base['#pre_render'] = array('ctools_dependent_pre_render');
|
79
|
}
|
80
|
$type['date_select'] = array_merge($date_base, array(
|
81
|
'#process' => array('date_select_element_process'),
|
82
|
'#theme_wrappers' => array('date_select'),
|
83
|
'#value_callback' => 'date_select_element_value_callback',
|
84
|
));
|
85
|
$type['date_text'] = array_merge($date_base, array(
|
86
|
'#process' => array('date_text_element_process'),
|
87
|
'#theme_wrappers' => array('date_text'),
|
88
|
'#value_callback' => 'date_text_element_value_callback',
|
89
|
));
|
90
|
$type['date_timezone'] = array(
|
91
|
'#input' => TRUE, '#tree' => TRUE,
|
92
|
'#process' => array('date_timezone_element_process'),
|
93
|
'#theme_wrappers' => array('date_text'),
|
94
|
'#value_callback' => 'date_timezone_element_value_callback',
|
95
|
);
|
96
|
$type['date_year_range'] = array(
|
97
|
'#input' => TRUE,
|
98
|
'#process' => array('date_year_range_element_process'),
|
99
|
'#value_callback' => 'date_year_range_element_value_callback',
|
100
|
'#element_validate' => array('date_year_range_validate'),
|
101
|
);
|
102
|
return $type;
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Create a date object from a datetime string value.
|
107
|
*/
|
108
|
function date_default_date($element) {
|
109
|
$granularity = date_format_order($element['#date_format']);
|
110
|
$default_value = $element['#default_value'];
|
111
|
$format = DATE_FORMAT_DATETIME;
|
112
|
|
113
|
// The text and popup widgets might return less than a full datetime string.
|
114
|
if (strlen($element['#default_value']) < 19) {
|
115
|
switch (strlen($element['#default_value'])) {
|
116
|
case 16:
|
117
|
$format = 'Y-m-d H:i';
|
118
|
break;
|
119
|
case 13:
|
120
|
$format = 'Y-m-d H';
|
121
|
break;
|
122
|
case 10:
|
123
|
$format = 'Y-m-d';
|
124
|
break;
|
125
|
case 7:
|
126
|
$format = 'Y-m';
|
127
|
break;
|
128
|
case 4:
|
129
|
$format = 'Y';
|
130
|
break;
|
131
|
}
|
132
|
}
|
133
|
$date = new DateObject($default_value, $element['#date_timezone'], $format);
|
134
|
if (is_object($date)) {
|
135
|
$date->limitGranularity($granularity);
|
136
|
if ($date->validGranularity($granularity, $element['#date_flexible'])) {
|
137
|
date_increment_round($date, $element['#date_increment']);
|
138
|
}
|
139
|
return $date;
|
140
|
}
|
141
|
return NULL;
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Process callback which creates a date_year_range form element.
|
146
|
*/
|
147
|
function date_year_range_element_process($element, &$form_state, $form) {
|
148
|
// Year range is stored in the -3:+3 format, but collected as two separate
|
149
|
// textfields.
|
150
|
$element['years_back'] = array(
|
151
|
'#type' => 'textfield',
|
152
|
'#title' => t('Starting year'),
|
153
|
'#default_value' => $element['#value']['years_back'],
|
154
|
'#size' => 10,
|
155
|
'#maxsize' => 10,
|
156
|
'#attributes' => array('class' => array('select-list-with-custom-option', 'back')),
|
157
|
'#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
|
158
|
);
|
159
|
$element['years_forward'] = array(
|
160
|
'#type' => 'textfield',
|
161
|
'#title' => t('Ending year'),
|
162
|
'#default_value' => $element['#value']['years_forward'],
|
163
|
'#size' => 10,
|
164
|
'#maxsize' => 10,
|
165
|
'#attributes' => array('class' => array('select-list-with-custom-option', 'forward')),
|
166
|
'#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
|
167
|
);
|
168
|
|
169
|
$element['#tree'] = TRUE;
|
170
|
$element['#attached']['js'][] = drupal_get_path('module', 'date_api') . '/date_year_range.js';
|
171
|
|
172
|
$context = array(
|
173
|
'form' => $form,
|
174
|
);
|
175
|
drupal_alter('date_year_range_process', $element, $form_state, $context);
|
176
|
|
177
|
return $element;
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Element value callback for the date_year_range form element.
|
182
|
*/
|
183
|
function date_year_range_element_value_callback($element, $input = FALSE, &$form_state = array()) {
|
184
|
// Convert the element's default value from a string to an array (to match
|
185
|
// what we will get from the two textfields when the form is submitted).
|
186
|
if ($input === FALSE) {
|
187
|
list($years_back, $years_forward) = explode(':', $element['#default_value']);
|
188
|
return array(
|
189
|
'years_back' => $years_back,
|
190
|
'years_forward' => $years_forward,
|
191
|
);
|
192
|
}
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
* Element validation function for the date_year_range form element.
|
197
|
*/
|
198
|
function date_year_range_validate(&$element, &$form_state) {
|
199
|
// Recombine the two submitted form values into the -3:+3 format we will
|
200
|
// validate and save.
|
201
|
$year_range_submitted = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
|
202
|
$year_range = $year_range_submitted['years_back'] . ':' . $year_range_submitted['years_forward'];
|
203
|
drupal_array_set_nested_value($form_state['values'], $element['#parents'], $year_range);
|
204
|
if (!date_range_valid($year_range)) {
|
205
|
form_error($element['years_back'], t('Starting year must be in the format -9, or an absolute year such as 1980.'));
|
206
|
form_error($element['years_forward'], t('Ending year must be in the format +9, or an absolute year such as 2030.'));
|
207
|
}
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Element value callback for date_timezone element.
|
212
|
*/
|
213
|
function date_timezone_element_value_callback($element, $input = FALSE, &$form_state = array()) {
|
214
|
$return = '';
|
215
|
if ($input !== FALSE) {
|
216
|
$return = $input;
|
217
|
}
|
218
|
elseif (!empty($element['#default_value'])) {
|
219
|
$return = array('timezone' => $element['#default_value']);
|
220
|
}
|
221
|
return $return;
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Creates a timezone form element.
|
226
|
*
|
227
|
* @param array $element
|
228
|
* The timezone form element.
|
229
|
*
|
230
|
* @return array
|
231
|
* the timezone form element
|
232
|
*/
|
233
|
function date_timezone_element_process($element, &$form_state, $form) {
|
234
|
if (date_hidden_element($element)) {
|
235
|
return $element;
|
236
|
}
|
237
|
|
238
|
$element['#tree'] = TRUE;
|
239
|
$label = theme('date_part_label_timezone', array('part_type' => 'select', 'element' => $element));
|
240
|
$element['timezone'] = array(
|
241
|
'#type' => 'select',
|
242
|
'#title' => $label,
|
243
|
'#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
|
244
|
'#options' => date_timezone_names($element['#required']),
|
245
|
'#value' => $element['#value'],
|
246
|
'#weight' => $element['#weight'],
|
247
|
'#required' => $element['#required'],
|
248
|
'#theme' => 'date_select_element',
|
249
|
'#theme_wrappers' => array('form_element'),
|
250
|
);
|
251
|
if (isset($element['#element_validate'])) {
|
252
|
array_push($element['#element_validate'], 'date_timezone_validate');
|
253
|
}
|
254
|
else {
|
255
|
$element['#element_validate'] = array('date_timezone_validate');
|
256
|
}
|
257
|
|
258
|
$context = array(
|
259
|
'form' => $form,
|
260
|
);
|
261
|
drupal_alter('date_timezone_process', $element, $form_state, $context);
|
262
|
|
263
|
return $element;
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Validation for timezone input
|
268
|
*
|
269
|
* Move the timezone value from the nested field back to the original field.
|
270
|
*/
|
271
|
function date_timezone_validate($element, &$form_state) {
|
272
|
if (date_hidden_element($element)) {
|
273
|
return;
|
274
|
}
|
275
|
|
276
|
form_set_value($element, $element['#value']['timezone'], $form_state);
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Element value callback for date_text element.
|
281
|
*/
|
282
|
function date_text_element_value_callback($element, $input = FALSE, &$form_state = array()) {
|
283
|
$return = array('date' => '');
|
284
|
$date = NULL;
|
285
|
|
286
|
// Normal input from submitting the form element.
|
287
|
// Check is_array() to skip the string input values created by Views pagers.
|
288
|
// Those string values, if present, should be interpreted as empty input.
|
289
|
if ($input != FALSE && is_array($input)) {
|
290
|
$return = $input;
|
291
|
$date = date_text_input_date($element, $input);
|
292
|
}
|
293
|
// No input? Try the default value.
|
294
|
elseif (!empty($element['#default_value'])) {
|
295
|
$date = date_default_date($element);
|
296
|
}
|
297
|
if (date_is_date($date)) {
|
298
|
$return['date'] = date_format_date($date, 'custom', $element['#date_format']);
|
299
|
}
|
300
|
return $return;
|
301
|
}
|
302
|
|
303
|
/**
|
304
|
* Text date input form.
|
305
|
*
|
306
|
* Display all or part of a date in a single textfield.
|
307
|
*
|
308
|
* The exact parts displayed in the field are those in #date_granularity.
|
309
|
* The display of each part comes from #date_format.
|
310
|
*
|
311
|
*/
|
312
|
function date_text_element_process($element, &$form_state, $form) {
|
313
|
if (date_hidden_element($element)) {
|
314
|
return $element;
|
315
|
}
|
316
|
|
317
|
$element['#tree'] = TRUE;
|
318
|
$element['#theme_wrappers'] = array('date_text');
|
319
|
$element['date']['#value'] = $element['#value']['date'];
|
320
|
$element['date']['#type'] = 'textfield';
|
321
|
$element['date']['#weight'] = !empty($element['date']['#weight']) ? $element['date']['#weight'] : $element['#weight'];
|
322
|
$element['date']['#attributes'] = array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-date') : array('date-date'));
|
323
|
$now = date_example_date();
|
324
|
$element['date']['#title'] = t('Date');
|
325
|
$element['date']['#title_display'] = 'invisible';
|
326
|
$element['date']['#description'] = ' ' . t('Format: @date', array('@date' => date_format_date(date_example_date(), 'custom', $element['#date_format'])));
|
327
|
$element['date']['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
|
328
|
|
329
|
// Keep the system from creating an error message for the sub-element.
|
330
|
// We'll set our own message on the parent element.
|
331
|
// $element['date']['#required'] = $element['#required'];
|
332
|
$element['date']['#theme'] = 'date_textfield_element';
|
333
|
if (isset($element['#element_validate'])) {
|
334
|
array_push($element['#element_validate'], 'date_text_validate');
|
335
|
}
|
336
|
else {
|
337
|
$element['#element_validate'] = array('date_text_validate');
|
338
|
}
|
339
|
if (!empty($element['#force_value'])) {
|
340
|
$element['date']['#value'] = $element['date']['#default_value'];
|
341
|
}
|
342
|
|
343
|
$context = array(
|
344
|
'form' => $form,
|
345
|
);
|
346
|
drupal_alter('date_text_process', $element, $form_state, $context);
|
347
|
|
348
|
return $element;
|
349
|
}
|
350
|
|
351
|
/**
|
352
|
* Validation for text input.
|
353
|
*
|
354
|
* When used as a Views widget, the validation step always gets triggered,
|
355
|
* even with no form submission. Before form submission $element['#value']
|
356
|
* contains a string, after submission it contains an array.
|
357
|
*
|
358
|
*/
|
359
|
function date_text_validate($element, &$form_state) {
|
360
|
if (date_hidden_element($element)) {
|
361
|
return;
|
362
|
}
|
363
|
|
364
|
if (is_string($element['#value'])) {
|
365
|
return;
|
366
|
}
|
367
|
$input_exists = NULL;
|
368
|
$input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
|
369
|
|
370
|
drupal_alter('date_text_pre_validate', $element, $form_state, $input);
|
371
|
|
372
|
$label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
|
373
|
$date = date_text_input_date($element, $input);
|
374
|
|
375
|
// If the field has errors, display them.
|
376
|
// If something was input but there is no date, the date is invalid.
|
377
|
// If the field is empty and required, set error message and return.
|
378
|
$error_field = implode('][', $element['#parents']);
|
379
|
if (empty($date) || !empty($date->errors)) {
|
380
|
if (is_object($date) && !empty($date->errors)) {
|
381
|
$message = t('The value input for field %field is invalid:', array('%field' => $label));
|
382
|
$message .= '<br />' . implode('<br />', $date->errors);
|
383
|
form_set_error($error_field, $message);
|
384
|
return;
|
385
|
}
|
386
|
if (!empty($element['#required'])) {
|
387
|
$message = t('A valid date is required for %title.', array('%title' => $label));
|
388
|
form_set_error($error_field, $message);
|
389
|
return;
|
390
|
}
|
391
|
// Fall through, some other error.
|
392
|
if (!empty($input['date'])) {
|
393
|
form_error($element, t('%title is invalid.', array('%title' => $label)));
|
394
|
return;
|
395
|
}
|
396
|
}
|
397
|
$value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
|
398
|
form_set_value($element, $value, $form_state);
|
399
|
}
|
400
|
|
401
|
/**
|
402
|
* Helper function for creating a date object out of user input.
|
403
|
*/
|
404
|
function date_text_input_date($element, $input) {
|
405
|
if (empty($input) || !is_array($input) || !array_key_exists('date', $input) || empty($input['date'])) {
|
406
|
return NULL;
|
407
|
}
|
408
|
$granularity = date_format_order($element['#date_format']);
|
409
|
$date = new DateObject($input['date'], $element['#date_timezone'], $element['#date_format']);
|
410
|
if (is_object($date)) {
|
411
|
$date->limitGranularity($granularity);
|
412
|
if ($date->validGranularity($granularity, $element['#date_flexible'])) {
|
413
|
date_increment_round($date, $element['#date_increment']);
|
414
|
}
|
415
|
return $date;
|
416
|
}
|
417
|
return NULL;
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Element value callback for date_select element.
|
422
|
*/
|
423
|
function date_select_element_value_callback($element, $input = FALSE, &$form_state = array()) {
|
424
|
$return = array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '');
|
425
|
$date = NULL;
|
426
|
if ($input !== FALSE) {
|
427
|
$return = $input;
|
428
|
$date = date_select_input_date($element, $input);
|
429
|
}
|
430
|
elseif (!empty($element['#default_value'])) {
|
431
|
$date = date_default_date($element);
|
432
|
}
|
433
|
$granularity = date_format_order($element['#date_format']);
|
434
|
$formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's');
|
435
|
foreach ($granularity as $field) {
|
436
|
if ($field != 'timezone') {
|
437
|
$return[$field] = date_is_date($date) ? $date->format($formats[$field]) : '';
|
438
|
}
|
439
|
}
|
440
|
return $return;
|
441
|
}
|
442
|
|
443
|
/**
|
444
|
* Flexible date/time drop-down selector.
|
445
|
*
|
446
|
* Splits date into a collection of date and time sub-elements, one
|
447
|
* for each date part. Each sub-element can be either a textfield or a
|
448
|
* select, based on the value of ['#date_settings']['text_fields'].
|
449
|
*
|
450
|
* The exact parts displayed in the field are those in #date_granularity.
|
451
|
* The display of each part comes from ['#date_settings']['format'].
|
452
|
*
|
453
|
*/
|
454
|
function date_select_element_process($element, &$form_state, $form) {
|
455
|
if (date_hidden_element($element)) {
|
456
|
return $element;
|
457
|
}
|
458
|
|
459
|
$date = NULL;
|
460
|
$granularity = date_format_order($element['#date_format']);
|
461
|
|
462
|
if (is_array($element['#default_value'])) {
|
463
|
$date = date_select_input_date($element, $element['#default_value']);
|
464
|
}
|
465
|
elseif (!empty($element['#default_value'])) {
|
466
|
$date = date_default_date($element);
|
467
|
}
|
468
|
|
469
|
$element['#tree'] = TRUE;
|
470
|
$element['#theme_wrappers'] = array('date_select');
|
471
|
|
472
|
$element += (array) date_parts_element($element, $date, $element['#date_format']);
|
473
|
|
474
|
// Store a hidden value for all date parts not in the current display.
|
475
|
$granularity = date_format_order($element['#date_format']);
|
476
|
$formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's');
|
477
|
foreach (date_nongranularity($granularity) as $field) {
|
478
|
if ($field != 'timezone') {
|
479
|
$element[$field] = array(
|
480
|
'#type' => 'value',
|
481
|
'#value' => 0,
|
482
|
);
|
483
|
}
|
484
|
}
|
485
|
if (isset($element['#element_validate'])) {
|
486
|
array_push($element['#element_validate'], 'date_select_validate');
|
487
|
}
|
488
|
else {
|
489
|
$element['#element_validate'] = array('date_select_validate');
|
490
|
}
|
491
|
|
492
|
$context = array(
|
493
|
'form' => $form,
|
494
|
);
|
495
|
drupal_alter('date_select_process', $element, $form_state, $context);
|
496
|
|
497
|
return $element;
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Creates form elements for one or more date parts.
|
502
|
*
|
503
|
* Get the order of date elements from the provided format.
|
504
|
* If the format order omits any date parts in the granularity, alter the
|
505
|
* granularity array to match the format, then flip the $order array
|
506
|
* to get the position for each element. Then iterate through the
|
507
|
* elements and create a sub-form for each part.
|
508
|
*
|
509
|
* @param array $element
|
510
|
* The date element.
|
511
|
* @param object $date
|
512
|
* The date object.
|
513
|
* @param string $format
|
514
|
* A date format string.
|
515
|
*
|
516
|
* @return array
|
517
|
* The form array for the submitted date parts.
|
518
|
*/
|
519
|
function date_parts_element($element, $date, $format) {
|
520
|
$granularity = date_format_order($format);
|
521
|
$sub_element = array('#granularity' => $granularity);
|
522
|
$order = array_flip($granularity);
|
523
|
|
524
|
$hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g': 'G';
|
525
|
$month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
|
526
|
$count = 0;
|
527
|
$increment = min(intval($element['#date_increment']), 1);
|
528
|
|
529
|
// Allow empty value as option if date is not required or there is no date.
|
530
|
$part_required = (bool) $element['#required'] && is_object($date);
|
531
|
foreach ($granularity as $field) {
|
532
|
$part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
|
533
|
$sub_element[$field] = array(
|
534
|
'#weight' => $order[$field],
|
535
|
'#required' => $part_required,
|
536
|
'#attributes' => array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-' . $field) : array('date-' . $field)),
|
537
|
'#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
|
538
|
);
|
539
|
switch ($field) {
|
540
|
case 'year':
|
541
|
$range = date_range_years($element['#date_year_range'], $date);
|
542
|
$min_year = $range[0];
|
543
|
$max_year = $range[1];
|
544
|
|
545
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format('Y') : '';
|
546
|
if ($part_type == 'select') {
|
547
|
$sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required));
|
548
|
}
|
549
|
break;
|
550
|
case 'month':
|
551
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format('n') : '';
|
552
|
if ($part_type == 'select') {
|
553
|
$sub_element[$field]['#options'] = $month_function($part_required);
|
554
|
}
|
555
|
break;
|
556
|
case 'day':
|
557
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format('j') : '';
|
558
|
if ($part_type == 'select') {
|
559
|
$sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
|
560
|
}
|
561
|
break;
|
562
|
case 'hour':
|
563
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format($hours_format) : '';
|
564
|
if ($part_type == 'select') {
|
565
|
$sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required));
|
566
|
}
|
567
|
$sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element);
|
568
|
break;
|
569
|
case 'minute':
|
570
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format('i') : '';
|
571
|
if ($part_type == 'select') {
|
572
|
$sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
|
573
|
}
|
574
|
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
|
575
|
break;
|
576
|
case 'second':
|
577
|
$sub_element[$field]['#default_value'] = is_object($date) ? $date->format('s') : '';
|
578
|
if ($part_type == 'select') {
|
579
|
$sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
|
580
|
}
|
581
|
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
|
582
|
break;
|
583
|
}
|
584
|
|
585
|
// Add handling for the date part label.
|
586
|
$label = theme('date_part_label_' . $field, array('part_type' => $part_type, 'element' => $element));
|
587
|
if (in_array($field, $element['#date_text_parts'])) {
|
588
|
$sub_element[$field]['#type'] = 'textfield';
|
589
|
$sub_element[$field]['#theme'] = 'date_textfield_element';
|
590
|
$sub_element[$field]['#size'] = 7;
|
591
|
$sub_element[$field]['#title'] = $label;
|
592
|
$sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
|
593
|
if ($element['#date_label_position'] == 'within') {
|
594
|
if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
|
595
|
$sub_element[$field]['#options'] = array(
|
596
|
'-' . $label => '-' . $label) + $sub_element[$field]['#options'];
|
597
|
}
|
598
|
if (empty($sub_element[$field]['#default_value'])) {
|
599
|
$sub_element[$field]['#default_value'] = '-' . $label;
|
600
|
}
|
601
|
}
|
602
|
}
|
603
|
else {
|
604
|
$sub_element[$field]['#type'] = 'select';
|
605
|
$sub_element[$field]['#theme'] = 'date_select_element';
|
606
|
$sub_element[$field]['#title'] = $label;
|
607
|
$sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
|
608
|
if ($element['#date_label_position'] == 'within') {
|
609
|
$sub_element[$field]['#options'] = array(
|
610
|
'' => '-' . $label) + $sub_element[$field]['#options'];
|
611
|
}
|
612
|
}
|
613
|
}
|
614
|
|
615
|
// Views exposed filters are treated as submitted even if not,
|
616
|
// so force the #default value in that case. Make sure we set
|
617
|
// a default that is in the option list.
|
618
|
if (!empty($element['#force_value'])) {
|
619
|
$options = $sub_element[$field]['#options'];
|
620
|
$default = !empty($sub_element[$field]['#default_value']) ? $sub_element[$field]['#default_value'] : array_shift($options);
|
621
|
$sub_element[$field]['#value'] = $default;
|
622
|
}
|
623
|
|
624
|
if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
|
625
|
$label = theme('date_part_label_ampm', array('part_type' => 'ampm', 'element' => $element));
|
626
|
$sub_element['ampm'] = array(
|
627
|
'#type' => 'select',
|
628
|
'#theme' => 'date_select_element',
|
629
|
'#title' => $label,
|
630
|
'#title_display' => in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before',
|
631
|
'#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '',
|
632
|
'#options' => drupal_map_assoc(date_ampm($part_required)),
|
633
|
'#required' => $part_required,
|
634
|
'#weight' => 8,
|
635
|
'#attributes' => array('class' => array('date-ampm')),
|
636
|
);
|
637
|
if ($element['#date_label_position'] == 'within') {
|
638
|
$sub_element['ampm']['#options'] = array('' => '-' . $label) + $sub_element['ampm']['#options'];
|
639
|
}
|
640
|
}
|
641
|
|
642
|
return $sub_element;
|
643
|
}
|
644
|
|
645
|
/**
|
646
|
* Validation function for date selector.
|
647
|
*
|
648
|
* When used as a Views widget, the validation step always gets triggered,
|
649
|
* even with no form submission. Before form submission $element['#value']
|
650
|
* contains a string, after submission it contains an array.
|
651
|
*/
|
652
|
function date_select_validate($element, &$form_state) {
|
653
|
if (date_hidden_element($element)) {
|
654
|
return;
|
655
|
}
|
656
|
|
657
|
if (is_string($element['#value'])) {
|
658
|
return;
|
659
|
}
|
660
|
|
661
|
$input_exists = NULL;
|
662
|
$input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
|
663
|
|
664
|
// Strip field labels out of the results.
|
665
|
foreach ($element['#value'] as $field => $field_value) {
|
666
|
if (substr($field_value, 0, 1) == '-') {
|
667
|
$input[$field] = '';
|
668
|
}
|
669
|
}
|
670
|
|
671
|
drupal_alter('date_select_pre_validate', $element, $form_state, $input);
|
672
|
|
673
|
$label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
|
674
|
if (isset($input['ampm'])) {
|
675
|
if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
|
676
|
$input['hour'] += 12;
|
677
|
}
|
678
|
elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
|
679
|
$input['hour'] -= 12;
|
680
|
}
|
681
|
}
|
682
|
unset($input['ampm']);
|
683
|
$date = date_select_input_date($element, $input);
|
684
|
|
685
|
// If the field has errors, display them.
|
686
|
$error_field = implode('][', $element['#parents']);
|
687
|
$entered = array_values(array_filter($input));
|
688
|
if (empty($date) || !empty($date->errors)) {
|
689
|
// The input created a date but it has errors.
|
690
|
if (is_object($date) && !empty($date->errors)) {
|
691
|
$message = t('The value input for field %field is invalid:', array('%field' => $label));
|
692
|
$message .= '<br />' . implode('<br />', $date->errors);
|
693
|
form_set_error($error_field, $message);
|
694
|
return;
|
695
|
}
|
696
|
// Nothing was entered but the date is required.
|
697
|
elseif (empty($entered) && $element['#required']) {
|
698
|
$message = t('A valid date is required for %title.', array('%title' => $label));
|
699
|
form_set_error($error_field, $message);
|
700
|
return;
|
701
|
}
|
702
|
// Something was input but it wasn't enough to create a valid date.
|
703
|
elseif (!empty($entered)) {
|
704
|
$message = t('The value input for field %field is invalid.', array('%field' => $label));
|
705
|
form_set_error($error_field, $message);
|
706
|
return;
|
707
|
}
|
708
|
}
|
709
|
$value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
|
710
|
form_set_value($element, $value, $form_state);
|
711
|
}
|
712
|
|
713
|
/**
|
714
|
* Helper function for creating a date object out of user input.
|
715
|
*/
|
716
|
function date_select_input_date($element, $input) {
|
717
|
|
718
|
// Was anything entered? If not, we have no date.
|
719
|
if (!is_array($input)) {
|
720
|
return NULL;
|
721
|
}
|
722
|
else {
|
723
|
$entered = array_values(array_filter($input));
|
724
|
if (empty($entered)) {
|
725
|
return NULL;
|
726
|
}
|
727
|
}
|
728
|
$granularity = date_format_order($element['#date_format']);
|
729
|
if (isset($input['ampm'])) {
|
730
|
if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
|
731
|
$input['hour'] += 12;
|
732
|
}
|
733
|
elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
|
734
|
$input['hour'] -= 12;
|
735
|
}
|
736
|
}
|
737
|
unset($input['ampm']);
|
738
|
|
739
|
// Make the input match the granularity.
|
740
|
foreach (date_nongranularity($granularity) as $part) {
|
741
|
unset($input[$part]);
|
742
|
}
|
743
|
|
744
|
$date = new DateObject($input, $element['#date_timezone']);
|
745
|
if (is_object($date)) {
|
746
|
$date->limitGranularity($granularity);
|
747
|
if ($date->validGranularity($granularity, $element['#date_flexible'])) {
|
748
|
date_increment_round($date, $element['#date_increment']);
|
749
|
}
|
750
|
return $date;
|
751
|
}
|
752
|
return NULL;
|
753
|
}
|