function webform_expand_time in Webform 7.4
Same name and namespace in other branches
- 6.3 components/time.inc \webform_expand_time()
- 7.3 components/time.inc \webform_expand_time()
Form API #process function for Webform time fields.
1 string reference to 'webform_expand_time'
- _webform_render_time in components/
time.inc - Implements _webform_render_component().
File
- components/
time.inc, line 175 - Webform module time component.
Code
function webform_expand_time($element) {
// Expand the default value from a string into an array.
if (!empty($element['#default_value'])) {
// Adjust the time based on the user or site timezone.
if (variable_get('configurable_timezones', 1) && $element['#timezone'] == 'user') {
$timezone_name = isset($GLOBALS['user']->timezone) ? $GLOBALS['user']->timezone : 'UTC';
}
else {
$timezone_name = variable_get('date_default_timezone', 'UTC');
}
$default_values = webform_date_array(webform_strtodate('c', $element['#default_value'], $timezone_name), 'time');
}
else {
$default_values = array(
'hour' => '',
'minute' => '',
'second' => '',
);
}
$start_hour = $element['#start_time'] ? date('G', strtotime('1-1-1970 ' . $element['#start_time'])) : FALSE;
$end_hour = $element['#end_time'] ? date('G', strtotime('1-1-1970 ' . $element['#end_time'])) : FALSE;
$reduced_range = $start_hour !== FALSE && $start_hour > 0 || $end_hour !== FALSE && $end_hour < 23;
$format_12_hour = $element['#hourformat'] == '12-hour';
// Generate the choices for the hour drop-down select.
$hours = $format_12_hour && !$reduced_range ? array_slice(range(0, 12), 1, 12, TRUE) : range(0, 23);
if ($format_12_hour && $reduced_range) {
$hours = array_map(function ($hour) {
return 1 + ($hour + 11) % 12 . ($hour < 12 ? ' am' : ' pm');
}, $hours);
}
// Prune the hours to the allowed range.
if ($reduced_range) {
// $start_hour of FALSE type-juggles nicely to 0.
$end_hour = $end_hour === FALSE ? 23 : $end_hour;
if ($start_hour <= $end_hour) {
$hours = array_intersect_key($hours, array_flip(range($start_hour, $end_hour)));
}
else {
$hours = array_intersect_key($hours, array_flip(range($start_hour, 23))) + array_intersect_key($hours, array_flip(range(0, $end_hour)));
}
}
// Generate the choices for the minute drop-down select.
$minutes = range(0, 59, $element['#minuteincrements']);
$minutes = array_combine($minutes, array_map(function ($minute) {
return substr('00' . $minute, -2);
}, $minutes));
// Add the labels to the drop-down selects.
$hours = array(
'' => t('Hour'),
) + $hours;
$minutes = array(
'' => t('Minute'),
) + $minutes;
// Adjust the default for minutes if needed, rounding down if needed.
// Rounding down eliminate the problem of rounding up going to the next hour.
// Worse, rounding 23:59 up would actually be the next day, which can't be
// represented because time components aren't linked to date components.
if (!isset($minutes[$default_values['minute']])) {
$default_values['minute'] -= $default_values['minute'] % $element['#minuteincrements'];
}
// Set the overall default value.
if ($default_values['hour'] !== '') {
$element['#default_value'] = webform_date_string($default_values);
}
// Convert default to 12-hour if needed.
if ($format_12_hour && !$reduced_range) {
$default_values = webform_time_convert($default_values, '12-hour');
}
$element['hour'] = array(
'#prefix' => '',
'#type' => 'select',
'#title' => t('Hour'),
'#title_display' => 'invisible',
'#default_value' => $default_values['hour'],
'#options' => $hours,
);
$element['minute'] = array(
'#prefix' => ':',
'#type' => 'select',
'#title' => t('Minute'),
'#title_display' => 'invisible',
'#default_value' => $default_values['minute'],
'#options' => $minutes,
);
if ($format_12_hour && !$reduced_range) {
$element['ampm'] = array(
'#type' => 'radios',
'#default_value' => $default_values['ampm'] ? $default_values['ampm'] : 'am',
'#options' => array(
'am' => t('am'),
'pm' => t('pm'),
),
);
}
return $element;
}