TRUE, '#tree' => TRUE, '#date' => NULL, '#date_timezone' => date_default_timezone_name(), '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'), '#date_text_parts' => array(), '#date_increment' => 1, '#date_year_range' => '-3:+3', '#date_label_position' => 'above', ); $type['date_select'] = array_merge($date_base, array( '#process' => array('date_select_process'), '#element_validate' => array('date_select_validate'), )); $type['date_text'] = array_merge($date_base, array( '#process' => array('date_text_process' => array()), '#element_validate' => array('date_text_validate'), )); $type['date_timezone'] = array( '#input' => TRUE, '#tree' => TRUE, '#process' => array('date_timezone_process'), '#element_validate' => array('date_timezone_validate'), ); return $type; } /** * Create a timezone form element. * * @param array $element * @return array * the timezone form element */ function date_timezone_process($element, $edit, $form_state, $form) { $element['timezone'] = array( '#type' => 'select', '#title' => theme('date_part_label_timezone', 'select', $element), '#default_value' => $element['#value'], '#options' => date_timezone_names($element['#required']), '#weight' => $element['#weight'], '#required' => $element['#required'], '#attributes' => array('class' => 'date-timezone'), '#theme' => 'date_select_element', ); return $element; } /** * Text date input form. * * Display all or part of a date in a single textfield. * * The exact parts displayed in the field are those in #date_granularity. * The display of each part comes from #date_format. */ function date_text_process($element, $edit, $form_state, $form) { // There are some cases, like when using this as a Views form element, // where $edit is empty and $element['#value'] holds an array of input values. // This happens when the processing bypasses the element validation step // that resets the value from the date and time // subparts. if (!empty($edit) || is_array($element['#value'])) { if (empty($edit)) { $edit = $element['#value']; } $datetime = date_convert_from_custom($edit['date'], $element['#date_format']); $date = date_make_date($datetime, $element['#date_timezone'], DATE_DATETIME); } elseif (!empty($element['#value'])) { $date = date_make_date($element['#value'], $element['#date_timezone']); } elseif ($element['#required']) { $date = date_now($element['#date_timezone']); } // Don't overwrite values already added to $element['date'] in case // using something like jscalendar that needs to set custom values. $element['#tree'] = TRUE; $element['date']['#type'] = 'textfield'; $element['date']['#weight'] = !empty($element['date']['#weight']) ? $element['date']['#weight'] : $element['#weight']; $element['date']['#default_value'] = is_object($date) ? date_format($date , $element['#date_format']) : ''; $element['date']['#attributes']['class'] = $element['date']['#attributes']['class'] .' date-date'; $element['date']['#required'] = $element['#required']; $element['date']['#theme'] = 'date_textfield_element'; return $element; } /** * Flexible date/time drop-down selector. * * Splits date into a collection of date and time sub-elements, one * for each date part. Each sub-element can be either a textfield or a * select, based on the value of ['#date_settings']['text_fields']. * * The exact parts displayed in the field are those in #date_granularity. * The display of each part comes from ['#date_settings']['format']. */ function date_select_process($element, $edit, $form_state, $form) { // There are some cases, like when using this as a Views form element, // where $edit is empty and $element['#value'] holds an array of input values. // This happens when the processing bypasses the element validation step // that resets the value from the date and time // subparts. $date = NULL; if (!empty($edit) || is_array($element['#value'])) { if (empty($edit)) { $edit = $element['#value']; } $date = date_make_date($edit, $element['#date_timezone'], DATE_ARRAY); } elseif (!empty($element['#value'])) { $date = date_make_date($element['#value'], $element['#date_timezone']); } // TODO keep an eye on this, commented out so it is possible to provide // blank initial value for required date. //elseif ($element['#required']) { // $date = date_now($element['#date_timezone']); //} $element['#tree'] = TRUE; date_increment_round($date, $element['#date_increment']); $element += (array) date_parts_element($element, $date, $element['#date_format']); // Store a hidden value for all date parts not in the current display. $granularity = date_format_order($element['#date_format']); foreach (date_nongranularity($granularity) as $field) { $formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's'); $element[$field] = array( '#type' => 'hidden', '#value' => is_object($date) ? intval(date_format($date, $formats[$field])) : 0); } return $element; } /** * Create form elements for one or more date parts. * * Get the order of date elements from the provided format. * If the format order omits any date parts in the granularity, alter the * granularity array to match the format, then flip the $order array * to get the position for each element. Then iterate through the * elements and create a sub-form for each part. * * @param array $element * @param object $date * @param array $granularity * @param string $format * @return array * the form array for the submitted date parts */ function date_parts_element($element, $date, $format) { $sub_element = array(); $granularity = date_format_order($format); $order = array_flip($granularity); $hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g': 'G'; $month_function = strpos($element['#date_format'], 'F') ? 'date_month_names' : 'date_month_names_abbr'; $count = 0; $increment = min(intval($element['#date_increment']), 1); foreach ($granularity as $field) { // Allow empty value as option if date is not required // or if empty value was provided as a starting point. $part_required = ($element['#required'] && is_object($date)) ? TRUE : FALSE; $part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select'; $sub_element[$field] = array( '#weight' => $order[$field], '#required' => $element['#required'], '#attributes' => array('class' => (isset($element['#attributes']['class']) ? $element['#attributes']['class'] : '') .' date-'. $field), ); switch ($field) { case 'year': // Center the range around the current year, but expand it far // enough so it will pick up the year value in the field in case // the value in the field is outside the initial range. $this_year = date_format(date_now(), 'Y'); $value_year = is_object($date) ? date_format($date, 'Y') : ''; $range = explode(':', $element['#date_year_range']); $min_year = $this_year + $range[0]; $max_year = $this_year + $range[1]; if (!empty($value_year)) { $min_year = min($value_year, $min_year); $max_year = max($value_year, $max_year); } $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'Y') : ''; $sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required)); break; case 'month': $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'n') : ''; $sub_element[$field]['#options'] = $month_function($part_required); break; case 'day': $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'j') : ''; $sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required)); break; case 'hour': $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, $hours_format) : ''; $sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required)); $sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element); break; case 'minute': $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 'i') : ''; $sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment'])); $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element); break; case 'second': $sub_element[$field]['#default_value'] = is_object($date) ? date_format($date, 's') : ''; $sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment'])); $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element); break; } // Add handling for the date part label. $label = theme('date_part_label_'. $field, $part_type, $element); if (in_array($field, $element['#date_text_parts'])) { $sub_element[$field]['#type'] = 'textfield'; $sub_element[$field]['#theme'] = 'date_textfield_element'; $sub_element[$field]['#size'] = 7; if ($element['#date_label_position'] == 'within') { $sub_element[$field]['#options'] = array( '-'. $label => '-'. $label) + $sub_element[$field]['#options']; if (empty($sub_element[$field]['#default_value'])) { $sub_element[$field]['#default_value'] = '-'. $label; } } elseif ($element['#date_label_position'] != 'none') { $sub_element[$field]['#title'] = $label; } } else { $sub_element[$field]['#type'] = 'select'; $sub_element[$field]['#theme'] = 'date_select_element'; if ($element['#date_label_position'] == 'within') { $sub_element[$field]['#options'] = array( '' => '-'. $label) + $sub_element[$field]['#options']; } elseif ($element['#date_label_position'] != 'none') { $sub_element[$field]['#title'] = $label; } } } if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) { $sub_element['ampm'] = array( '#type' => 'select', '#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '', '#options' => drupal_map_assoc(date_ampm()), '#weight' => 8, '#attributes' => array('class' => 'date-ampm'), ); if ($element['#date_label_position'] == 'within') { $sub_element['ampm']['#options'] = array('' => '-'. theme('date_part_label_ampm', 'ampm', $element)) + $sub_element['ampm']['#options']; } elseif ($element['#date_label_position'] != 'none') { $sub_element['ampm']['#title'] = theme('date_part_label_ampm', 'ampm', $element); } } return $sub_element; } /** * Helper function to round minutes and seconds to requested value. */ function date_increment_round(&$date, $increment) { // Round minutes and seconds, if necessary. if (is_object($date) && $increment > 1) { $day = intval(date_format($date, 'j')); $hour = intval(date_format($date, 'H')); $second = intval(round(intval(date_format($date, 's')) / $increment) * $increment); $minute = intval(date_format($date, 'i')); if ($second == 60) { $minute += 1; $second = 0; } $minute = intval(round($minute / $increment) * $increment); if ($minute == 60) { $hour += 1; $minute = 0; } date_time_set($date, $hour, $minute, $second); if ($hour == 24) { $day += 1; $hour = 0; $year = date_format($date, 'Y'); $month = date_format($date, 'n'); date_date_set($date, $year, $month, $day); } } return $date; } /** * Validation function for date selector. */ function date_select_validate($element, &$form_state) { if (empty($element['#value'])) { $element['#value'] = array(); } $error_field = implode('][', $element['#parents']); $granularity = array_values(date_format_order($element['#date_format'])); // Strip field labels out of the results. foreach ($element['#value'] as $field => $value) { if (substr($value, 0, 1) == '-') { $element['#value'][$field] = ''; } } if (isset($element['#value']['ampm'])) { if ($element['#value']['ampm'] == 'pm' && $element['#value']['hour'] < 12) { $element['#value']['hour'] += 12; } elseif ($element['#value']['ampm'] == 'am' && $element['#value']['hour'] == 12) { $element['#value']['hour'] -= 12; } } if ($element['#required'] || !empty($form_values['year'])) { if ($element['#value']['year'] < variable_get('date_min_year', 1) || $element['#value']['year'] > variable_get('date_max_year', 4000)) { form_set_error($error_field .'][year', t('year must be a number between %min and %max.', array( '%min' => variable_get('date_min_year', 1), '%max' => variable_get('date_max_year', 4000)))); } } if (in_array('month', $granularity) && ($element['#required'] || !empty($element['#value']['month']))) { if ($element['#value']['month'] < 1 || $element['#value']['month'] > 12) { form_set_error($error_field .'][month', t('month must be a number between 1 and 12.')); } } if (in_array('day', $granularity) && ($element['#required'] || !empty($element['#value']['day']))) { if ($element['#value']['day'] < 1 || $element['#value']['day'] > 31) { form_set_error($error_field .'][day', t('day must be a number between 1 and 31.')); } } if (!form_get_errors()) { // If it creates a valid date, set it. $value = date_select_input_value($element); if (!empty($value)) { form_set_value($element, $value, $form_state); } elseif ($element['#required']) { form_set_error($error_field, t('A valid date cannot be constructed from %m-%d-%y.', array( '%m' => $element['#value']['month'], '%d' => $element['#value']['day'], '%y' => $element['#value']['year'], ))); } else { form_set_value($element, NULL, $form_state); } } } /** * Helper function for extracting a date value out of user input. */ function date_select_input_value($element) { if (date_is_valid($element['#value'], DATE_ARRAY)) { return date_convert($element['#value'], DATE_ARRAY, DATE_DATETIME); } return NULL; } /** * Validation for text input. */ function date_text_validate($element, &$form_state) { $form_values = $element['#value']; $value = date_text_input_value($element); if (!$element['#required'] && empty($value)) { form_set_value($element, NULL, $form_state); } elseif (!empty($value)) { form_set_value($element, $value, $form_state); } else { $error_field = implode('][', $element['#parents']); form_set_error($error_field, t('A valid date cannot be created from %date', array('%date' => $value))); } } /** * Helper function for extracting a date value out of user input. */ function date_text_input_value($element) { $form_values = $element['#value']; $input = $form_values['date']; if (!$element['#required'] && trim($input) == '') return NULL; $value = date_convert_from_custom($input, $element['#date_format']); // If it creates a valid date, use it. if (date_is_valid($value)) { return $value; } return NULL; } /** * Validation for timezone input * * Move the timezone value from the nested field back to the original field. */ function date_timezone_validate($element, &$form_state) { form_set_value($element, $element['#value']['timezone'], $form_state); } /** * Convert a date input in a custom format to a standard date type * * Handles conversion of translated month names (i.e. turns t('Mar') or * t('March') into 3). Also properly handles dates input in European style * short formats, like DD/MM/YYYY. Works by parsing the format string * to create a regex that can be used on the input value. * * The original code to do this was created by Yves Chedemois (yched). * * @param string $date * a date value * @param string $format * a date format string that describes the format of the input value * @return mixed * input value converted to a DATE_DATETIME value */ function date_convert_from_custom($date, $format) { $array = date_format_patterns(); foreach ($array as $key => $value) { $patterns[] = "`(^|[^\\\\\\\\])". $key ."`"; // the letter with no preceding '\' $repl1[] = '${1}(.)'; // a single character $repl2[] = '${1}('. $value .')'; // the } $patterns[] = "`\\\\\\\\([". implode(array_keys($array)) ."])`"; $repl1[] = '${1}'; $repl2[] = '${1}'; $format_regexp = preg_quote($format); // extract letters $regex1 = preg_replace($patterns, $repl1, $format_regexp, 1); $regex1 = str_replace('A', '(.)', $regex1); $regex1 = str_replace('a', '(.)', $regex1); preg_match('`^'. $regex1 .'$`', stripslashes($format), $letters); array_shift($letters); // extract values $regex2 = preg_replace($patterns, $repl2, $format_regexp, 1); $regex2 = str_replace('A', '(AM|PM)', $regex2); $regex2 = str_replace('a', '(am|pm)', $regex2); preg_match('`^'. $regex2 .'$`', $date, $values); array_shift($values); // if we did not find all the values for the patterns in the format, abort if (count($letters) != count($values)) { return NULL; } $final_date = array('hour' => 0, 'minute' => 0, 'second' => 0, 'month' => 0, 'day' => 0, 'year' => 0); foreach ($letters as $i => $letter) { $value = $values[$i]; switch ($letter) { case 'd': case 'j': $final_date['day'] = intval($value); break; case 'n': case 'm': $final_date['month'] = intval($value); break; case 'F': $array_month_long = array_flip(date_month_names()); $final_date['month'] = $array_month_long[$value]; break; case 'M': $array_month = array_flip(date_month_names_abbr()); $final_date['month'] = $array_month[$value]; break; case 'Y': case 'y': $year = $value; // if no century, we add the current one ("06" => "2006") $final_date['year'] = str_pad($year, 4, substr(date("Y"), 0, 2), STR_PAD_LEFT); break; case 'a': case 'A': $ampm = strtolower($value); break; case 'g': case 'h': case 'G': case 'H': $final_date['hour'] = intval($value); break; case 'i': $final_date['minute'] = intval($value); break; case 's': $final_date['second'] = intval($value); break; case 'U': return date_convert($value, DATE_UNIX, DATE_DATETIME); break; } } if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) { $final_date['hour'] += 12; } elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) { $final_date['hour'] -= 12; } if (date_is_valid($final_date, DATE_ARRAY)) { return date_convert($final_date, DATE_ARRAY, DATE_DATETIME); } else { return NULL; } } /** * Format a date timezone element. * * @param $element * An associative array containing the properties of the element. * Properties used: title, value, options, description, required and attributes. * @return * A themed HTML string representing the date selection boxes. */ function theme_date_timezone($element) { return theme('form_element', $element, $element['#children']); } /** * Format a date selection element. * * @param $element * An associative array containing the properties of the element. * Properties used: title, value, options, description, required and attributes. * @return * A themed HTML string representing the date selection boxes. */ function theme_date_select($element) { $output = ''; if (isset($element['#children'])) { $output = $element['#children']; } return '
'. theme('form_element', $element, $output) .'
'; } /** * Format a date text element. * * @param $element * An associative array containing the properties of the element. * Properties used: title, value, options, description, required and attributes. * @return * A themed HTML string representing the date selection boxes. */ function theme_date_text($element) { return theme('form_element', $element, $element['#children']); } /** * Themes for date input form elements */ function theme_date_select_element($element) { $part = array_pop($element['#parents']); return '
'. theme('select', $element) .'
'; } function theme_date_textfield_element($element) { $part = array_pop($element['#parents']); return '
'. theme('textfield', $element) .'
'; } /** * Functions to separate date parts in form. * * Separators float up to the title level for elements with titles, * so won't work if this element has titles above the element date parts. */ function theme_date_part_hour_prefix($element) { if ($element['#date_label_position'] != 'above') { return ' - '; } } function theme_date_part_minsec_prefix($element) { if ($element['#date_label_position'] != 'above') { return ':'; } } /** * Format labels for each date part in a date_select. * * @param $part_type * the type of field used for this part, 'textfield' or 'select' * @param $element * An associative array containing the properties of the element. * Properties used: title, value, options, description, required and attributes. */ function theme_date_part_label_year($part_type, $element) { return t('Year'); } function theme_date_part_label_month($part_type, $element) { return t('Month'); } function theme_date_part_label_day($part_type, $element) { return t('Day'); } function theme_date_part_label_hour($part_type, $element) { return t('Hour'); } function theme_date_part_label_minute($part_type, $element) { return t('Minute'); } function theme_date_part_label_second($part_type, $element) { return t('Second'); } function theme_date_part_label_ampm($part_type, $element) { return t('am/pm'); } function theme_date_part_label_timezone($part_type, $element) { return t('Timezone'); }