'',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'value' => '',
'required' => 0,
'extra' => array(
'timezone' => 'user',
'start_time' => '',
'end_time' => '',
'hourformat' => '12-hour',
'minuteincrements' => 1,
'title_display' => 0,
'description' => '',
'description_above' => FALSE,
'private' => FALSE,
'analysis' => 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['validation']['start_time'] = array(
'#type' => 'textfield',
'#title' => t('Start time'),
'#default_value' => $component['extra']['start_time'],
'#description' => t('The earliest time that may be entered into the field.'),
'#size' => 10,
'#weight' => 3,
'#parents' => array('extra', 'start_time'),
);
$form['validation']['end_time'] = array(
'#type' => 'textfield',
'#title' => t('End time'),
'#default_value' => $component['extra']['end_time'],
'#description' => t('The latest time that may be entered into the field.'),
'#size' => 10,
'#weight' => 4,
'#parents' => array('extra', 'end_time'),
);
$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 (for example, "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'),
);
$form['#validate'] = array('_webform_edit_time_validate');
return $form;
}
/**
* Implements hook_form_id_validate().
*
* Validate start and end times.
*/
function _webform_edit_time_validate($form, &$form_state) {
// Validate that the start and end times are valid. Don't validate the default
// time because with token substitution, it might not be valid at component
// definition time. The end time may be before the start time to facilitate
// time ranges spanning midnight.
foreach (array('start_time', 'end_time') as $field) {
$time[$field] = FALSE;
if (trim($form_state['values']['extra'][$field]) && ($time[$field] = strtotime('1-1-1970 UTC ' . $form_state['values']['extra'][$field])) === FALSE) {
form_set_error("extra][$field", t("The @field isn't a valid time.", array('@field' => $form['validation'][$field]['#title'])));
}
}
}
/**
* Implements _webform_render_component().
*/
function _webform_render_time($component, $value = NULL, $filter = TRUE, $submission = NULL) {
$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['required'],
'#weight' => $component['weight'],
'#description' => $filter ? webform_filter_descriptions($component['extra']['description'], $node) : $component['extra']['description'],
'#element_validate' => array('webform_validate_time'),
'#start_time' => trim($component['extra']['start_time']),
'#end_time' => trim($component['extra']['end_time']),
'#hourformat' => $component['extra']['hourformat'],
'#minuteincrements' => $component['extra']['minuteincrements'],
'#default_value' => $filter ? webform_replace_tokens($component['value'], $node) : $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' => '',
);
}
$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;
}
/**
* Theme a webform time element.
*/
function theme_webform_time($variables) {
$element = $variables['element'];
$element['hour']['#attributes']['class'][] = 'hour';
$element['minute']['#attributes']['class'][] = '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';
}
// Add HTML5 required attribute, if needed.
if ($element['#required']) {
$element['hour']['#attributes']['required'] = 'required';
$element['minute']['#attributes']['required'] = 'required';
if (!empty($element['ampm'])) {
$element['ampm']['am']['#attributes']['required'] = 'required';
$element['ampm']['pm']['#attributes']['required'] = 'required';
}
}
$output = '