Projet

Général

Profil

Paste
Télécharger (13,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / includes / plugins.inc @ 64156087

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains helper code for plugins and contexts.
6
 */
7

    
8
/**
9
 * Determine if a pane is visible.
10
 *
11
 * @param $pane
12
 *   The pane object to test for access.
13
 * @param $display
14
 *   The display object containing the pane object to be tested.
15
 */
16
function panels_pane_access($pane, $display) {
17
  ctools_include('context');
18
  return ctools_access($pane->access, $display->context);
19
}
20

    
21
/**
22
 * Get a list of panels available in the layout.
23
 */
24
function panels_get_regions($layout, $display) {
25
  if ($function = ctools_plugin_get_function($layout, 'regions function')) {
26
    return $function($display, $display->layout_settings, $layout);
27
  }
28

    
29
  if (!empty($layout['regions'])) {
30
    return $layout['regions'];
31
  }
32

    
33
  return array();
34
}
35

    
36
/**
37
 * Get cached content for a given display and possibly pane.
38
 *
39
 * @return
40
 *   The cached content, or FALSE to indicate no cached content exists.
41
 */
42
function panels_get_cached_content($display, $args, $context, $pane = NULL) {
43
  // Never use cache on a POST.
44
  if (!empty($_POST)) {
45
    return FALSE;
46
  }
47

    
48
  $method = $pane ? $pane->cache['method'] : $display->cache['method'];
49
  $function = panels_plugin_get_function('cache', $method, 'cache get');
50

    
51
  if (!$function) {
52
    return FALSE;
53
  }
54

    
55
  $conf = $pane ? $pane->cache['settings'] : $display->cache['settings'];
56
  $cache = $function($conf, $display, $args, $context, $pane);
57
  if (empty($cache)) {
58
    return FALSE;
59
  }
60

    
61
  // Restore it.
62
  $cache->restore();
63
  return $cache;
64
}
65

    
66
/**
67
 * Store cached content for a given display and possibly pane.
68
 */
69
function panels_set_cached_content($cache, $display, $args, $context, $pane = NULL) {
70
  // Never use cache on a POST.
71
  if (!empty($_POST)) {
72
    return FALSE;
73
  }
74

    
75
  $method = $pane ? $pane->cache['method'] : $display->cache['method'];
76
  $function = panels_plugin_get_function('cache', $method, 'cache set');
77

    
78
  if (!$function) {
79
    return FALSE;
80
  }
81

    
82
  $conf = $pane ? $pane->cache['settings'] : $display->cache['settings'];
83

    
84
  // Snapshot it.
85
  $cache->cache();
86
  return $function($conf, $cache, $display, $args, $context, $pane);
87
}
88

    
89
/**
90
 * Clear all cached content for a display.
91
 */
92
function panels_clear_cached_content($display) {
93
  // Figure out every method we might be using to cache content in this display:
94
  $methods = array();
95
  if (!empty($display->cache['method'])) {
96
    $methods[$display->cache['method']] = TRUE;
97
  }
98

    
99
  foreach ($display->content as $pane) {
100
    if (!empty($pane->cache['method'])) {
101
      $methods[$pane->cache['method']] = TRUE;
102
    }
103
  }
104

    
105
  foreach (array_keys($methods) as $method) {
106
    $function = panels_plugin_get_function('cache', $method, 'cache clear');
107
    if ($function) {
108
      $function($display);
109
    }
110
  }
111
}
112

    
113
/**
114
 * An object to hold caching information while it is happening.
115
 */
116
class panels_cache_object {
117
  var $content = '';
118
  var $head = NULL;
119
  var $css = NULL;
120
  var $js = NULL;
121
  var $tokens = NULL;
122
  var $ready = FALSE;
123

    
124
  /**
125
   * When constructed, take a snapshot of our existing out of band data.
126
   */
127
  function __construct() {
128
    $this->head = drupal_add_html_head();
129
    $this->css = drupal_add_css();
130
    $this->tokens = ctools_set_page_token();
131
    $this->js = drupal_add_js();
132
  }
133

    
134
  /**
135
   * Add content to the cache. This assumes a pure stream;
136
   * use set_content() if it's something else.
137
   */
138
  function add_content($content) {
139
    $this->content .= $content;
140
  }
141

    
142
  function set_content($content) {
143
    $this->content = $content;
144
  }
145

    
146
  /**
147
   * Set the object for storing. This overwrites.
148
   */
149
  function cache() {
150
    if ($this->ready) {
151
      return;
152
    }
153

    
154
    $this->ready = TRUE;
155

    
156
    // Simple replacement for head.
157
    $this->head = str_replace($this->head, '', drupal_add_html_head());
158

    
159
    // Slightly less simple for CSS:
160
    $css = drupal_add_css();
161
    $start = $this->css;
162
    $this->css = array();
163

    
164
    foreach ($css as $name => $data) {
165
      if (!isset($start[$name])) {
166
        $this->css[$name] = $data;
167
      }
168
    }
169

    
170
    $js = drupal_add_js();
171

    
172
    $start = $this->js;
173
    $this->js = array();
174

    
175
    // Use the advanced mapping function from Drupal >= 7.23 if available.
176
    $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
177

    
178
    // If there are any differences between the old and the new javascript then
179
    // store them to be added later.
180
    if ($diff = $array_mapping_func($js, $start)) {
181
      // Iterate over the diff to ensure we keep the keys on merge and don't add
182
      // unnecessary items.
183
      foreach ($diff as $key => $diff_data) {
184
        // Special case the settings key and get the difference of the data.
185
        if ($key === 'settings') {
186
          // Iterate over the diff to ensure we keep the keys on merge and don't
187
          // add unnecessary items.
188
          if (isset($diff[$key]['data'])) {
189
            foreach ($diff[$key]['data'] as $settings_key => $settings_data) {
190
              // Merge the changes with the base to get a complete settings
191
              // array.
192
              $this->js[$key]['data'][] = drupal_array_merge_deep($settings_data, $diff[$key]['data'][$settings_key]);
193
            }
194
          }
195
        }
196
        else {
197
          $this->js[$key] = $diff_data;
198
          // Check if the key was present already and if so merge the changes
199
          // with the original data to get the full settings array.
200
          if (isset($start[$key])) {
201
            $this->js[$key] = drupal_array_merge_deep($start[$key], $this->js[$key]);
202
          }
203
        }
204
      }
205
    }
206

    
207
    // And for tokens:
208
    $tokens = ctools_set_page_token();
209
    foreach ($this->tokens as $token => $argument) {
210
      if (isset($tokens[$token])) {
211
        unset($tokens[$token]);
212
      }
213
    }
214

    
215
    $this->tokens = $tokens;
216
  }
217

    
218
  /**
219
   * Restore out of band data saved to cache.
220
   */
221
  function restore() {
222
    if (!empty($this->head)) {
223
      drupal_add_html_head($this->head);
224
    }
225
    if (!empty($this->css)) {
226
      foreach ($this->css as $args) {
227
        drupal_add_css($args['data'], $args);
228
      }
229
    }
230
    if (!empty($this->js)) {
231
      foreach ($this->js as $key => $args) {
232
        if ($key !== 'settings') {
233
          drupal_add_js($args['data'], $args);
234
        }
235
        else {
236
          foreach ($args['data'] as $setting) {
237
            drupal_add_js($setting, 'setting');
238
          }
239
        }
240
      }
241
    }
242

    
243
    if (!empty($this->tokens)) {
244
      foreach ($this->tokens as $token => $key) {
245
        list($type, $argument) = $key;
246
        ctools_set_page_token($token, $type, $argument);
247
      }
248
    }
249
  }
250

    
251
}
252

    
253
/**
254
 * Get the title of a pane.
255
 *
256
 * @deprecated @todo this function should be removed.
257
 *
258
 * @param $pane
259
 *   The $pane object.
260
 */
261
function panels_get_pane_title(&$pane, $context = array(), $incoming_content = NULL) {
262
  ctools_include('content');
263
  return ctools_content_admin_title($pane->type, $pane->subtype, $pane->configuration, $context);
264
}
265

    
266
/**
267
 * Fetch metadata on a specific layout plugin.
268
 *
269
 * @param $layout
270
 *   Name of a panel layout. If the layout name contains a ':' this
271
 *   indicates that we need to separate the sublayout out and
272
 *   load it individually.
273
 *
274
 * @return
275
 *   An array with information about the requested panel layout.
276
 */
277
function panels_get_layout($layout) {
278
  ctools_include('plugins');
279
  return ctools_get_plugins('panels', 'layouts', $layout);
280
}
281

    
282
/**
283
 * Fetch metadata for all layout plugins.
284
 *
285
 * @return
286
 *   An array of arrays with information about all available panel layouts.
287
 */
288
function panels_get_layouts() {
289
  ctools_include('plugins');
290
  return ctools_get_plugins('panels', 'layouts');
291
}
292

    
293
/**
294
 * Fetch metadata for all layout plugins that provide builders.
295
 *
296
 * The layout builders allow reusable layouts be stored in the database and
297
 * exported. Since there are different methods, we are not limiting this
298
 * to just one plugin.
299
 *
300
 * @return
301
 *   An array of arrays with information about panel layouts with builders.
302
 */
303
function panels_get_layout_builders() {
304
  ctools_include('plugins');
305
  $plugins = ctools_get_plugins('panels', 'layouts');
306
  $builders = array();
307
  foreach ($plugins as $name => $plugin) {
308
    if (!empty($plugin['builder'])) {
309
      $builders[$name] = $plugin;
310
    }
311
  }
312

    
313
  return $builders;
314
}
315

    
316
/**
317
 * Fetch metadata on a specific style plugin.
318
 *
319
 * @param $style
320
 *   Name of a panel style.
321
 *
322
 * @return
323
 *   An array with information about the requested panel style.
324
 */
325
function panels_get_style($style) {
326
  ctools_include('plugins');
327
  return ctools_get_plugins('panels', 'styles', $style);
328
}
329

    
330
/**
331
 * Fetch metadata for all style plugins.
332
 *
333
 * @return
334
 *   An array of arrays with information about all available panel styles.
335
 */
336
function panels_get_styles() {
337
  ctools_include('plugins');
338
  return ctools_get_plugins('panels', 'styles');
339
}
340

    
341
/**
342
 * Fetch metadata on a specific caching plugin.
343
 *
344
 * @param $cache
345
 *   Name of a panel cache.
346
 *
347
 * @return
348
 *   An array with information about the requested panel cache.
349
 */
350
function panels_get_cache($cache) {
351
  ctools_include('plugins');
352
  return ctools_get_plugins('panels', 'cache', $cache);
353
}
354

    
355
/**
356
 * Fetch metadata for all context plugins.
357
 *
358
 * @return
359
 *   An array of arrays with information about all available panel caches.
360
 */
361
function panels_get_caches() {
362
  ctools_include('plugins');
363
  return ctools_get_plugins('panels', 'cache');
364
}
365

    
366
/**
367
 * Fetch metadata on a specific display renderer plugin.
368
 *
369
 * @return
370
 *   An array of arrays with information about the requested panels display
371
 *   renderer.
372
 */
373
function panels_get_display_renderer($renderer) {
374
  ctools_include('plugins');
375
  return ctools_get_plugins('panels', 'display_renderers', $renderer);
376
}
377

    
378
/**
379
 * Fetch metadata for all display renderer plugins.
380
 *
381
 * @return
382
 *   An array of arrays with information about all available panels display
383
 *   renderer.
384
 */
385
function panels_get_display_renderers() {
386
  ctools_include('plugins');
387
  return ctools_get_plugins('panels', 'display_renderers');
388
}
389

    
390
/**
391
 * Get and initialize the class to handle rendering a display.
392
 *
393
 * @return
394
 *   Either the instantiated renderer or FALSE if one could not be found.
395
 */
396
function panels_get_renderer_handler($plugin, &$display) {
397
  if (is_string($plugin)) {
398
    $plugin = panels_get_display_renderer($plugin);
399
  }
400

    
401
  $class = ctools_plugin_get_class($plugin, 'renderer');
402
  if ($class) {
403
    $renderer = new $class();
404
    $renderer->init($plugin, $display);
405
    return $renderer;
406
  }
407

    
408
  return FALSE;
409
}
410

    
411
/**
412
 * Choose a renderer for a display based on a render pipeline setting.
413
 */
414
function panels_get_renderer($pipeline_name, &$display) {
415
  // Load the pipeline.
416
  ctools_include('export');
417
  $pipeline = ctools_export_crud_load('panels_renderer_pipeline', $pipeline_name);
418

    
419
  // If we can't, or it has no renderers, default.
420
  if (!$pipeline || empty($pipeline->settings['renderers'])) {
421
    return panels_get_renderer_handler('standard', $display);
422
  }
423

    
424
  // Get contexts set on the pipeline:
425
  $contexts = array();
426
  if (!empty($pipeline->settings['contexts'])) {
427
    $contexts = ctools_context_load_contexts($pipeline->settings['context']);
428
  }
429

    
430
  // Cycle through our renderers and see.
431
  foreach ($pipeline->settings['renderers'] as $candidate) {
432
    // See if this passes selection criteria.
433
    if (!ctools_access($candidate['access'], $contexts)) {
434
      continue;
435
    }
436

    
437
    $renderer = panels_get_renderer_handler($candidate['renderer'], $display);
438

    
439
    if (!empty($candidate['options'])) {
440
      $renderer->set_options($candidate['options']);
441
    }
442

    
443
    return $renderer;
444
  }
445

    
446
  // Fall through. If no renderer is selected, use the standard renderer.
447
  return panels_get_renderer_handler('standard', $display);
448
}
449

    
450
/**
451
 * Sort callback for sorting renderer pipelines.
452
 *
453
 * Sort first by weight, then by title.
454
 */
455
function _panels_renderer_pipeline_sort($a, $b) {
456
  if ($a->weight == $b->weight) {
457
    if ($a->admin_title == $b->admin_title) {
458
      return 0;
459
    }
460
    return ($a->admin_title < $b->admin_title) ? -1 : 1;
461
  }
462
  return ($a->weight < $b->weight) ? -1 : 1;
463
}
464

    
465
/**
466
 * Get a list of available renderer pipelines.
467
 *
468
 * This can be used to form a select or radios widget by enabling
469
 * sorting. Descriptions are left in.
470
 */
471
function panels_get_renderer_pipelines($sort = TRUE) {
472
  ctools_include('export');
473
  $pipelines = ctools_export_crud_load_all('panels_renderer_pipeline');
474
  if ($sort) {
475
    uasort($pipelines, '_panels_renderer_pipeline_sort');
476
  }
477

    
478
  return $pipelines;
479
}
480

    
481
/**
482
 * Fetch metadata on a specific panels_storage plugin.
483
 *
484
 * @param $storage
485
 *   Name of a panel_storage plugin.
486
 *
487
 * @return
488
 *   An array with information about the requested panels_storage plugin
489
 */
490
function panels_get_panels_storage_plugin($storage) {
491
  ctools_include('plugins');
492
  return ctools_get_plugins('panels', 'panels_storage', $storage);
493
}
494

    
495
/**
496
 * Fetch metadata for all panels_storage plugins.
497
 *
498
 * @return
499
 *   An array of arrays with information about all available panels_storage plugins.
500
 */
501
function panels_get_panels_storage_plugins() {
502
  ctools_include('plugins');
503
  return ctools_get_plugins('panels', 'panels_storage');
504
}
505

    
506
/**
507
 * Get a function from a plugin, if it exists.
508
 *
509
 * @param $plugin
510
 *   The type of plugin
511
 * @param $which
512
 *   Either the loaded plugin object (or the same data in array form)
513
 *   or a string with the name of the desired the specific plugin.
514
 * @param $function_name
515
 *   The identifier of the function. For example, 'settings form'.
516
 *
517
 * @return
518
 *   The actual name of the function to call, or NULL if the function
519
 *   does not exist.
520
 *
521
 * @deprecated All calls to this function should be removed.
522
 */
523
function panels_plugin_get_function($plugin, $which, $function_name) {
524
  ctools_include('plugins');
525
  if (is_object($which) || is_array($which)) {
526
    return ctools_plugin_get_function($which, $function_name);
527
  }
528
  else {
529
    return ctools_plugin_load_function('panels', $plugin, $which, $function_name);
530
  }
531
}