Removing translation directories
[project/ctools.git] / includes / content.inc
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 // Another ease of use check:
51 if (!isset($plugin['content types'])) {
52 // If a subtype plugin exists, try to use it. Otherwise assume single.
53 if (function_exists($function_base . 'content_types')) {
54 $plugin['content types'] = $function_base . 'content_types';
55 }
56 else {
57 $type = array(
58 'title' => $plugin['title'],
59 'description' => $plugin['description'],
60 'icon' => ctools_content_admin_icon($plugin),
61 'category' => $plugin['category'],
62 );
63
64 if (isset($plugin['required context'])) {
65 $type['required context'] = $plugin['required context'];
66 }
67 if (isset($plugin['top level'])) {
68 $type['top level'] = $plugin['top level'];
69 }
70 $plugin['content types'] = array($plugin['name'] => $type);
71 if (!isset($plugin['single'])) {
72 $plugin['single'] = TRUE;
73 }
74 }
75 }
76 }
77
78 /**
79 * Fetch metadata on a specific content_type plugin.
80 *
81 * @param $content type
82 * Name of a panel content type.
83 *
84 * @return
85 * An array with information about the requested panel content type.
86 */
87 function ctools_get_content_type($content_type) {
88 ctools_include('context');
89 ctools_include('plugins');
90 return ctools_get_plugins('ctools', 'content_types', $content_type);
91 }
92
93 /**
94 * Fetch metadata for all content_type plugins.
95 *
96 * @return
97 * An array of arrays with information about all available panel content types.
98 */
99 function ctools_get_content_types() {
100 ctools_include('context');
101 ctools_include('plugins');
102 return ctools_get_plugins('ctools', 'content_types');
103 }
104
105 /**
106 * Get all of the individual subtypes provided by a given content type. This
107 * would be all of the blocks for the block type, or all of the views for
108 * the view type.
109 *
110 * @param $type
111 * The content type to load.
112 *
113 * @return
114 * An array of all subtypes available.
115 */
116 function ctools_content_get_subtypes($type) {
117 static $cache = array();
118
119 $subtypes = array();
120
121 if (is_array($type)) {
122 $plugin = $type;
123 }
124 else {
125 $plugin = ctools_get_content_type($type);
126 }
127
128 if (empty($plugin) || empty($plugin['name'])) {
129 return;
130 }
131
132 if (isset($cache[$plugin['name']])) {
133 return $cache[$plugin['name']];
134 }
135
136 if (isset($plugin['content types'])) {
137 $function = $plugin['content types'];
138 if (is_array($function)) {
139 $subtypes = $function;
140 }
141 else if (function_exists($function)) {
142 // Cast to array to prevent errors from non-array returns.
143 $subtypes = (array) $function($plugin);
144 }
145 }
146
147 // Walk through the subtypes and ensure minimal settings are
148 // retained.
149 foreach ($subtypes as $id => $subtype) {
150 // Use exact name since this is a modify by reference.
151 ctools_content_prepare_subtype($subtypes[$id], $plugin);
152 }
153
154 $cache[$plugin['name']] = $subtypes;
155
156 return $subtypes;
157 }
158
159 /**
160 * Given a content type and a subtype id, return the information about that
161 * content subtype.
162 *
163 * @param $type
164 * The content type being fetched.
165 * @param $subtype_id
166 * The id of the subtype being fetched.
167 *
168 * @return
169 * An array of information describing the content subtype.
170 */
171 function ctools_content_get_subtype($type, $subtype_id) {
172 $subtype = array();
173 if (is_array($type)) {
174 $plugin = $type;
175 }
176 else {
177 $plugin = ctools_get_content_type($type);
178 }
179
180 $function = ctools_plugin_get_function($plugin, 'content type');
181 if ($function) {
182 $subtype = $function($subtype_id, $plugin);
183 }
184 else {
185 $subtypes = ctools_content_get_subtypes($type);
186 if (isset($subtypes[$subtype_id])) {
187 $subtype = $subtypes[$subtype_id];
188 }
189 // If there's only 1 and we somehow have the wrong subtype ID, do not
190 // care. Return the proper subtype anyway.
191 if (empty($subtype) && !empty($plugin['single'])) {
192 $subtype = current($subtypes);
193 }
194 }
195
196 if ($subtype) {
197 ctools_content_prepare_subtype($subtype, $plugin);
198 }
199 return $subtype;
200 }
201
202 /**
203 * Ensure minimal required settings on a content subtype exist.
204 */
205 function ctools_content_prepare_subtype(&$subtype, $plugin) {
206 foreach (array('path', 'js', 'css') as $key) {
207 if (!isset($subtype[$key]) && isset($plugin[$key])) {
208 $subtype[$key] = $plugin[$key];
209 }
210 }
211 }
212
213 /**
214 * Get the content from a given content type.
215 *
216 * @param $type
217 * The content type. May be the name or an already loaded content type plugin.
218 * @param $subtype
219 * The name of the subtype being rendered.
220 * @param $conf
221 * The configuration for the content type.
222 * @param $keywords
223 * An array of replacement keywords that come from outside contexts.
224 * @param $args
225 * The arguments provided to the owner of the content type. Some content may
226 * wish to configure itself based on the arguments the panel or dashboard
227 * received.
228 * @param $context
229 * An array of context objects available for use.
230 * @param $incoming_content
231 * Any incoming content, if this display is a wrapper.
232 *
233 * @return
234 * The content as rendered by the plugin. This content should be an array
235 * with the following possible keys:
236 * - title: The safe to render title of the content.
237 * - content: The safe to render HTML content.
238 * - links: An array of links associated with the content suitable for
239 * theme('links').
240 * - more: An optional 'more' link (destination only)
241 * - admin_links: Administrative links associated with the content, suitable
242 * for theme('links').
243 * - feeds: An array of feed icons or links associated with the content.
244 * Each member of the array is rendered HTML.
245 * - type: The content type.
246 * - subtype: The content subtype. These two may be used together as
247 * module-delta for block style rendering.
248 */
249 function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') {
250 if (is_array($type)) {
251 $plugin = $type;
252 }
253 else {
254 $plugin = ctools_get_content_type($type);
255 }
256
257 $subtype_info = ctools_content_get_subtype($plugin, $subtype);
258
259 $function = ctools_plugin_get_function($subtype_info, 'render callback');
260 if (!$function) {
261 $function = ctools_plugin_get_function($plugin, 'render callback');
262 }
263
264 if ($function) {
265 $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
266 if ($pane_context === FALSE) {
267 return;
268 }
269
270 $content = $function($subtype, $conf, $args, $pane_context, $incoming_content);
271
272 // Set up some defaults and other massaging on the content before we hand
273 // it back to the caller.
274 if (!isset($content->type)) {
275 $content->type = $plugin['name'];
276 }
277
278 if (!isset($content->subtype)) {
279 $content->subtype = $subtype;
280 }
281
282 // Override the title if configured to
283 if (!empty($conf['override_title'])) {
284 // Give previous title as an available substitution here.
285 $keywords['%title'] = empty($content->title) ? '' : $content->title;
286 $content->original_title = $keywords['%title'];
287 $content->title = $conf['override_title_text'];
288 }
289
290 if (!empty($content->title)) {
291 // Perform substitutions
292 if (!empty($keywords) || !empty($context)) {
293 $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context);
294 }
295
296 // Sterilize the title
297 $content->title = filter_xss_admin($content->title);
298
299 // If a link is specified, populate.
300 if (!empty($content->title_link)) {
301 if (!is_array($content->title_link)) {
302 $url = array('href' => $content->title_link);
303 }
304 else {
305 $url = $content->title_link;
306 }
307 // set defaults so we don't bring up notices
308 $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE);
309 $content->title = l($content->title, $url['href'], $url);
310 }
311 }
312
313 return $content;
314 }
315 }
316
317 /**
318 * Determine if a content type can be edited or not.
319 *
320 * Some content types simply have their content and no options. This function
321 * lets a UI determine if it should display an edit link or not.
322 */
323 function ctools_content_editable($type, $subtype, $conf) {
324 if (empty($type['edit form']) && empty($subtype['edit form'])) {
325 return FALSE;
326 }
327
328 if ($function = ctools_plugin_get_function($subtype, 'check editable')) {
329 return $function($type, $subtype, $conf);
330 }
331
332 return TRUE;
333 }
334
335 /**
336 * Get the administrative title from a given content type.
337 *
338 * @param $type
339 * The content type. May be the name or an already loaded content type object.
340 * @param $subtype
341 * The subtype being rendered.
342 * @param $conf
343 * The configuration for the content type.
344 * @param $context
345 * An array of context objects available for use. These may be placeholders.
346 */
347 function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) {
348 if (is_array($type)) {
349 $plugin = $type;
350 }
351 else if (is_string($type)) {
352 $plugin = ctools_get_content_type($type);
353 }
354 else {
355 return;
356 }
357
358 if ($function = ctools_plugin_get_function($plugin, 'admin title')) {
359 $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context);
360 if ($pane_context === FALSE) {
361 if ($plugin['name'] == $subtype) {
362 return t('@type will not display due to missing context', array('@type' => $plugin['name']));
363 }
364 return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype));
365 }
366
367 return $function($subtype, $conf, $pane_context);
368 }
369 else if (isset($plugin['admin title'])) {
370 return $plugin['admin title'];
371 }
372 else if (isset($plugin['title'])) {
373 return $plugin['title'];
374 }
375 }
376
377 /**
378 * Get the proper icon path to use, falling back to default icons if no icon exists.
379 *
380 * $subtype
381 * The loaded subtype info.
382 */
383 function ctools_content_admin_icon($subtype) {
384 $icon = '';
385
386 if (isset($subtype['icon'])) {
387 $icon = $subtype['icon'];
388 if (!file_exists($icon)) {
389 $icon = $subtype['path'] . '/' . $icon;
390 }
391 }
392
393 if (empty($icon) || !file_exists($icon)) {
394 $icon = ctools_image_path('no-icon.png');
395 }
396
397 return $icon;
398 }
399
400 /**
401 * Set up the default $conf for a new instance of a content type.
402 */
403 function ctools_content_get_defaults($plugin, $subtype) {
404 if (isset($plugin['defaults'])) {
405 $defaults = $plugin['defaults'];
406 }
407 else if (isset($subtype['defaults'])) {
408 $defaults = $subtype['defaults'];
409 }
410 if (isset($defaults)) {
411 if (is_string($defaults) && function_exists($defaults)) {
412 if ($return = $defaults($pane)) {
413 return $return;
414 }
415 }
416 else if (is_array($defaults)) {
417 return $defaults;
418 }
419 }
420
421 return array();
422 }
423
424 /**
425 * Get the administrative title from a given content type.
426 *
427 * @param $type
428 * The content type. May be the name or an already loaded content type object.
429 * @param $subtype
430 * The subtype being rendered.
431 * @param $conf
432 * The configuration for the content type.
433 * @param $context
434 * An array of context objects available for use. These may be placeholders.
435 */
436 function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) {
437 if (is_array($type)) {
438 $plugin = $type;
439 }
440 else {
441 $plugin = ctools_get_content_type($type);
442 }
443
444 if ($function = ctools_plugin_get_function($plugin, 'admin info')) {
445 $output = $function($subtype, $conf, $context);
446 }
447 if (empty($output) || !is_object($output)) {
448 $output = new stdClass();
449 $output->title = t('No info');
450 $output->content = t('No info available.');
451 }
452 return $output;
453 }
454
455 /**
456 * Add the default FAPI elements to the content type configuration form
457 */
458 function ctools_content_configure_form_defaults($form, &$form_state) {
459 $plugin = $form_state['plugin'];
460 $subtype = $form_state['subtype'];
461 $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL;
462 $conf = $form_state['conf'];
463
464 $add_submit = FALSE;
465 if (!empty($subtype['required context']) && is_array($contexts)) {
466 $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array());
467 $add_submit = TRUE;
468 }
469
470 ctools_include('dependent');
471
472 // Unless we're not allowed to override the title on this content type, add this
473 // gadget to all panes.
474 if (empty($plugin['no title override']) && empty($subtype['no title override'])) {
475 $form['aligner_start'] = array(
476 '#markup' => '<div class="option-text-aligner clearfix">',
477 );
478 $form['override_title'] = array(
479 '#type' => 'checkbox',
480 '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '',
481 '#title' => t('Override title'),
482 '#id' => 'override-title-checkbox',
483 );
484 $form['override_title_text'] = array(
485 '#type' => 'textfield',
486 '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '',
487 '#size' => 35,
488 '#id' => 'override-title-textfield',
489 '#process' => array('ctools_dependent_process'),
490 '#dependency' => array('override-title-checkbox' => array(1)),
491 '#dependency_type' => 'disable',
492 );
493 $form['aligner_stop'] = array(
494 '#markup' => '</div>',
495 );
496 if (is_array($contexts)) {
497 $form['override_title_markup'] = array(
498 '#prefix' => '<div class="description">',
499 '#suffix' => '</div>',
500 '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'),
501 );
502 }
503 $add_submit = TRUE;
504 }
505
506 if ($add_submit) {
507 // '#submit' is already set up due to the wizard.
508 $form['#submit'][] = 'ctools_content_configure_form_defaults_submit';
509 }
510 return $form;
511 }
512
513 /**
514 * Submit handler to store context/title override info.
515 */
516 function ctools_content_configure_form_defaults_submit(&$form, &$form_state) {
517 if (isset($form_state['values']['context'])) {
518 $form_state['conf']['context'] = $form_state['values']['context'];
519 }
520 if (isset($form_state['values']['override_title'])) {
521 $form_state['conf']['override_title'] = $form_state['values']['override_title'];
522 $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text'];
523 }
524 }
525
526 /**
527 * Get the config form.
528 *
529 * The $form_info and $form_state need to be preconfigured with data you'll need
530 * such as whether or not you're using ajax, or the modal. $form_info will need
531 * your next/submit callbacks so that you can cache your data appropriately.
532 *
533 * @return
534 * If this function returns false, no form exists.
535 */
536 function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) {
537 $form_state += array(
538 'plugin' => $plugin,
539 'subtype' => $subtype,
540 'subtype_name' => $subtype_name,
541 'conf' => &$conf,
542 'op' => $op,
543 );
544
545 $form_info += array(
546 'id' => 'ctools_content_form',
547 'show back' => TRUE,
548 );
549
550 // Turn the forms defined in the plugin into the format the wizard needs.
551 if ($op == 'add') {
552 if (!empty($subtype['add form'])) {
553 _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op);
554 }
555 else if (!empty($plugin['add form'])) {
556 _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op);
557 }
558 }
559
560 if (empty($form_info['order'])) {
561 // Use the edit form for the add form if add form was completely left off.
562 if (!empty($subtype['edit form'])) {
563 _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op);
564 }
565 else if (!empty($plugin['edit form'])) {
566 _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op);
567 }
568 }
569
570 if (empty($form_info['order'])) {
571 return FALSE;
572 }
573
574 ctools_include('wizard');
575 return ctools_wizard_multistep_form($form_info, $step, $form_state);
576
577 }
578
579 function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) {
580 if (is_string($info)) {
581 if (empty($subtype['title'])) {
582 $title = t('Configure');
583 }
584 else if ($op == 'add') {
585 $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title']));
586 }
587 else {
588 $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title']));
589 }
590 $form_info['order'] = array('form' => $title);
591 $form_info['forms'] = array(
592 'form' => array(
593 'title' => $title,
594 'form id' => $info,
595 'wrapper' => 'ctools_content_configure_form_defaults',
596 ),
597 );
598 }
599 else if (is_array($info)) {
600 $form_info['order'] = array();
601 $form_info['forms'] = array();
602 $count = 0;
603 $base = 'step';
604 $wrapper = NULL;
605 foreach ($info as $form_id => $title) {
606 // @todo -- docs say %title can be used to sub for the admin title.
607 $step = $base . ++$count;
608 if (empty($wrapper)) {
609 $wrapper = $step;
610 }
611
612 if (is_array($title)) {
613 if (!empty($title['default'])) {
614 $wrapper = $step;
615 }
616 $title = $title['title'];
617 }
618
619 $form_info['order'][$step] = $title;
620 $form_info['forms'][$step] = array(
621 'title' => $title,
622 'form id' => $form_id,
623 );
624 }
625 if ($wrapper) {
626 $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults';
627 }
628 }
629 }
630
631 /**
632 * Get an array of all available content types that can be fed into the
633 * display editor for the add content list.
634 *
635 * @param $context
636 * If a context is provided, content that requires that context can apepar.
637 * @param $has_content
638 * Whether or not the display will have incoming content
639 * @param $allowed_types
640 * An array of allowed content types (pane types) keyed by content_type . '-' . sub_type
641 * @param $default_types
642 * A default allowed/denied status for content that isn't known about
643 */
644 function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) {
645 $plugins = ctools_get_content_types();
646 $available = array();
647
648 foreach ($plugins as $id => $plugin) {
649 foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
650 // exclude items that require content if we're saying we don't
651 // provide it.
652 if (!empty($subtype['requires content']) && !$has_content) {
653 continue;
654 }
655
656 // Check to see if the content type can be used in this context.
657 if (!empty($subtype['required context'])) {
658 if (!ctools_context_match_requirements($contexts, $subtype['required context'])) {
659 continue;
660 }
661 }
662
663 // Check to see if the passed-in allowed types allows this content.
664 if ($allowed_types) {
665 $key = $id . '-' . $subtype_id;
666 if (!isset($allowed_types[$key])) {
667 $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other'];
668 }
669 if (!$allowed_types[$key]) {
670 continue;
671 }
672 }
673
674 // If we made it through all the tests, then we can use this content.
675 $available[$id][$subtype_id] = $subtype;
676 }
677 }
678 return $available;
679 }
680
681 /**
682 * Get an array of all content types that can be fed into the
683 * display editor for the add content list, regardless of
684 * availability.
685 *
686 */
687 function ctools_content_get_all_types() {
688 $plugins = ctools_get_content_types();
689 $available = array();
690
691 foreach ($plugins as $id => $plugin) {
692 foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) {
693 // If we made it through all the tests, then we can use this content.
694 $available[$id][$subtype_id] = $subtype;
695 }
696 }
697 return $available;
698 }
699
700 /**
701 * Select the context to be used for a piece of content, based upon config.
702 *
703 * @param $plugin
704 * The content plugin
705 * @param $subtype
706 * The subtype of the content.
707 * @param $conf
708 * The configuration array that should contain the context.
709 * @param $contexts
710 * A keyed array of available contexts.
711 *
712 * @return
713 * The matching contexts or NULL if none or necessary, or FALSE if
714 * requirements can't be met.
715 */
716 function ctools_content_select_context($plugin, $subtype, $conf, $contexts) {
717 // Identify which of our possible contexts apply.
718 if (empty($subtype)) {
719 return;
720 }
721
722 $subtype_info = ctools_content_get_subtype($plugin, $subtype);
723 if (empty($subtype_info)) {
724 return;
725 }
726
727 if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) {
728 return $contexts;
729 }
730
731 // If the content requires a context, fetch it; if no context is returned,
732 // do not display the pane.
733 if (empty($subtype_info['required context'])) {
734 return;
735 }
736
737 // Deal with dynamic required contexts not getting updated in the panes.
738 // For example, Views let you dynamically change context info. While
739 // we cannot be perfect, one thing we can do is if no context at all
740 // was asked for, and then was later added but none is selected, make
741 // a best guess as to what context should be used. THis is right more
742 // than it's wrong.
743 if (is_array($subtype_info['required context'])) {
744 if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) {
745 foreach($subtype_info['required context'] as $index => $required) {
746 if (!isset($conf['context'][$index])) {
747 $filtered = ctools_context_filter($contexts, $required);
748 if ($filtered) {
749 $keys = array_keys($filtered);
750 $conf['context'][$index] = array_shift($keys);
751 }
752 }
753 }
754 }
755 }
756 else {
757 if (empty($conf['context'])) {
758 $filtered = ctools_context_filter($contexts, $subtype_info['required context']);
759 if ($filtered) {
760 $keys = array_keys($filtered);
761 $conf['context'] = array_shift($keys);
762 }
763 }
764 }
765
766 if (empty($conf['context'])) {
767 return;
768 }
769
770 $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']);
771
772 return $context;
773 }