<?php
// $Id: date.module,v 1.21 2006/07/18 18:01:04 JonBob Exp $

/**
 * @file
 * Defines a date/time field type.
 */

/**
 * Implementation of hook_help().
 */
function date_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Defines a date/time field type. <em>Note: Requires content.module.</em>');
  }
}

/**
 * Implementation of hook_field_info().
 */
function date_field_info() {
  return array(
    'date' => array('label' => 'Date'),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function date_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array();
      $options = array(
        0 => t('Year'),
        1 => t('Year and month'),
        2 => t('Date'),
        3 => t('Date and time'),
      );
      $form['granularity'] = array(
        '#type' => 'radios',
        '#title' => t('Granularity'),
        '#default_value' => $field['granularity'] ? $field['granularity'] : 0,
        '#options' => $options,
      );
      return $form;

    case 'save':
      return array('granularity');

    case 'database columns':
      return array(
        'value' => array('type' => 'varchar', 'length' => 20, 'not null' => TRUE, 'default' => "'0001-01-01T00:00:00'", 'sortable' => TRUE),
      );
  }
}


/**
 * Implementation of hook_field().
 */
function date_field($op, $node, &$field, &$node_field, $teaser, $page) {
  switch ($op) {
    case 'view':
      foreach ($node_field as $delta => $item) {
        $node_field[$delta]['view'] = date_field_view_item($field, $item);
      }
      return theme('field', $node, $field, $node_field, $teaser, $page);
  }
}

/**
 * Implementation of hook_field_view_item().
 */
function date_field_view_item($field, $node_field_item) {
  return check_plain($node_field_item['value']);
}


/**
 * Implementation of hook_widget_info().
 */
function date_widget_info() {
  return array(
    'date_text' => array(
      'label' => 'Text Field',
      'field types' => array('date'),
    ),
  );
}

/**
 * Implementation of hook_widget_settings().
 */
function date_widget_settings($op, $widget) {
  switch ($op) {
    case 'form':
      $form = array();
      $options = array(
        0 => t('Times are entered and displayed with site\'s time zone'),
        1 => t('Times are entered and displayed with user\'s time zone'),
      );
      $form['time_zone'] = array(
        '#type' => 'radios',
        '#title' => t('Time zone handling'),
        '#default_value' => $widget['time_zone'] ? $widget['time_zone'] : 0,
        '#options' => $options,
      );
      return $form;

    case 'save':
      return array('time_zone');
  }
}

/**
 * Implementation of hook_widget().
 */
function date_widget($op, &$node, $field, &$node_field) {
  switch ($op) {
    case 'form':
      $form = array();

      $form[$field['field_name']] = array('#tree' => TRUE);

      if ($field['multiple']) {
        foreach (range(0, 2) as $delta) {
          $form[$field['field_name']][$delta]['value'] = array(
            '#type' => 'textfield',
            '#title' => t($field['widget']['label']),
            '#default_value' => $node_field[$delta]['value'],
            '#required' => ($delta == 0) ? $field['required'] : FALSE,
          );
        }
      }
      else {
        $form[$field['field_name']][0]['value'] = array(
          '#type' => 'textfield',
          '#title' => t($field['widget']['label']),
          '#default_value' => $node_field[0]['value'],
          '#required' => $field['required'],
        );
      }
      return $form;

    case 'validate':
      if (is_array($node_field)) {
        foreach ($node_field as $delta => $item) {
          if ($item['value'] != '') {
            switch ($field['granularity']) {
              case 0: // Year
                if (!preg_match('/^[0-9]{4}$/', $item['value'])) {
                  form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY).', array('%name' => t($field['widget']['label']))));
                }
                break;
              case 1: // Year and month
                if (!preg_match('/^[0-9]{4}-[0-9]{2}$/', $item['value'])) {
                  form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY-MM).', array('%name' => t($field['widget']['label']))));
                }
                break;
              case 2: // Date
                if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $item['value'])) {
                  form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY-MM-DD).', array('%name' => t($field['widget']['label']))));
                }
                break;
              case 3: // Date and time
                if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $item['value'])) {
                  form_set_error($field['field_name'] .']['. $delta .'][value', t('%name must be entered in ISO 8601 format (YYYY-MM-DDThh:mm:ss).', array('%name' => t($field['widget']['label']))));
                }
                break;
            }
          }
        }
      }
      return;
  }
}
