1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Helper for identifying Date API fields for views.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Identify all potential date/timestamp fields.
|
10
|
*
|
11
|
* @return array
|
12
|
* An array with fieldname, type, and table.
|
13
|
*
|
14
|
* @see date_views_date_views_fields()
|
15
|
*/
|
16
|
function _date_views_fields($base = 'node') {
|
17
|
// Make sure $base is never empty.
|
18
|
if (empty($base)) {
|
19
|
$base = 'node';
|
20
|
}
|
21
|
|
22
|
$cid = 'date_views_fields_' . $base;
|
23
|
cache_clear_all($cid, 'cache_views');
|
24
|
|
25
|
// We use fields that provide filter handlers as our universe of possible
|
26
|
// fields of interest.
|
27
|
$all_fields = date_views_views_fetch_fields($base, 'filter');
|
28
|
|
29
|
// Iterate over all the fields that Views knows about.
|
30
|
$fields = array();
|
31
|
foreach ((array) $all_fields as $alias => $val) {
|
32
|
// Set up some default values.
|
33
|
$granularity = array('year', 'month', 'day', 'hour', 'minute', 'second');
|
34
|
$tz_handling = 'site';
|
35
|
$related_fields = array();
|
36
|
$timezone_field = '';
|
37
|
$offset_field = '';
|
38
|
$rrule_field = '';
|
39
|
$delta_field = '';
|
40
|
$sql_type = DATE_UNIX;
|
41
|
$type = '';
|
42
|
|
43
|
$name = $alias;
|
44
|
$tmp = explode('.', $name);
|
45
|
$field_name = $tmp[1];
|
46
|
$table_name = $tmp[0];
|
47
|
|
48
|
// Unset the date filter to avoid ugly recursion and broken values.
|
49
|
if ($field_name == 'date_filter') {
|
50
|
continue;
|
51
|
}
|
52
|
|
53
|
$fromto = array($name, $name);
|
54
|
|
55
|
// If we don't have a filter handler, we don't need to do anything more.
|
56
|
if (!$handler = views_get_handler($table_name, $field_name, 'filter')) {
|
57
|
continue;
|
58
|
}
|
59
|
$handler = views_get_handler($table_name, $field_name, 'filter');
|
60
|
$handler_name = $handler->definition['handler'];
|
61
|
|
62
|
// We don't care about anything but date handlers.
|
63
|
if (empty($handler->definition['is date'])) {
|
64
|
continue;
|
65
|
}
|
66
|
$is_field = FALSE;
|
67
|
|
68
|
// For Field module fields, get the date type.
|
69
|
$custom = array();
|
70
|
if (isset($handler->definition['field_name'])) {
|
71
|
$field = field_info_field($handler->definition['field_name']);
|
72
|
$is_field = TRUE;
|
73
|
switch ($field['type']) {
|
74
|
case 'date':
|
75
|
$sql_type = DATE_ISO;
|
76
|
break;
|
77
|
|
78
|
case 'datestamp':
|
79
|
break;
|
80
|
|
81
|
case 'datetime':
|
82
|
$sql_type = DATE_DATETIME;
|
83
|
break;
|
84
|
|
85
|
default:
|
86
|
// If this is not a date field, nothing more to do.
|
87
|
continue 2;
|
88
|
}
|
89
|
|
90
|
$revision = in_array($base, array('node_revision')) ? FIELD_LOAD_REVISION : FIELD_LOAD_CURRENT;
|
91
|
$db_info = date_api_database_info($field, $revision);
|
92
|
$name = $table_name . "." . $field_name;
|
93
|
$grans = array('year', 'month', 'day', 'hour', 'minute', 'second');
|
94
|
$granularity = !empty($field['settings']['granularity']) ? $field['settings']['granularity'] : $grans;
|
95
|
|
96
|
$fromto = array(
|
97
|
$table_name . '.' . $db_info['columns'][$table_name]['value'],
|
98
|
$table_name . '.' . (!empty($field['settings']['todate']) ? $db_info['columns'][$table_name]['value2'] : $db_info['columns'][$table_name]['value']),
|
99
|
);
|
100
|
|
101
|
if (isset($field['settings']['tz_handling'])) {
|
102
|
$tz_handling = $field['settings']['tz_handling'];
|
103
|
$db_info = date_api_database_info($field, $revision);
|
104
|
if ($tz_handling == 'date') {
|
105
|
$offset_field = $table_name . '.' . $db_info['columns'][$table_name]['offset'];
|
106
|
}
|
107
|
$related_fields = array(
|
108
|
$table_name . '.' . $db_info['columns'][$table_name]['value'],
|
109
|
);
|
110
|
if (isset($db_info['columns'][$table_name]['value2'])) {
|
111
|
$related_fields = array_merge($related_fields, array($table_name . '.' . $db_info['columns'][$table_name]['value2']));
|
112
|
}
|
113
|
if (isset($db_info['columns'][$table_name]['timezone'])) {
|
114
|
$related_fields = array_merge($related_fields, array($table_name . '.' . $db_info['columns'][$table_name]['timezone']));
|
115
|
$timezone_field = $table_name . '.' . $db_info['columns'][$table_name]['timezone'];
|
116
|
}
|
117
|
if (isset($db_info['columns'][$table_name]['rrule'])) {
|
118
|
$related_fields = array_merge($related_fields, array($table_name . '.' . $db_info['columns'][$table_name]['rrule']));
|
119
|
$rrule_field = $table_name . '.' . $db_info['columns'][$table_name]['rrule'];
|
120
|
}
|
121
|
}
|
122
|
// Get the delta value into the query.
|
123
|
if ($field['cardinality'] != 1) {
|
124
|
array_push($related_fields, "$table_name.delta");
|
125
|
$delta_field = $table_name . '_delta';
|
126
|
}
|
127
|
}
|
128
|
|
129
|
// Allow custom modules to provide date fields.
|
130
|
else {
|
131
|
foreach (module_implements('date_views_fields') as $module) {
|
132
|
$function = $module . '_date_views_fields';
|
133
|
if ($custom = $function("$table_name.$field_name")) {
|
134
|
$type = 'custom';
|
135
|
break;
|
136
|
}
|
137
|
}
|
138
|
}
|
139
|
// Don't do anything if this is not a date field we can handle.
|
140
|
if (!empty($type) || empty($custom)) {
|
141
|
$alias = str_replace('.', '_', $alias);
|
142
|
$fields['name'][$name] = array(
|
143
|
'is_field' => $is_field,
|
144
|
'sql_type' => $sql_type,
|
145
|
'label' => $val['group'] . ': ' . $val['title'],
|
146
|
'granularity' => $granularity,
|
147
|
'fullname' => $name,
|
148
|
'table_name' => $table_name,
|
149
|
'field_name' => $field_name,
|
150
|
'query_name' => $alias,
|
151
|
'fromto' => $fromto,
|
152
|
'tz_handling' => $tz_handling,
|
153
|
'offset_field' => $offset_field,
|
154
|
'timezone_field' => $timezone_field,
|
155
|
'rrule_field' => $rrule_field,
|
156
|
'related_fields' => $related_fields,
|
157
|
'delta_field' => $delta_field,
|
158
|
);
|
159
|
|
160
|
// Allow the custom fields to over-write values.
|
161
|
if (!empty($custom)) {
|
162
|
foreach ($custom as $key => $val) {
|
163
|
$fields['name'][$name][$key] = $val;
|
164
|
}
|
165
|
}
|
166
|
$fields['name'][$name]['real_field_name'] = $field_name;
|
167
|
$fields['alias'][$alias] = $fields['name'][$name];
|
168
|
}
|
169
|
}
|
170
|
cache_set($cid, $fields, 'cache_views');
|
171
|
return $fields;
|
172
|
}
|