Projet

Général

Profil

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

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

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
  $function = FALSE;
345
  
346
  if (!empty($subtype['check editable'])) {
347
    $function = ctools_plugin_get_function($subtype, 'check editable');
348
  }
349
  elseif (!empty($type['check editable'])) {
350
    $function = ctools_plugin_get_function($type, 'check editable');
351
  }
352

    
353
  if ($function) {
354
    return $function($type, $subtype, $conf);
355
  }
356

    
357
  return TRUE;
358
}
359

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

    
383
  if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
384
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
385
    if ($pane_context === FALSE) {
386
      if ($plugin['name'] == $subtype) {
387
        return t('@type will not display due to missing context', array('@type' => $plugin['name']));
388
      }
389
      return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
390
    }
391

    
392
    return $function($subtype, $conf, $pane_context);
393
  }
394
  else if (isset($plugin['admin title'])) {
395
    return $plugin['admin title'];
396
  }
397
  else if (isset($plugin['title'])) {
398
    return $plugin['title'];
399
  }
400
}
401

    
402
/**
403
 * Get the proper icon path to use, falling back to default icons if no icon exists.
404
 *
405
 * $subtype
406
 *   The loaded subtype info.
407
 */
408
function ctools_content_admin_icon($subtype) {
409
  $icon = '';
410

    
411
  if (isset($subtype['icon'])) {
412
    $icon = $subtype['icon'];
413
    if (!file_exists($icon)) {
414
      $icon = $subtype['path'] . '/' . $icon;
415
    }
416
  }
417

    
418
  if (empty($icon) || !file_exists($icon)) {
419
    $icon = ctools_image_path('no-icon.png');
420
  }
421

    
422
  return $icon;
423
}
424

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

    
446
  return array();
447
}
448

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

    
469
  if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
470
    $output = $function($subtype, $conf, $context);
471
  }
472

    
473
  if (empty($output) || !is_object($output)) {
474
    $output = new stdClass();
475
    // replace the _ with " " for a better output
476
    $subtype = check_plain(str_replace("_", " ", $subtype));
477
    $output->title = $subtype;
478
    $output->content = t('No info available.');
479
  }
480
  return $output;
481
}
482

    
483
/**
484
 * Add the default FAPI elements to the content type configuration form
485
 */
486
function ctools_content_configure_form_defaults($form, &$form_state) {
487
  $plugin = $form_state['plugin'];
488
  $subtype = $form_state['subtype'];
489
  $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
490
  $conf = $form_state['conf'];
491

    
492
  $add_submit = FALSE;
493
  if (!empty($subtype['required context']) && is_array($contexts)) {
494
    $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
495
    $add_submit = TRUE;
496
  }
497

    
498
  ctools_include('dependent');
499

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

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

    
551
  if ($add_submit) {
552
    // '#submit' is already set up due to the wizard.
553
    $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
554
  }
555
  return $form;
556
}
557

    
558
/**
559
 * Submit handler to store context/title override info.
560
 */
561
function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
562
  if (isset($form_state['values']['context'])) {
563
    $form_state['conf']['context'] = $form_state['values']['context'];
564
  }
565
  if (isset($form_state['values']['override_title'])) {
566
    $form_state['conf']['override_title'] = $form_state['values']['override_title'];
567
    $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
568
    $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading'];
569
  }
570
}
571

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

    
591
  $form_info += array(
592
    'id' => 'ctools_content_form',
593
    'show back' => TRUE,
594
  );
595

    
596
  // Turn the forms defined in the plugin into the format the wizard needs.
597
  if ($op == 'add') {
598
    if (!empty($subtype['add form'])) {
599
      _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
600
    }
601
    else if (!empty($plugin['add form'])) {
602
      _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
603
    }
604
  }
605

    
606
  if (empty($form_info['order'])) {
607
    // Use the edit form for the add form if add form was completely left off.
608
    if (!empty($subtype['edit form'])) {
609
      _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
610
    }
611
    else if (!empty($plugin['edit form'])) {
612
      _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
613
    }
614
  }
615

    
616
  if (empty($form_info['order'])) {
617
    return FALSE;
618
  }
619

    
620
  ctools_include('wizard');
621
  return ctools_wizard_multistep_form($form_info, $step, $form_state);
622

    
623
}
624

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

    
658
      if (is_array($title)) {
659
        if (!empty($title['default'])) {
660
          $wrapper = $step;
661
        }
662
        $title = $title['title'];
663
      }
664

    
665
      $form_info['order'][$step] = $title;
666
      $form_info['forms'][$step] = array(
667
        'title' => $title,
668
        'form id' => $form_id,
669
      );
670
    }
671
    if ($wrapper) {
672
      $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
673
    }
674
  }
675
}
676

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

    
694
  foreach ($plugins as $id => $plugin) {
695
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
696
      // exclude items that require content if we're saying we don't
697
      // provide it.
698
      if (!empty($subtype['requires content']) && !$has_content) {
699
        continue;
700
      }
701

    
702
      // Check to see if the content type can be used in this context.
703
      if (!empty($subtype['required context'])) {
704
        if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
705
          continue;
706
        }
707
      }
708

    
709
      // Check to see if the passed-in allowed types allows this content.
710
      if ($allowed_types) {
711
        $key = $id . '-' . $subtype_id;
712
        if (!isset($allowed_types[$key])) {
713
          $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
714
        }
715
        if (!$allowed_types[$key]) {
716
          continue;
717
        }
718
      }
719

    
720
      // Check if the content type provides an access callback.
721
      if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
722
        continue;
723
      }
724

    
725
      // If we made it through all the tests, then we can use this content.
726
      $available[$id][$subtype_id] = $subtype;
727
    }
728
  }
729
  return $available;
730
}
731

    
732
/**
733
 * Get an array of all content types that can be fed into the
734
 * display editor for the add content list, regardless of
735
 * availability.
736
 *
737
 */
738
function ctools_content_get_all_types() {
739
  $plugins = ctools_get_content_types();
740
  $available = array();
741

    
742
  foreach ($plugins as $id => $plugin) {
743
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
744
      // If we made it through all the tests, then we can use this content.
745
      $available[$id][$subtype_id] = $subtype;
746
    }
747
  }
748
  return $available;
749
}
750

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

    
773
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
774
  if (empty($subtype_info)) {
775
    return;
776
  }
777

    
778
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
779
    return $contexts;
780
  }
781

    
782
  // If the content requires a context, fetch it; if no context is returned,
783
  // do not display the pane.
784
  if (empty($subtype_info['required context'])) {
785
    return;
786
  }
787

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

    
817
  if (empty($conf['context'])) {
818
    return;
819
  }
820

    
821
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
822

    
823
  return $context;
824
}
825

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

    
845
  if (!$address) {
846
    return;
847
  }
848

    
849
  // This removes the module from the address so the
850
  // implementor is not responsible for that part.
851
  $module = array_shift($address);
852
  return module_invoke($module, 'addressable_content', $address, $type);
853
}