You are here

function date_popup_process_time_part in Date 7.3

Same name and namespace in other branches
  1. 7.2 date_popup/date_popup.module \date_popup_process_time_part()

Process the time portion of the element.

1 call to date_popup_process_time_part()
date_popup_element_process in date_popup/date_popup.module
Javascript popup element processing.

File

date_popup/date_popup.module, line 460
A module to enable jQuery calendar and time entry popups.

Code

function date_popup_process_time_part(&$element) {
  $granularity = date_format_order($element['#date_format']);
  $has_time = date_has_time($granularity);
  if (empty($has_time)) {
    return array();
  }

  // When used as a Views exposed filter widget, $element['#value'] contains an
  // array instead an string. Fill the 'time' string in this case.
  $mock = NULL;
  $callback_values = date_popup_element_value_callback($element, FALSE, $mock);
  if (!isset($element['#value']['time']) && isset($callback_values['time'])) {
    $element['#value']['time'] = $callback_values['time'];
  }
  switch ($element['#timepicker']) {
    case 'default':
      $func = 'timeEntry';
      $settings = array(
        'show24Hours' => strpos($element['#date_format'], 'H') !== FALSE ? TRUE : FALSE,
        'showSeconds' => in_array('second', $granularity) ? TRUE : FALSE,
        'timeSteps' => array(
          1,
          intval($element['#date_increment']),
          in_array('second', $granularity) ? $element['#date_increment'] : 0,
        ),
        'spinnerImage' => '',
        'fromTo' => isset($fromto),
      );
      if (strpos($element['#date_format'], 'a') !== FALSE) {

        // Then we are using lowercase am/pm.
        $options = date_ampm_options(FALSE, FALSE);
        $settings['ampmNames'] = array(
          $options['am'],
          $options['pm'],
        );
      }
      if (strpos($element['#date_format'], 'A') !== FALSE) {

        // Then we are using uppercase am/pm.
        $options = date_ampm_options(FALSE, TRUE);
        $settings['ampmNames'] = array(
          $options['am'],
          $options['pm'],
        );
        $settings['ampmPrefix'] = ' ';
      }
      break;
    case 'wvega':
      $func = 'timepicker';
      $grans = array(
        'hour',
        'minute',
        'second',
      );
      $time_granularity = array_intersect($granularity, $grans);
      $format = date_popup_format_to_popup_time(date_limit_format($element['#date_format'], $time_granularity), 'wvega');
      $default_value = isset($element['#default_value']) ? $element['#default_value'] : '';

      // The first value in the dropdown list should be the same as the element
      // default_value, but it needs to be in JS format (i.e. milliseconds since
      // the epoch).
      $start_time = new DateObject($default_value, $element['#date_timezone'], DATE_FORMAT_DATETIME);
      date_increment_round($start_time, $element['#date_increment']);
      $start_time = $start_time
        ->format(DATE_FORMAT_UNIX) * 1000;
      $settings = array(
        'timeFormat' => $format,
        'interval' => $element['#date_increment'],
        'startTime' => $start_time,
        'scrollbar' => TRUE,
      );
      break;
    default:
      $func = '';
      $settings = array();
  }

  // Create a unique id for each set of custom settings.
  $id = date_popup_js_settings_id($element['#id'], $func, $settings);

  // Manually build this element and set the value - this will prevent
  // corrupting the parent value.
  $parents = array_merge($element['#parents'], array(
    'time',
  ));
  $sub_element = array(
    '#type' => 'textfield',
    '#title' => theme('date_part_label_time', array(
      'part_type' => 'time',
      'element' => $element,
    )),
    '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
    '#default_value' => $element['#value']['time'],
    '#id' => $id,
    '#size' => 15,
    '#maxlength' => 10,
    '#attributes' => $element['#attributes'],
    '#parents' => $parents,
    '#name' => array_shift($parents) . '[' . implode('][', $parents) . ']',
    '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
    '#required' => $element['#required'],
  );

  // Do NOT overwrite the actual input with the default value.
  // @todo Figure out exactly when this is needed, in many places it is not.
  $example_date = date_now();
  date_increment_round($example_date, $element['#date_increment']);
  $sub_element['#description'] = t('E.g., @date', array(
    '@date' => date_format_date($example_date, 'custom', date_popup_time_format($element)),
  ));
  return $sub_element;
}