You are here

function date_select_input in Date 5

Flexible Date/Time Drop-Down Selector

$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 am/pm = 0 to display 24 hour format, 1 to display 12 hour format with am/pm selector, default is 0 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 increment = increment minutes and seconds by increment amount, default is 1 opt_fields = an array of fields that need not be filled out, default is empty array blank_default = 1 to show an empty date field with blank values, 0 to fill with current date, default is 0 required = 1 if the field must contain a valid date, default is 1 description = text to be used as a description for the fieldset

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

File

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

Code

function date_select_input($params) {
  drupal_add_css('./' . drupal_get_path('module', 'date_api') . '/date.css');

  // 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',
  );
  $increment = isset($params['increment']) ? $params['increment'] : 1;
  $required = isset($params['required']) ? $params['required'] : 1;
  $format = isset($params['format']) ? $params['format'] : DATE_ISO;
  $formats = $params['formats'];
  $weight = isset($params['weight']) ? $params['weight'] : 0;
  $opt_fields = is_array($params['opt_fields']) ? $params['opt_fields'] : array();
  $blank_default = isset($params['blank_default']) ? $params['blank_default'] : 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'];
  $select_month = !in_array('mon', $params['text_parts']);
  $select_day = !in_array('mday', $params['text_parts']);
  $select_year = !in_array('year', $params['text_parts']);
  $year_range = explode(':', $params['year_range']);
  $years_back = abs($year_range[0]);
  $years_forward = abs($year_range[1]);
  if ($formats['input']['am_pm']) {
    $hours_format = 'g';
    for ($i = 0; $i <= 12; $i++) {
      $hours_array[$i] = $i < 10 ? "0{$i}" : $i;
    }
  }
  else {
    $hours_format = 'G';
    for ($i = 0; $i <= 23; $i++) {
      $hours_array[$i] = $i < 10 ? "0{$i}" : $i;
    }
  }

  // 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');
  }

  // find current date
  switch ($format) {
    case DATE_UNIX:
      $now = date_gmadj_zone(time());
      break;
    default:
      $now = date_unix2iso(date_gmadj_zone(time()));
  }
  if (!$blank_default && $params['value'] == '') {
    date_set_date($date, $now, $timezone_out, 'local', $format);
  }
  if (!$blank_default || $params['value'] > '') {
    $year = date_show_date($date, 'Y');
    $mon = date_show_date($date, 'n');
    $mday = date_show_date($date, 'j');
    $hours = date_show_date($date, $hours_format);
    $minutes = intval(date_show_date($date, 'i'));
    $seconds = intval(date_show_date($date, 's'));
    $ampm = strtolower(date_show_date($date, 'a'));
  }

  // create the form values
  $form['#title'] = $label;
  $form['#weight'] = intval($weight);
  $form['#theme'] = 'date_form_select';
  $form['#attributes'] = array(
    'class' => 'container-inline-date',
  );
  if (in_array('D', $granularity)) {
    $form['mday'] = array(
      '#default_value' => $mday,
      '#title' => t('day'),
      '#required' => $required && !in_array('mday', $opt_fields) ? $required : 0,
      '#weight' => $formats['input']['select']['D'],
    );
    if ($select_day) {
      $days_array = drupal_map_assoc(range(1, 31));
      if (!$required || in_array('mday', $opt_fields) || $blank_default) {
        array_unshift($days_array, '');
      }
      $form['mday']['#type'] = 'select';
      $form['mday']['#options'] = $days_array;
    }
    else {
      $form['mday']['#type'] = 'textfield';
      $form['mday']['#maxlength'] = 2;
      $form['mday']['#size'] = 2;
    }
  }
  if (in_array('M', $granularity)) {
    $form['mon'] = array(
      '#default_value' => $mon,
      '#title' => t('month'),
      '#required' => $required && !in_array('mon', $opt_fields) ? $required : 0,
      '#weight' => $formats['input']['select']['M'],
    );
    if ($select_month) {
      $months_array = drupal_map_assoc(range(1, 12), 'map_month');
      if (!$required || in_array('mon', $opt_fields) || $blank_default) {
        array_unshift($months_array, '');
      }
      $form['mon']['#type'] = 'select';
      $form['mon']['#options'] = $months_array;
    }
    else {
      $form['mon']['#type'] = 'textfield';
      $form['mon']['#maxlength'] = 2;
      $form['mon']['#size'] = 2;
    }
  }
  if (in_array('Y', $granularity)) {
    $form['year'] = array(
      '#default_value' => $year,
      '#title' => t('year'),
      '#required' => $required && !in_array('year', $opt_fields) ? $required : 0,
      '#weight' => $formats['input']['select']['Y'],
    );
    if ($select_year) {
      $year = $year > 0 ? $year : date_gmdate('Y', date_gmadj_zone(time()));
      $years_array = drupal_map_assoc(range($year - $years_back, $year + $years_forward));

      // array_unshift converts the assoc array to a numeric one, can't use it here
      if (!$required || in_array('year', $opt_fields) || $blank_default) {
        $years_array = array(
          0 => '',
        ) + $years_array;
      }
      $form['year']['#type'] = 'select';
      $form['year']['#options'] = $years_array;
    }
    else {
      $form['year']['#type'] = 'textfield';
      $form['year']['#maxlength'] = 4;
      $form['year']['#size'] = 4;
    }
  }
  if (in_array('H', $granularity)) {
    $form['hours'] = array(
      '#type' => 'select',
      '#default_value' => $hours,
      '#options' => $hours_array,
      '#required' => $required && !in_array('hours', $opt_fields) ? $required : 0,
      '#title' => t('hour'),
      '#weight' => 4,
    );
  }
  if (in_array('N', $granularity)) {
    for ($i = 0; $i <= 59; $i += $increment) {
      $minutes_array[$i] = $i < 10 ? "0{$i}" : $i;
    }
    $form['minutes'] = array(
      '#type' => 'select',
      '#default_value' => $minutes,
      '#options' => $minutes_array,
      '#required' => $required && !in_array('minutes', $opt_fields) ? $required : 0,
      '#title' => t('minute'),
      '#weight' => 5,
    );
  }
  if (in_array('S', $granularity)) {
    for ($i = 0; $i <= 59; $i += $increment) {
      $seconds_array[$i] = $i < 10 ? "0{$i}" : $i;
    }
    $form['seconds'] = array(
      '#type' => 'select',
      '#default_value' => $seconds,
      '#options' => $seconds_array,
      '#required' => $required && !in_array('seconds', $opt_fields) ? $required : 0,
      '#title' => t('second'),
      '#weight' => 6,
    );
  }
  if ($formats['input']['am_pm']) {
    $options = array(
      'am' => t('am'),
      'pm' => t('pm'),
    );
    if (!$required || in_array('hours', $opt_fields) || $blank_default) {
      array_unshift($options, '');
    }
    $form['ampm'] = array(
      '#type' => 'select',
      '#default_value' => $ampm,
      '#options' => $options,
      '#required' => $required && !in_array('hours', $opt_fields) ? $required : 0,
      '#title' => t('am/pm'),
      '#weight' => 8,
    );
  }
  $form[] = array(
    '#theme' => 'date_form_select_description',
    '#weight' => 11,
    'description' => array(
      '#value' => $description,
    ),
  );
  return $form;
}