You are here

date.module in Date 6

Same filename and directory in other branches
  1. 5.2 date/date.module
  2. 6.2 date/date.module

Defines date/time field types for the Content Construction Kit (CCK).

File

date/date.module
View source
<?php

/**
 * @file
 * Defines date/time field types for the Content Construction Kit (CCK).
 */

/**
 * Implementation of hook_init().
 */
function date_init() {
  include_once drupal_get_path('module', 'date') . '/date.theme';
  if (module_exists('token')) {
    include_once drupal_get_path('module', 'date') . '/date_token.inc';
  }
}

/**
 * Implementation of hook_content_is_empty().
 */
function date_content_is_empty($item, $field) {
  if (empty($item['value'])) {
    return TRUE;
  }
  elseif ($field['todate'] == 'required' && empty($item['value2'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_field_info().
 */
function date_field_info() {
  return array(
    'date' => array(
      'label' => 'Date',
      'description' => t('Store a date in the database as an ISO date, recommended for historical or partial dates.'),
      'callbacks' => array(
        'tables' => CONTENT_CALLBACK_DEFAULT,
        'arguments' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
    'datestamp' => array(
      'label' => 'Datestamp',
      'description' => t('Store a date in the database as a timestamp, deprecated format to suppport legacy data.'),
      'callbacks' => array(
        'tables' => CONTENT_CALLBACK_DEFAULT,
        'arguments' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
    'datetime' => array(
      'label' => 'Datetime',
      'description' => t('Store a date in the database as a datetime field, recommended for complete dates and times that may need timezone conversion.'),
      'callbacks' => array(
        'tables' => CONTENT_CALLBACK_DEFAULT,
        'arguments' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget_info().
 */
function date_widget_info() {
  $info = array(
    'date_select' => array(
      'label' => t('Select List'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_CUSTOM,
      ),
    ),
    'date_text' => array(
      'label' => t('Text Field with custom input format'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_CUSTOM,
      ),
    ),
  );
  if (module_exists('date_popup')) {
    $info['date_popup'] = array(
      'label' => t('Text Field with jquery pop-up calendar'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_CUSTOM,
      ),
    );
  }
  return $info;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function date_field_formatter_info() {
  return array(
    'default' => array(
      'label' => t('Default'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'long' => array(
      'label' => t('Long'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'medium' => array(
      'label' => t('Medium'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'short' => array(
      'label' => t('Short'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'iso' => array(
      'label' => t('ISO'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'timestamp' => array(
      'label' => t('Timestamp'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'feed' => array(
      'label' => t('Feed'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'ical' => array(
      'label' => t('iCal'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'format_interval' => array(
      'label' => t('As Time Ago'),
      'field types' => array(
        'date',
        'datestamp',
        'datetime',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function date_theme() {
  return array(
    'date_combo' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'date_text_parts' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'date' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'date_formatter_default' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_long' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_medium' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_short' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_timestamp' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_iso' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_ical' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_feed' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_display_combination',
    ),
    'date_formatter_format_interval' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'function' => 'theme_date_format_interval',
    ),
  );
}

/**
 * Helper function for creating formatted date arrays from a formatter.
 *
 * Use the Date API to get an object representation of a date field
 *
 * @param array $field
 * @param array $item - a node field item, like $node->myfield[0]
 *
 * @return array that holds the From and To date objects
 *  Each date object looks like:
 *       date [value] => array (
 *         [db] => array (  // the value stored in the database
 *           [object] => the datetime object
 *           [datetime] => 2007-02-15 20:00:00
 *         )
 *         [local] => array (  // the local representation of that value
 *           [object] => the datetime object
 *           [datetime] => 2007-02-15 14:00:00
 *           [timezone] => US/Central
 *           [offset] => -21600
 *         )
 *      )
 */
function date_formatter_process($element) {
  $dates = array();
  $timezone = date_default_timezone_name();
  if (empty($timezone)) {
    return $dates;
  }
  $field_name = $element['#field_name'];
  $fields = content_fields();
  $field = $fields[$field_name];
  $formatter = $element['#formatter'];
  $format = date_formatter_format($formatter, $field_name);
  $item = $element['#item'];
  $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  $process = date_process_values($field);
  foreach ($process as $processed) {
    if (empty($item[$processed])) {
      $dates[$processed] = NULL;
    }
    else {

      // create a date object with a gmt timezone from the database value
      $value = $item[$processed];
      if ($field['type'] == DATE_ISO) {
        $value = date_fuzzy_datetime($value);
      }
      $date = date_make_date($value, 'UTC', $field['type']);
      $dates[$processed] = array();
      $dates[$processed]['db']['object'] = $date;
      $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);

      // For no timezone handling, set local value to the same as the db value.
      if (!date_timezone_convert($field, $item[$processed])) {
        $dates[$processed]['local'] = $dates[$processed]['db'];
      }
      else {
        $timezone = date_get_timezone($field['tz_handling'], $timezone);
        date_timezone_set($date, timezone_open($timezone));
        $dates[$processed]['local']['object'] = $date;
        $dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
        $dates[$processed]['local']['timezone'] = $timezone;
        $dates[$processed]['local']['offset'] = date_offset_get($date);
      }

      //format the date, special casing the 'interval' format which doesnt need to be processed
      $dates[$processed]['formatted'] = '';
      if (is_object($date)) {
        if ($format == 'format_interval') {
          $dates[$processed]['interval'] = date_format_interval($date);
        }
        elseif (!empty($format)) {
          $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format) . $append;
          $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', date_limit_format($format, array(
            'year',
            'month',
            'day',
          )));
          $dates[$processed]['formatted_time'] = date_format_date($date, 'custom', date_limit_format($format, array(
            'hour',
            'minute',
            'second',
          )));
        }
      }
    }
  }
  $dates['format'] = $format;
  return $dates;
}

/**
 * $field['granularity'] will contain an array like ('hour' => 'hour', 'month' => 0)
 * where the values turned on return their own names and the values turned off return a zero
 * need to reconfigure this into a simple array of the turned on values
 */
function date_granularity($field) {
  if (!is_array($field) || !is_array($field['granularity'])) {
    $field['granularity'] = drupal_map_assoc(array(
      'year',
      'month',
      'day',
    ));
  }
  return array_values(array_filter($field['granularity']));
}

/**
 *  Function to figure out which timezone applies to a date and select it
 */
function date_get_timezone($handling, $timezone = '') {
  switch ($handling) {
    case 'site':
      $timezone = date_default_timezone_name(FALSE);
      break;
    case 'date':
      $timezone = !empty($timezone) ? $timezone : date_default_timezone_name();
      break;
    case 'none':
    case 'utc':
      $timezone = 'UTC';
      break;
    default:
      $timezone = date_default_timezone_name();
  }
  return $timezone > '' ? $timezone : date_default_timezone_name();
}

/**
 * Helper function to determine if any timezone conversion should be
 * done on this field.
 */
function date_timezone_convert($field, $item = array()) {
  if ($field['tz_handling'] == 'none' || !in_array('hour', date_granularity($field)) || $field['tz_handling'] == 'date' && empty($item['timezone'])) {
    return FALSE;
  }
  return TRUE;
}

/**
 * Empty or reset cached values.
 *
 * @param $remove
 *   whether or not to completely remove the caches.
 */
function date_clear_all($remove = FALSE) {
  cache_clear_all('date_', 'cache', '*');
  if (module_exists('views')) {
    if ($remove) {
      cache_clear_all('date_browser_views', 'cache_views');
    }
    else {

      //date_views_browser_get_views(TRUE);
    }
  }
}

/**
 * Helper function to create an array of the date values in a
 * field that need to be processed.
 */
function date_process_values($field) {
  return $field['todate'] ? array(
    'value',
    'value2',
  ) : array(
    'value',
  );
}

/**
 * Implementation of hook_help().
 */
function date_help($section) {
  switch ($section) {
    case 'admin/help#date':
      return t('<p>Complete documentation for the Date and Date API modules is available at !link.</p>', array(
        '!link' => l('http://drupal.org/node/92460', 'http://drupal.org/node/92460'),
      ));
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 * Make sure date information gets updated.
 */
function date_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_edit_view') {
    $form['#submit'] = array_merge($form['#submit'], array(
      'date_clear_all',
    ));
  }
  elseif ($form_id == '_content_admin_field' && ($form['type']['#value'] == 'date' || $form['type']['#value'] == 'datestamp' || $form['type']['#value'] == 'datetime')) {
    $form['#validate'] = array_merge($form['#validate'], array(
      'date_settings_validate',
    ));
  }
}
function date_settings_validate($form, &$form_state) {
  form_set_value(array(
    '#parents' => array(
      'granularity',
    ),
  ), array_keys(array_filter($form_state['values']['granularity'])), $form_state);
}

/**
 * Implementation of hook_field().
 *
 * Validation and submission operation code is moved into a separate
 * file and included only when processing forms.
 */
function date_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':
      include_once drupal_get_path('module', 'date') . '/date_elements.inc';
      return _date_field_validate($op, $node, $field, $items, $teaser, $page);
      break;
    case 'insert':
    case 'update':
      include_once drupal_get_path('module', 'date') . '/date_elements.inc';
      return _date_field_update($op, $node, $field, $items, $teaser, $page);
      break;
    case 'sanitize':
      foreach ($items as $delta => $item) {

        //$dates = date_formatter_process($field, $item, $node, $formatter);

        //$node->$field['field_name'][$delta]['dates'] = $dates;
      }
      break;
  }
}

/**
 * Implementation of hook_widget().
 *
 * This code and all the processes it uses are in a separate file,
 * included only when processing forms.
 */
function date_widget(&$form, &$form_state, &$field, $items, $delta) {
  include_once drupal_get_path('module', 'date') . '/date_elements.inc';
  return _date_widget($form, $form_state, $field, $items, $delta);
}

/**
 * Implementation of hook_elements().
 *
 * This code and all the processes it uses are in a separate file,
 * included only when processing forms.
 */
function date_elements() {
  include_once drupal_get_path('module', 'date') . '/date_elements.inc';
  return _date_elements();
}

/**
 * Implementation of Devel module's hook_content_generate().
 */
function date_content_generate($node, $field) {
  include_once drupal_get_path('module', 'date') . '/date_content_generate.inc';
  return _date_content_generate($node, $field);
}

/**
 * Wrapper functions for date administration, included only when
 * processing field settings.
 */
function date_widget_settings($op, $widget) {
  include_once drupal_get_path('module', 'date') . '/date_admin.inc';
  return _date_widget_settings($op, $widget);
}
function date_field_settings($op, $field) {
  include_once drupal_get_path('module', 'date') . '/date_admin.inc';
  return _date_field_settings($op, $field);
}

/**
 * Helper function to return the date format used by a specific formatter.
 */
function date_formatter_format($formatter, $field_name) {
  $fields = content_fields();
  $field = $fields[$field_name];
  if ($field['tz_handling'] == 'date') {
    $field['granularity'][] = 'timezone';
  }
  switch ($formatter) {
    case 'ical':
      return 'Ymd\\THis';
    case 'timestamp':
      return 'U';
    case 'iso':
      return DATE_FORMAT_ISO . 'P';
    case 'feed':
      return 'D, j M Y H:i:s O';
    case 'format_interval':
      return 'format_interval';
    case 'long':
    case 'medium':
    case 'short':
    case 'default':
      $custom = 'output_format_custom' . ($formatter != 'default' ? '_' . $formatter : '');
      $value = 'output_format_date' . ($formatter != 'default' ? '_' . $formatter : '');
      if ($field[$custom] > '') {
        $format = $field[$custom];
      }
      elseif ($field[$value]) {
        $format = $field[$value];
      }
      else {
        switch ($formatter) {
          case 'long':
            $format = variable_get('date_format_long', 'l, F j, Y - H:i');
            break;
          case 'medium':
            $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
            break;
          default:
            $format = variable_get('date_format_short', 'm/d/Y - H:i');
            break;
        }
      }
      break;
  }
  return date_limit_format($format, date_granularity($field));
}

Functions

Namesort descending Description
date_clear_all Empty or reset cached values.
date_content_generate Implementation of Devel module's hook_content_generate().
date_content_is_empty Implementation of hook_content_is_empty().
date_elements Implementation of hook_elements().
date_field Implementation of hook_field().
date_field_formatter_info Implementation of hook_field_formatter_info().
date_field_info Implementation of hook_field_info().
date_field_settings
date_formatter_format Helper function to return the date format used by a specific formatter.
date_formatter_process Helper function for creating formatted date arrays from a formatter.
date_form_alter Implementation of hook_form_alter(). Make sure date information gets updated.
date_get_timezone Function to figure out which timezone applies to a date and select it
date_granularity $field['granularity'] will contain an array like ('hour' => 'hour', 'month' => 0) where the values turned on return their own names and the values turned off return a zero need to reconfigure this into a…
date_help Implementation of hook_help().
date_init Implementation of hook_init().
date_process_values Helper function to create an array of the date values in a field that need to be processed.
date_settings_validate
date_theme Implementation of hook_theme().
date_timezone_convert Helper function to determine if any timezone conversion should be done on this field.
date_widget Implementation of hook_widget().
date_widget_info Implementation of hook_widget_info().
date_widget_settings Wrapper functions for date administration, included only when processing field settings.