You are here

function date_text_input in Date 5

Text date input form, with optional jscalendar popup

$params = an array of values, including label = the label for the date group, default is 'Date' value = the date/time to be processed, default is the current date and time timezone_in = the timezone of the date/time value to be processed, default is GMT timezone_out = the timezone to be used when displaying the date, default is date site timezone, if set, or GMT valid timezones are standard timezone ids like US/Central, America/New_York, GMT format = the format of the date value, default is DATE_ISO DATE_UNIX => unix timestamp DATE_ISO => iso 8601 YYYY-MM-DDThh:mm:ss weight = the weight of the date group, default is 0 delta = a delta value for the group to accomodate multiple date fields, default is 0 granularity = an array of date parts to be selected, like array('Y','M','D'), default is M, D, Y Y => year, M => month, D => day, H => hours, N => minutes, S => seconds, T => timezone required = 1 if the field must contain a valid date, default is 1 description = text to be used as a description for the fieldset blank_default = 1 to show an empty date field with blank values, 0 to fill with current date, default is 0 jscalendar = 1 use if available, 0 do not use

1 call to date_text_input()
date_widget in ./date.module
Implementation of hook_widget().

File

./date.inc, line 871
Date/time API functions

Code

function date_text_input($params) {

  // set the variables
  $label = $params['label'] ? $params['label'] : t('Date');
  $delta = isset($params['delta']) ? $params['delta'] : 0;
  $granularity = is_array($params['granularity']) ? $params['granularity'] : array(
    'M',
    'D',
    'Y',
  );
  $required = isset($params['required']) ? $params['required'] : 1;
  $blank_default = isset($params['blank_default']) ? $params['blank_default'] : 0;
  $format = isset($params['format']) ? $params['format'] : DATE_ISO;
  $formats = $params['formats'];
  $weight = isset($params['weight']) ? $params['weight'] : 0;
  $timezone_in = isset($params['timezone_in']) ? $params['timezone_in'] : (!$blank_default || $params['value'] ? 'GMT' : '');
  $timezone_out = isset($params['timezone_out']) ? $params['timezone_out'] : (!$blank_default || $params['value'] ? date_get_site_timezone() : '');
  $description = $params['description'];
  $jscalendar = $params['jscalendar'] ? 1 : 0;
  $opt_fields = is_array($params['opt_fields']) ? $params['opt_fields'] : array();
  $field_name = $params['field_name'] ? $params['field_name'] : 'value';

  // create a new date object and convert it to the right timezone
  // create a date object with the desired date
  $date = date_make_date();
  if ($params['value']) {
    date_set_date($date, $params['value'], $timezone_in, 'db', $format);
    date_convert_timezone($date, $timezone_in, $timezone_out, 'local');
  }

  // if required and no date provided, use current date
  if ($required && !$blank_default && $params['value'] == '') {
    switch ($format) {
      case DATE_UNIX:
        $now = date_gmadj_zone(time());
        break;
      default:
        $now = date_unix2iso(date_gmadj_zone(time()));
    }
    date_set_date($date, $now, $timezone_out, 'local', $format);
  }

  // get the local iso version of the database value
  $value = date_iso2custom(date_show_value($date, 'local'), $formats['input']['text']);

  // date-specific timezone not requested, just get date
  $form[$field_name] = array(
    '#type' => 'textfield',
    '#title' => $label,
    '#default_value' => $value,
    '#required' => $delta == 0 ? $required : 0,
    '#description' => $description,
    '#weight' => $weight,
  );

  // if the jscalendar is used for input, add some attributes to be passed to the js
  // also need to adjust the date format slightly to accomodate the js capability
  if ($jscalendar && module_exists('jscalendar')) {
    $form[$field_name]['#attributes'] = array(
      'class' => 'jscalendar',
    );
    $form[$field_name]['#jscalendar_ifFormat'] = $formats['input']['jscal'];
    $form[$field_name]['#jscalendar_showsTime'] = date_has_time($granularity) ? 'true' : 'false';
    $form[$field_name]['#jscalendar_timeFormat'] = $formats['input']['am_pm'] ? '12' : '24';
    $form['#theme'] = 'date_form_jscalendar';
  }
  else {
    $form['#theme'] = 'date_form_text';
  }
  return $form;
}