Projet

Général

Profil

Paste
Télécharger (24,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / content.inc @ 74f6bef0

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the tools to handle pluggable content that can be used by other
6
 * applications such as Panels or Dashboard.
7
 *
8
 * See the context-content.html file in advanced help for documentation
9
 * of this tool.
10
 */
11

    
12
/**
13
 * Provide defaults for a content type.
14
 *
15
 * Currently we check for automatically named callbacks to make life a little
16
 * easier on the developer.
17
 */
18
function ctools_content_process(&$plugin, $info) {
19
  $function_base = $plugin['module'] . '_' . $plugin['name'] . '_content_type_';
20

    
21
  if (empty($plugin['render callback']) && function_exists($function_base . 'render')) {
22
    $plugin['render callback'] = $function_base . 'render';
23
  }
24

    
25
  if (empty($plugin['admin title'])) {
26
    if (function_exists($function_base . 'admin_title')) {
27
      $plugin['admin title'] = $function_base . 'admin_title';
28
    }
29
    else {
30
      $plugin['admin title'] = $plugin['title'];
31
    }
32
  }
33

    
34
  if (empty($plugin['admin info']) && function_exists($function_base . 'admin_info')) {
35
    $plugin['admin info'] = $function_base . 'admin_info';
36
  }
37

    
38
  if (!isset($plugin['edit form']) && function_exists($function_base . 'edit_form')) {
39
    $plugin['edit form'] = $function_base . 'edit_form';
40
  }
41

    
42
  if (!isset($plugin['add form']) && function_exists($function_base . 'add_form')) {
43
    $plugin['add form'] = $function_base . 'add_form';
44
  }
45

    
46
  if (!isset($plugin['add form']) && function_exists($function_base . 'edit_form')) {
47
    $plugin['add form'] = $function_base . 'edit_form';
48
  }
49

    
50
  if (!isset($plugin['description'])) {
51
    $plugin['description'] = '';
52
  }
53

    
54
  if (!isset($plugin['icon'])) {
55
    $plugin['icon'] = ctools_content_admin_icon($plugin);
56
  }
57

    
58
  // Another ease of use check:
59
  if (!isset($plugin['content types'])) {
60
    // If a subtype plugin exists, try to use it. Otherwise assume single.
61
    if (function_exists($function_base . 'content_types')) {
62
      $plugin['content types'] = $function_base . 'content_types';
63
    }
64
    else {
65
      $type = array(
66
        'title' => $plugin['title'],
67
        'description' => $plugin['description'],
68
        'icon' => ctools_content_admin_icon($plugin),
69
        'category' => $plugin['category'],
70
      );
71

    
72
      if (isset($plugin['required context'])) {
73
        $type['required context'] = $plugin['required context'];
74
      }
75
      if (isset($plugin['top level'])) {
76
        $type['top level'] = $plugin['top level'];
77
      }
78
      $plugin['content types'] = array($plugin['name'] => $type);
79
      if (!isset($plugin['single'])) {
80
        $plugin['single'] = TRUE;
81
      }
82
    }
83
  }
84
}
85

    
86
/**
87
 * Fetch metadata on a specific content_type plugin.
88
 *
89
 * @param $content type
90
 *   Name of a panel content type.
91
 *
92
 * @return
93
 *   An array with information about the requested panel content type.
94
 */
95
function ctools_get_content_type($content_type) {
96
  ctools_include('context');
97
  ctools_include('plugins');
98
  return ctools_get_plugins('ctools', 'content_types', $content_type);
99
}
100

    
101
/**
102
 * Fetch metadata for all content_type plugins.
103
 *
104
 * @return
105
 *   An array of arrays with information about all available panel content types.
106
 */
107
function ctools_get_content_types() {
108
  ctools_include('context');
109
  ctools_include('plugins');
110
  return ctools_get_plugins('ctools', 'content_types');
111
}
112

    
113
/**
114
 * Get all of the individual subtypes provided by a given content type. This
115
 * would be all of the blocks for the block type, or all of the views for
116
 * the view type.
117
 *
118
 * @param $type
119
 *   The content type to load.
120
 *
121
 * @return
122
 *   An array of all subtypes available.
123
 */
124
function ctools_content_get_subtypes($type) {
125
  static $cache = array();
126

    
127
  $subtypes = array();
128

    
129
  if (is_array($type)) {
130
    $plugin = $type;
131
  }
132
  else {
133
    $plugin = ctools_get_content_type($type);
134
  }
135

    
136
  if (empty($plugin) || empty($plugin['name'])) {
137
    return;
138
  }
139

    
140
  if (isset($cache[$plugin['name']])) {
141
    return $cache[$plugin['name']];
142
  }
143

    
144
  if (isset($plugin['content types'])) {
145
    $function = $plugin['content types'];
146
    if (is_array($function)) {
147
      $subtypes = $function;
148
    }
149
    else if (function_exists($function)) {
150
      // Cast to array to prevent errors from non-array returns.
151
      $subtypes = (array) $function($plugin);
152
    }
153
  }
154

    
155
  // Walk through the subtypes and ensure minimal settings are
156
  // retained.
157
  foreach ($subtypes as $id => $subtype) {
158
    // Use exact name since this is a modify by reference.
159
    ctools_content_prepare_subtype($subtypes[$id], $plugin);
160
  }
161

    
162
  $cache[$plugin['name']] = $subtypes;
163

    
164
  return $subtypes;
165
}
166

    
167
/**
168
 * Given a content type and a subtype id, return the information about that
169
 * content subtype.
170
 *
171
 * @param $type
172
 *   The content type being fetched.
173
 * @param $subtype_id
174
 *   The id of the subtype being fetched.
175
 *
176
 * @return
177
 *   An array of information describing the content subtype.
178
 */
179
function ctools_content_get_subtype($type, $subtype_id) {
180
  $subtype = array();
181
  if (is_array($type)) {
182
    $plugin = $type;
183
  }
184
  else {
185
    $plugin = ctools_get_content_type($type);
186
  }
187

    
188
  $function = ctools_plugin_get_function($plugin, 'content type');
189
  if ($function) {
190
    $subtype = $function($subtype_id, $plugin);
191
  }
192
  else {
193
    $subtypes = ctools_content_get_subtypes($type);
194
    if (isset($subtypes[$subtype_id])) {
195
      $subtype = $subtypes[$subtype_id];
196
    }
197
    // If there's only 1 and we somehow have the wrong subtype ID, do not
198
    // care. Return the proper subtype anyway.
199
    if (empty($subtype) && !empty($plugin['single'])) {
200
      $subtype = current($subtypes);
201
    }
202
  }
203

    
204
  if ($subtype) {
205
    ctools_content_prepare_subtype($subtype, $plugin);
206
  }
207
  return $subtype;
208
}
209

    
210
/**
211
 * Ensure minimal required settings on a content subtype exist.
212
 */
213
function ctools_content_prepare_subtype(&$subtype, $plugin) {
214
  foreach (array('path', 'js', 'css') as $key) {
215
    if (!isset($subtype[$key]) && isset($plugin[$key])) {
216
      $subtype[$key] = $plugin[$key];
217
    }
218
  }
219

    
220
  drupal_alter('ctools_content_subtype', $subtype, $plugin);
221
}
222

    
223
/**
224
 * Get the content from a given content type.
225
 *
226
 * @param $type
227
 *   The content type. May be the name or an already loaded content type plugin.
228
 * @param $subtype
229
 *   The name of the subtype being rendered.
230
 * @param $conf
231
 *   The configuration for the content type.
232
 * @param $keywords
233
 *   An array of replacement keywords that come from outside contexts.
234
 * @param $args
235
 *   The arguments provided to the owner of the content type. Some content may
236
 *   wish to configure itself based on the arguments the panel or dashboard
237
 *   received.
238
 * @param $context
239
 *   An array of context objects available for use.
240
 * @param $incoming_content
241
 *   Any incoming content, if this display is a wrapper.
242
 *
243
 * @return
244
 *   The content as rendered by the plugin. This content should be an array
245
 *   with the following possible keys:
246
 *   - title: The safe to render title of the content.
247
 *   - content: The safe to render HTML content.
248
 *   - links: An array of links associated with the content suitable for
249
 *     theme('links').
250
 *   - more: An optional 'more' link (destination only)
251
 *   - admin_links: Administrative links associated with the content, suitable
252
 *     for theme('links').
253
 *   - feeds: An array of feed icons or links associated with the content.
254
 *     Each member of the array is rendered HTML.
255
 *   - type: The content type.
256
 *   - subtype: The content subtype. These two may be used together as
257
 *     module-delta for block style rendering.
258
 */
259
function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
260
  if (is_array($type)) {
261
    $plugin = $type;
262
  }
263
  else {
264
    $plugin = ctools_get_content_type($type);
265
  }
266

    
267
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
268

    
269
  $function = ctools_plugin_get_function($subtype_info, 'render callback');
270
  if (!$function) {
271
    $function = ctools_plugin_get_function($plugin, 'render callback');
272
  }
273

    
274
  if ($function) {
275
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
276
    if ($pane_context === FALSE) {
277
      return;
278
    }
279

    
280
    $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
281

    
282
    if (empty($content)) {
283
      return;
284
    }
285

    
286
    // Set up some defaults and other massaging on the content before we hand
287
    // it back to the caller.
288
    if (!isset($content->type)) {
289
      $content->type = $plugin['name'];
290
    }
291

    
292
    if (!isset($content->subtype)) {
293
      $content->subtype = $subtype;
294
    }
295

    
296
    // Override the title if configured to
297
    if (!empty($conf['override_title'])) {
298
      // Give previous title as an available substitution here.
299
      $keywords['%title'] = empty($content->title) ? '' : $content->title;
300
      $content->original_title = $keywords['%title'];
301
      $content->title = $conf['override_title_text'];
302
    }
303

    
304
    if (!empty($content->title)) {
305
      // Perform substitutions
306
      if (!empty($keywords) || !empty($context)) {
307
        $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
308
      }
309

    
310
      // Sterilize the title
311
      $content->title = filter_xss_admin($content->title);
312

    
313
      // If a link is specified, populate.
314
      if (!empty($content->title_link)) {
315
        if (!is_array($content->title_link)) {
316
          $url = array('href' => $content->title_link);
317
        }
318
        else {
319
          $url = $content->title_link;
320
        }
321
        // set defaults so we don't bring up notices
322
        $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE);
323
        $content->title = l($content->title, $url['href'], $url);
324
      }
325
    }
326

    
327
    return $content;
328
  }
329
}
330

    
331
/**
332
 * Determine if a content type can be edited or not.
333
 *
334
 * Some content types simply have their content and no options. This function
335
 * lets a UI determine if it should display an edit link or not.
336
 */
337
function ctools_content_editable($type, $subtype, $conf) {
338
  if (empty($type['edit form']) && empty($subtype['edit form'])) {
339
    return FALSE;
340
  }
341

    
342
  if ($function = ctools_plugin_get_function($subtype, 'check editable')) {
343
    return $function($type, $subtype, $conf);
344
  }
345

    
346
  return TRUE;
347
}
348

    
349
/**
350
 * Get the administrative title from a given content type.
351
 *
352
 * @param $type
353
 *   The content type. May be the name or an already loaded content type object.
354
 * @param $subtype
355
 *   The subtype being rendered.
356
 * @param $conf
357
 *   The configuration for the content type.
358
 * @param $context
359
 *   An array of context objects available for use. These may be placeholders.
360
 */
361
function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) {
362
  if (is_array($type)) {
363
    $plugin = $type;
364
  }
365
  else if (is_string($type)) {
366
    $plugin = ctools_get_content_type($type);
367
  }
368
  else {
369
    return;
370
  }
371

    
372
  if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
373
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
374
    if ($pane_context === FALSE) {
375
      if ($plugin['name'] == $subtype) {
376
        return t('@type will not display due to missing context', array('@type' => $plugin['name']));
377
      }
378
      return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
379
    }
380

    
381
    return $function($subtype, $conf, $pane_context);
382
  }
383
  else if (isset($plugin['admin title'])) {
384
    return $plugin['admin title'];
385
  }
386
  else if (isset($plugin['title'])) {
387
    return $plugin['title'];
388
  }
389
}
390

    
391
/**
392
 * Get the proper icon path to use, falling back to default icons if no icon exists.
393
 *
394
 * $subtype
395
 *   The loaded subtype info.
396
 */
397
function ctools_content_admin_icon($subtype) {
398
  $icon = '';
399

    
400
  if (isset($subtype['icon'])) {
401
    $icon = $subtype['icon'];
402
    if (!file_exists($icon)) {
403
      $icon = $subtype['path'] . '/' . $icon;
404
    }
405
  }
406

    
407
  if (empty($icon) || !file_exists($icon)) {
408
    $icon = ctools_image_path('no-icon.png');
409
  }
410

    
411
  return $icon;
412
}
413

    
414
/**
415
 * Set up the default $conf for a new instance of a content type.
416
 */
417
function ctools_content_get_defaults($plugin, $subtype) {
418
  if (isset($plugin['defaults'])) {
419
    $defaults = $plugin['defaults'];
420
  }
421
  else if (isset($subtype['defaults'])) {
422
    $defaults = $subtype['defaults'];
423
  }
424
  if (isset($defaults)) {
425
    if (is_string($defaults) && function_exists($defaults)) {
426
      if ($return = $defaults($pane)) {
427
        return $return;
428
      }
429
    }
430
    else if (is_array($defaults)) {
431
      return $defaults;
432
    }
433
  }
434

    
435
  return array();
436
}
437

    
438
/**
439
 * Get the administrative title from a given content type.
440
 *
441
 * @param $type
442
 *   The content type. May be the name or an already loaded content type object.
443
 * @param $subtype
444
 *   The subtype being rendered.
445
 * @param $conf
446
 *   The configuration for the content type.
447
 * @param $context
448
 *   An array of context objects available for use. These may be placeholders.
449
 */
450
function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) {
451
  if (is_array($type)) {
452
    $plugin = $type;
453
  }
454
  else {
455
    $plugin = ctools_get_content_type($type);
456
  }
457

    
458
  if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
459
    $output = $function($subtype, $conf, $context);
460
  }
461

    
462
  if (empty($output) || !is_object($output)) {
463
    $output = new stdClass();
464
    // replace the _ with " " for a better output
465
    $subtype = check_plain(str_replace("_", " ", $subtype));
466
    $output->title = $subtype;
467
    $output->content = t('No info available.');
468
  }
469
  return $output;
470
}
471

    
472
/**
473
 * Add the default FAPI elements to the content type configuration form
474
 */
475
function ctools_content_configure_form_defaults($form, &$form_state) {
476
  $plugin = $form_state['plugin'];
477
  $subtype = $form_state['subtype'];
478
  $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
479
  $conf = $form_state['conf'];
480

    
481
  $add_submit = FALSE;
482
  if (!empty($subtype['required context']) && is_array($contexts)) {
483
    $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
484
    $add_submit = TRUE;
485
  }
486

    
487
  ctools_include('dependent');
488

    
489
  // Unless we're not allowed to override the title on this content type, add this
490
  // gadget to all panes.
491
  if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
492
    $form['aligner_start'] = array(
493
      '#markup' => '<div class="option-text-aligner clearfix">',
494
    );
495
    $form['override_title'] = array(
496
      '#type' => 'checkbox',
497
      '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
498
      '#title' => t('Override title'),
499
      '#id' => 'override-title-checkbox',
500
    );
501
    $form['override_title_text'] = array(
502
      '#type' => 'textfield',
503
      '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
504
      '#size' => 35,
505
      '#id' => 'override-title-textfield',
506
      '#dependency' => array('override-title-checkbox' => array(1)),
507
      '#dependency_type' => 'disable',
508
    );
509
    $form['aligner_stop'] = array(
510
      '#markup' => '</div>',
511
    );
512
    if (is_array($contexts)) {
513
      $form['override_title_markup'] = array(
514
        '#prefix' => '<div class="description">',
515
        '#suffix' => '</div>',
516
        '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
517
      );
518
    }
519
    $add_submit = TRUE;
520
  }
521

    
522
  if ($add_submit) {
523
    // '#submit' is already set up due to the wizard.
524
    $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
525
  }
526
  return $form;
527
}
528

    
529
/**
530
 * Submit handler to store context/title override info.
531
 */
532
function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
533
  if (isset($form_state['values']['context'])) {
534
    $form_state['conf']['context'] = $form_state['values']['context'];
535
  }
536
  if (isset($form_state['values']['override_title'])) {
537
    $form_state['conf']['override_title'] = $form_state['values']['override_title'];
538
    $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
539
  }
540
}
541

    
542
/**
543
 * Get the config form.
544
 *
545
 * The $form_info and $form_state need to be preconfigured with data you'll need
546
 * such as whether or not you're using ajax, or the modal. $form_info will need
547
 * your next/submit callbacks so that you can cache your data appropriately.
548
 *
549
 * @return
550
 *   If this function returns false, no form exists.
551
 */
552
function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
553
  $form_state += array(
554
    'plugin' => $plugin,
555
    'subtype' => $subtype,
556
    'subtype_name' => $subtype_name,
557
    'conf' => &$conf,
558
    'op' => $op,
559
  );
560

    
561
  $form_info += array(
562
    'id' => 'ctools_content_form',
563
    'show back' => TRUE,
564
  );
565

    
566
  // Turn the forms defined in the plugin into the format the wizard needs.
567
  if ($op == 'add') {
568
    if (!empty($subtype['add form'])) {
569
      _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
570
    }
571
    else if (!empty($plugin['add form'])) {
572
      _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
573
    }
574
  }
575

    
576
  if (empty($form_info['order'])) {
577
    // Use the edit form for the add form if add form was completely left off.
578
    if (!empty($subtype['edit form'])) {
579
      _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
580
    }
581
    else if (!empty($plugin['edit form'])) {
582
      _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
583
    }
584
  }
585

    
586
  if (empty($form_info['order'])) {
587
    return FALSE;
588
  }
589

    
590
  ctools_include('wizard');
591
  return ctools_wizard_multistep_form($form_info, $step, $form_state);
592

    
593
}
594

    
595
function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
596
  if (is_string($info)) {
597
    if (empty($subtype['title'])) {
598
      $title = t('Configure');
599
    }
600
    else if ($op == 'add') {
601
      $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
602
    }
603
    else {
604
      $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
605
    }
606
    $form_info['order'] = array('form' => $title);
607
    $form_info['forms'] = array(
608
      'form' => array(
609
        'title' => $title,
610
        'form id' => $info,
611
        'wrapper' => 'ctools_content_configure_form_defaults',
612
      ),
613
    );
614
  }
615
  else if (is_array($info)) {
616
    $form_info['order'] = array();
617
    $form_info['forms'] = array();
618
    $count = 0;
619
    $base = 'step';
620
    $wrapper = NULL;
621
    foreach ($info as $form_id => $title) {
622
      // @todo -- docs say %title can be used to sub for the admin title.
623
      $step = $base . ++$count;
624
      if (empty($wrapper)) {
625
        $wrapper = $step;
626
      }
627

    
628
      if (is_array($title)) {
629
        if (!empty($title['default'])) {
630
          $wrapper = $step;
631
        }
632
        $title = $title['title'];
633
      }
634

    
635
      $form_info['order'][$step] = $title;
636
      $form_info['forms'][$step] = array(
637
        'title' => $title,
638
        'form id' => $form_id,
639
      );
640
    }
641
    if ($wrapper) {
642
      $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
643
    }
644
  }
645
}
646

    
647
/**
648
 * Get an array of all available content types that can be fed into the
649
 * display editor for the add content list.
650
 *
651
 * @param $context
652
 *   If a context is provided, content that requires that context can apepar.
653
 * @param $has_content
654
 *   Whether or not the display will have incoming content
655
 * @param $allowed_types
656
 *   An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
657
 * @param $default_types
658
 *   A default allowed/denied status for content that isn't known about
659
 */
660
function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
661
  $plugins = ctools_get_content_types();
662
  $available = array();
663

    
664
  foreach ($plugins as $id => $plugin) {
665
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
666
      // exclude items that require content if we're saying we don't
667
      // provide it.
668
      if (!empty($subtype['requires content']) && !$has_content) {
669
        continue;
670
      }
671

    
672
      // Check to see if the content type can be used in this context.
673
      if (!empty($subtype['required context'])) {
674
        if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
675
          continue;
676
        }
677
      }
678

    
679
      // Check to see if the passed-in allowed types allows this content.
680
      if ($allowed_types) {
681
        $key = $id . '-' . $subtype_id;
682
        if (!isset($allowed_types[$key])) {
683
          $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
684
        }
685
        if (!$allowed_types[$key]) {
686
          continue;
687
        }
688
      }
689

    
690
      // Check if the content type provides an access callback.
691
      if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
692
        continue;
693
      }
694

    
695
      // If we made it through all the tests, then we can use this content.
696
      $available[$id][$subtype_id] = $subtype;
697
    }
698
  }
699
  return $available;
700
}
701

    
702
/**
703
 * Get an array of all content types that can be fed into the
704
 * display editor for the add content list, regardless of
705
 * availability.
706
 *
707
 */
708
function ctools_content_get_all_types() {
709
  $plugins = ctools_get_content_types();
710
  $available = array();
711

    
712
  foreach ($plugins as $id => $plugin) {
713
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
714
      // If we made it through all the tests, then we can use this content.
715
      $available[$id][$subtype_id] = $subtype;
716
    }
717
  }
718
  return $available;
719
}
720

    
721
/**
722
 * Select the context to be used for a piece of content, based upon config.
723
 *
724
 * @param $plugin
725
 *   The content plugin
726
 * @param $subtype
727
 *   The subtype of the content.
728
 * @param $conf
729
 *   The configuration array that should contain the context.
730
 * @param $contexts
731
 *   A keyed array of available contexts.
732
 *
733
 * @return
734
 *   The matching contexts or NULL if none or necessary, or FALSE if
735
 *   requirements can't be met.
736
 */
737
function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
738
  // Identify which of our possible contexts apply.
739
  if (empty($subtype)) {
740
    return;
741
  }
742

    
743
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
744
  if (empty($subtype_info)) {
745
    return;
746
  }
747

    
748
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
749
    return $contexts;
750
  }
751

    
752
  // If the content requires a context, fetch it; if no context is returned,
753
  // do not display the pane.
754
  if (empty($subtype_info['required context'])) {
755
    return;
756
  }
757

    
758
  // Deal with dynamic required contexts not getting updated in the panes.
759
  // For example, Views let you dynamically change context info. While
760
  // we cannot be perfect, one thing we can do is if no context at all
761
  // was asked for, and then was later added but none is selected, make
762
  // a best guess as to what context should be used. THis is right more
763
  // than it's wrong.
764
  if (is_array($subtype_info['required context'])) {
765
    if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
766
      foreach ($subtype_info['required context'] as $index => $required) {
767
        if (!isset($conf['context'][$index])) {
768
          $filtered = ctools_context_filter($contexts, $required);
769
          if ($filtered) {
770
            $keys = array_keys($filtered);
771
            $conf['context'][$index] = array_shift($keys);
772
          }
773
        }
774
      }
775
    }
776
  }
777
  else {
778
    if (empty($conf['context'])) {
779
      $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
780
      if ($filtered) {
781
        $keys = array_keys($filtered);
782
        $conf['context'] = array_shift($keys);
783
      }
784
    }
785
  }
786

    
787
  if (empty($conf['context'])) {
788
    return;
789
  }
790

    
791
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
792

    
793
  return $context;
794
}
795

    
796
/**
797
 * Fetch a piece of content from the addressable content system.
798
 *
799
 * @param $address
800
 *   A string or an array representing the address of the content.
801
 * @param $type
802
 *   The type of content to return. The type is dependent on what
803
 *   the content actually is. The default, 'content' means a simple
804
 *   string representing the content. However, richer systems may
805
 *   offer more options. For example, Panels might allow the
806
 *   fetching of 'display' and 'pane' objects. Page Manager
807
 *   might allow for the fetching of 'task_handler' objects
808
 *   (AKA variants).
809
 */
810
function ctools_get_addressable_content($address, $type = 'content') {
811
  if (!is_array($address)) {
812
    $address = explode('::', $address);
813
  }
814

    
815
  if (!$address) {
816
    return;
817
  }
818

    
819
  // This removes the module from the address so the
820
  // implementor is not responsible for that part.
821
  $module = array_shift($address);
822
  return module_invoke($module, 'addressable_content', $address, $type);
823
}