'',
'form_key' => NULL,
'pid' => 0,
'weight' => 0,
'value' => '',
'mandatory' => 0,
'extra' => array(
'timezone' => 'user',
'year_start' => '-2',
'year_end' => '+2',
'year_textfield' => 0,
'datepicker' => 1,
'title_display' => 0,
'description' => '',
),
);
}
/**
* Implementation of _webform_theme_component().
*/
function _webform_theme_date() {
return array(
'webform_date' => array(
'render element' => 'element',
),
'webform_display_date' => array(
'render element' => 'element',
),
'webform_calendar' => array(
'variables' => array('component' => NULL, 'calendar_classes' => NULL),
'template' => 'templates/webform-calendar',
),
);
}
/**
* Implementation of _webform_edit_component().
*/
function _webform_edit_date($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 any date in any GNU Date Input Format. Strings such as today, +2 months, and Dec 9 2004 are all valid.'),
'#size' => 60,
'#maxlength' => 127,
'#weight' => 0,
);
$form['extra']['timezone'] = array(
'#type' => 'radios',
'#title' => t('Default value timezone'),
'#default_value' => empty($component['extra']['timezone']) ? 'user' : $component['extra']['timezone'],
'#description' => t('Adjust the default time value according to a specific timezone.'),
'#options' => array('user' => t('User timezone'), 'site' => t('Website timezone')),
'#weight' => -1,
'#access' => variable_get('configurable_timezones', 1),
);
$form['display']['datepicker'] = array(
'#type' => 'checkbox',
'#title' => t('Enable popup calendar'),
'#default_value' => $component['extra']['datepicker'],
'#description' => t('Enable a JavaScript date picker next to the date field.'),
'#weight' => 2,
'#parents' => array('extra', 'datepicker'),
);
$form['display']['year_textfield'] = array(
'#type' => 'checkbox',
'#title' => t('Use a textfield for year'),
'#default_value' => $component['extra']['year_textfield'],
'#description' => t('If checked, the generated date field will use a textfield for the year. Otherwise it will use a select list.'),
'#weight' => 5,
'#parents' => array('extra', 'year_textfield'),
);
$form['validation']['year_start'] = array(
'#type' => 'textfield',
'#title' => t('Start year'),
'#default_value' => $component['extra']['year_start'],
'#description' => t('The first year that is allowed to be entered. May be relative (i.e. -2 or +2) or simply the year (i.e. 1950).'),
'#size' => 10,
'#maxlength' => 4,
'#weight' => 3,
'#parents' => array('extra', 'year_start'),
);
$form['validation']['year_end'] = array(
'#type' => 'textfield',
'#title' => t('End year'),
'#default_value' => $component['extra']['year_end'],
'#description' => t('The last year that is allowed to be entered. May be relative (i.e. -2 or +2) or simply the year (i.e. 1950).'),
'#size' => 10,
'#maxlength' => 4,
'#weight' => 4,
'#parents' => array('extra', 'year_end'),
);
return $form;
}
/**
* Implementation of _webform_render_component().
*/
function _webform_render_date($component, $value = NULL, $filter = TRUE) {
$element = array(
'#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
'#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
'#weight' => $component['weight'],
'#type' => 'date',
'#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description'],
'#required' => $component['mandatory'],
'#year_start' => $component['extra']['year_start'],
'#year_end' => $component['extra']['year_end'],
'#process' => array('webform_expand_date'),
'#theme' => 'webform_date',
'#theme_wrappers' => array('webform_element'),
'#element_validate' => array('webform_validate_date'),
'#webform_component' => $component,
);
if ($component['extra']['datepicker']) {
$element['#datepicker'] = TRUE;
$element['#attached'] = array(
'library' => array(
array('system', 'ui.datepicker'),
),
);
}
if (isset($value[0]) && $value[0] !== '') {
$value = webform_date_array($value[0], 'date');
$element['#default_value'] = $value;
}
return $element;
}
/**
* Form API #process function for Webform date fields.
*/
function webform_expand_date($element) {
$component = $element['#webform_component'];
// Accept a string or array value for #default_value.
if (isset($element['#default_value']) && is_string($element['#default_value'])) {
$element['#default_value'] = webform_date_array($element['#default_value'], 'date');
}
// Set defaults according to existing #default_value (set by Form API)
if (isset($element['#default_value']['month']) || isset($element['#default_value']['day']) || isset($element['#default_value']['year'])) {
$default_values = array(
'month' => $element['#default_value']['month'],
'day' => $element['#default_value']['day'],
'year' => $element['#default_value']['year'],
);
}
// Or, if none, use set the defaults of the component.
elseif (drupal_strlen($component['value']) > 0) {
$timezone = $component['extra']['timezone'] != 'user' ? NULL : 'user';
$default_values = webform_date_array(webform_strtodate('c', $component['value'], $timezone), 'date');
}
else {
$default_values = array(
'day' => NULL,
'month' => NULL,
'year' => NULL,
);
}
// Let Drupal do it's normal expansion.
$element = form_process_date($element);
// Set default values.
foreach ($default_values as $type => $value) {
switch ($type) {
case 'month':
$none = t('Month');
break;
case 'day':
$none = t('Day');
break;
case 'year':
$none = t('Year');
break;
}
unset($element[$type]['#value']);
$element[$type]['#default_value'] = isset($default_values[$type]) ? $default_values[$type] : NULL;
$element[$type]['#options'] = array('' => $none) + $element[$type]['#options'];
}
// Convert relative dates to absolute ones.
foreach (array('year_start', 'year_end') as $start_end) {
$year = $element['#' . $start_end];
if (strpos($year, '-') === 0 || strpos($year, '+') === 0) {
$timezone = $component['extra']['timezone'] != 'user' ? NULL : 'user';
$element['#' . $start_end] = webform_strtodate('Y', $year . ' years', $timezone);
}
}
// Tweak the year field.
if ($component['extra']['year_textfield']) {
$element['year']['#type'] = 'textfield';
$element['year']['#size'] = 5;
$element['year']['#maxlength'] = 4;
unset($element['year']['#options']);
}
elseif (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
$element['year']['#options'] = array('' => t('Year')) + drupal_map_assoc(range($element['#year_start'], $element['#year_end']));
}
return $element;
}
/**
* Theme a webform date element.
*/
function theme_webform_date($variables) {
$element = $variables['element'];
$element['year']['#attributes']['class'] = array('year');
$element['month']['#attributes']['class'] = array('month');
$element['day']['#attributes']['class'] = array('day');
// Add error classes to all items within the element.
if (form_get_error($element)) {
$element['year']['#attributes']['class'][] = 'error';
$element['month']['#attributes']['class'][] = 'error';
$element['day']['#attributes']['class'][] = 'error';
}
$class = array('webform-container-inline');
// Add the JavaScript calendar if available (provided by Date module package).
if (!empty($element['#datepicker'])) {
$class[] = 'webform-datepicker';
$calendar_class = array('webform-calendar');
if ($element['#year_start'] && is_numeric($element['#year_start'])) {
$calendar_class[] = 'webform-calendar-start-' . $element['#year_start'];
}
if ($element['#year_start'] && is_numeric($element['#year_end'])) {
$calendar_class[] = 'webform-calendar-end-' . $element['#year_end'];
}
$calendar_class[] ='webform-calendar-day-' . variable_get('date_first_day', 0);
$calendar = theme('webform_calendar', array('component' => $element['#webform_component'], 'calendar_classes' => $calendar_class));
}
$output = '';
$output .= '