1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * Contains helper code for plugins and contexts. |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * Determine if a pane is visible. |
---|
11 | * |
---|
12 | * @param $pane |
---|
13 | * The pane object to test for access. |
---|
14 | * @param $display |
---|
15 | * The display object containing the pane object to be tested. |
---|
16 | */ |
---|
17 | function panels_pane_access($pane, $display) { |
---|
18 | ctools_include('context'); |
---|
19 | return ctools_access($pane->access, $display->context); |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * Get a list of panels available in the layout. |
---|
24 | */ |
---|
25 | function panels_get_regions($layout, $display) { |
---|
26 | if (!empty($layout['panels function']) && function_exists($layout['panels function'])) { |
---|
27 | return $layout['panels function']($display, $display->layout_settings, $layout); |
---|
28 | } |
---|
29 | if (!empty($layout['panels'])) { |
---|
30 | return $layout['panels']; |
---|
31 | } |
---|
32 | return array(); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * Get cached content for a given display and possibly pane. |
---|
37 | * |
---|
38 | * @return |
---|
39 | * The cached content, or FALSE to indicate no cached content exists. |
---|
40 | */ |
---|
41 | function panels_get_cached_content($display, $args, $context, $pane = NULL) { |
---|
42 | // Never use cache on a POST |
---|
43 | if (!empty($_POST)) { |
---|
44 | return FALSE; |
---|
45 | } |
---|
46 | |
---|
47 | $method = $pane ? $pane->cache['method'] : $display->cache['method']; |
---|
48 | $function = panels_plugin_get_function('cache', $method, 'cache get'); |
---|
49 | |
---|
50 | if (!$function) { |
---|
51 | return FALSE; |
---|
52 | } |
---|
53 | |
---|
54 | $conf = $pane ? $pane->cache['settings'] : $display->cache['settings']; |
---|
55 | $cache = $function($conf, $display, $args, $context, $pane); |
---|
56 | if (empty($cache)) { |
---|
57 | return FALSE; |
---|
58 | } |
---|
59 | |
---|
60 | // restore it. |
---|
61 | $cache->restore(); |
---|
62 | return $cache; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Store cached content for a given display and possibly pane. |
---|
67 | */ |
---|
68 | function panels_set_cached_content($cache, $display, $args, $context, $pane = NULL) { |
---|
69 | // Never use cache on a POST |
---|
70 | if (!empty($_POST)) { |
---|
71 | return FALSE; |
---|
72 | } |
---|
73 | |
---|
74 | $method = $pane ? $pane->cache['method'] : $display->cache['method']; |
---|
75 | $function = panels_plugin_get_function('cache', $method, 'cache set'); |
---|
76 | |
---|
77 | if (!$function) { |
---|
78 | return FALSE; |
---|
79 | } |
---|
80 | |
---|
81 | $conf = $pane ? $pane->cache['settings'] : $display->cache['settings']; |
---|
82 | |
---|
83 | // snapshot it. |
---|
84 | $cache->cache(); |
---|
85 | return $function($conf, $cache, $display, $args, $context, $pane); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Clear all cached content for a display. |
---|
90 | */ |
---|
91 | function panels_clear_cached_content($display) { |
---|
92 | // Figure out every method we might be using to cache content in this display: |
---|
93 | $methods = array(); |
---|
94 | if (!empty($display->cache['method'])) { |
---|
95 | $methods[$display->cache['method']] = TRUE; |
---|
96 | } |
---|
97 | |
---|
98 | foreach ($display->content as $pane) { |
---|
99 | if (!empty($pane->cache['method'])) { |
---|
100 | $methods[$pane->cache['method']] = TRUE; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | foreach (array_keys($methods) as $method) { |
---|
105 | $function = panels_plugin_get_function('cache', $method, 'cache clear'); |
---|
106 | if ($function) { |
---|
107 | $function($display); |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * An object to hold caching information while it is happening. |
---|
114 | */ |
---|
115 | class panels_cache_object { |
---|
116 | var $content = ''; |
---|
117 | var $head = NULL; |
---|
118 | var $css = NULL; |
---|
119 | var $js = NULL; |
---|
120 | var $tokens = NULL; |
---|
121 | var $ready = FALSE; |
---|
122 | |
---|
123 | /** |
---|
124 | * When constructed, take a snapshot of our existing out of band data. |
---|
125 | */ |
---|
126 | function panels_cache_object() { |
---|
127 | $this->head = drupal_set_html_head(); |
---|
128 | $this->css = drupal_add_css(); |
---|
129 | $this->tokens = ctools_set_page_token(); |
---|
130 | |
---|
131 | foreach (array('header', 'footer') as $scope) { |
---|
132 | $this->js[$scope] = drupal_add_js(NULL, NULL, $scope); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Add content to the cache. This assumes a pure stream; |
---|
138 | * use set_content() if it's something else. |
---|
139 | */ |
---|
140 | function add_content($content) { |
---|
141 | $this->content .= $content; |
---|
142 | } |
---|
143 | |
---|
144 | function set_content($content) { |
---|
145 | $this->content = $content; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Set the object for storing. This overwrites. |
---|
150 | */ |
---|
151 | function cache() { |
---|
152 | if ($this->ready) { |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | $this->ready = TRUE; |
---|
157 | |
---|
158 | // Simple replacement for head |
---|
159 | $this->head = str_replace($this->head, '', drupal_set_html_head()); |
---|
160 | |
---|
161 | // Slightly less simple for CSS: |
---|
162 | $css = drupal_add_css(); |
---|
163 | $start = $this->css; |
---|
164 | $this->css = array(); |
---|
165 | |
---|
166 | foreach ($css as $media => $medias) { |
---|
167 | foreach ($medias as $type => $types) { |
---|
168 | foreach ($types as $path => $preprocess) { |
---|
169 | if (!isset($start[$media][$type][$path])) { |
---|
170 | $this->css[] = array($path, $type, $media, $preprocess); |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | $js = array(); |
---|
177 | // A little less simple for js |
---|
178 | foreach (array('header', 'footer') as $scope) { |
---|
179 | $js[$scope] = drupal_add_js(NULL, NULL, $scope); |
---|
180 | } |
---|
181 | |
---|
182 | $start = $this->js; |
---|
183 | $this->js = array(); |
---|
184 | |
---|
185 | foreach ($js as $scope => $scopes) { |
---|
186 | foreach ($scopes as $type => $types) { |
---|
187 | foreach ($types as $id => $info) { |
---|
188 | if (!isset($start[$scope][$type][$id])) { |
---|
189 | switch ($type) { |
---|
190 | case 'setting': |
---|
191 | $this->js[] = array($info, $type, $scope); |
---|
192 | break; |
---|
193 | |
---|
194 | case 'inline': |
---|
195 | $this->js[] = array($info['code'], $type, $scope, $info['defer']); |
---|
196 | break; |
---|
197 | |
---|
198 | default: |
---|
199 | $this->js[] = array($id, $type, $scope, $info['defer'], $info['cache']); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | // And for tokens: |
---|
207 | $tokens = ctools_set_page_token(); |
---|
208 | foreach ($this->tokens as $token => $argument) { |
---|
209 | if (isset($tokens[$token])) { |
---|
210 | unset($tokens); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | $this->tokens = $tokens; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Restore out of band data saved to cache. |
---|
219 | */ |
---|
220 | function restore() { |
---|
221 | if (!empty($this->head)) { |
---|
222 | drupal_set_html_head($this->head); |
---|
223 | } |
---|
224 | if (!empty($this->css)) { |
---|
225 | foreach ($this->css as $args) { |
---|
226 | call_user_func_array('drupal_add_css', $args); |
---|
227 | } |
---|
228 | } |
---|
229 | if (!empty($this->js)) { |
---|
230 | foreach ($this->js as $args) { |
---|
231 | call_user_func_array('drupal_add_js', $args); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | if (!empty($this->tokens)) { |
---|
236 | foreach ($this->tokens as $token => $key) { |
---|
237 | list($type, $argument) = $key; |
---|
238 | ctools_set_page_token($token, $type, $argument); |
---|
239 | } |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | /** |
---|
245 | * Get the title of a pane. |
---|
246 | * |
---|
247 | * @param $pane |
---|
248 | * The $pane object. |
---|
249 | */ |
---|
250 | function panels_get_pane_title(&$pane, $context = array(), $incoming_content = NULL) { |
---|
251 | ctools_include('content'); |
---|
252 | return ctools_content_admin_title($pane->type, $pane->subtype, $pane->configuration, $context); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Fetch metadata on a specific layout plugin. |
---|
257 | * |
---|
258 | * @param $layout |
---|
259 | * Name of a panel layout. If the layout name contains a ':' this |
---|
260 | * indicates that we need to separate the sublayout out and |
---|
261 | * load it individually. |
---|
262 | * |
---|
263 | * @return |
---|
264 | * An array with information about the requested panel layout. |
---|
265 | */ |
---|
266 | function panels_get_layout($layout) { |
---|
267 | ctools_include('plugins'); |
---|
268 | return ctools_get_plugins('panels', 'layouts', $layout); |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * Fetch metadata for all layout plugins. |
---|
273 | * |
---|
274 | * @return |
---|
275 | * An array of arrays with information about all available panel layouts. |
---|
276 | */ |
---|
277 | function panels_get_layouts() { |
---|
278 | ctools_include('plugins'); |
---|
279 | return ctools_get_plugins('panels', 'layouts'); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * Fetch metadata for all layout plugins that provide builders. |
---|
284 | * |
---|
285 | * The layout builders allow reusable layouts be stored in the database and |
---|
286 | * exported. Since there are different methods, we are not limiting this |
---|
287 | * to just one plugin. |
---|
288 | * |
---|
289 | * @return |
---|
290 | * An array of arrays with information about panel layouts with builders. |
---|
291 | */ |
---|
292 | function panels_get_layout_builders() { |
---|
293 | ctools_include('plugins'); |
---|
294 | $plugins = ctools_get_plugins('panels', 'layouts'); |
---|
295 | $builders = array(); |
---|
296 | foreach ($plugins as $name => $plugin) { |
---|
297 | if (!empty($plugin['builder'])) { |
---|
298 | $builders[$name] = $plugin; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | return $builders; |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * Fetch metadata on a specific style plugin. |
---|
307 | * |
---|
308 | * @param $style |
---|
309 | * Name of a panel style. |
---|
310 | * |
---|
311 | * @return |
---|
312 | * An array with information about the requested panel style. |
---|
313 | */ |
---|
314 | function panels_get_style($style) { |
---|
315 | ctools_include('plugins'); |
---|
316 | return ctools_get_plugins('panels', 'styles', $style); |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * Fetch metadata for all style plugins. |
---|
321 | * |
---|
322 | * @return |
---|
323 | * An array of arrays with information about all available panel styles. |
---|
324 | */ |
---|
325 | function panels_get_styles() { |
---|
326 | ctools_include('plugins'); |
---|
327 | return ctools_get_plugins('panels', 'styles'); |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Fetch metadata on a specific caching plugin. |
---|
332 | * |
---|
333 | * @param $cache |
---|
334 | * Name of a panel cache. |
---|
335 | * |
---|
336 | * @return |
---|
337 | * An array with information about the requested panel cache. |
---|
338 | */ |
---|
339 | function panels_get_cache($cache) { |
---|
340 | ctools_include('plugins'); |
---|
341 | return ctools_get_plugins('panels', 'cache', $cache); |
---|
342 | } |
---|
343 | |
---|
344 | /** |
---|
345 | * Fetch metadata for all context plugins. |
---|
346 | * |
---|
347 | * @return |
---|
348 | * An array of arrays with information about all available panel caches. |
---|
349 | */ |
---|
350 | function panels_get_caches() { |
---|
351 | ctools_include('plugins'); |
---|
352 | return ctools_get_plugins('panels', 'cache'); |
---|
353 | } |
---|
354 | |
---|
355 | /** |
---|
356 | * Fetch metadata on a specific display renderer plugin. |
---|
357 | * |
---|
358 | * @return |
---|
359 | * An array of arrays with information about the requested panels display |
---|
360 | * renderer. |
---|
361 | */ |
---|
362 | function panels_get_display_renderer($renderer) { |
---|
363 | ctools_include('plugins'); |
---|
364 | return ctools_get_plugins('panels', 'display_renderers', $renderer); |
---|
365 | } |
---|
366 | |
---|
367 | /** |
---|
368 | * Fetch metadata for all display renderer plugins. |
---|
369 | * |
---|
370 | * @return |
---|
371 | * An array of arrays with information about all available panels display |
---|
372 | * renderer. |
---|
373 | */ |
---|
374 | function panels_get_display_renderers() { |
---|
375 | ctools_include('plugins'); |
---|
376 | return ctools_get_plugins('panels', 'display_renderers'); |
---|
377 | } |
---|
378 | |
---|
379 | /** |
---|
380 | * Get and initialize the class to handle rendering a display. |
---|
381 | * |
---|
382 | * @return |
---|
383 | * Either the instantiated renderer or FALSE if one could not be found. |
---|
384 | */ |
---|
385 | function panels_get_renderer_handler($plugin, &$display) { |
---|
386 | if (is_string($plugin)) { |
---|
387 | $plugin = panels_get_display_renderer($plugin); |
---|
388 | } |
---|
389 | |
---|
390 | $class = ctools_plugin_get_class($plugin, 'handler'); |
---|
391 | if ($class) { |
---|
392 | $renderer = new $class(); |
---|
393 | $renderer->init($plugin, $display); |
---|
394 | return $renderer; |
---|
395 | } |
---|
396 | |
---|
397 | return FALSE; |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * Choose a renderer for a display based on a render pipeline setting. |
---|
402 | */ |
---|
403 | function panels_get_renderer($pipeline_name, &$display) { |
---|
404 | // If operating in legacy mode, only the legacy renderer is available: |
---|
405 | if (variable_get('panels_legacy_rendering_mode', TRUE)) { |
---|
406 | return panels_get_renderer_handler('legacy', $display); |
---|
407 | } |
---|
408 | |
---|
409 | // Load the pipeline |
---|
410 | ctools_include('export'); |
---|
411 | $pipeline = ctools_export_crud_load('panels_renderer_pipeline', $pipeline_name); |
---|
412 | |
---|
413 | // If we can't, or it has no renderers, default. |
---|
414 | if (!$pipeline || empty($pipeline->settings['renderers'])) { |
---|
415 | return panels_get_renderer_handler('standard', $display); |
---|
416 | } |
---|
417 | |
---|
418 | // Get contexts set on the pipeline: |
---|
419 | $contexts = array(); |
---|
420 | if (!empty($pipeline->settings['contexts'])) { |
---|
421 | $contexts = ctools_context_load_contexts($pipeline->settings['context']); |
---|
422 | } |
---|
423 | |
---|
424 | // Cycle through our renderers and see. |
---|
425 | foreach ($pipeline->settings['renderers'] as $candidate) { |
---|
426 | // See if this passes selection criteria. |
---|
427 | if (!ctools_access($candidate['access'], $contexts)) { |
---|
428 | continue; |
---|
429 | } |
---|
430 | |
---|
431 | $renderer = panels_get_renderer_handler($candidate['renderer'], $display); |
---|
432 | |
---|
433 | if (!empty($candidate['options'])) { |
---|
434 | $renderer->set_options($candidate['options']); |
---|
435 | } |
---|
436 | |
---|
437 | return $renderer; |
---|
438 | } |
---|
439 | |
---|
440 | // Fall through. If no renderer is selected, use the standard renderer |
---|
441 | return panels_get_renderer_handler('standard', $display); |
---|
442 | } |
---|
443 | |
---|
444 | /** |
---|
445 | * Sort callback for sorting renderer pipelines. |
---|
446 | * |
---|
447 | * Sort first by weight, then by title. |
---|
448 | */ |
---|
449 | function _panels_renderer_pipeline_sort($a, $b) { |
---|
450 | if ($a->weight == $b->weight) { |
---|
451 | if ($a->admin_title == $b->admin_title) { |
---|
452 | return 0; |
---|
453 | } |
---|
454 | return ($a->admin_title < $b->admin_title) ? -1 : 1; |
---|
455 | } |
---|
456 | return ($a->weight < $b->weight) ? -1 : 1; |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * Get a list of available renderer pipelines. |
---|
461 | * |
---|
462 | * This can be used to form a select or radios widget by enabling |
---|
463 | * sorting. Descriptions are left in. |
---|
464 | */ |
---|
465 | function panels_get_renderer_pipelines($sort = TRUE) { |
---|
466 | // If operating in legacy mode, only the legacy renderer is available: |
---|
467 | if (variable_get('panels_legacy_rendering_mode', TRUE)) { |
---|
468 | return array(); |
---|
469 | } |
---|
470 | |
---|
471 | ctools_include('export'); |
---|
472 | $pipelines = ctools_export_crud_load_all('panels_renderer_pipeline'); |
---|
473 | if ($sort) { |
---|
474 | uasort($pipelines, '_panels_renderer_pipeline_sort'); |
---|
475 | } |
---|
476 | |
---|
477 | return $pipelines; |
---|
478 | } |
---|
479 | |
---|
480 | /** |
---|
481 | * Get a function from a plugin, if it exists. |
---|
482 | * |
---|
483 | * @param $plugin |
---|
484 | * The type of plugin |
---|
485 | * @param $which |
---|
486 | * Either the loaded plugin object (or the same data in array form) |
---|
487 | * or a string with the name of the desired the specific plugin. |
---|
488 | * @param $function_name |
---|
489 | * The identifier of the function. For example, 'settings form'. |
---|
490 | * |
---|
491 | * @return |
---|
492 | * The actual name of the function to call, or NULL if the function |
---|
493 | * does not exist. |
---|
494 | */ |
---|
495 | function panels_plugin_get_function($plugin, $which, $function_name) { |
---|
496 | ctools_include('plugins'); |
---|
497 | if (is_object($which) || is_array($which)) { |
---|
498 | return ctools_plugin_get_function($which, $function_name); |
---|
499 | } |
---|
500 | else { |
---|
501 | return ctools_plugin_load_function('panels', $plugin, $which, $function_name); |
---|
502 | } |
---|
503 | |
---|
504 | } |
---|
505 | |
---|
506 | // @todo these are DEPRECATED and can probably be removed. |
---|
507 | // These are placeholders to prevent crashes from the former plugins |
---|
508 | class panels_required_context { function filter() { } }; |
---|
509 | class panels_optional_context extends panels_required_context {}; |
---|