You are here

date_views.module in Date 7

File

date_views/date_views.module
View source
<?php

/**
 * Implement hook_views_api().
 *
 * This one is used as the base to reduce errors when updating.
 */
function date_views_theme() {
  $path = drupal_get_path('module', 'date_views');
  $base = array(
    'file' => 'theme.inc',
    'path' => "{$path}/theme",
  );
  return array(
    'date_nav_title' => $base + array(
      'variables' => array(
        'granularity' => NULL,
        'view' => NULL,
        'link' => NULL,
        'format' => NULL,
      ),
    ),
    'date_vcalendar' => $base + array(
      'variables' => array(
        'events' => NULL,
        'calname' => NULL,
      ),
    ),
    'date_vevent' => $base + array(
      'variables' => array(
        'event' => NULL,
      ),
    ),
    'date_valarm' => $base + array(
      'variables' => array(
        'alarm' => NULL,
      ),
    ),
    'date_views_filter_form' => $base + array(
      'template' => 'date-views-filter-form',
      'render element' => 'form',
    ),
    'date_calendar_day' => $base + array(
      'variables' => array(
        'date' => NULL,
      ),
    ),
  );
}
function date_views_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'date_views') . '/includes',
  );
}

/**
 * Wrapper function to make sure this function will always work.
 */
function date_views_views_fetch_fields($base, $type) {
  if (!module_exists('views')) {
    return array();
  }
  module_load_include('inc', 'views', 'includes/admin');
  return views_fetch_fields($base, $type);
}

/**
 * Implement hook_date_api_tables().
 */
function date_views_date_views_tables() {
  return array(
    'node',
    'comments',
    'users',
    'files',
  );
}

/**
 * Implement hook_date_views_fields().
 * on behalf of core fields.
 * 
 * All modules that create custom fields that use the 
 * 'views_handler_field_date' handler can provide
 * additional information here about the type of
 * date they create so the date can be used by
 * the Date API views date argument and date filter.
 */
function date_views_date_views_fields($field) {
  $values = array(
    // The type of date: DATE_UNIX, DATE_ISO, DATE_DATETIME.
    'sql_type' => DATE_UNIX,
    // Timezone handling options: 'none', 'site', 'date', 'utc' .
    'tz_handling' => 'site',
    // Needed only for dates that use 'date' tz_handling.
    'timezone_field' => '',
    // Needed only for dates that use 'date' tz_handling.
    'offset_field' => '',
    // Array of "table.field" values for related fields that should be
    // loaded automatically in the Views SQL.
    'related_fields' => array(),
    // Granularity of this date field's db data.
    'granularity' => array(
      'year',
      'month',
      'day',
      'hour',
      'minute',
      'second',
    ),
  );
  switch ($field) {
    case 'users.created':
    case 'users.access':
    case 'users.login':
    case 'node.created':
    case 'node.changed':
    case 'node_revisions.timestamp':
    case 'files.timestamp':
    case 'node_counter.timestamp':
    case 'accesslog.timestamp':
    case 'comments.timestamp':
    case 'node_comment_statistics.last_comment_timestamp':
      return $values;
  }
}

/**
 * Rebuild the theme registry and all the caches.
 * needed to pick up changes created by updated Views API
 * and other changes to Views definitions.
 */
function date_api_views_clear() {
  if (db_table_exists('cache_content')) {
    db_query('DELETE FROM {cache_content}');
  }
  if (db_table_exists('cache_views')) {
    db_query('DELETE FROM {cache_views}');
  }
  if (db_table_exists('views_object_cache')) {
    db_query('DELETE FROM {views_object_cache}');
  }
  db_query("DELETE FROM {cache} where cid LIKE 'theme_registry%'");
}

/**
 * Figure out the URL of the date view we're currently looking at,
 * adapted to various date types or specific date arguments.
 * 
 * @param $date_type
 *  - if not empty, return the url of a specific date type.
 * @param $date_arg
 *  - if not empty, return the url for a view with a specific date argument.
 * @param $force_view_url
 *  - always use the view url, even if embedded.
 * @return 
 *   return the requested view url.
 */
function date_real_url($view, $date_type = NULL, $date_arg = NULL, $force_view_url = FALSE) {
  $args = $view->args;
  $pos = $view->date_info->date_arg_pos;

  // The View arguments array is indexed numerically but is not necessarily
  // in numerical order. Sort the arguments to ensure the correct order.
  ksort($args);

  // If there are empty arguments before the date argument,
  // pad them with the wildcard so the date argument will be in
  // the right position.
  if (count($args) < $pos) {
    foreach ($view->argument as $name => $argument) {
      if ($argument->position == $pos) {
        break;
      }
      $args[] = $argument->options['wildcard'];
    }
  }
  if (!empty($date_type)) {
    switch ($date_type) {
      case 'year':
        $args[$pos] = date_pad($view->date_info->year, 4);
        break;
      case 'week':
        $args[$pos] = date_pad($view->date_info->year, 4) . '-W' . date_pad($view->date_info->week);
        break;
      case 'day':
        $args[$pos] = date_pad($view->date_info->year, 4) . '-' . date_pad($view->date_info->month) . '-' . date_pad($view->date_info->day);
        break;
      default:
        $args[$pos] = date_pad($view->date_info->year, 4) . '-' . date_pad($view->date_info->month);
        break;
    }
  }
  elseif (!empty($date_arg)) {
    $args[$pos] = $date_arg;
  }
  else {
    $args = $view->args;
  }

  // Is this an embedded or a block view?
  if (!$force_view_url && (!empty($view->preview) || !empty($view->date_info->block_identifier))) {
    $url = $view
      ->get_url($args);
    $key = date_block_identifier($view);
    if (!empty($key)) {
      return url($_GET['q'], array(
        'query' => drupal_get_query_parameters(NULL, array(
          $key => $url,
        )),
        'absolute' => TRUE,
      ));
    }
  }

  // Normal views may need querystrings appended to them
  // if they use exposed filters.
  return url($view
    ->get_url($args), array(
    'query' => drupal_get_query_parameters(),
    'absolute' => TRUE,
  ));
}
function date_block_identifier($view) {
  if (!empty($view->block_identifier)) {
    return $view->block_identifier;
  }
  return isset($view->date_info->block_identifier) ? $view->date_info->block_identifier : NULL;
}

/**
 * Identify all fields in this view that use the CCK Date handler.
 */
function date_handler_fields($view) {
  $field_names = array();
  if (empty($view->date_info->date_fields)) {
    $view->date_info->date_fields = array();
  }
  foreach ($view->field as $field) {
    if ($field->definition['handler'] == 'date_handler_field_multiple') {
      $name = $field->field;
      $group = $field->options['multiple'];
      if (substr($name, -7) == '_value2') {
        $field_name = substr($name, 0, strlen($name) - 7);
      }
      elseif (substr($name, -6) == '_value') {
        $field_name = substr($name, 0, strlen($name) - 6);
      }
      else {
        $field_name = '';
        $group = array();
        continue;
      }
      foreach ($view->date_info->date_fields as $date_field) {
        if (strstr($date_field, ' . ' . $field_name)) {
          $delta_field = 'entity_data_' . $field_name . '_delta';
          $field_names[$field_name] = array(
            'options' => $group,
            'delta_field' => $delta_field,
            'view_field' => clone $field,
          );

          // Get rid of the huge view object in the field handler.
          unset($field_names[$field_name]['view_field']->view);
        }
      }
    }
  }
  return $field_names;
}

/**
 * Create a Views field for each date column we care about
 * to supplement the generic 'entity_id' and 'revision_id'
 * fields that are automatically created.
 */
function date_field_views_data_alter(&$result, $field, $module) {
  if ($module == 'date') {
    foreach ($result as $table => $data) {
      $additional = array();
      $field_name = $field['field_name'];
      $key = array_key_exists('entity_id', $data) ? $field_name : $field_name . '-revision_id';
      $field_name_data = $data[$key]['field'];
      foreach ($data as $column => $value) {
        if (array_key_exists('argument', $value)) {
          $result[$table][$column]['argument']['handler'] = 'date_views_argument_handler';

          // The following code would add a FIELDNAME_value and FIELDNAME_value2 field as well as the basic FIELDNAME field.
          // Not sure yet if that is desirable.

          //$result[$table][$column]['field'] = $field_name_data;

          //$result[$table][$column]['field']['field'] = $value['argument']['field'];

          //$result[$table][$column]['field']['add fields to query'] = TRUE;
        }
        if (array_key_exists('filter', $value)) {
          $result[$table][$column]['filter']['handler'] = 'date_views_filter_handler';
        }

        // The old 'entity_id' and 'revision_id' values got rewritten in Views.
        // The old values are still there with a 'moved to' key, so ignore them.
        if (array_key_exists('field', $value) && !array_key_exists('moved to', $value['field'])) {

          // Not sure yet if we still need a custom field handler in D7 now that custom formatters are available.
          // Might still need it to handle grouping of multiple value dates.

          //$result[$table][$column]['field']['handler'] = 'date_handler_field_date';
          $result[$table][$column]['field']['add fields to query'] = TRUE;
        }
      }
    }
  }
}

Functions

Namesort descending Description
date_api_views_clear Rebuild the theme registry and all the caches. needed to pick up changes created by updated Views API and other changes to Views definitions.
date_block_identifier
date_field_views_data_alter Create a Views field for each date column we care about to supplement the generic 'entity_id' and 'revision_id' fields that are automatically created.
date_handler_fields Identify all fields in this view that use the CCK Date handler.
date_real_url Figure out the URL of the date view we're currently looking at, adapted to various date types or specific date arguments.
date_views_date_views_fields Implement hook_date_views_fields(). on behalf of core fields.
date_views_date_views_tables Implement hook_date_api_tables().
date_views_theme Implement hook_views_api().
date_views_views_api
date_views_views_fetch_fields Wrapper function to make sure this function will always work.