You are here

function date_popup_process_date in Date 5.2

Same name and namespace in other branches
  1. 6.2 date_popup/date_popup.module \date_popup_process_date()
  2. 6 date_popup/date_popup.module \date_popup_process_date()
  3. 7 date_popup/date_popup.module \date_popup_process_date()

Process the date portion of the element.

1 call to date_popup_process_date()
date_popup_process in date_popup/date_popup.module
Javascript popup element processing. Add popup attributes to $element.

File

date_popup/date_popup.module, line 199
A module to enable jquery calendar and time entry popups. Requires the Date API.

Code

function date_popup_process_date(&$element, $edit = NULL, $date = NULL) {
  $granularity = $element['#granularity'];
  $date_granularity = array_intersect($granularity, array(
    'month',
    'day',
    'year',
  ));
  $time_granularity = array_intersect($granularity, array(
    'hour',
    'minute',
    'second',
  ));
  $date_format = date_limit_format($element['#date_format'], $date_granularity);
  if (empty($date_granularity)) {
    return array();
  }

  // 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.
  // The datepicker can't handle zero or negative values like 0:+1
  // even though the Date API can handle them, so rework the value
  // we pass to the datepicker to use defaults it can accept.
  $this_year = date_format(date_now(), 'Y');
  $value_year = is_object($date) ? date_format($date, 'Y') : $this_year;
  $range = explode(':', $element['#date_year_range']);
  $min_year = min($value_year, $this_year + $range[0]);
  $max_year = max($value_year, $this_year + $range[1]);
  $year_range = sprintf('%+d', $min_year - $this_year) . ':' . sprintf('%+d', $max_year - $this_year);
  $settings = "\n" . "prevText:'<" . t('Prev') . "', \n" . "nextText:'" . t('Next') . ">', \n" . "currentText:'" . t('Today') . "', \n" . "clearText:'" . t('Clear') . "', \n" . "closeText:'" . t('Close') . "', \n" . "firstDay:" . variable_get('date_first_day', 0) . ", \n" . "dayNames: new Array('" . implode("','", date_week_days_abbr(TRUE, TRUE, 1)) . "'), \n" . "monthNames:new Array('" . implode("','", date_month_names(TRUE)) . "'), \n" . "autoPopUp: 'focus', \n" . "closeAtTop:false, \n" . "speed: 'immediate', \n" . "dateFormat:'" . date_popup_format_to_popup($date_format) . "', \n" . "yearRange:'" . $year_range . "'\n" . "\n";

  // This is just a placeholder to indicate the method to constrain from and to dates.
  // Not yet implemented.
  if ($fromto) {
    $settings . +", minDate: (input.id == 'dTo' ? getDate(\$('#dFrom').val()) : null), \n" . "maxDate: (input.id == 'dFrom' ? getDate(\$('#dTo').val()) : null) ";
  }

  // Create a unique class for each element so we can use custom settings.
  $class = date_popup_js_settings_class('jquery-calendar', 'calendar', $settings);
  $sub_element = array(
    '#type' => 'textfield',
    '#default_value' => (!empty($element['#value']['date']) || !empty($edit['date'])) && is_object($date) ? date_format($date, $date_format) : '',
    '#attributes' => array(
      'class' => 'date_popup ' . $class,
    ),
    '#size' => !empty($element['#size']) ? $element['#size'] : 20,
    '#maxlength' => !empty($element['#maxlength']) ? $element['#maxlength'] : 30,
  );

  // TODO, figure out exactly when we want this description. In many places it is not desired.
  $element['#description'] .= t(' Format: @date', array(
    '@date' => date($date_format, time()),
  ));
  return $sub_element;
}