You are here

function date_widget in Date 5

Same name and namespace in other branches
  1. 5.2 date/date.module \date_widget()
  2. 6.2 date/date.module \date_widget()
  3. 6 date/date.module \date_widget()

Implementation of hook_widget().

File

./date.module, line 144
Defines a date/time field type.

Code

function date_widget($op, &$node, $field, &$items, $delta = NULL) {
  include_once drupal_get_path('module', 'date_api') . '/date.inc';
  if ($field['todate']) {
    $process = array(
      'value',
      'value2',
    );
  }
  else {
    $process = array(
      'value',
    );
  }
  $max = $field['multiple'] ? 2 + sizeof($items) : 0;
  switch ($op) {
    case 'default value':
      return date_default_value($node, $field, $items, $delta);
    case 'form':
      $form = array();
      $form[$field['field_name']]['#tree'] = TRUE;
      $form[$field['field_name']]['#theme'] = 'date_form_fieldgroup';

      // Multiple value and from/to fields need a field title at the top of the form.
      if ($field['multiple'] || $field['fromto']) {
        $form[$field['field_name']]['#title'] = t($field['widget']['label']);
      }
      $tz_handling = $field['tz_handling'] ? $field['tz_handling'] : 'site';
      $function = 'date_widget_' . $field['widget']['type'];
      foreach (range(0, $max) as $delta) {
        $granularity = date_granularity_array($field);
        if ($tz_handling == 'date') {
          array_push($granularity, 'T');
        }
        $timezone = date_get_timezone($tz_handling, $items[$delta]['timezone']);
        $params = array(
          'label' => t($field['widget']['label']),
          'value' => $items[$delta]['value'],
          'weight' => $field['widget']['weight'],
          'delta' => $delta,
          'granularity' => $granularity,
          'format' => $field['type'],
          'timezone_out' => $items[$delta]['value'] ? $timezone : '',
          'timezone_in' => $items[$delta]['value'] ? 'GMT' : '',
          'description' => t($field['widget']['description']),
          'text_parts' => (array) $field['widget']['text_parts'],
          'year_range' => $field['widget']['year_range'],
        );
        $params['required'] = $field['required'] && $delta == 0 ? 1 : 0;
        $params['formats'] = date_get_formats($field);
        if ($field['todate'] != 'required' && $items[$delta]['value2'] == $items[$delta]['value']) {
          unset($items[$delta]['value2']);
        }
        switch ($field['widget']['type']) {
          case 'date_select':
            if ($delta > 0) {
              $params['opt_fields'] = array(
                'year',
                'month',
                'day',
              );
            }
            if ($delta > 0 || !$params['required'] && $params['value'] == '') {
              $params['blank_default'] = 1;
            }
            $params['increment'] = $field['widget']['increment'];

            // use the api date selector form from date.inc to create the date selector form
            if (!$field['todate']) {
              $form[$field['field_name']][$delta]['value'] = date_select_input($params);
              $form[$field['field_name']][$delta]['#empty'] = empty($items[$delta]['value']['year']) ? TRUE : FALSE;
            }
            else {
              $params['label'] = t('From date');
              $form[$field['field_name']][$delta]['value'] = date_select_input($params);
              $params['label'] = t('To date');
              $params['value'] = $items[$delta]['value2'];
              $params['required'] = date_is_required($field, 'value2', $delta, $items[$delta]['value'], $items[$delta]['value2']);
              if (!$params['required'] && empty($params['value'])) {
                $params['blank_default'] = 1;
              }
              $form[$field['field_name']][$delta]['value2'] = date_select_input($params);
              $form[$field['field_name']][$delta]['value2']['#weight'] += 0.1;
              $form[$field['field_name']][$delta]['#empty'] = empty($items[$delta]['value']['year']) && empty($items[$delta]['value2']['year']) ? TRUE : FALSE;
            }
            break;
          default:

            // use the api text input form from date.inc
            if ($delta > 0) {
              $params['blank_default'] = 1;
            }
            $params['jscalendar'] = $field['widget']['type'] == 'date_js' ? 1 : 0;
            if ($delta > 0 || !$params['required'] && $params['value'] == '') {
              $params['blank_default'] = 1;
            }
            if (!$field['todate']) {
              $form[$field['field_name']][$delta] = date_text_input($params);
              $form[$field['field_name']][$delta]['#empty'] = empty($items[$delta]['value']) ? TRUE : FALSE;
            }
            else {
              $params['label'] = t('From date');
              $params['field_name'] = 'value';
              $form[$field['field_name']][$delta] = date_text_input($params);
              $params['label'] = t('To date');
              $params['field_name'] = 'value2';
              $params['value'] = $items[$delta]['value2'];
              $params['required'] = date_is_required($field, 'value2', $delta, $items[$delta]['value'], $items[$delta]['value2']);
              if (!$params['required'] && empty($delta['value2'])) {
                $params['blank_default'] = 1;
              }
              $form[$field['field_name']][$delta] += date_text_input($params);
              $form[$field['field_name']][$delta]['value2']['#weight'] += 0.1;
              $form[$field['field_name']][$delta]['#empty'] = empty($items[$delta]['value']) && empty($items[$delta]['value2']) ? TRUE : FALSE;
            }
        }

        // Group from/to items together.
        if ($field['todate']) {
          $form[$field['field_name']][$delta]['#theme'] = 'date_form_combination';
        }

        // Add other info to the form that the themes will need.
        $form[$field['field_name']][$delta]['#title'] = t($field['widget']['label']);
        $form[$field['field_name']][$delta]['#delta'] = $delta;
        $params['label'] = t($field['widget']['label']);
        $params['weight'] = $field['widget']['weight'] + 0.2;
        $form[$field['field_name']] += date_timezone_input($params);
      }
      return $form;
    case 'process form values':

      /**
       *  Rebuild $items with converted dates and timezones
       *
       *  input text field dates will hold an array like:
       *  [0] => Array (
       *    [value] => 2006-04-06T02:00:00
       *  [timezone] => US/Central
       *  input date selector dates will hold an array like:
       *  [0] => Array (
       *    [value] => Array (
       *      [month] => 4
       *      [day] => 05
       *      [year] => 2006
       *      [hour] => 1
       *      [minute] => 12
       *   [timezone] => US/Central
       */
      $formats = date_get_formats($field);
      $add = array();

      // Don't save empty fields.
      if ($field['multiple']) {
        foreach ($items as $delta => $item) {
          switch ($field['widget']['type']) {
            case 'date_select':
              if (empty($item['value']['year']) && ($delta > 0 || !$field['required'])) {
                unset($items[$delta]);
              }
              break;
            default:
              if (empty($item['value']) && ($delta > 0 || !$field['required'])) {
                unset($items[$delta]);
              }
              break;
          }
        }
      }
      $timezone = date_get_timezone($field['tz_handling'], $items['timezone']);
      unset($items['timezone']);
      foreach ($items as $delta => $item) {
        if (!empty($item)) {
          foreach ($process as $processed) {

            // Handle empty to date values by substituting in the from date.
            // Otherwise, replace $items values with the converted date, timezone, and offset values
            switch ($field['widget']['type']) {
              case 'date_select':
                if ($processed == 'value2' && empty($item['value2']['year'])) {
                  $add[$delta]['value2'] = $add[$delta]['value'];
                }
                else {
                  $date = date_selector_make_dbdate($item[$processed], $field['type'], $timezone, date_granularity_array($field));
                  $add[$delta][$processed] = date_show_value($date, 'db', $field['type']);
                }
                break;
              default:
                if ($processed == 'value2' && empty($item['value2'])) {
                  $add[$delta]['value2'] = $add[$delta]['value'];
                }
                else {
                  if ($field['widget']['type'] == 'date_js') {
                    $function = 'date_jscalendar_make_dbdate';
                  }
                  else {
                    $function = 'date_text_make_dbdate';
                  }
                  $date = $function(trim($item[$processed]), $field['type'], $formats['input']['text'], $timezone, date_granularity_array($field));
                  $add[$delta][$processed] = date_show_value($date, 'db', $field['type']);
                }
                break;
            }

            // Replace $item values with calculated values from $date object.
            // This will also eliminate timezone and offset for dates too old
            // for accurate timezone adjustments.
            if ($field['tz_handling'] == 'date') {
              $add[$delta]['timezone'] = $add[$delta]['value'] || $field['required'] ? $date->local->timezone : NULL;
              $add[$delta]['offset'] = $add[$delta]['value'] || $field['required'] ? $date->local->offset : NULL;
            }
          }
        }
      }
      if ($add) {
        $items = $add;
      }
      return $items;
  }
}