Projet

Général

Profil

Paste
Télécharger (25,4 ko) Statistiques
| Branche: | Révision:

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

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
 *   - title_heading: The title heading.
248
 *   - content: The safe to render HTML content.
249
 *   - links: An array of links associated with the content suitable for
250
 *     theme('links').
251
 *   - more: An optional 'more' link (destination only)
252
 *   - admin_links: Administrative links associated with the content, suitable
253
 *     for theme('links').
254
 *   - feeds: An array of feed icons or links associated with the content.
255
 *     Each member of the array is rendered HTML.
256
 *   - type: The content type.
257
 *   - subtype: The content subtype. These two may be used together as
258
 *     module-delta for block style rendering.
259
 */
260
function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
261
  if (is_array($type)) {
262
    $plugin = $type;
263
  }
264
  else {
265
    $plugin = ctools_get_content_type($type);
266
  }
267

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

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

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

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

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

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

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

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

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

    
312
      // Sterilize the title
313
      $content->title = filter_xss_admin($content->title);
314

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

    
329
    return $content;
330
  }
331
}
332

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

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

    
348
  return TRUE;
349
}
350

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

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

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

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

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

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

    
413
  return $icon;
414
}
415

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

    
437
  return array();
438
}
439

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

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

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

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

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

    
489
  ctools_include('dependent');
490

    
491
  // Unless we're not allowed to override the title on this content type, add this
492
  // gadget to all panes.
493
  if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
494
    $form['aligner_start'] = array(
495
      '#markup' => '<div class="option-text-aligner clearfix">',
496
    );
497
    $form['override_title'] = array(
498
      '#type' => 'checkbox',
499
      '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
500
      '#title' => t('Override title'),
501
      '#id' => 'override-title-checkbox',
502
    );
503
    $form['override_title_text'] = array(
504
      '#type' => 'textfield',
505
      '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
506
      '#size' => 35,
507
      '#id' => 'override-title-textfield',
508
      '#dependency' => array('override-title-checkbox' => array(1)),
509
      '#dependency_type' => 'hidden',
510
    );
511
    $form['override_title_heading'] = array(
512
      '#type' => 'select',
513
      '#default_value' => isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2',
514
      '#options' => array(
515
        'h1' => t('h1'),
516
        'h2' => t('h2'),
517
        'h3' => t('h3'),
518
        'h4' => t('h4'),
519
        'h5' => t('h5'),
520
        'h6' => t('h6'),
521
        'div' => t('div'),
522
        'span' => t('span'),
523
      ),
524
      '#id' => 'override-title-heading',
525
      '#dependency' => array('override-title-checkbox' => array(1)),
526
      '#dependency_type' => 'hidden',
527
    );
528

    
529
    $form['aligner_stop'] = array(
530
      '#markup' => '</div>',
531
    );
532
    if (is_array($contexts)) {
533
      $form['override_title_markup'] = array(
534
        '#prefix' => '<div class="description">',
535
        '#suffix' => '</div>',
536
        '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
537
      );
538
    }
539
    $add_submit = TRUE;
540
  }
541

    
542
  if ($add_submit) {
543
    // '#submit' is already set up due to the wizard.
544
    $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
545
  }
546
  return $form;
547
}
548

    
549
/**
550
 * Submit handler to store context/title override info.
551
 */
552
function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
553
  if (isset($form_state['values']['context'])) {
554
    $form_state['conf']['context'] = $form_state['values']['context'];
555
  }
556
  if (isset($form_state['values']['override_title'])) {
557
    $form_state['conf']['override_title'] = $form_state['values']['override_title'];
558
    $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
559
    $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading'];
560
  }
561
}
562

    
563
/**
564
 * Get the config form.
565
 *
566
 * The $form_info and $form_state need to be preconfigured with data you'll need
567
 * such as whether or not you're using ajax, or the modal. $form_info will need
568
 * your next/submit callbacks so that you can cache your data appropriately.
569
 *
570
 * @return
571
 *   If this function returns false, no form exists.
572
 */
573
function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
574
  $form_state += array(
575
    'plugin' => $plugin,
576
    'subtype' => $subtype,
577
    'subtype_name' => $subtype_name,
578
    'conf' => &$conf,
579
    'op' => $op,
580
  );
581

    
582
  $form_info += array(
583
    'id' => 'ctools_content_form',
584
    'show back' => TRUE,
585
  );
586

    
587
  // Turn the forms defined in the plugin into the format the wizard needs.
588
  if ($op == 'add') {
589
    if (!empty($subtype['add form'])) {
590
      _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
591
    }
592
    else if (!empty($plugin['add form'])) {
593
      _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
594
    }
595
  }
596

    
597
  if (empty($form_info['order'])) {
598
    // Use the edit form for the add form if add form was completely left off.
599
    if (!empty($subtype['edit form'])) {
600
      _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
601
    }
602
    else if (!empty($plugin['edit form'])) {
603
      _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
604
    }
605
  }
606

    
607
  if (empty($form_info['order'])) {
608
    return FALSE;
609
  }
610

    
611
  ctools_include('wizard');
612
  return ctools_wizard_multistep_form($form_info, $step, $form_state);
613

    
614
}
615

    
616
function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
617
  if (is_string($info)) {
618
    if (empty($subtype['title'])) {
619
      $title = t('Configure');
620
    }
621
    else if ($op == 'add') {
622
      $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
623
    }
624
    else {
625
      $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
626
    }
627
    $form_info['order'] = array('form' => $title);
628
    $form_info['forms'] = array(
629
      'form' => array(
630
        'title' => $title,
631
        'form id' => $info,
632
        'wrapper' => 'ctools_content_configure_form_defaults',
633
      ),
634
    );
635
  }
636
  else if (is_array($info)) {
637
    $form_info['order'] = array();
638
    $form_info['forms'] = array();
639
    $count = 0;
640
    $base = 'step';
641
    $wrapper = NULL;
642
    foreach ($info as $form_id => $title) {
643
      // @todo -- docs say %title can be used to sub for the admin title.
644
      $step = $base . ++$count;
645
      if (empty($wrapper)) {
646
        $wrapper = $step;
647
      }
648

    
649
      if (is_array($title)) {
650
        if (!empty($title['default'])) {
651
          $wrapper = $step;
652
        }
653
        $title = $title['title'];
654
      }
655

    
656
      $form_info['order'][$step] = $title;
657
      $form_info['forms'][$step] = array(
658
        'title' => $title,
659
        'form id' => $form_id,
660
      );
661
    }
662
    if ($wrapper) {
663
      $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
664
    }
665
  }
666
}
667

    
668
/**
669
 * Get an array of all available content types that can be fed into the
670
 * display editor for the add content list.
671
 *
672
 * @param $context
673
 *   If a context is provided, content that requires that context can apepar.
674
 * @param $has_content
675
 *   Whether or not the display will have incoming content
676
 * @param $allowed_types
677
 *   An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
678
 * @param $default_types
679
 *   A default allowed/denied status for content that isn't known about
680
 */
681
function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
682
  $plugins = ctools_get_content_types();
683
  $available = array();
684

    
685
  foreach ($plugins as $id => $plugin) {
686
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
687
      // exclude items that require content if we're saying we don't
688
      // provide it.
689
      if (!empty($subtype['requires content']) && !$has_content) {
690
        continue;
691
      }
692

    
693
      // Check to see if the content type can be used in this context.
694
      if (!empty($subtype['required context'])) {
695
        if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
696
          continue;
697
        }
698
      }
699

    
700
      // Check to see if the passed-in allowed types allows this content.
701
      if ($allowed_types) {
702
        $key = $id . '-' . $subtype_id;
703
        if (!isset($allowed_types[$key])) {
704
          $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
705
        }
706
        if (!$allowed_types[$key]) {
707
          continue;
708
        }
709
      }
710

    
711
      // Check if the content type provides an access callback.
712
      if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
713
        continue;
714
      }
715

    
716
      // If we made it through all the tests, then we can use this content.
717
      $available[$id][$subtype_id] = $subtype;
718
    }
719
  }
720
  return $available;
721
}
722

    
723
/**
724
 * Get an array of all content types that can be fed into the
725
 * display editor for the add content list, regardless of
726
 * availability.
727
 *
728
 */
729
function ctools_content_get_all_types() {
730
  $plugins = ctools_get_content_types();
731
  $available = array();
732

    
733
  foreach ($plugins as $id => $plugin) {
734
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
735
      // If we made it through all the tests, then we can use this content.
736
      $available[$id][$subtype_id] = $subtype;
737
    }
738
  }
739
  return $available;
740
}
741

    
742
/**
743
 * Select the context to be used for a piece of content, based upon config.
744
 *
745
 * @param $plugin
746
 *   The content plugin
747
 * @param $subtype
748
 *   The subtype of the content.
749
 * @param $conf
750
 *   The configuration array that should contain the context.
751
 * @param $contexts
752
 *   A keyed array of available contexts.
753
 *
754
 * @return
755
 *   The matching contexts or NULL if none or necessary, or FALSE if
756
 *   requirements can't be met.
757
 */
758
function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
759
  // Identify which of our possible contexts apply.
760
  if (empty($subtype)) {
761
    return;
762
  }
763

    
764
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
765
  if (empty($subtype_info)) {
766
    return;
767
  }
768

    
769
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
770
    return $contexts;
771
  }
772

    
773
  // If the content requires a context, fetch it; if no context is returned,
774
  // do not display the pane.
775
  if (empty($subtype_info['required context'])) {
776
    return;
777
  }
778

    
779
  // Deal with dynamic required contexts not getting updated in the panes.
780
  // For example, Views let you dynamically change context info. While
781
  // we cannot be perfect, one thing we can do is if no context at all
782
  // was asked for, and then was later added but none is selected, make
783
  // a best guess as to what context should be used. THis is right more
784
  // than it's wrong.
785
  if (is_array($subtype_info['required context'])) {
786
    if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
787
      foreach ($subtype_info['required context'] as $index => $required) {
788
        if (!isset($conf['context'][$index])) {
789
          $filtered = ctools_context_filter($contexts, $required);
790
          if ($filtered) {
791
            $keys = array_keys($filtered);
792
            $conf['context'][$index] = array_shift($keys);
793
          }
794
        }
795
      }
796
    }
797
  }
798
  else {
799
    if (empty($conf['context'])) {
800
      $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
801
      if ($filtered) {
802
        $keys = array_keys($filtered);
803
        $conf['context'] = array_shift($keys);
804
      }
805
    }
806
  }
807

    
808
  if (empty($conf['context'])) {
809
    return;
810
  }
811

    
812
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
813

    
814
  return $context;
815
}
816

    
817
/**
818
 * Fetch a piece of content from the addressable content system.
819
 *
820
 * @param $address
821
 *   A string or an array representing the address of the content.
822
 * @param $type
823
 *   The type of content to return. The type is dependent on what
824
 *   the content actually is. The default, 'content' means a simple
825
 *   string representing the content. However, richer systems may
826
 *   offer more options. For example, Panels might allow the
827
 *   fetching of 'display' and 'pane' objects. Page Manager
828
 *   might allow for the fetching of 'task_handler' objects
829
 *   (AKA variants).
830
 */
831
function ctools_get_addressable_content($address, $type = 'content') {
832
  if (!is_array($address)) {
833
    $address = explode('::', $address);
834
  }
835

    
836
  if (!$address) {
837
    return;
838
  }
839

    
840
  // This removes the module from the address so the
841
  // implementor is not responsible for that part.
842
  $module = array_shift($address);
843
  return module_invoke($module, 'addressable_content', $address, $type);
844
}