Removing translation directories
[project/views.git] / views_cache.inc
CommitLineData
f4f02cb1 1<?php
f4f02cb1
EM
2
3// Load all our module 'on behalfs'.
4// Whenever we decide we need this, load our module 'on behalfs'.
5$path = drupal_get_path('module', 'views') . '/modules';
4f599ff0 6$files = drupal_system_listing('views_.*\.inc$', $path, 'name', 0);
f4f02cb1
EM
7
8foreach($files as $file) {
9 // The filename format is very specific. It must be views_MODULENAME.inc
10 $module = substr_replace($file->name, '', 0, 6);
11 if (module_exists($module)) {
12 require_once("./$file->filename");
13 }
14}
15
16// ---------------------------------------------------------------------------
17// Acquire Views Data
18
19/**
20 * Return the arguments array; construct one if we haven't already. The
21 * array is cached in a global, safely named variable so that arguments
22 * are only constructed once per run.
23 */
24function _views_get_arguments($titles = false) {
25 static $views_arguments;
26 global $locale;
27
28 if (!$views_arguments) {
d06152e9 29 $data = cache_get("views_arguments:$locale", 'cache_views');
f4f02cb1
EM
30 $cache = unserialize($data->data);
31 if (is_array($cache)) {
32 $views_arguments = $cache;
33 }
34 else {
35 $arguments = module_invoke_all('views_arguments');
bd4e854a
EM
36 // allow modules to alter the definitions supplied others
37 foreach (module_implements('views_arguments_alter') as $module) {
38 $function = $module . '_views_arguments_alter';
39 $function($arguments);
40 }
41
118515fd 42 uasort($arguments, '_views_sort_arrays');
f4f02cb1
EM
43 foreach ($arguments as $name => $arg) {
44 if ($arg['option'] && !is_array($arg['option'])) {
45 if ($arg['option'] == 'string' || $arg['option'] == 'integer') {
46 $arg['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
47 }
48 else {
49 $arg['option'] = array('#type' => 'select', '#options' => $arg['option']);
50 }
51 }
52 $views_arguments['base'][$name] = $arg['name'];
53 $views_arguments['title'][$name] = $arg;
54 }
55 $cache = $views_arguments;
d06152e9 56 cache_set("views_arguments:$locale", 'cache_views', serialize($cache));
f4f02cb1
EM
57 }
58 }
59 return ($titles ? $views_arguments['base'] : $views_arguments['title']);
60}
61
62/**
63 * Constructs the full table information array. Caches it into a global array
64 * so that it will only be called once per run.
65 */
66function _views_get_tables($full = false) {
67 static $views_tables;
68 global $locale;
69
70 if (!$views_tables) {
d06152e9 71 $data = cache_get("views_tables:$locale", 'cache_views');
f4f02cb1
EM
72 $cache = unserialize($data->data);
73
74 if (is_array($cache)) {
75 $views_tables = $cache;
76 }
77 else {
78 $table_data = module_invoke_all('views_tables');
e09da08a 79
bd4e854a
EM
80 // allow modules to alter the definitions supplied others
81 foreach (module_implements('views_tables_alter') as $module) {
82 $function = $module . '_views_tables_alter';
83 $function($table_data);
84 }
e09da08a 85
5de5446f 86 $views_tables['tables'] = $table_data;
f4f02cb1
EM
87
88 foreach ($table_data as $name => $table) {
89 if (is_array($table['filters'])) {
90 foreach ($table['filters'] as $filter => $data) {
2703a498 91 $data['table'] = $name;
f4f02cb1
EM
92 // translate for deprecated APIs...
93 if ($data['option'] && !is_array($data['option'])) {
94 if ($data['option'] == 'string' || $data['option'] == 'integer') {
95 $data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
96 }
97 else {
98 $data['option'] = array('#type' => 'select', '#options' => $data['option']);
99 }
100 }
e09da08a
EM
101 if ($data['value'] && !is_array($data['value'])) {
102 if ($data['value'] == 'string' || $data['value'] == 'integer') {
103 $data['value'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
104 }
105 else {
106 $data['value'] = array('#type' => 'select', '#values' => $data['value']);
107 }
108 }
109 else if ($data['list']) {
aafb50da 110 $data['value'] = array('#type' => 'select', '#options' => $data['list'], '#validate' => array('views_filter_validate_array' => array($data['name'])));
f4f02cb1
EM
111 if ($data['list-type'] != 'select') {
112 $data['value']['#multiple'] = TRUE;
113 }
114 }
115 else if (!$data['value']) {
116 $data['value'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
117 }
118 $views_tables['filters']['titles']["$name.$filter"] = $data['name'];
119 $views_tables['filters']['base']["$name.$filter"] = $data;
120 }
121 }
122 if (is_array($table['fields'])) {
123 foreach ($table['fields'] as $field => $data) {
124 if ($data['option'] && !is_array($data['option'])) {
125 if ($data['option'] == 'string' || $data['option'] == 'integer') {
126 $data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
127 }
128 else {
129 $data['option'] = array('#type' => 'select', '#options' => $data['option']);
130 }
131 }
132 $data['table'] = $name;
133 $views_tables['fields']['titles']["$name.$field"] = $data['name'];
134 $views_tables['fields']['base']["$name.$field"] = $data;
135 }
136 }
137 if (is_array($table['sorts'])) {
138 foreach ($table['sorts'] as $field => $data) {
139 $data['table'] = $name;
140 if ($data['option'] && !is_array($data['option'])) {
141 if ($data['option'] == 'string' || $data['option'] == 'integer') {
142 $data['option'] = array('#type' => 'textfield', '#size' => 10, '#maxlength' => 255);
143 }
144 else {
145 $data['option'] = array('#type' => 'select', '#options' => $data['option']);
146 }
147 }
148 $views_tables['sorts']['titles']["$name.$field"] = $data['name'];
149 $views_tables['sorts']['base']["$name.$field"] = $data;
150 }
151 }
152 }
d06152e9 153 cache_set("views_tables:$locale", 'cache_views', serialize($views_tables));
f4f02cb1
EM
154 }
155 }
156 return ($full ? $views_tables : $views_tables['tables']);
157}
158
159/**
160 * Gets the filter information; if it doesn't exist, call the function
161 * that constructs all that.
162 */
163function _views_get_filters($titles = false) {
164 $table_data = _views_get_tables(true);
165 return ($titles ? $table_data['filters']['titles'] : $table_data['filters']['base']);
166}
167
168/**
169 * Gets the field information; if it doesn't exist, call the function
170 * that constructs all that.
171 */
172function _views_get_fields($titles = false) {
173 $table_data = _views_get_tables(true);
174 return ($titles ? $table_data['fields']['titles'] : $table_data['fields']['base']);
175}
176
177/**
178 * Gets the sort information; if it doesn't exist, call the function
179 * that constructs all that.
180 */
181function _views_get_sorts($titles = false) {
182 $table_data = _views_get_tables(true);
183 return ($titles ? $table_data['sorts']['titles'] : $table_data['sorts']['base']);
184}
185
186/**
187 * Ensures that views have legitimate information; a bit more is stored on
188 * the $view object than is in the database, and this isn't necessarily
189 * set when a view is constructed externally.
190 */
191function views_sanitize_view(&$view) {
192 _views_check_arrays($view); // so reference works.
193 foreach ($view->field as $i => $field) {
194 if (!isset($view->field[$i]['id'])) {
195 $view->field[$i]['id'] = $view->field[$i]['fullname'] = "$field[tablename].$field[field]";
196 $view->field[$i]['queryname'] = "$field[tablename]_$field[field]";
197 }
198 }
199
200 foreach ($view->filter as $i => $filter) {
201 if (!isset($view->filter[$i]['id'])) {
202 $view->filter[$i]['id'] = $view->filter[$i]['field'] = "$filter[tablename].$filter[field]";
203 }
204 }
205
206 foreach ($view->exposed_filter as $i => $exposed_filter) {
207 if (!isset($view->exposed_filter[$i]['id'])) {
208 $view->exposed_filter[$i]['id'] = $view->exposed_filter[$i]['field'] = "$exposed_filter[tablename].$exposed_filter[field]";
209 }
210 }
211
212 foreach ($view->sort as $i => $sort) {
213 if (!isset($view->sort[$i]['id'])) {
214 $view->sort[$i]['id'] = $view->sort[$i]['field'] = "$sort[tablename].$sort[field]";
215 }
216 }
217
218 foreach ($view->argument as $i => $argument) {
219 if (!isset($view->argument[$i]['id'])) {
220 $view->argument[$i]['id'] = $view->argument[$i]['type'];
221 }
222 }
223}
224
225/**
226 * Build default view information from all modules and cache it.
227 */
228function _views_get_default_views() {
229 static $views_default_views;
230 global $locale;
231
232 if (!$views_default_views) {
d06152e9 233 $data = cache_get("views_default_views:$locale", 'cache_views');
f4f02cb1
EM
234 $cache = unserialize($data->data);
235
236 if (is_array($cache)) {
237 $views_default_views = $cache;
238 }
239 else {
240 // We have to make sure table data is built in order to be sure about providers.
241 $tables = array_keys(_views_get_tables());
242
243 $views = module_invoke_all('views_default_views');
118515fd 244 uasort($views, '_views_sort_arrays');
f4f02cb1
EM
245 $views_default_views = array();
246 foreach ($views as $i => $view) {
247 if (!is_array($view->requires) || !array_diff($view->requires, $tables)) {
248 views_sanitize_view($view);
4c6e47e0 249 $view->is_default = TRUE;
f4f02cb1
EM
250 $views_default_views[$i] = $view;
251 }
252 }
d06152e9 253 cache_set("views_default_views:$locale", 'cache_views', serialize($views_default_views));
f4f02cb1
EM
254 }
255 }
256 return $views_default_views;
257}
258
259/**
118515fd
EM
260 * sort callback
261 */
262function _views_sort_arrays($a, $b) {
4192f54a
EM
263 $a = (array) $a; // safety -- something send objects.
264 $b = (array) $b;
118515fd
EM
265 if ($a['weight'] == $b['weight']) {
266 if ($a['name'] == $b['name']) {
267 return 0;
268 }
269 return ($a['name'] < $b['name']) ? -1 : 1;
270 }
271 return ($a['weight'] > $b['weight']) ? -1 : 1;
272}
273
2a0f225c 274function _views_get_query(&$view, $args, $filters) {
d06152e9 275 if ($view->is_cacheable && ($cached = cache_get('views_query:' . $view->name, 'cache_views'))) {
02305141
EM
276 $info = unserialize($cached->data);
277
278 $plugins = _views_get_style_plugins();
279 if ($plugins[$view->type]['needs_table_header']) {
280 $view->table_header = _views_construct_header($view, _views_get_fields());
281 }
282 }
283 else {
284 views_load_query();
2a0f225c 285 $info = _views_build_query($view, $args, $filters);
55d9cec4 286 if ($view->is_cacheable) {
69d05b64 287 cache_set('views_query:' . $view->name, 'cache_views', serialize($info));
02305141
EM
288 }
289 }
290
291 // Run-time replacement so we can do cacheing
292 $replacements = module_invoke_all('views_query_substitutions', $view);
293 foreach ($replacements as $src => $dest) {
294 $info['query'] = str_replace($src, $dest, $info['query']);
295 $info['countquery'] = str_replace($src, $dest, $info['countquery']);
296
297 if (is_array($info['args'])) {
298 foreach ($info['args'] as $id => $arg) {
299 $info['args'][$id] = str_replace($src, $dest, $arg);
300 }
301 }
302 }
303
304 return $info;
305}
306
118515fd 307/**
f4f02cb1
EM
308 * Return the style plugins; construct one if we haven't already. The
309 * array is cached in a static variable so that arguments
310 * are only constructed once per run.
311 */
312function _views_get_style_plugins($titles = false) {
313 static $views_style_plugins;
314 global $locale;
315
316 if (!$views_style_plugins) {
d06152e9 317 $data = cache_get("views_style_plugins:$locale", 'cache_views');
f4f02cb1
EM
318 $cache = unserialize($data->data);
319 if (is_array($cache)) {
320 $views_style_plugins = $cache;
321 }
322 else {
323 $arguments = module_invoke_all('views_style_plugins');
118515fd 324 uasort($arguments, '_views_sort_arrays');
f4f02cb1
EM
325 foreach ($arguments as $name => $arg) {
326 if (!isset($arg['summary_theme'])) {
327 $arg['summary_theme'] = 'views_summary';
328 }
329 $views_style_plugins['title'][$name] = $arg['name'];
330 $views_style_plugins['base'][$name] = $arg;
331 }
332 $cache = $views_style_plugins;
d06152e9 333 cache_set("views_style_plugins:$locale", 'cache_views', serialize($cache));
f4f02cb1
EM
334 }
335 }
336 return ($titles ? $views_style_plugins['title'] : $views_style_plugins['base']);
69d05b64 337}