You are here

function _calendar_fields in Calendar 5

Same name and namespace in other branches
  1. 5.2 calendar_admin.inc \_calendar_fields()

Identify all potential date/timestamp fields.

Return value

array with fieldname, type, and table

1 call to _calendar_fields()
calendar_fields in ./calendar.module
Identify all potential date/timestamp fields

File

./calendar_admin.inc, line 86
This file contains administrative functions used only when setting up the calendar and views_hooks() that are called infrequently and cached. No need to parse all this code the rest of the time.

Code

function _calendar_fields() {
  $cid = 'calendar_fields';
  cache_clear_all($cid, 'cache_views');
  $delta = 0;
  $event_fields_processed = array();
  views_load_cache();
  foreach (_views_get_fields() as $name => $val) {
    $timestamp_fromto = array();
    $string_fromto = array();
    $tmp = explode('.', $name);
    $field_name = $val['content_field']['field_name'] ? $val['content_field']['field_name'] : $tmp[1];

    // We need to treat event_start and event_end as a single date, all other fields have
    // the same field_name for both start and end dates.
    $processed_name = strstr($field_name, 'event_') ? 'event' : $field_name;
    $type = '';

    // for cck fields, get the date type
    if ($val['content_field']['type'] == 'date' || $val['content_field']['type'] == 'datestamp') {
      $type = $val['content_field']['type'] == 'date' ? 'cck_string' : 'cck_timestamp';
    }
    elseif ($val['handler'] == views_handler_field_dates()) {
      $type = 'timestamp';
    }

    // don't do anything if this is not a date field
    if ($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
      // Handling for event fromto dates
      if (module_exists('event') && !in_array($processed_name, $event_fields_processed) && ($name == 'event.event_start' || $name == 'event.event_end')) {
        $timestamp_fromto = array(
          'event.event_start',
          'event.event_end',
        );
        $offset_field = 'event.timezone';
        $tz_handling = variable_get('event_timezone_display', 'site');
        $event_fields_processed[] = $processed_name;
      }
      elseif ($val['content_field']['tz_handling']) {
        $tz_handling = $val['content_field']['tz_handling'];
        if ($tz_handling == 'date') {
          $offset_field = $val['table'] . '.' . $val['content_db_info']['columns']['offset']['column'];
        }
      }
      else {
        $tz_handling = 'site';
      }

      // Handling for cck fromto dates
      if (!in_array($processed_name, $event_fields_processed) && $val['content_field']['todate']) {
        switch ($val['content_field']['type']) {
          case 'datestamp':
            $timestamp_fromto = array(
              $val['table'] . '.' . $field_name . '_value',
              $val['table'] . '.' . $field_name . '_value2',
            );
            break;
          case 'date':
            $string_fromto = array(
              $val['table'] . '.' . $field_name . '_value',
              $val['table'] . '.' . $field_name . '_value2',
            );
            break;
        }
        $event_fields_processed[] = $processed_name;
      }

      // skip this step on second occurance of fromto date fields, if more than one exists in view
      if (!in_array($processed_name, $event_fields_processed) || $timestamp_fromto || $string_fromto) {

        // 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
        $fields[$tmp[1]] = array(
          'type' => $type,
          'delta' => $delta,
          'label' => $val['name'],
          'granularity' => is_array($val['content_field']['granularity']) ? (array) array_keys($val['content_field']['granularity']) : array(
            'Y',
            'M',
            'D',
            'H',
            'N',
          ),
          'fullname' => $name,
          'table' => $tmp[0],
          'field' => $tmp[1],
          'field_name' => $field_name,
          'query_name' => str_replace('.', '_', $name),
          'timestamp_fromto' => $timestamp_fromto,
          'string_fromto' => $string_fromto,
          'tz_handling' => $tz_handling,
          'offset_field' => $offset_field,
        );
      }
    }
  }
  cache_set($cid, 'cache_views', serialize($fields));
  return $fields;
}