| 1 |
<?php
|
| 2 |
//$Id: date_api_elements.inc,v 1.57 2009/09/08 14:19:24 karens Exp $
|
| 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 |
* Implementation of hook_elements().
|
| 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 re-formatted back into 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 |
* #date_timezone
|
| 34 |
* The local timezone to be used to create this date.
|
| 35 |
*
|
| 36 |
* #date_format
|
| 37 |
* A format string that describes the format and order of date parts to
|
| 38 |
* display in the edit form for this element. This makes it possible
|
| 39 |
* to show date parts in a custom order, or to leave some of them out.
|
| 40 |
* Be sure to add 'A' or 'a' to get an am/pm selector. Defaults to the
|
| 41 |
* short site default format.
|
| 42 |
*
|
| 43 |
* #date_label_position
|
| 44 |
* Handling option for date part labels, like 'Year', 'Month', and 'Day',
|
| 45 |
* can be 'above' the date part, 'within' it, or 'none', default is 'above' .
|
| 46 |
* The 'within' option shows the label as the first option in a select list
|
| 47 |
* or the default value for an empty textfield, taking up less screen space.
|
| 48 |
*
|
| 49 |
* #date_increment
|
| 50 |
* Increment minutes and seconds by this amount, default is 1.
|
| 51 |
*
|
| 52 |
* #date_year_range
|
| 53 |
* The number of years to go back and forward in a year selector,
|
| 54 |
* default is -3:+3 (3 back and 3 forward).
|
| 55 |
*
|
| 56 |
* #date_text_parts
|
| 57 |
* Array of date parts that should use textfields instead of selects
|
| 58 |
* i.e. array('year') will format the year as a textfield and other
|
| 59 |
* date parts as drop-down selects.
|
| 60 |
*/
|
| 61 |
function _date_api_elements() {
|
| 62 |
$date_base = array(
|
| 63 |
'#input' => TRUE, '#tree' => TRUE,
|
| 64 |
'#date_timezone' => date_default_timezone(),
|
| 65 |
'#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
|
| 66 |
'#date_text_parts' => array(),
|
| 67 |
'#date_increment' => 1,
|
| 68 |
'#date_year_range' => '-3:+3',
|
| 69 |
'#date_label_position' => 'above',
|
| 70 |
);
|
| 71 |
$type['date_select'] = array_merge($date_base, array(
|
| 72 |
'#process' => array('date_select_process'),
|
| 73 |
));
|
| 74 |
$type['date_text'] = array_merge($date_base, array(
|
| 75 |
'#process' => array('date_text_process'),
|
| 76 |
));
|
| 77 |
$type['date_timezone'] = array(
|
| 78 |
'#input' => TRUE, '#tree' => TRUE,
|
| 79 |
'#process' => array('date_timezone_process'),
|
| 80 |
);
|
| 81 |
return $type;
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* Create a timezone form element.
|
| 86 |
*
|
| 87 |
* @param array $element
|
| 88 |
* @return array
|
| 89 |
* the timezone form element
|
| 90 |
*/
|
| 91 |
function date_timezone_process($element, $form_state, $form) {
|
| 92 |
$element['#tree'] = TRUE;
|
| 93 |
$element['timezone'] = array(
|
| 94 |
'#type' => 'select',
|
| 95 |
'#title' => theme('date_part_label_timezone', 'select', $element),
|
| 96 |
'#default_value' => $element['#value'],
|
| 97 |
'#options' => date_timezone_names($element['#required']),
|
| 98 |
'#weight' => $element['#weight'],
|
| 99 |
'#required' => $element['#required'],
|
| 100 |
'#theme' => 'date_select_element',
|
| 101 |
);
|
| 102 |
if (isset($element['#element_validate'])) {
|
| 103 |
array_push($element['#element_validate'], 'date_timezone_validate');
|
| 104 |
}
|
| 105 |
else {
|
| 106 |
$element['#element_validate'] = array('date_timezone_validate');
|
| 107 |
}
|
| 108 |
// TODO This sometimes causes problems, do we need it?
|
| 109 |
//$element['#attributes'] = array('class' => array('date-timezone clear-block'));
|
| 110 |
return $element;
|
| 111 |
}
|
| 112 |
|
| 113 |
/**
|
| 114 |
* Text date input form.
|
| 115 |
*
|
| 116 |
* Display all or part of a date in a single textfield.
|
| 117 |
*
|
| 118 |
* The exact parts displayed in the field are those in #date_granularity.
|
| 119 |
* The display of each part comes from #date_format.
|
| 120 |
*
|
| 121 |
* In regular FAPI processing $element['#value'] will contain a string
|
| 122 |
* value before the form is submitted, and an array during submission.
|
| 123 |
*
|
| 124 |
* In regular FAPI processing $edit is empty until the form is submitted
|
| 125 |
* when it will contain an array.
|
| 126 |
*
|
| 127 |
* Views widget processing now receives the same values as normal FAPI
|
| 128 |
* processing (that was not true in Views 1).
|
| 129 |
*
|
| 130 |
*/
|
| 131 |
function date_text_process($element, $form_state, $form) {
|
| 132 |
$date = NULL;
|
| 133 |
$granularity = date_format_order($element['#date_format']);
|
| 134 |
$edit = $form_state['values'];
|
| 135 |
|
| 136 |
// We sometimes get $edit without $edit['date'] from
|
| 137 |
// Views exposed filters.
|
| 138 |
if (!empty($edit) && is_array($edit) && !empty($edit['date'])) {
|
| 139 |
$datetime = date_convert_from_custom($edit['date'], $element['#date_format']);
|
| 140 |
$date = date_make_date($datetime, $element['#date_timezone'], DATE_DATETIME, $granularity);
|
| 141 |
}
|
| 142 |
elseif (!empty($element['#value'])) {
|
| 143 |
$date = date_make_date($element['#value'], $element['#date_timezone'], DATE_DATETIME, $granularity);
|
| 144 |
}
|
| 145 |
$element['#tree'] = TRUE;
|
| 146 |
$element['#theme_wrappers'] = array('date_text');
|
| 147 |
|
| 148 |
$element['date']['#type'] = 'textfield';
|
| 149 |
$element['date']['#weight'] = !empty($element['date']['#weight']) ? $element['date']['#weight'] : $element['#weight'];
|
| 150 |
$element['date']['#default_value'] = is_object($date) ? date_format($date , $element['#date_format']) : '';
|
| 151 |
$element['date']['#attributes'] = array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-date') : array('date-date'));
|
| 152 |
|
| 153 |
$element['date']['#description'] = ' ' . t('Format: @date', array('@date' => date($element['#date_format'], REQUEST_TIME)));
|
| 154 |
|
| 155 |
// Keep the system from creating an error message for the sub-element.
|
| 156 |
// We'll set our own message on the parent element.
|
| 157 |
//$element['date']['#required'] = $element['#required'];
|
| 158 |
$element['date']['#theme'] = 'date_textfield_element';
|
| 159 |
if (isset($element['#element_validate'])) {
|
| 160 |
array_push($element['#element_validate'], 'date_text_validate');
|
| 161 |
}
|
| 162 |
else {
|
| 163 |
$element['#element_validate'] = array('date_text_validate');
|
| 164 |
}
|
| 165 |
if (!empty($element['#force_value'])) {
|
| 166 |
$element['date']['#value'] = $element['date']['#default_value'];
|
| 167 |
}
|
| 168 |
return $element;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Flexible date/time drop-down selector.
|
| 173 |
*
|
| 174 |
* Splits date into a collection of date and time sub-elements, one
|
| 175 |
* for each date part. Each sub-element can be either a textfield or a
|
| 176 |
* select, based on the value of ['#date_settings']['text_fields'].
|
| 177 |
*
|
| 178 |
* The exact parts displayed in the field are those in #date_granularity.
|
| 179 |
* The display of each part comes from ['#date_settings']['format'].
|
| 180 |
*
|
| 181 |
* In regular FAPI processing $element['#value'] will contain a string
|
| 182 |
* value before the form is submitted, and an array during submission.
|
| 183 |
*
|
| 184 |
* In regular FAPI processing $edit is empty until the form is submitted
|
| 185 |
* when it will contain an array.
|
| 186 |
*
|
| 187 |
* Views widget processing now receives the same values as normal FAPI
|
| 188 |
* processing (that was not true in Views 1).
|
| 189 |
*
|
| 190 |
*/
|
| 191 |
function date_select_process($element, $form_state, $form) {
|
| 192 |
$date = NULL;
|
| 193 |
$granularity = date_format_order($element['#date_format']);
|
| 194 |
$edit = $form_state['values'];
|
| 195 |
|
| 196 |
if (!empty($edit)) {
|
| 197 |
$date = date_make_date($edit, $element['#date_timezone'], DATE_ARRAY, $granularity);
|
| 198 |
}
|
| 199 |
elseif (!empty($element['#value'])) {
|
| 200 |
$date = date_make_date($element['#value'], $element['#date_timezone'], DATE_DATETIME, $granularity);
|
| 201 |
}
|
| 202 |
$element['#tree'] = TRUE;
|
| 203 |
$element['#theme_wrappers'] = array('date_select');
|
| 204 |
date_increment_round($date, $element['#date_increment']);
|
| 205 |
$element += (array) date_parts_element($element, $date, $element['#date_format']);
|
| 206 |
|
| 207 |
// Store a hidden value for all date parts not in the current display.
|
| 208 |
$granularity = date_format_order($element['#date_format']);
|
| 209 |
$formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's');
|
| 210 |
foreach (date_nongranularity($granularity) as $field) {
|
| 211 |
if ($field != 'timezone') {
|
| 212 |
$element[$field] = array(
|
| 213 |
'#type' => 'hidden',
|
| 214 |
'#value' => 0,
|
| 215 |
);
|
| 216 |
}
|
| 217 |
}
|
| 218 |
if (isset($element['#element_validate'])) {
|
| 219 |
array_push($element['#element_validate'], 'date_select_validate');
|
| 220 |
}
|
| 221 |
else {
|
| 222 |
$element['#element_validate'] = array('date_select_validate');
|
| 223 |
}
|
| 224 |
return $element;
|
| 225 |
}
|
| 226 |
|
| 227 |
/**
|
| 228 |
* Create form elements for one or more date parts.
|
| 229 |
*
|
| 230 |
* Get the order of date elements from the provided format.
|
| 231 |
* If the format order omits any date parts in the granularity, alter the
|
| 232 |
* granularity array to match the format, then flip the $order array
|
| 233 |
* to get the position for each element. Then iterate through the
|
| 234 |
* elements and create a sub-form for each part.
|
| 235 |
*
|
| 236 |
* @param array $element
|
| 237 |
* @param object $date
|
| 238 |
* @param array $granularity
|
| 239 |
* @param string $format
|
| 240 |
* @return array
|
| 241 |
* the form array for the submitted date parts
|
| 242 |
*/
|
| 243 |
function date_parts_element($element, $date, $format) {
|
| 244 |
$granularity = date_format_order($format);
|
| 245 |
$sub_element = array('#granularity' => $granularity);
|
| 246 |
$order = array_flip($granularity);
|
| 247 |
|
| 248 |
$hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g': 'G';
|
| 249 |
$month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
|
| 250 |
$count = 0;
|
| 251 |
$increment = min(intval($element['#date_increment']), 1);
|
| 252 |
foreach ($granularity as $field) {
|
| 253 |
// Allow empty value as option if date is not required
|
| 254 |
// or if empty value was provided as a starting point.
|
| 255 |
$part_required = ($element['#required'] && is_object($date)) ? TRUE : FALSE;
|
| 256 |
$part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
|
| 257 |
$sub_element[$field] = array(
|
| 258 |
'#weight' => $order[$field],
|
| 259 |
'#required' => $element['#required'],
|
| 260 |
'#attributes' => array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-' . $field) : array('date-' . $field)),
|
| 261 |
);
|
| 262 |
switch ($field) {
|
| 263 |
case 'year':
|
| 264 |
$range = date_range_years($element['#date_year_range'], $date);
|
| 265 |
$min_year = $range[0];
|
| 266 |
$max_year = $range[1];
|
| 267 |
|
| 268 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'Y') : '';
|
| 269 |
if ($part_type == 'select') {
|
| 270 |
$sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required));
|
| 271 |
}
|
| 272 |
break;
|
| 273 |
case 'month':
|
| 274 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'n') : '';
|
| 275 |
if ($part_type == 'select') {
|
| 276 |
$sub_element[$field]['#options'] = $month_function($part_required);
|
| 277 |
}
|
| 278 |
break;
|
| 279 |
case 'day':
|
| 280 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'j') : '';
|
| 281 |
if ($part_type == 'select') {
|
| 282 |
$sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
|
| 283 |
}
|
| 284 |
break;
|
| 285 |
case 'hour':
|
| 286 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, $hours_format) : '';
|
| 287 |
if ($part_type == 'select') {
|
| 288 |
$sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required));
|
| 289 |
}
|
| 290 |
$sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element);
|
| 291 |
break;
|
| 292 |
case 'minute':
|
| 293 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'i') : '';
|
| 294 |
if ($part_type == 'select') {
|
| 295 |
$sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
|
| 296 |
}
|
| 297 |
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
|
| 298 |
break;
|
| 299 |
case 'second':
|
| 300 |
$sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 's') : '';
|
| 301 |
if ($part_type == 'select') {
|
| 302 |
$sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
|
| 303 |
}
|
| 304 |
$sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
|
| 305 |
break;
|
| 306 |
}
|
| 307 |
|
| 308 |
// Add handling for the date part label.
|
| 309 |
$label = theme('date_part_label_' . $field, $part_type, $element);
|
| 310 |
if (in_array($field, $element['#date_text_parts'])) {
|
| 311 |
$sub_element[$field]['#type'] = 'textfield';
|
| 312 |
$sub_element[$field]['#theme'] = 'date_textfield_element';
|
| 313 |
$sub_element[$field]['#size'] = 7;
|
| 314 |
if ($element['#date_label_position'] == 'within') {
|
| 315 |
if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
|
| 316 |
$sub_element[$field]['#options'] = array(
|
| 317 |
'-' . $label => '-' . $label) + $sub_element[$field]['#options'];
|
| 318 |
}
|
| 319 |
if (empty($sub_element[$field]['#default_value'])) {
|
| 320 |
$sub_element[$field]['#default_value'] = '-' . $label;
|
| 321 |
}
|
| 322 |
}
|
| 323 |
elseif ($element['#date_label_position'] != 'none') {
|
| 324 |
$sub_element[$field]['#title'] = $label;
|
| 325 |
}
|
| 326 |
}
|
| 327 |
else {
|
| 328 |
$sub_element[$field]['#type'] = 'select';
|
| 329 |
$sub_element[$field]['#theme'] = 'date_select_element';
|
| 330 |
if ($element['#date_label_position'] == 'within') {
|
| 331 |
$sub_element[$field]['#options'] = array(
|
| 332 |
'' => '-' . $label) + $sub_element[$field]['#options'];
|
| 333 |
}
|
| 334 |
elseif ($element['#date_label_position'] != 'none') {
|
| 335 |
$sub_element[$field]['#title'] = $label;
|
| 336 |
}
|
| 337 |
}
|
| 338 |
}
|
| 339 |
|
| 340 |
// Views exposed filters are treated as submitted even if not,
|
| 341 |
// so force the #default value in that case.
|
| 342 |
if (!empty($element[$field]['#force_value'])) {
|
| 343 |
$sub_element[$field]['#value'] = $sub_element[$field]['#default_value'];
|
| 344 |
}
|
| 345 |
|
| 346 |
if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
|
| 347 |
$sub_element['ampm'] = array(
|
| 348 |
'#type' => 'select',
|
| 349 |
'#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '',
|
| 350 |
'#options' => drupal_map_assoc(date_ampm()),
|
| 351 |
'#weight' => 8,
|
| 352 |
'#attributes' => array('class' => array('date-ampm')),
|
| 353 |
);
|
| 354 |
if ($element['#date_label_position'] == 'within') {
|
| 355 |
$sub_element['ampm']['#options'] = array('' => '-' . theme('date_part_label_ampm', 'ampm', $element)) + $sub_element['ampm']['#options'];
|
| 356 |
}
|
| 357 |
elseif ($element['#date_label_position'] != 'none') {
|
| 358 |
$sub_element['ampm']['#title'] = theme('date_part_label_ampm', 'ampm', $element);
|
| 359 |
}
|
| 360 |
}
|
| 361 |
return $sub_element;
|
| 362 |
}
|
| 363 |
|
| 364 |
/**
|
| 365 |
* Helper function to round minutes and seconds to requested value.
|
| 366 |
*/
|
| 367 |
function date_increment_round(&$date, $increment) {
|
| 368 |
// Round minutes and seconds, if necessary.
|
| 369 |
if (is_object($date) && $increment > 1) {
|
| 370 |
$day = intval(date_format($date, 'j'));
|
| 371 |
$hour = intval(date_format($date, 'H'));
|
| 372 |
$second = intval(round(intval(date_format($date, 's')) / $increment) * $increment);
|
| 373 |
$minute = intval(date_format($date, 'i'));
|
| 374 |
if ($second == 60) {
|
| 375 |
$minute += 1;
|
| 376 |
$second = 0;
|
| 377 |
}
|
| 378 |
$minute = intval(round($minute / $increment) * $increment);
|
| 379 |
if ($minute == 60) {
|
| 380 |
$hour += 1;
|
| 381 |
$minute = 0;
|
| 382 |
}
|
| 383 |
date_time_set($date, $hour, $minute, $second);
|
| 384 |
if ($hour == 24) {
|
| 385 |
$day += 1;
|
| 386 |
$hour = 0;
|
| 387 |
$year = date_format($date, 'Y');
|
| 388 |
$month = date_format($date, 'n');
|
| 389 |
date_date_set($date, $year, $month, $day);
|
| 390 |
}
|
| 391 |
}
|
| 392 |
return $date;
|
| 393 |
}
|
| 394 |
|
| 395 |
/**
|
| 396 |
* Validation function for date selector.
|
| 397 |
*
|
| 398 |
* When used as a Views widget, the validation step always gets triggered,
|
| 399 |
* even with no form submission. Before form submission $element['#value']
|
| 400 |
* contains a string, after submission it contains an array.
|
| 401 |
*
|
| 402 |
*/
|
| 403 |
function date_select_validate($element, &$form_state) {
|
| 404 |
if (is_string($element['#value'])) {
|
| 405 |
return;
|
| 406 |
}
|
| 407 |
// Strip field labels out of the results.
|
| 408 |
foreach ($element['#value'] as $field => $field_value) {
|
| 409 |
if (substr($field_value, 0, 1) == '-') {
|
| 410 |
$element['#value'][$field] = '';
|
| 411 |
}
|
| 412 |
}
|
| 413 |
|
| 414 |
$error_field = implode('][', $element['#parents']);
|
| 415 |
$errors = array();
|
| 416 |
$label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
|
| 417 |
|
| 418 |
if (in_array('year', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['year']))) {
|
| 419 |
if ($element['#value']['year'] < variable_get('date_min_year', 1) || $element['#value']['year'] > variable_get('date_max_year', 4000)) {
|
| 420 |
$errors[] = t('The year must be a number between %min and %max.', array(
|
| 421 |
'%min' => variable_get('date_min_year', 1), '%max' => variable_get('date_max_year', 4000)));
|
| 422 |
}
|
| 423 |
else {
|
| 424 |
$year = $element['#value']['year'];
|
| 425 |
}
|
| 426 |
}
|
| 427 |
if (in_array('month', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['month']))) {
|
| 428 |
if ($element['#value']['month'] < 1 || $element['#value']['month'] > 12) {
|
| 429 |
$errors[] = t('The month must be a number between 1 and 12.');
|
| 430 |
}
|
| 431 |
else {
|
| 432 |
$month = $element['#value']['month'];
|
| 433 |
}
|
| 434 |
}
|
| 435 |
if (in_array('day', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['day']))) {
|
| 436 |
$min = 1;
|
| 437 |
$max = isset($year) && isset($month) ? date_days_in_month($year, $month) : 31;
|
| 438 |
if ($element['#value']['day'] < $min || $element['#value']['day'] > $max) {
|
| 439 |
$errors[] = t('The day must be a number between !min and !max.', array('!min' => $min, '!max' => $max));
|
| 440 |
}
|
| 441 |
}
|
| 442 |
if (in_array('hour', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['hour']))) {
|
| 443 |
$min = isset($element['#value']['ampm']) ? 1 : 0;
|
| 444 |
$max = isset($element['#value']['ampm']) ? 12 : 23;
|
| 445 |
if ($element['#value']['hour'] < $min || $element['#value']['hour'] > $max) {
|
| 446 |
$errors[] = t('The hour must be a number between !min and !max.', array('!min' => $min, '!max' => $max));
|
| 447 |
}
|
| 448 |
}
|
| 449 |
if (in_array('minute', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['minute']))) {
|
| 450 |
$min = 0;
|
| 451 |
$max = 59;
|
| 452 |
if ($element['#value']['minute'] < $min || $element['#value']['minute'] > $max) {
|
| 453 |
$errors[] = t('The minute must be a number between !min and !max.', array('!min' => $min, '!max' => $max));
|
| 454 |
}
|
| 455 |
}
|
| 456 |
if (in_array('second', $element['#granularity']) && ($element['#required'] || !empty($element['#value']['second']))) {
|
| 457 |
$min = 0;
|
| 458 |
$max = 59;
|
| 459 |
if ($element['#value']['second'] < $min || $element['#value']['second'] > $max) {
|
| 460 |
$errors[] = t('The second must be a number between !min and !max.', array('!min' => $min, '!max' => $max));
|
| 461 |
}
|
| 462 |
}
|
| 463 |
if (isset($element['#value']['ampm'])) {
|
| 464 |
if ($element['#value']['ampm'] == 'pm' && $element['#value']['hour'] < 12) {
|
| 465 |
$element['#value']['hour'] += 12;
|
| 466 |
}
|
| 467 |
elseif ($element['#value']['ampm'] == 'am' && $element['#value']['hour'] == 12) {
|
| 468 |
$element['#value']['hour'] -= 12;
|
| 469 |
}
|
| 470 |
}
|
| 471 |
$value = date_select_input_value($element);
|
| 472 |
if (empty($value) && empty($errors) && $element['#required']) {
|
| 473 |
$errors[] = t('A valid value is required.');
|
| 474 |
}
|
| 475 |
if (!empty($errors)) {
|
| 476 |
array_unshift($errors, t('Field %field has errors. ', array('%field' => $label)));
|
| 477 |
form_set_error($error_field, implode(' ', $errors));
|
| 478 |
}
|
| 479 |
|
| 480 |
// If there are no errors and the value is valid, set it.
|
| 481 |
if (empty($errors) && !empty($value)) {
|
| 482 |
form_set_value($element, $value, $form_state);
|
| 483 |
}
|
| 484 |
else {
|
| 485 |
form_set_value($element, NULL, $form_state);
|
| 486 |
}
|
| 487 |
}
|
| 488 |
|
| 489 |
/**
|
| 490 |
* Helper function for extracting a date value out of user input.
|
| 491 |
*/
|
| 492 |
function date_select_input_value($element) {
|
| 493 |
$granularity = date_format_order($element['#date_format']);
|
| 494 |
if (date_is_valid($element['#value'], DATE_ARRAY, $granularity)) {
|
| 495 |
// Use fuzzy_datetime here to be sure year-only dates
|
| 496 |
// aren't inadvertantly shifted to the wrong year by trying
|
| 497 |
// to save '2009-00-00 00:00:00' .
|
| 498 |
return date_fuzzy_datetime(date_convert($element['#value'], DATE_ARRAY, DATE_DATETIME));
|
| 499 |
}
|
| 500 |
return NULL;
|
| 501 |
}
|
| 502 |
|
| 503 |
/**
|
| 504 |
* Validation for text input.
|
| 505 |
*
|
| 506 |
* When used as a Views widget, the validation step always gets triggered,
|
| 507 |
* even with no form submission. Before form submission $element['#value']
|
| 508 |
* contains a string, after submission it contains an array.
|
| 509 |
*
|
| 510 |
*/
|
| 511 |
function date_text_validate($element, &$form_state) {
|
| 512 |
if (is_string($element['#value'])) {
|
| 513 |
return;
|
| 514 |
}
|
| 515 |
$parents = $element['#parents'];
|
| 516 |
$label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
|
| 517 |
$value = date_text_input_value($element);
|
| 518 |
|
| 519 |
if (empty($value) && !empty($element['#required'])) {
|
| 520 |
form_error($element, t('A valid date is required for %title.', array('%title' => $label)));
|
| 521 |
}
|
| 522 |
elseif (empty($value) && !empty($element['#value']['date'])) {
|
| 523 |
form_error($element, t('%title is invalid. ', array('%title' => $label)));
|
| 524 |
}
|
| 525 |
elseif (!empty($value)) {
|
| 526 |
form_set_value($element, $value, $form_state);
|
| 527 |
}
|
| 528 |
}
|
| 529 |
|
| 530 |
/**
|
| 531 |
* Helper function for extracting a date value out of user input.
|
| 532 |
*/
|
| 533 |
function date_text_input_value($element) {
|
| 534 |
$form_values = $element['#value'];
|
| 535 |
$granularity = date_format_order($element['#date_format']);
|
| 536 |
$input = $form_values['date'];
|
| 537 |
if (!$element['#required'] && trim($input) == '') return NULL;
|
| 538 |
|
| 539 |
$value = date_limit_value(date_convert_from_custom($input, $element['#date_format']), $granularity);
|
| 540 |
|
| 541 |
// If it creates a valid date, use it.
|
| 542 |
if (date_is_valid($value, DATE_DATETIME, $granularity)) {
|
| 543 |
return $value;
|
| 544 |
}
|
| 545 |
// TODO come back and try to find a way to use strtotime to guess
|
| 546 |
// a valid value. Previous attempts to do it were too forgiving and
|
| 547 |
// invalid input was just silently converted to 'now' .
|
| 548 |
// See http://drupal.org/node/265076.
|
| 549 |
|
| 550 |
return NULL;
|
| 551 |
}
|
| 552 |
|
| 553 |
/**
|
| 554 |
* Validation for timezone input
|
| 555 |
*
|
| 556 |
* Move the timezone value from the nested field back to the original field.
|
| 557 |
*/
|
| 558 |
function date_timezone_validate($element, &$form_state) {
|
| 559 |
form_set_value($element, $element['#value']['timezone'], $form_state);
|
| 560 |
}
|
| 561 |
|
| 562 |
|
| 563 |
/**
|
| 564 |
* Convert a date input in a custom format to a standard date type
|
| 565 |
*
|
| 566 |
* Handles conversion of translated month names (i.e. turns t('Mar') or
|
| 567 |
* t('March') into 3). Also properly handles dates input in European style
|
| 568 |
* short formats, like DD/MM/YYYY. Works by parsing the format string
|
| 569 |
* to create a regex that can be used on the input value.
|
| 570 |
*
|
| 571 |
* The original code to do this was created by Yves Chedemois (yched).
|
| 572 |
*
|
| 573 |
* @param string $date
|
| 574 |
* a date value
|
| 575 |
* @param string $format
|
| 576 |
* a date format string that describes the format of the input value
|
| 577 |
* @return mixed
|
| 578 |
* input value converted to a DATE_DATETIME value
|
| 579 |
*/
|
| 580 |
function date_convert_from_custom($date, $format) {
|
| 581 |
$array = date_format_patterns();
|
| 582 |
foreach ($array as $key => $value) {
|
| 583 |
$patterns[] = "`(^|[^\\\\\\\\])". $key ."`"; // the letter with no preceding '\'
|
| 584 |
$repl1[] = '${1}(.)'; // a single character
|
| 585 |
$repl2[] = '${1}(' . $value .')'; // the
|
| 586 |
}
|
| 587 |
$patterns[] = "`\\\\\\\\([". implode(array_keys($array)) ."])`";
|
| 588 |
$repl1[] = '${1}';
|
| 589 |
$repl2[] = '${1}';
|
| 590 |
|
| 591 |
$format_regexp = preg_quote($format);
|
| 592 |
|
| 593 |
// extract letters
|
| 594 |
$regex1 = preg_replace($patterns, $repl1, $format_regexp, 1);
|
| 595 |
$regex1 = str_replace('A', '(.)', $regex1);
|
| 596 |
$regex1 = str_replace('a', '(.)', $regex1);
|
| 597 |
preg_match('`^' . $regex1 . '$`', stripslashes($format), $letters);
|
| 598 |
array_shift($letters);
|
| 599 |
|
| 600 |
// extract values
|
| 601 |
$regex2 = preg_replace($patterns, $repl2, $format_regexp, 1);
|
| 602 |
$regex2 = str_replace('A', '(AM|PM)', $regex2);
|
| 603 |
$regex2 = str_replace('a', '(am|pm)', $regex2);
|
| 604 |
preg_match('`^' . $regex2 . '$`', $date, $values);
|
| 605 |
array_shift($values);
|
| 606 |
|
| 607 |
// if we did not find all the values for the patterns in the format, abort
|
| 608 |
if (count($letters) != count($values)) {
|
| 609 |
return NULL;
|
| 610 |
}
|
| 611 |
$final_date = array('hour' => 0, 'minute' => 0, 'second' => 0,
|
| 612 |
'month' => 0, 'day' => 0, 'year' => 0);
|
| 613 |
foreach ($letters as $i => $letter) {
|
| 614 |
$value = $values[$i];
|
| 615 |
switch ($letter) {
|
| 616 |
case 'd':
|
| 617 |
case 'j':
|
| 618 |
$final_date['day'] = intval($value);
|
| 619 |
break;
|
| 620 |
case 'n':
|
| 621 |
case 'm':
|
| 622 |
$final_date['month'] = intval($value);
|
| 623 |
break;
|
| 624 |
case 'F':
|
| 625 |
$array_month_long = array_flip(date_month_names());
|
| 626 |
$final_date['month'] = $array_month_long[$value];
|
| 627 |
break;
|
| 628 |
case 'M':
|
| 629 |
$array_month = array_flip(date_month_names_abbr());
|
| 630 |
$final_date['month'] = $array_month[$value];
|
| 631 |
break;
|
| 632 |
case 'Y':
|
| 633 |
case 'y':
|
| 634 |
$year = $value;
|
| 635 |
// if no century, we add the current one ("06" => "2006")
|
| 636 |
$final_date['year'] = str_pad($year, 4, substr(date("Y"), 0, 2), STR_PAD_LEFT);
|
| 637 |
break;
|
| 638 |
case 'a':
|
| 639 |
case 'A':
|
| 640 |
$ampm = strtolower($value);
|
| 641 |
break;
|
| 642 |
case 'g':
|
| 643 |
case 'h':
|
| 644 |
case 'G':
|
| 645 |
case 'H':
|
| 646 |
$final_date['hour'] = intval($value);
|
| 647 |
break;
|
| 648 |
case 'i':
|
| 649 |
$final_date['minute'] = intval($value);
|
| 650 |
break;
|
| 651 |
case 's':
|
| 652 |
$final_date['second'] = intval($value);
|
| 653 |
break;
|
| 654 |
case 'U':
|
| 655 |
return date_convert($value, DATE_UNIX, DATE_DATETIME);
|
| 656 |
break;
|
| 657 |
}
|
| 658 |
}
|
| 659 |
if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) {
|
| 660 |
$final_date['hour'] += 12;
|
| 661 |
}
|
| 662 |
elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) {
|
| 663 |
$final_date['hour'] -= 12;
|
| 664 |
}
|
| 665 |
// Don't test for valid date, we might use this to extract
|
| 666 |
// incomplete date part info from user input.
|
| 667 |
return date_convert($final_date, DATE_ARRAY, DATE_DATETIME);
|
| 668 |
}
|