'',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'value' => '',
'mandatory' => 0,
'extra' => array(
'timezone' => 'user',
'hourformat' => '12-hour',
'minuteincrements' => 1,
'title_display' => 0,
'description' => '',
'private' => FALSE,
),
);
}
/**
* Implements _webform_theme_component().
*/
function _webform_theme_time() {
return array(
'webform_time' => array(
'render element' => 'element',
'file' => 'components/time.inc',
),
'webform_display_time' => array(
'render element' => 'element',
'file' => 'components/time.inc',
),
);
}
/**
* Implements _webform_edit_component().
*/
function _webform_edit_time($component) {
$form = array();
$form['value'] = array(
'#type' => 'textfield',
'#title' => t('Default value'),
'#default_value' => $component['value'],
'#description' => t('The default value of the field.') . '
' . t('Accepts a time in any GNU Date Input Format. Strings such as now, +2 hours, and 10:30pm are all valid.'),
'#size' => 60,
'#maxlength' => 127,
'#weight' => 0,
);
$form['extra']['timezone'] = array(
'#type' => 'radios',
'#title' => t('Default value timezone'),
'#default_value' => $component['extra']['timezone'],
'#description' => t('If using relative dates for a default value (e.g. "now") base the current time on this timezone.'),
'#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
'#weight' => 2,
'#access' => variable_get('configurable_timezones', 1),
);
$form['display']['hourformat'] = array(
'#type' => 'radios',
'#title' => t('Time format'),
'#default_value' => $component['extra']['hourformat'],
'#options' => array('12-hour' => t('12-hour (am/pm)'), '24-hour' => t('24-hour')),
'#weight' => 2,
'#parents' => array('extra', 'hourformat'),
);
$form['display']['minuteincrements'] = array(
'#type' => 'select',
'#title' => t('Minute increments'),
'#default_value' => $component['extra']['minuteincrements'],
'#options' => array(
1 => t('1 minute'),
5 => t('5 minute'),
10 => t('10 minute'),
15 => t('15 minute'),
30 => t('30 minute'),
),
'#weight' => 3,
'#parents' => array('extra', 'minuteincrements'),
);
return $form;
}
/**
* Implements _webform_render_component().
*/
function _webform_render_time($component, $value = NULL, $filter = TRUE) {
$node = isset($component['nid']) ? node_load($component['nid']) : NULL;
$element = array(
'#type' => 'webform_time',
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#required' => $component['mandatory'],
'#weight' => $component['weight'],
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#element_validate' => array('webform_validate_time'),
'#hourformat' => $component['extra']['hourformat'],
'#minuteincrements' => $component['extra']['minuteincrements'],
'#default_value' => $filter ? _webform_filter_values($component['value'], $node, NULL, NULL, FALSE) : $component['value'],
'#timezone' => $component['extra']['timezone'],
'#process' => array('webform_expand_time'),
'#theme' => 'webform_time',
'#theme_wrappers' => array('webform_element'),
'#translatable' => array('title', 'description'),
);
// Set the value from Webform if available.
if (!empty($value[0])) {
$element['#default_value'] = $value[0];
}
return $element;
}
/**
* Form API #process function for Webform time fields.
*/
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' => '',
);
}
$first_hour = 0;
$last_hour = 23;
if ($element['#hourformat'] == '12-hour') {
$first_hour = 1;
$last_hour = 12;
$default_values = webform_time_convert($default_values, '12-hour');
$default_values['ampm'] = $default_values['ampm'] ? $default_values['ampm'] : 'am';
}
// Generate the choices for drop-down selects.
$hours[''] = t('hour');
$minutes[''] = t('minute');
for ($i = $first_hour; $i <= $last_hour; $i++) {
$hours[$i] = $i;
}
for ($i = 0; $i <= 59; $i += $element['#minuteincrements']) {
$minutes[$i] = $i < 10 ? "0$i" : $i;
}
$ampms = array('am' => t('am'), 'pm' => t('pm'));
// Adjust the default for minutes if needed, rounding up to the closest value.
if (!isset($minutes[$default_values['minute']])) {
foreach ($minutes as $minute => $padded_minute) {
if ($minute > $default_values['minute']) {
$default_values['minute'] = $minute;
break;
}
}
}
// If the above loop didn't set a value, it's because rounding up would go to
// the next hour. This gets quite a bit more complicated, since we need to
// deal with looping around on hours, as well as flipping am/pm.
if (!isset($minutes[$default_values['minute']])) {
$default_values['minute'] = 0;
$default_values['hour']++;
// If the hour rolls over also, set hour to the first hour in the list.
if (!isset($hours[$default_values['hour']])) {
$default_values['hour'] = $element['#hourformat'] == '12-hour' ? 1 : 0;
}
// If the hour has been incremented to 12:00 in 12-hour format, flip am/pm.
// Note that technically midnight and noon are neither am or pm, but common
// convention (and US standard) is to represent 12:00am as midnight.
// See http://en.wikipedia.org/wiki/Midnight#Start_and_end_of_day.
if ($element['#hourformat'] == '12-hour' && $default_values['hour'] == 12) {
$default_values['ampm'] = $default_values['ampm'] == 'am' ? 'pm' : 'am';
}
}
$element['hour'] = array(
'#prefix' => '',
'#type' => 'select',
'#default_value' => $default_values['hour'],
'#options' => $hours,
);
$element['minute'] = array(
'#prefix' => ':',
'#type' => 'select',
'#default_value' => $default_values['minute'],
'#options' => $minutes,
);
if (strcmp($element['#hourformat'], '12-hour') == 0) {
$element['ampm'] = array(
'#type' => 'radios',
'#default_value' => $default_values['ampm'],
'#options' => $ampms,
);
}
// Set the overall default value.
if ($default_values['hour'] !== '') {
$element['#default_value'] = webform_date_string($default_values);
}
return $element;
}
/**
* Theme a webform time element.
*/
function theme_webform_time($variables) {
$element = $variables['element'];
$element['hour']['#attributes']['class'] = array('hour');
$element['minute']['#attributes']['class'] = array('minute');
// Add error classes to all items within the element.
if (form_get_error($element)) {
$element['hour']['#attributes']['class'][] = 'error';
$element['minute']['#attributes']['class'][] = 'error';
}
$output = '