Projet

Général

Profil

Paste
Télécharger (26,1 ko) Statistiques
| Branche: | Révision:

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

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 mixed $content
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
    elseif (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
      $subtypes[$id]['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
    // Ensure that the 'subtype_id' value exists. This is also done in
211
    // ctools_content_get_subtypes(), but it wouldn't be called if the plugin
212
    // provides the subtype through its own function.
213
    if (!isset($subtype['subtype_id'])) {
214
      $subtype['subtype_id'] = $subtype_id;
215
    }
216

    
217
    ctools_content_prepare_subtype($subtype, $plugin);
218
  }
219
  return $subtype;
220
}
221

    
222
/**
223
 * Ensure minimal required settings on a content subtype exist.
224
 */
225
function ctools_content_prepare_subtype(&$subtype, $plugin) {
226
  foreach (array('path', 'js', 'css') as $key) {
227
    if (!isset($subtype[$key]) && isset($plugin[$key])) {
228
      $subtype[$key] = $plugin[$key];
229
    }
230
  }
231

    
232
  // Trigger hook_ctools_content_subtype_alter().
233
  drupal_alter('ctools_content_subtype', $subtype, $plugin);
234
}
235

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

    
281
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
282

    
283
  $function = ctools_plugin_get_function($subtype_info, 'render callback');
284
  if (!$function) {
285
    $function = ctools_plugin_get_function($plugin, 'render callback');
286
  }
287

    
288
  if ($function) {
289
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
290
    if ($pane_context === FALSE) {
291
      return;
292
    }
293

    
294
    $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
295

    
296
    if (empty($content)) {
297
      return;
298
    }
299

    
300
    // Set up some defaults and other massaging on the content before we hand
301
    // it back to the caller.
302
    if (!isset($content->type)) {
303
      $content->type = $plugin['name'];
304
    }
305

    
306
    if (!isset($content->subtype)) {
307
      $content->subtype = $subtype;
308
    }
309

    
310
    // Override the title if configured to.
311
    if (!empty($conf['override_title'])) {
312
      // Give previous title as an available substitution here.
313
      $keywords['%title'] = empty($content->title) ? '' : $content->title;
314
      $content->original_title = $keywords['%title'];
315
      $content->title = $conf['override_title_text'];
316
      $content->title_heading = isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2';
317
    }
318

    
319
    if (!empty($content->title)) {
320
      // Perform substitutions.
321
      if (!empty($keywords) || !empty($context)) {
322
        $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
323
      }
324

    
325
      // Sterilize the title.
326
      $content->title = filter_xss_admin($content->title);
327

    
328
      // If a link is specified, populate.
329
      if (!empty($content->title_link)) {
330
        if (!is_array($content->title_link)) {
331
          $url = array('href' => $content->title_link);
332
        }
333
        else {
334
          $url = $content->title_link;
335
        }
336
        // Set defaults so we don't bring up notices.
337
        $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE);
338
        $content->title = l($content->title, $url['href'], $url);
339
      }
340
    }
341

    
342
    return $content;
343
  }
344
}
345

    
346
/**
347
 * Determine if a content type can be edited or not.
348
 *
349
 * Some content types simply have their content and no options. This function
350
 * lets a UI determine if it should display an edit link or not.
351
 */
352
function ctools_content_editable($type, $subtype, $conf) {
353
  if (empty($type['edit form']) && empty($subtype['edit form'])) {
354
    return FALSE;
355
  }
356

    
357
  $function = FALSE;
358

    
359
  if (!empty($subtype['check editable'])) {
360
    $function = ctools_plugin_get_function($subtype, 'check editable');
361
  }
362
  elseif (!empty($type['check editable'])) {
363
    $function = ctools_plugin_get_function($type, 'check editable');
364
  }
365

    
366
  if ($function) {
367
    return $function($type, $subtype, $conf);
368
  }
369

    
370
  return TRUE;
371
}
372

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

    
396
  if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
397
    $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
398
    if ($pane_context === FALSE) {
399
      if ($plugin['name'] == $subtype) {
400
        return t('@type will not display due to missing context', array('@type' => $plugin['name']));
401
      }
402
      return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
403
    }
404

    
405
    return $function($subtype, $conf, $pane_context);
406
  }
407
  elseif (isset($plugin['admin title'])) {
408
    return $plugin['admin title'];
409
  }
410
  elseif (isset($plugin['title'])) {
411
    return $plugin['title'];
412
  }
413
}
414

    
415
/**
416
 * Get the proper icon path to use, falling back to default icons if no icon exists.
417
 *
418
 * $subtype
419
 *   The loaded subtype info.
420
 */
421
function ctools_content_admin_icon($subtype) {
422
  $icon = '';
423

    
424
  if (isset($subtype['icon'])) {
425
    $icon = $subtype['icon'];
426
    if (!file_exists($icon)) {
427
      $icon = $subtype['path'] . '/' . $icon;
428
    }
429
  }
430

    
431
  if (empty($icon) || !file_exists($icon)) {
432
    $icon = ctools_image_path('no-icon.png');
433
  }
434

    
435
  return $icon;
436
}
437

    
438
/**
439
 * Set up the default $conf for a new instance of a content type.
440
 */
441
function ctools_content_get_defaults($plugin, $subtype) {
442
  if (isset($plugin['defaults'])) {
443
    $defaults = $plugin['defaults'];
444
  }
445
  elseif (isset($subtype['defaults'])) {
446
    $defaults = $subtype['defaults'];
447
  }
448
  if (isset($defaults)) {
449
    if (is_string($defaults) && function_exists($defaults)) {
450
      if ($return = $defaults($pane)) {
451
        return $return;
452
      }
453
    }
454
    elseif (is_array($defaults)) {
455
      return $defaults;
456
    }
457
  }
458

    
459
  return array();
460
}
461

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

    
482
  if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
483
    $output = $function($subtype, $conf, $context);
484
  }
485

    
486
  if (empty($output) || !is_object($output)) {
487
    $output = new stdClass();
488
    // Replace the _ with " " for a better output.
489
    $subtype = check_plain(str_replace("_", " ", $subtype));
490
    $output->title = $subtype;
491
    $output->content = t('No info available.');
492
  }
493
  return $output;
494
}
495

    
496
/**
497
 * Add the default FAPI elements to the content type configuration form.
498
 */
499
function ctools_content_configure_form_defaults($form, &$form_state) {
500
  $plugin = $form_state['plugin'];
501
  $subtype = $form_state['subtype'];
502
  $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
503
  $conf = $form_state['conf'];
504

    
505
  $add_submit = FALSE;
506
  if (!empty($subtype['required context']) && is_array($contexts)) {
507
    $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
508
    $add_submit = TRUE;
509
  }
510

    
511
  ctools_include('dependent');
512

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

    
551
    $form['aligner_stop'] = array(
552
      '#markup' => '</div>',
553
    );
554
    if (is_array($contexts)) {
555
      $form['override_title_markup'] = array(
556
        '#prefix' => '<div class="description">',
557
        '#suffix' => '</div>',
558
        '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
559
      );
560
    }
561
    $add_submit = TRUE;
562
  }
563

    
564
  if ($add_submit) {
565
    // '#submit' is already set up due to the wizard.
566
    $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
567
  }
568
  return $form;
569
}
570

    
571
/**
572
 * Submit handler to store context/title override info.
573
 */
574
function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
575
  if (isset($form_state['values']['context'])) {
576
    $form_state['conf']['context'] = $form_state['values']['context'];
577
  }
578
  if (isset($form_state['values']['override_title'])) {
579
    $form_state['conf']['override_title'] = $form_state['values']['override_title'];
580
    $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
581
    $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading'];
582
  }
583
}
584

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

    
604
  $form_info += array(
605
    'id' => 'ctools_content_form',
606
    'show back' => TRUE,
607
  );
608

    
609
  // Turn the forms defined in the plugin into the format the wizard needs.
610
  if ($op == 'add') {
611
    if (!empty($subtype['add form'])) {
612
      _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
613
    }
614
    elseif (!empty($plugin['add form'])) {
615
      _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
616
    }
617
  }
618

    
619
  if (empty($form_info['order'])) {
620
    // Use the edit form for the add form if add form was completely left off.
621
    if (!empty($subtype['edit form'])) {
622
      _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
623
    }
624
    elseif (!empty($plugin['edit form'])) {
625
      _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
626
    }
627
  }
628

    
629
  if (empty($form_info['order'])) {
630
    return FALSE;
631
  }
632

    
633
  ctools_include('wizard');
634
  return ctools_wizard_multistep_form($form_info, $step, $form_state);
635

    
636
}
637

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

    
671
      if (is_array($title)) {
672
        if (!empty($title['default'])) {
673
          $wrapper = $step;
674
        }
675
        $title = $title['title'];
676
      }
677

    
678
      $form_info['order'][$step] = $title;
679
      $form_info['forms'][$step] = array(
680
        'title' => $title,
681
        'form id' => $form_id,
682
      );
683
    }
684
    if ($wrapper) {
685
      $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
686
    }
687
  }
688
}
689

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

    
707
  foreach ($plugins as $id => $plugin) {
708
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
709
      // Exclude items that require content if we're saying we don't
710
      // provide it.
711
      if (!empty($subtype['requires content']) && !$has_content) {
712
        continue;
713
      }
714

    
715
      // Check to see if the content type can be used in this context.
716
      if (!empty($subtype['required context'])) {
717
        if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
718
          continue;
719
        }
720
      }
721

    
722
      // Check to see if the passed-in allowed types allows this content.
723
      if ($allowed_types) {
724
        $key = $id . '-' . $subtype_id;
725
        if (!isset($allowed_types[$key])) {
726
          $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
727
        }
728
        if (!$allowed_types[$key]) {
729
          continue;
730
        }
731
      }
732

    
733
      // Check if the content type provides an access callback.
734
      if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) {
735
        continue;
736
      }
737

    
738
      // If we made it through all the tests, then we can use this content.
739
      $available[$id][$subtype_id] = $subtype;
740
    }
741
  }
742
  return $available;
743
}
744

    
745
/**
746
 * Get an array of all content types that can be fed into the
747
 * display editor for the add content list, regardless of
748
 * availability.
749
 */
750
function ctools_content_get_all_types() {
751
  $plugins = ctools_get_content_types();
752
  $available = array();
753

    
754
  foreach ($plugins as $id => $plugin) {
755
    foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
756
      // If we made it through all the tests, then we can use this content.
757
      $available[$id][$subtype_id] = $subtype;
758
    }
759
  }
760
  return $available;
761
}
762

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

    
785
  $subtype_info = ctools_content_get_subtype($plugin, $subtype);
786
  if (empty($subtype_info)) {
787
    return;
788
  }
789

    
790
  if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
791
    return $contexts;
792
  }
793

    
794
  // If the content requires a context, fetch it; if no context is returned,
795
  // do not display the pane.
796
  if (empty($subtype_info['required context'])) {
797
    return;
798
  }
799

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

    
829
  if (empty($conf['context'])) {
830
    return;
831
  }
832

    
833
  $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
834

    
835
  return $context;
836
}
837

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

    
857
  if (!$address) {
858
    return;
859
  }
860

    
861
  // This removes the module from the address so the
862
  // implementor is not responsible for that part.
863
  $module = array_shift($address);
864
  return module_invoke($module, 'addressable_content', $address, $type);
865
}