You are here

function date_api_fields in Date 6

Same name and namespace in other branches
  1. 6.2 includes/date_api.views.inc \date_api_fields()

Identify all potential date/timestamp fields.

Return value

array with fieldname, type, and table

2 calls to date_api_fields()
date_api_argument_handler::get_query_fields in ./date_api.views.inc
date_api_argument_handler::options_form in ./date_api.views.inc
Add a form element to select date_fields for this argument.

File

./date_api.views.inc, line 843
Defines date-related Views data and plugins:

Code

function date_api_fields($base = 'node') {
  $cid = 'date_api_fields';
  cache_clear_all($cid, 'cache_views');
  $all_fields = views_fetch_fields($base, 'field');
  $fields = array();
  foreach ((array) $all_fields as $name => $val) {
    $fromto = array();
    $tmp = explode('.', $name);
    $field_name = $tmp[1];
    $table_name = $tmp[0];
    $alias = str_replace('.', '_', $name);
    $handler = views_get_handler($table_name, $field_name, 'field');
    $type = '';

    // For cck fields, get the date type.
    if (isset($handler->content_field)) {
      if ($handler->content_field['type'] == 'date') {
        $type = 'cck_string';
      }
      elseif ($handler->content_field['type'] == 'datestamp') {
        $type = 'cck_timestamp';
      }
      elseif ($handler->content_field['type'] == 'datetime') {
        $type = 'cck_datetime';
      }
    }
    elseif (strstr($field_name, 'timestamp') || strstr($field_name, 'updated') || strstr($field_name, 'created') || strstr($field_name, 'changed')) {
      $type = 'timestamp';
    }

    // Don't do anything if this is not a date field we can handle.
    if (!empty($type)) {

      // dates with from and to dates need to handle both fields as one
      // add the from and to dates to the first one found and ignore the second
      $fields[$name]['table_name'] = $table_name;
      $fields[$name]['field_name'] = $field_name;
      $fields[$name]['type'] = $type;

      // Handling for content field dates
      if ($handler->content_field['tz_handling']) {
        $tz_handling = $handler->content_field['tz_handling'];
        $db_info = content_database_info($handler->content_field);
        if ($tz_handling == 'date') {
          $offset_field = $table_name . '.' . $db_info['columns']['offset']['column'];
        }
        $related_fields = array(
          $table_name . '.' . $field_name,
          $table_name . '.' . $db_info['columns']['value2']['column'],
          $table_name . '.' . $db_info['columns']['timezone']['column'],
        );
        $timezone_field = $table_name . '.' . $db_info['columns']['timezone']['column'];
      }
      else {
        $fromto = array(
          $alias,
          $alias,
        );
        $tz_handling = 'site';
        $related_fields = array();
        $timezone_field = '';
      }

      // Handling for cck fromto dates
      switch ($handler->content_field['type']) {
        case 'date':
        case 'datetime':
        case 'datestamp':
          $db_info = content_database_info($handler->content_field);
          $fromto = array(
            $table_name . '_' . $db_info['columns']['value']['column'],
            $table_name . '_' . ($handler->content_field['todate'] ? $db_info['columns']['value2']['column'] : $db_info['columns']['value']['column']),
          );
          break;
      }
      if (is_array($handler->content_field['granularity'])) {
        $granularity = $handler->content_field['granularity'];
      }
      else {
        $granularity = array(
          'year',
          'month',
          'day',
          'hour',
          'minute',
        );
      }

      // CCK fields append a column name to the field, others do not
      // need a real field_name with no column name appended for cck date formatters
      switch ($type) {
        case 'cck_string':
          $sql_type = DATE_ISO;
          break;
        case 'cck_datetime':
          $sql_type = DATE_DATETIME;
          break;
        default:
          $sql_type = DATE_UNIX;
          break;
      }
      $fields['name'][$name] = array(
        'type' => $type,
        'sql_type' => $sql_type,
        'label' => $val['group'] . ': ' . $val['title'],
        'granularity' => $granularity,
        'fullname' => $name,
        'table_name' => $table_name,
        'field_name' => $field_name,
        'query_name' => $alias,
        'fromto' => $fromto,
        'tz_handling' => $tz_handling,
        'offset_field' => $offset_field,
        'timezone_field' => $timezone_field,
        'related_fields' => $related_fields,
      );
      $fields['alias'][$alias] = $fields['name'][$name];
    }
  }

  //cache_set($cid, $fields, 'cache_views');
  return $fields;
}