2 // $Id: date_api_fields.inc,v 1.1.2.7 2009/03/20 16:57:15 karens Exp $
4 * Identify all potential date/timestamp fields.
7 * array with fieldname, type, and table.
9 * date_api_date_api_fields() which implements
10 * the hook_date_api_fields() for the core date fields.
12 function _date_api_fields($base = 'node') {
13 $cid = 'date_api_fields_'. $base;
14 cache_clear_all($cid, 'cache_views');
16 $all_fields = date_api_views_fetch_fields($base, 'field');
18 foreach ((array) $all_fields as $name => $val) {
20 $tmp = explode('.', $name);
21 $field_name = $tmp[1];
22 $table_name = $tmp[0];
23 $alias = str_replace('.', '_', $name);
25 if (!$handler = views_get_handler($table_name, $field_name, 'field')) {
29 $handler_name = $handler->definition['handler'];
32 // For cck fields, get the date type.
34 if (isset($handler->content_field)) {
35 if ($handler->content_field['type'] == 'date') {
38 elseif ($handler->content_field['type'] == 'datestamp') {
39 $type = 'cck_timestamp';
41 elseif ($handler->content_field['type'] == 'datetime') {
42 $type = 'cck_datetime';
46 // Allow custom modules to provide date fields.
47 elseif ($handler_name == 'views_handler_field_date') {
48 foreach (module_implements('date_api_fields') as $module) {
49 $function = $module .'_date_api_fields';
50 if ($custom = $function("$table_name.$field_name")) {
57 // Don't do anything if this is not a date field we can handle.
60 // Handling for simple timestamp fields
61 $fromto = array($alias, $alias);
62 $tz_handling = 'site';
63 $related_fields = array();
68 $granularity = array('year', 'month', 'day', 'hour', 'minute');
70 // Handling for content field dates
71 if (isset($handler->content_field['tz_handling'])) {
72 $tz_handling = $handler->content_field['tz_handling'];
73 $db_info = content_database_info($handler->content_field);
74 if ($tz_handling == 'date') {
75 $offset_field = $table_name .'.'. $db_info['columns']['offset']['column'];
77 $related_fields = array(
78 $table_name .'.'. $field_name
80 if (isset($db_info['columns']['value2']['column'])) {
81 $related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['value2']['column']));
83 if (isset($db_info['columns']['timezone']['column'])) {
84 $related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['timezone']['column']));
85 $timezone_field = $table_name .'.'. $db_info['columns']['timezone']['column'];
87 if (isset($db_info['columns']['rrule']['column'])) {
88 $related_fields = array_merge($related_fields, array($table_name .'.'. $db_info['columns']['rrule']['column']));
89 $rrule_field = $table_name .'.'. $db_info['columns']['rrule']['column'];
92 // Get the delta value into the query.
93 if (!empty($handler->content_field['multiple'])) {
94 array_push($related_fields, "$table_name.delta");
95 $delta_field = $table_name .'_delta';
98 // Handling for cck fromto dates
99 if (isset($handler->content_field)) {
100 switch ($handler->content_field['type']) {
104 $db_info = content_database_info($handler->content_field);
106 $table_name .'_'. $db_info['columns']['value']['column'],
107 $table_name .'_'. (!empty($handler->content_field['todate']) ? $db_info['columns']['value2']['column'] : $db_info['columns']['value']['column']),
111 $granularity = !empty($handler->content_field['granularity']) ? $handler->content_field['granularity'] : array('year', 'month', 'day', 'hour', 'minute');
114 // CCK fields append a column name to the field, others do not
115 // need a real field_name with no column name appended for cck date formatters
118 $sql_type = DATE_ISO;
121 $sql_type = DATE_DATETIME;
124 $sql_type = DATE_UNIX;
127 $fields['name'][$name] = array(
129 'sql_type' => $sql_type,
130 'label' => $val['group'] .': '. $val['title'],
131 'granularity' => $granularity,
133 'table_name' => $table_name,
134 'field_name' => $field_name,
135 'query_name' => $alias,
137 'tz_handling' => $tz_handling,
138 'offset_field' => $offset_field,
139 'timezone_field' => $timezone_field,
140 'rrule_field' => $rrule_field,
141 'related_fields' => $related_fields,
142 'delta_field' => $delta_field,
145 // Allow the custom fields to over-write values.
146 if (!empty($custom)) {
147 foreach ($custom as $key => $val) {
148 $fields['name'][$name][$key] = $val;
151 if (isset($handler->content_field)) {
152 if (substr($field_name, -1) == '2') {
153 $len = (strlen($field_name) - 7);
156 $len = (strlen($field_name) - 6);
158 $fields['name'][$name]['real_field_name'] = substr($field_name, 0, $len);
161 $fields['name'][$name]['real_field_name'] = $field_name;
163 $fields['alias'][$alias] = $fields['name'][$name];
166 cache_set($cid, $fields, 'cache_views');