| Commit | Line | Data |
|---|---|---|
| db205271 | 1 | <?php |
| db205271 | 2 | |
| db205271 | 3 | /** |
| 26e7b0ef | 4 | * @file |
| db205271 EM |
5 | * |
| 6 | * Contains helper code for plugins and contexts. | |
| 7 | */ | |
| 8 | ||
| 9 | /** | |
| 26e7b0ef | 10 | * Determine if a pane is visible. |
| db205271 EM |
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) { | |
| 26e7b0ef SB |
18 | ctools_include('context'); |
| 19 | return ctools_access($pane->access, $display->context); | |
| db205271 EM |
20 | } |
| 21 | ||
| 22 | /** | |
| 23 | * Get a list of panels available in the layout. | |
| 24 | */ | |
| 946024d2 | 25 | function panels_get_regions($layout, $display) { |
| 2f354acb SB |
26 | if ($function = ctools_plugin_get_function($layout, 'regions function')) { |
| 27 | return $function($display, $display->layout_settings, $layout); | |
| db205271 | 28 | } |
| 2f354acb SB |
29 | |
| 30 | if (!empty($layout['regions'])) { | |
| 31 | return $layout['regions']; | |
| db205271 | 32 | } |
| 2f354acb | 33 | |
| db205271 EM |
34 | return array(); |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| db205271 EM |
38 | * Get cached content for a given display and possibly pane. |
| 39 | * | |
| 40 | * @return | |
| 41 | * The cached content, or FALSE to indicate no cached content exists. | |
| 42 | */ | |
| 43 | function panels_get_cached_content($display, $args, $context, $pane = NULL) { | |
| 26e7b0ef SB |
44 | // Never use cache on a POST |
| 45 | if (!empty($_POST)) { | |
| 46 | return FALSE; | |
| 47 | } | |
| 48 | ||
| db205271 EM |
49 | $method = $pane ? $pane->cache['method'] : $display->cache['method']; |
| 50 | $function = panels_plugin_get_function('cache', $method, 'cache get'); | |
| 51 | ||
| 52 | if (!$function) { | |
| 53 | return FALSE; | |
| 54 | } | |
| 55 | ||
| 56 | $conf = $pane ? $pane->cache['settings'] : $display->cache['settings']; | |
| 57 | $cache = $function($conf, $display, $args, $context, $pane); | |
| 58 | if (empty($cache)) { | |
| 59 | return FALSE; | |
| 60 | } | |
| 61 | ||
| 62 | // restore it. | |
| 63 | $cache->restore(); | |
| 64 | return $cache; | |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Store cached content for a given display and possibly pane. | |
| 69 | */ | |
| 70 | function panels_set_cached_content($cache, $display, $args, $context, $pane = NULL) { | |
| 26e7b0ef SB |
71 | // Never use cache on a POST |
| 72 | if (!empty($_POST)) { | |
| 73 | return FALSE; | |
| 74 | } | |
| 75 | ||
| db205271 EM |
76 | $method = $pane ? $pane->cache['method'] : $display->cache['method']; |
| 77 | $function = panels_plugin_get_function('cache', $method, 'cache set'); | |
| 78 | ||
| 79 | if (!$function) { | |
| 80 | return FALSE; | |
| 81 | } | |
| 82 | ||
| 83 | $conf = $pane ? $pane->cache['settings'] : $display->cache['settings']; | |
| 84 | ||
| 85 | // snapshot it. | |
| 86 | $cache->cache(); | |
| 87 | return $function($conf, $cache, $display, $args, $context, $pane); | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Clear all cached content for a display. | |
| 92 | */ | |
| 93 | function panels_clear_cached_content($display) { | |
| 94 | // Figure out every method we might be using to cache content in this display: | |
| 95 | $methods = array(); | |
| 96 | if (!empty($display->cache['method'])) { | |
| 97 | $methods[$display->cache['method']] = TRUE; | |
| 98 | } | |
| 99 | ||
| 100 | foreach ($display->content as $pane) { | |
| 101 | if (!empty($pane->cache['method'])) { | |
| 102 | $methods[$pane->cache['method']] = TRUE; | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | foreach (array_keys($methods) as $method) { | |
| 107 | $function = panels_plugin_get_function('cache', $method, 'cache clear'); | |
| 108 | if ($function) { | |
| 109 | $function($display); | |
| 110 | } | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * An object to hold caching information while it is happening. | |
| 116 | */ | |
| 117 | class panels_cache_object { | |
| 118 | var $content = ''; | |
| 119 | var $head = NULL; | |
| 120 | var $css = NULL; | |
| 121 | var $js = NULL; | |
| 26e7b0ef | 122 | var $tokens = NULL; |
| db205271 EM |
123 | var $ready = FALSE; |
| 124 | ||
| 125 | /** | |
| 126 | * When constructed, take a snapshot of our existing out of band data. | |
| 127 | */ | |
| 128 | function panels_cache_object() { | |
| 13c4dc14 | 129 | $this->head = drupal_add_html_head(); |
| db205271 | 130 | $this->css = drupal_add_css(); |
| 26e7b0ef | 131 | $this->tokens = ctools_set_page_token(); |
| db205271 EM |
132 | |
| 133 | foreach (array('header', 'footer') as $scope) { | |
| 134 | $this->js[$scope] = drupal_add_js(NULL, NULL, $scope); | |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Add content to the cache. This assumes a pure stream; | |
| 140 | * use set_content() if it's something else. | |
| 141 | */ | |
| 142 | function add_content($content) { | |
| 143 | $this->content .= $content; | |
| 144 | } | |
| 145 | ||
| 146 | function set_content($content) { | |
| 147 | $this->content = $content; | |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * Set the object for storing. This overwrites. | |
| 152 | */ | |
| 153 | function cache() { | |
| 154 | if ($this->ready) { | |
| 155 | return; | |
| 156 | } | |
| 157 | ||
| 158 | $this->ready = TRUE; | |
| 159 | ||
| 160 | // Simple replacement for head | |
| 13c4dc14 | 161 | $this->head = str_replace($this->head, '', drupal_add_html_head()); |
| db205271 EM |
162 | |
| 163 | // Slightly less simple for CSS: | |
| 164 | $css = drupal_add_css(); | |
| 165 | $start = $this->css; | |
| 166 | $this->css = array(); | |
| 167 | ||
| 2ded4244 EM |
168 | foreach ($css as $name => $data) { |
| 169 | if (!isset($start[$name])) { | |
| 170 | $this->css[$name] = array($data['data'], $data['type'], $data['media'], $data['preprocess']); | |
| db205271 EM |
171 | } |
| 172 | } | |
| 173 | ||
| 174 | $js = array(); | |
| 175 | // A little less simple for js | |
| 176 | foreach (array('header', 'footer') as $scope) { | |
| 177 | $js[$scope] = drupal_add_js(NULL, NULL, $scope); | |
| 178 | } | |
| 179 | ||
| 180 | $start = $this->js; | |
| 181 | $this->js = array(); | |
| 182 | ||
| 183 | foreach ($js as $scope => $scopes) { | |
| 184 | foreach ($scopes as $type => $types) { | |
| 185 | foreach ($types as $id => $info) { | |
| 186 | if (!isset($start[$scope][$type][$id])) { | |
| 187 | switch ($type) { | |
| 188 | case 'setting': | |
| 189 | $this->js[] = array($info, $type, $scope); | |
| 190 | break; | |
| 191 | ||
| 192 | case 'inline': | |
| 193 | $this->js[] = array($info['code'], $type, $scope, $info['defer']); | |
| 194 | break; | |
| 195 | ||
| 196 | default: | |
| 197 | $this->js[] = array($id, $type, $scope, $info['defer'], $info['cache']); | |
| 198 | } | |
| 199 | } | |
| 200 | } | |
| 201 | } | |
| 202 | } | |
| 26e7b0ef SB |
203 | |
| 204 | // And for tokens: | |
| 205 | $tokens = ctools_set_page_token(); | |
| 206 | foreach ($this->tokens as $token => $argument) { | |
| 207 | if (isset($tokens[$token])) { | |
| 208 | unset($tokens); | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | $this->tokens = $tokens; | |
| db205271 EM |
213 | } |
| 214 | ||
| 215 | /** | |
| 216 | * Restore out of band data saved to cache. | |
| 217 | */ | |
| 218 | function restore() { | |
| 219 | if (!empty($this->head)) { | |
| 13c4dc14 | 220 | drupal_add_html_head($this->head); |
| db205271 EM |
221 | } |
| 222 | if (!empty($this->css)) { | |
| 223 | foreach ($this->css as $args) { | |
| 224 | call_user_func_array('drupal_add_css', $args); | |
| 225 | } | |
| 226 | } | |
| 227 | if (!empty($this->js)) { | |
| 228 | foreach ($this->js as $args) { | |
| 229 | call_user_func_array('drupal_add_js', $args); | |
| 230 | } | |
| 231 | } | |
| 26e7b0ef SB |
232 | |
| 233 | if (!empty($this->tokens)) { | |
| 234 | foreach ($this->tokens as $token => $key) { | |
| 235 | list($type, $argument) = $key; | |
| 236 | ctools_set_page_token($token, $type, $argument); | |
| 237 | } | |
| 238 | } | |
| db205271 EM |
239 | } |
| 240 | } | |
| 241 | ||
| 242 | /** | |
| 243 | * Get the title of a pane. | |
| 244 | * | |
| 2f354acb SB |
245 | * @deprecated @todo this function should be removed. |
| 246 | * | |
| db205271 EM |
247 | * @param $pane |
| 248 | * The $pane object. | |
| 249 | */ | |
| 250 | function panels_get_pane_title(&$pane, $context = array(), $incoming_content = NULL) { | |
| 26e7b0ef SB |
251 | ctools_include('content'); |
| 252 | return ctools_content_admin_title($pane->type, $pane->subtype, $pane->configuration, $context); | |
| db205271 EM |
253 | } |
| 254 | ||
| 255 | /** | |
| 26e7b0ef SB |
256 | * Fetch metadata on a specific layout plugin. |
| 257 | * | |
| 258 | * @param $layout | |
| 946024d2 SB |
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. | |
| 26e7b0ef SB |
262 | * |
| 263 | * @return | |
| 264 | * An array with information about the requested panel layout. | |
| db205271 | 265 | */ |
| 26e7b0ef SB |
266 | function panels_get_layout($layout) { |
| 267 | ctools_include('plugins'); | |
| 268 | return ctools_get_plugins('panels', 'layouts', $layout); | |
| db205271 EM |
269 | } |
| 270 | ||
| 271 | /** | |
| 26e7b0ef SB |
272 | * Fetch metadata for all layout plugins. |
| 273 | * | |
| 274 | * @return | |
| 275 | * An array of arrays with information about all available panel layouts. | |
| db205271 | 276 | */ |
| 26e7b0ef SB |
277 | function panels_get_layouts() { |
| 278 | ctools_include('plugins'); | |
| 279 | return ctools_get_plugins('panels', 'layouts'); | |
| db205271 EM |
280 | } |
| 281 | ||
| 282 | /** | |
| 946024d2 SB |
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 | /** | |
| 26e7b0ef | 306 | * Fetch metadata on a specific style plugin. |
| db205271 | 307 | * |
| 26e7b0ef SB |
308 | * @param $style |
| 309 | * Name of a panel style. | |
| db205271 EM |
310 | * |
| 311 | * @return | |
| 26e7b0ef | 312 | * An array with information about the requested panel style. |
| db205271 | 313 | */ |
| 26e7b0ef SB |
314 | function panels_get_style($style) { |
| 315 | ctools_include('plugins'); | |
| 316 | return ctools_get_plugins('panels', 'styles', $style); | |
| db205271 EM |
317 | } |
| 318 | ||
| 319 | /** | |
| 26e7b0ef | 320 | * Fetch metadata for all style plugins. |
| db205271 EM |
321 | * |
| 322 | * @return | |
| 26e7b0ef | 323 | * An array of arrays with information about all available panel styles. |
| db205271 | 324 | */ |
| 26e7b0ef SB |
325 | function panels_get_styles() { |
| 326 | ctools_include('plugins'); | |
| 327 | return ctools_get_plugins('panels', 'styles'); | |
| db205271 EM |
328 | } |
| 329 | ||
| db205271 | 330 | /** |
| 26e7b0ef | 331 | * Fetch metadata on a specific caching plugin. |
| db205271 | 332 | * |
| 26e7b0ef SB |
333 | * @param $cache |
| 334 | * Name of a panel cache. | |
| db205271 EM |
335 | * |
| 336 | * @return | |
| 26e7b0ef | 337 | * An array with information about the requested panel cache. |
| db205271 | 338 | */ |
| 26e7b0ef SB |
339 | function panels_get_cache($cache) { |
| 340 | ctools_include('plugins'); | |
| 341 | return ctools_get_plugins('panels', 'cache', $cache); | |
| 342 | } | |
| db205271 EM |
343 | |
| 344 | /** | |
| 26e7b0ef | 345 | * Fetch metadata for all context plugins. |
| db205271 EM |
346 | * |
| 347 | * @return | |
| 26e7b0ef | 348 | * An array of arrays with information about all available panel caches. |
| db205271 | 349 | */ |
| 26e7b0ef SB |
350 | function panels_get_caches() { |
| 351 | ctools_include('plugins'); | |
| 352 | return ctools_get_plugins('panels', 'cache'); | |
| db205271 EM |
353 | } |
| 354 | ||
| 355 | /** | |
| 946024d2 SB |
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 | ||
| 2f354acb | 390 | $class = ctools_plugin_get_class($plugin, 'renderer'); |
| 946024d2 SB |
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) { | |
| 946024d2 SB |
404 | // Load the pipeline |
| 405 | ctools_include('export'); | |
| 406 | $pipeline = ctools_export_crud_load('panels_renderer_pipeline', $pipeline_name); | |
| 407 | ||
| 408 | // If we can't, or it has no renderers, default. | |
| 409 | if (!$pipeline || empty($pipeline->settings['renderers'])) { | |
| 410 | return panels_get_renderer_handler('standard', $display); | |
| 411 | } | |
| 412 | ||
| 413 | // Get contexts set on the pipeline: | |
| 414 | $contexts = array(); | |
| 415 | if (!empty($pipeline->settings['contexts'])) { | |
| 416 | $contexts = ctools_context_load_contexts($pipeline->settings['context']); | |
| 417 | } | |
| 418 | ||
| 419 | // Cycle through our renderers and see. | |
| 420 | foreach ($pipeline->settings['renderers'] as $candidate) { | |
| 421 | // See if this passes selection criteria. | |
| 422 | if (!ctools_access($candidate['access'], $contexts)) { | |
| 423 | continue; | |
| 424 | } | |
| 425 | ||
| 426 | $renderer = panels_get_renderer_handler($candidate['renderer'], $display); | |
| 427 | ||
| 428 | if (!empty($candidate['options'])) { | |
| 429 | $renderer->set_options($candidate['options']); | |
| 430 | } | |
| 431 | ||
| 432 | return $renderer; | |
| 433 | } | |
| 434 | ||
| 435 | // Fall through. If no renderer is selected, use the standard renderer | |
| 436 | return panels_get_renderer_handler('standard', $display); | |
| 437 | } | |
| 438 | ||
| 439 | /** | |
| 440 | * Sort callback for sorting renderer pipelines. | |
| 441 | * | |
| 442 | * Sort first by weight, then by title. | |
| 443 | */ | |
| 444 | function _panels_renderer_pipeline_sort($a, $b) { | |
| 445 | if ($a->weight == $b->weight) { | |
| 446 | if ($a->admin_title == $b->admin_title) { | |
| 447 | return 0; | |
| 448 | } | |
| 449 | return ($a->admin_title < $b->admin_title) ? -1 : 1; | |
| 450 | } | |
| 451 | return ($a->weight < $b->weight) ? -1 : 1; | |
| 452 | } | |
| 453 | ||
| 454 | /** | |
| 455 | * Get a list of available renderer pipelines. | |
| 456 | * | |
| 457 | * This can be used to form a select or radios widget by enabling | |
| 458 | * sorting. Descriptions are left in. | |
| 459 | */ | |
| 460 | function panels_get_renderer_pipelines($sort = TRUE) { | |
| 946024d2 SB |
461 | ctools_include('export'); |
| 462 | $pipelines = ctools_export_crud_load_all('panels_renderer_pipeline'); | |
| 463 | if ($sort) { | |
| 464 | uasort($pipelines, '_panels_renderer_pipeline_sort'); | |
| 465 | } | |
| 466 | ||
| 467 | return $pipelines; | |
| 468 | } | |
| 469 | ||
| 470 | /** | |
| db205271 EM |
471 | * Get a function from a plugin, if it exists. |
| 472 | * | |
| 473 | * @param $plugin | |
| 474 | * The type of plugin | |
| 475 | * @param $which | |
| 476 | * Either the loaded plugin object (or the same data in array form) | |
| 477 | * or a string with the name of the desired the specific plugin. | |
| 478 | * @param $function_name | |
| 479 | * The identifier of the function. For example, 'settings form'. | |
| 480 | * | |
| 481 | * @return | |
| 482 | * The actual name of the function to call, or NULL if the function | |
| 483 | * does not exist. | |
| 2f354acb SB |
484 | * |
| 485 | * @deprecated All calls to this function should be removed. | |
| db205271 EM |
486 | */ |
| 487 | function panels_plugin_get_function($plugin, $which, $function_name) { | |
| 26e7b0ef | 488 | ctools_include('plugins'); |
| db205271 | 489 | if (is_object($which) || is_array($which)) { |
| 26e7b0ef | 490 | return ctools_plugin_get_function($which, $function_name); |
| db205271 EM |
491 | } |
| 492 | else { | |
| 26e7b0ef | 493 | return ctools_plugin_load_function('panels', $plugin, $which, $function_name); |
| db205271 | 494 | } |
| db205271 | 495 | } |