Projet

Général

Profil

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

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

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
    // Ensure that the 'subtype_id' value exists.
159
    if (!isset($subtype['subtype_id'])) {
160
      $subtype['subtype_id'] = $id;
161
    }
162

    
163
    // Use exact name since this is a modify by reference.
164
    ctools_content_prepare_subtype($subtypes[$id], $plugin);
165
  }
166

    
167
  $cache[$plugin['name']] = $subtypes;
168

    
169
  return $subtypes;
170
}
171

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

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

    
209
  if ($subtype) {
210
    ctools_content_prepare_subtype($subtype, $plugin);
211
  }
212
  return $subtype;
213
}
214

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

    
225
  // Trigger hook_ctools_content_subtype_alter().
226
  drupal_alter('ctools_content_subtype', $subtype, $plugin);
227
}
228

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

    
274
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
275

    
276
  $function = ctools_plugin_get_function($subtype_info, 'render callback');
277
  if (!$function) {
278
    $function = ctools_plugin_get_function($plugin, 'render callback');
279
  }
280

    
281
  if ($function) {
282
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
283
    if ($pane_context === FALSE) {
284
      return;
285
    }
286

    
287
    $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
288

    
289
    if (empty($content)) {
290
      return;
291
    }
292

    
293
    // Set up some defaults and other massaging on the content before we hand
294
    // it back to the caller.
295
    if (!isset($content->type)) {
296
      $content->type = $plugin['name'];
297
    }
298

    
299
    if (!isset($content->subtype)) {
300
      $content->subtype = $subtype;
301
    }
302

    
303
    // Override the title if configured to
304
    if (!empty($conf['override_title'])) {
305
      // Give previous title as an available substitution here.
306
      $keywords['%title'] = empty($content->title) ? '' : $content->title;
307
      $content->original_title = $keywords['%title'];
308
      $content->title = $conf['override_title_text'];
309
      $content->title_heading = isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2';
310
    }
311

    
312
    if (!empty($content->title)) {
313
      // Perform substitutions
314
      if (!empty($keywords) || !empty($context)) {
315
        $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
316
      }
317

    
318
      // Sterilize the title
319
      $content->title = filter_xss_admin($content->title);
320

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

    
335
    return $content;
336
  }
337
}
338

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

    
350
  $function = FALSE;
351
  
352
  if (!empty($subtype['check editable'])) {
353
    $function = ctools_plugin_get_function($subtype, 'check editable');
354
  }
355
  elseif (!empty($type['check editable'])) {
356
    $function = ctools_plugin_get_function($type, 'check editable');
357
  }
358

    
359
  if ($function) {
360
    return $function($type, $subtype, $conf);
361
  }
362

    
363
  return TRUE;
364
}
365

    
366
/**
367
 * Get the administrative title from a given content type.
368
 *
369
 * @param $type
370
 *   The content type. May be the name or an already loaded content type object.
371
 * @param $subtype
372
 *   The subtype being rendered.
373
 * @param $conf
374
 *   The configuration for the content type.
375
 * @param $context
376
 *   An array of context objects available for use. These may be placeholders.
377
 */
378
function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) {
379
  if (is_array($type)) {
380
    $plugin = $type;
381
  }
382
  else if (is_string($type)) {
383
    $plugin = ctools_get_content_type($type);
384
  }
385
  else {
386
    return;
387
  }
388

    
389
  if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
390
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
391
    if ($pane_context === FALSE) {
392
      if ($plugin['name'] == $subtype) {
393
        return t('@type will not display due to missing context', array('@type' => $plugin['name']));
394
      }
395
      return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
396
    }
397

    
398
    return $function($subtype, $conf, $pane_context);
399
  }
400
  else if (isset($plugin['admin title'])) {
401
    return $plugin['admin title'];
402
  }
403
  else if (isset($plugin['title'])) {
404
    return $plugin['title'];
405
  }
406
}
407

    
408
/**
409
 * Get the proper icon path to use, falling back to default icons if no icon exists.
410
 *
411
 * $subtype
412
 *   The loaded subtype info.
413
 */
414
function ctools_content_admin_icon($subtype) {
415
  $icon = '';
416

    
417
  if (isset($subtype['icon'])) {
418
    $icon = $subtype['icon'];
419
    if (!file_exists($icon)) {
420
      $icon = $subtype['path'] . '/' . $icon;
421
    }
422
  }
423

    
424
  if (empty($icon) || !file_exists($icon)) {
425
    $icon = ctools_image_path('no-icon.png');
426
  }
427

    
428
  return $icon;
429
}
430

    
431
/**
432
 * Set up the default $conf for a new instance of a content type.
433
 */
434
function ctools_content_get_defaults($plugin, $subtype) {
435
  if (isset($plugin['defaults'])) {
436
    $defaults = $plugin['defaults'];
437
  }
438
  else if (isset($subtype['defaults'])) {
439
    $defaults = $subtype['defaults'];
440
  }
441
  if (isset($defaults)) {
442
    if (is_string($defaults) && function_exists($defaults)) {
443
      if ($return = $defaults($pane)) {
444
        return $return;
445
      }
446
    }
447
    else if (is_array($defaults)) {
448
      return $defaults;
449
    }
450
  }
451

    
452
  return array();
453
}
454

    
455
/**
456
 * Get the administrative title from a given content type.
457
 *
458
 * @param $type
459
 *   The content type. May be the name or an already loaded content type object.
460
 * @param $subtype
461
 *   The subtype being rendered.
462
 * @param $conf
463
 *   The configuration for the content type.
464
 * @param $context
465
 *   An array of context objects available for use. These may be placeholders.
466
 */
467
function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) {
468
  if (is_array($type)) {
469
    $plugin = $type;
470
  }
471
  else {
472
    $plugin = ctools_get_content_type($type);
473
  }
474

    
475
  if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
476
    $output = $function($subtype, $conf, $context);
477
  }
478

    
479
  if (empty($output) || !is_object($output)) {
480
    $output = new stdClass();
481
    // replace the _ with " " for a better output
482
    $subtype = check_plain(str_replace("_", " ", $subtype));
483
    $output->title = $subtype;
484
    $output->content = t('No info available.');
485
  }
486
  return $output;
487
}
488

    
489
/**
490
 * Add the default FAPI elements to the content type configuration form
491
 */
492
function ctools_content_configure_form_defaults($form, &$form_state) {
493
  $plugin = $form_state['plugin'];
494
  $subtype = $form_state['subtype'];
495
  $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
496
  $conf = $form_state['conf'];
497

    
498
  $add_submit = FALSE;
499
  if (!empty($subtype['required context']) && is_array($contexts)) {
500
    $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
501
    $add_submit = TRUE;
502
  }
503

    
504
  ctools_include('dependent');
505

    
506
  // Unless we're not allowed to override the title on this content type, add this
507
  // gadget to all panes.
508
  if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
509
    $form['aligner_start'] = array(
510
      '#markup' => '<div class="option-text-aligner clearfix">',
511
    );
512
    $form['override_title'] = array(
513
      '#type' => 'checkbox',
514
      '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
515
      '#title' => t('Override title'),
516
      '#id' => 'override-title-checkbox',
517
    );
518
    $form['override_title_text'] = array(
519
      '#type' => 'textfield',
520
      '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
521
      '#size' => 35,
522
      '#id' => 'override-title-textfield',
523
      '#dependency' => array('override-title-checkbox' => array(1)),
524
      '#dependency_type' => 'hidden',
525
    );
526
    $form['override_title_heading'] = array(
527
      '#type' => 'select',
528
      '#default_value' => isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2',
529
      '#options' => array(
530
        'h1' => t('h1'),
531
        'h2' => t('h2'),
532
        'h3' => t('h3'),
533
        'h4' => t('h4'),
534
        'h5' => t('h5'),
535
        'h6' => t('h6'),
536
        'div' => t('div'),
537
        'span' => t('span'),
538
      ),
539
      '#id' => 'override-title-heading',
540
      '#dependency' => array('override-title-checkbox' => array(1)),
541
      '#dependency_type' => 'hidden',
542
    );
543

    
544
    $form['aligner_stop'] = array(
545
      '#markup' => '</div>',
546
    );
547
    if (is_array($contexts)) {
548
      $form['override_title_markup'] = array(
549
        '#prefix' => '<div class="description">',
550
        '#suffix' => '</div>',
551
        '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
552
      );
553
    }
554
    $add_submit = TRUE;
555
  }
556

    
557
  if ($add_submit) {
558
    // '#submit' is already set up due to the wizard.
559
    $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
560
  }
561
  return $form;
562
}
563

    
564
/**
565
 * Submit handler to store context/title override info.
566
 */
567
function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
568
  if (isset($form_state['values']['context'])) {
569
    $form_state['conf']['context'] = $form_state['values']['context'];
570
  }
571
  if (isset($form_state['values']['override_title'])) {
572
    $form_state['conf']['override_title'] = $form_state['values']['override_title'];
573
    $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
574
    $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading'];
575
  }
576
}
577

    
578
/**
579
 * Get the config form.
580
 *
581
 * The $form_info and $form_state need to be preconfigured with data you'll need
582
 * such as whether or not you're using ajax, or the modal. $form_info will need
583
 * your next/submit callbacks so that you can cache your data appropriately.
584
 *
585
 * @return
586
 *   If this function returns false, no form exists.
587
 */
588
function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
589
  $form_state += array(
590
    'plugin' => $plugin,
591
    'subtype' => $subtype,
592
    'subtype_name' => $subtype_name,
593
    'conf' => &$conf,
594
    'op' => $op,
595
  );
596

    
597
  $form_info += array(
598
    'id' => 'ctools_content_form',
599
    'show back' => TRUE,
600
  );
601

    
602
  // Turn the forms defined in the plugin into the format the wizard needs.
603
  if ($op == 'add') {
604
    if (!empty($subtype['add form'])) {
605
      _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
606
    }
607
    else if (!empty($plugin['add form'])) {
608
      _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
609
    }
610
  }
611

    
612
  if (empty($form_info['order'])) {
613
    // Use the edit form for the add form if add form was completely left off.
614
    if (!empty($subtype['edit form'])) {
615
      _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
616
    }
617
    else if (!empty($plugin['edit form'])) {
618
      _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
619
    }
620
  }
621

    
622
  if (empty($form_info['order'])) {
623
    return FALSE;
624
  }
625

    
626
  ctools_include('wizard');
627
  return ctools_wizard_multistep_form($form_info, $step, $form_state);
628

    
629
}
630

    
631
function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
632
  if (is_string($info)) {
633
    if (empty($subtype['title'])) {
634
      $title = t('Configure');
635
    }
636
    else if ($op == 'add') {
637
      $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
638
    }
639
    else {
640
      $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
641
    }
642
    $form_info['order'] = array('form' => $title);
643
    $form_info['forms'] = array(
644
      'form' => array(
645
        'title' => $title,
646
        'form id' => $info,
647
        'wrapper' => 'ctools_content_configure_form_defaults',
648
      ),
649
    );
650
  }
651
  else if (is_array($info)) {
652
    $form_info['order'] = array();
653
    $form_info['forms'] = array();
654
    $count = 0;
655
    $base = 'step';
656
    $wrapper = NULL;
657
    foreach ($info as $form_id => $title) {
658
      // @todo -- docs say %title can be used to sub for the admin title.
659
      $step = $base . ++$count;
660
      if (empty($wrapper)) {
661
        $wrapper = $step;
662
      }
663

    
664
      if (is_array($title)) {
665
        if (!empty($title['default'])) {
666
          $wrapper = $step;
667
        }
668
        $title = $title['title'];
669
      }
670

    
671
      $form_info['order'][$step] = $title;
672
      $form_info['forms'][$step] = array(
673
        'title' => $title,
674
        'form id' => $form_id,
675
      );
676
    }
677
    if ($wrapper) {
678
      $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
679
    }
680
  }
681
}
682

    
683
/**
684
 * Get an array of all available content types that can be fed into the
685
 * display editor for the add content list.
686
 *
687
 * @param $context
688
 *   If a context is provided, content that requires that context can apepar.
689
 * @param $has_content
690
 *   Whether or not the display will have incoming content
691
 * @param $allowed_types
692
 *   An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
693
 * @param $default_types
694
 *   A default allowed/denied status for content that isn't known about
695
 */
696
function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
697
  $plugins = ctools_get_content_types();
698
  $available = array();
699

    
700
  foreach ($plugins as $id => $plugin) {
701
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
702
      // exclude items that require content if we're saying we don't
703
      // provide it.
704
      if (!empty($subtype['requires content']) && !$has_content) {
705
        continue;
706
      }
707

    
708
      // Check to see if the content type can be used in this context.
709
      if (!empty($subtype['required context'])) {
710
        if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
711
          continue;
712
        }
713
      }
714

    
715
      // Check to see if the passed-in allowed types allows this content.
716
      if ($allowed_types) {
717
        $key = $id . '-' . $subtype_id;
718
        if (!isset($allowed_types[$key])) {
719
          $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
720
        }
721
        if (!$allowed_types[$key]) {
722
          continue;
723
        }
724
      }
725

    
726
      // Check if the content type provides an access callback.
727
      if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
728
        continue;
729
      }
730

    
731
      // If we made it through all the tests, then we can use this content.
732
      $available[$id][$subtype_id] = $subtype;
733
    }
734
  }
735
  return $available;
736
}
737

    
738
/**
739
 * Get an array of all content types that can be fed into the
740
 * display editor for the add content list, regardless of
741
 * availability.
742
 *
743
 */
744
function ctools_content_get_all_types() {
745
  $plugins = ctools_get_content_types();
746
  $available = array();
747

    
748
  foreach ($plugins as $id => $plugin) {
749
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
750
      // If we made it through all the tests, then we can use this content.
751
      $available[$id][$subtype_id] = $subtype;
752
    }
753
  }
754
  return $available;
755
}
756

    
757
/**
758
 * Select the context to be used for a piece of content, based upon config.
759
 *
760
 * @param $plugin
761
 *   The content plugin
762
 * @param $subtype
763
 *   The subtype of the content.
764
 * @param $conf
765
 *   The configuration array that should contain the context.
766
 * @param $contexts
767
 *   A keyed array of available contexts.
768
 *
769
 * @return
770
 *   The matching contexts or NULL if none or necessary, or FALSE if
771
 *   requirements can't be met.
772
 */
773
function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
774
  // Identify which of our possible contexts apply.
775
  if (empty($subtype)) {
776
    return;
777
  }
778

    
779
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
780
  if (empty($subtype_info)) {
781
    return;
782
  }
783

    
784
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
785
    return $contexts;
786
  }
787

    
788
  // If the content requires a context, fetch it; if no context is returned,
789
  // do not display the pane.
790
  if (empty($subtype_info['required context'])) {
791
    return;
792
  }
793

    
794
  // Deal with dynamic required contexts not getting updated in the panes.
795
  // For example, Views let you dynamically change context info. While
796
  // we cannot be perfect, one thing we can do is if no context at all
797
  // was asked for, and then was later added but none is selected, make
798
  // a best guess as to what context should be used. THis is right more
799
  // than it's wrong.
800
  if (is_array($subtype_info['required context'])) {
801
    if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
802
      foreach ($subtype_info['required context'] as $index => $required) {
803
        if (!isset($conf['context'][$index])) {
804
          $filtered = ctools_context_filter($contexts, $required);
805
          if ($filtered) {
806
            $keys = array_keys($filtered);
807
            $conf['context'][$index] = array_shift($keys);
808
          }
809
        }
810
      }
811
    }
812
  }
813
  else {
814
    if (empty($conf['context'])) {
815
      $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
816
      if ($filtered) {
817
        $keys = array_keys($filtered);
818
        $conf['context'] = array_shift($keys);
819
      }
820
    }
821
  }
822

    
823
  if (empty($conf['context'])) {
824
    return;
825
  }
826

    
827
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
828

    
829
  return $context;
830
}
831

    
832
/**
833
 * Fetch a piece of content from the addressable content system.
834
 *
835
 * @param $address
836
 *   A string or an array representing the address of the content.
837
 * @param $type
838
 *   The type of content to return. The type is dependent on what
839
 *   the content actually is. The default, 'content' means a simple
840
 *   string representing the content. However, richer systems may
841
 *   offer more options. For example, Panels might allow the
842
 *   fetching of 'display' and 'pane' objects. Page Manager
843
 *   might allow for the fetching of 'task_handler' objects
844
 *   (AKA variants).
845
 */
846
function ctools_get_addressable_content($address, $type = 'content') {
847
  if (!is_array($address)) {
848
    $address = explode('::', $address);
849
  }
850

    
851
  if (!$address) {
852
    return;
853
  }
854

    
855
  // This removes the module from the address so the
856
  // implementor is not responsible for that part.
857
  $module = array_shift($address);
858
  return module_invoke($module, 'addressable_content', $address, $type);
859
}