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