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 @ fc3d89c3

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 ($function = ctools_plugin_get_function($layout, 'regions function')) {
27
    return $function($display, $display->layout_settings, $layout);
28
  }
29

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

    
34
  return array();
35
}
36

    
37
/**
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) {
44
  // Never use cache on a POST
45
  if (!empty($_POST)) {
46
    return FALSE;
47
  }
48

    
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) {
71
  // Never use cache on a POST
72
  if (!empty($_POST)) {
73
    return FALSE;
74
  }
75

    
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;
122
  var $tokens = NULL;
123
  var $ready = FALSE;
124

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

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

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

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

    
155
    $this->ready = TRUE;
156

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

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

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

    
171
    $js = drupal_add_js();
172

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

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

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

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

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

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

    
244
    if (!empty($this->tokens)) {
245
      foreach ($this->tokens as $token => $key) {
246
        list($type, $argument) = $key;
247
        ctools_set_page_token($token, $type, $argument);
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
}