commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / ctools / views_content / plugins / content_types / views_panes.inc
1 <?php
2
3 /**
4 * @file
5 * Content type plugin to allow Views to be exposed as a display type,
6 * leaving most of the configuration on the view.
7 */
8
9 /**
10 * Implements hook_ctools_content_types()
11 */
12 function views_content_views_panes_ctools_content_types() {
13 return array(
14 'title' => t('View panes'),
15 'admin settings' => 'views_content_admin_form',
16 'all contexts' => TRUE,
17 );
18 }
19
20 /**
21 * Return all content types available.
22 */
23 function views_content_views_panes_content_type_content_types($plugin) {
24 $types = array();
25 // It can be fairly intensive to calculate this, so let's cache this in the
26 // cache_views table. The nice thing there is that if views ever change, that
27 // table will always be cleared. Except for the occasional default view, so
28 // we must use the Views caching functions in order to respect Views caching
29 // settings.
30 views_include('cache');
31 $data = views_cache_get('views_content_panes', TRUE);
32 if (!empty($data->data)) {
33 $types = $data->data;
34 }
35
36 if (empty($types)) {
37 $types = array();
38
39 $views = views_get_all_views();
40
41 foreach ($views as $view) {
42 if (!empty($view->disabled)) {
43 continue;
44 }
45
46 $view->init_display();
47
48 foreach ($view->display as $id => $display) {
49 if (empty($display->handler->panel_pane_display)) {
50 continue;
51 }
52 $info = _views_content_panes_content_type($view, $display);
53 if ($info) {
54 $types[$view->name . '-' . $id] = $info;
55 }
56 }
57
58 $view->destroy();
59 }
60 views_cache_set('views_content_panes', $types, TRUE);
61 }
62
63 return $types;
64 }
65
66 /**
67 * Return a single content type.
68 */
69 function views_content_views_panes_content_type_content_type($subtype, $plugin) {
70 list($name, $display) = explode('-', $subtype);
71 $view = views_get_view($name);
72 if (empty($view)) {
73 return;
74 }
75
76 $view->set_display($display);
77 $retval = _views_content_panes_content_type($view, $view->display[$display]);
78
79 $view->destroy();
80 return $retval;
81 }
82
83 function _views_content_panes_content_type($view, $display) {
84 // Ensure the handler is the right type, as Views will fall back to
85 // the default display if something is broken:
86 if (!is_a($display->handler, 'views_content_plugin_display_panel_pane')) {
87 return;
88 }
89
90 $title = views_content_get_display_title($view, $display->id);
91
92 $description = $display->handler->get_option('pane_description');
93 if (!$description) {
94 $description = $view->description;
95 }
96
97 $category = $display->handler->get_option('pane_category');
98 if (!$category['name']) {
99 $category['name'] = t('View panes');
100 }
101
102 $icon = 'icon_views_page.png';
103
104 $contexts = array();
105
106 $arguments = $display->handler->get_argument_input();
107 ctools_include('views');
108 foreach ($arguments as $argument) {
109 $contexts[] = ctools_views_get_argument_context($argument);
110 }
111
112 $allow = $display->handler->get_option('allow');
113 return array(
114 'title' => $title,
115 'icon' => $icon,
116 'description' => filter_xss_admin($description),
117 'required context' => $contexts,
118 'category' => array($category['name'], $category['weight']),
119 'no title override' => empty($allow['title_override']),
120 );
121 }
122
123 /**
124 * Output function for the 'views' content type.
125 *
126 * Outputs a view based on the module and delta supplied in the configuration.
127 */
128 function views_content_views_panes_content_type_render($subtype, $conf, $panel_args, $contexts) {
129 if (!is_array($contexts)) {
130 $contexts = array($contexts);
131 }
132
133 list($name, $display) = explode('-', $subtype);
134 $view = views_get_view($name);
135 if (empty($view)) {
136 return;
137 }
138
139 $view->set_display($display);
140 views_content_views_panes_add_defaults($conf, $view);
141
142 if (!$view->display_handler->access($GLOBALS['user']) || empty($view->display_handler->panel_pane_display)) {
143 return;
144 }
145
146 $view->display_handler->set_pane_conf($conf);
147
148 $args = array();
149 $arguments = $view->display_handler->get_option('arguments');
150
151 $context_keys = isset($conf['context']) ? $conf['context'] : array();
152 foreach ($view->display_handler->get_argument_input() as $id => $argument) {
153 switch ($argument['type']) {
154 case 'context':
155 $key = array_shift($context_keys);
156 if (isset($contexts[$key])) {
157 if (strpos($argument['context'], '.')) {
158 list($context, $converter) = explode('.', $argument['context'], 2);
159 $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
160 }
161 else {
162 $args[] = $contexts[$key]->argument;
163 }
164 }
165 else {
166 $args[] = isset($arguments[$id]['exception']['value']) ? $arguments[$id]['exception']['value'] : 'all';
167 }
168 break;
169
170 case 'fixed':
171 $args[] = $argument['fixed'];
172 break;
173
174 case 'panel':
175 $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
176 break;
177
178 case 'user':
179 $args[] = (isset($conf['arguments'][$id]) && $conf['arguments'][$id] !== '') ? ctools_context_keyword_substitute($conf['arguments'][$id], array(), $contexts) : NULL;
180 break;
181
182 case 'wildcard':
183 // Put in the wildcard.
184 $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
185 break;
186
187 case 'none':
188 default:
189 // Put in NULL.
190 // views.module knows what to do with NULL (or missing) arguments
191 $args[] = NULL;
192 break;
193 }
194 }
195
196 // remove any trailing NULL arguments as these are non-args:
197 while (count($args) && end($args) === NULL) {
198 array_pop($args);
199 }
200
201 $view->set_arguments($args);
202
203 $allow = $view->display_handler->get_option('allow');
204
205 if (!empty($conf['path'])) {
206 $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
207 }
208 if ($allow['path_override'] && !empty($conf['path'])) {
209 $view->override_path = $conf['path'];
210 }
211 else if ($path = $view->display_handler->get_option('inherit_panels_path')) {
212 $view->override_path = $_GET['q'];
213 }
214
215 $block = new stdClass();
216 $block->module = 'views';
217 $block->delta = $view->name . '-' . $display;
218
219 if (($allow['link_to_view'] && !empty($conf['link_to_view'])) ||
220 (!$allow['link_to_view'] && $view->display_handler->get_option('link_to_view'))) {
221 $block->title_link = $view->get_url();
222 }
223
224 // more link
225 if ($allow['more_link']) {
226 if (empty($conf['more_link'])) {
227 $view->display_handler->set_option('use_more', FALSE);
228 }
229 else {
230 $view->display_handler->set_option('use_more', TRUE);
231 // make sure the view runs the count query so we know whether or not the
232 // more link applies.
233 $view->get_total_rows = TRUE;
234 }
235 }
236
237 if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
238 $view->set_items_per_page($conf['items_per_page']);
239 }
240
241 if ($allow['offset']) {
242 $view->set_offset($conf['offset']);
243 }
244
245 if ($allow['use_pager']) {
246 // Only set use_pager if they differ, this way we can avoid overwriting the
247 // pager type that Views uses.
248 $pager = $view->display_handler->get_option('pager');
249 if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
250 $pager['type'] = 'full';
251 }
252 elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
253 $pager['type'] = $view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
254 }
255
256 if ($conf['use_pager']) {
257 if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
258 $pager['options']['id'] = (int) $conf['pager_id'];
259 }
260 }
261
262 $view->display_handler->set_option('pager', $pager);
263 }
264
265 if ($allow['fields_override']) {
266 if ($conf['fields_override']) {
267 $fields = $view->get_items('field');
268 foreach ($fields as $field => $field_display) {
269 $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
270 }
271 $view->display_handler->set_option('fields', $fields);
272
273 }
274 }
275
276 if ($allow['exposed_form'] && !empty($conf['exposed'])) {
277 foreach ($conf['exposed'] as $filter_name => $filter_value) {
278 if (!is_array($filter_value)) {
279 $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
280 }
281 }
282 $view->set_exposed_input($conf['exposed']);
283 }
284
285 $stored_feeds = drupal_add_feed();
286
287 $block->content = $view->preview();
288 if (empty($view->result) && !$view->display_handler->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
289 return;
290 }
291
292 // Add contextual links to the output.
293 $block = (array) $block;
294 views_add_block_contextual_links($block, $view, $display, 'panel_pane');
295 $block = (object) $block;
296
297 $block->title = $view->get_title();
298
299 if (empty($view->total_rows) || $view->total_rows <= $view->get_items_per_page()) {
300 unset($block->more);
301 }
302
303 if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
304 (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
305 $new_feeds = drupal_add_feed();
306 if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
307 foreach ($diff as $url) {
308 $block->feeds[$url] = $new_feeds[$url];
309 }
310 }
311 }
312
313 return $block;
314 }
315
316 /**
317 * Add defaults to view pane settings.
318 * This helps cover us if $allow settings changed and there are no actual
319 * settings for a particular item.
320 */
321 function views_content_views_panes_add_defaults(&$conf, $view) {
322 $pager = $view->display_handler->get_option('pager');
323
324 $conf += array(
325 'link_to_view' => $view->display_handler->get_option('link_to_view'),
326 'more_link' => $view->display_handler->get_option('use_more'),
327 'feed_icons' => FALSE,
328 'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
329 'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
330 'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
331 'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
332 'path_override' => FALSE,
333 'path' => $view->get_path(),
334 'fields_override' => $view->display_handler->get_option('fields_override'),
335 );
336 }
337
338 /**
339 * Returns an edit form for a block.
340 */
341 function views_content_views_panes_content_type_edit_form($form, &$form_state) {
342 $conf = $form_state['conf'];
343 $contexts = $form_state['contexts'];
344 // This allows older content to continue to work, where we used to embed
345 // the display directly.
346 list($name, $display_id) = explode('-', $form_state['subtype_name']);
347 $view = views_get_view($name);
348
349 if (empty($view)) {
350 $form['markup'] = array('#markup' => t('Broken/missing/deleted view.'));
351 return $form;
352 }
353
354 $view->set_display($display_id);
355
356 // If it couldn't set the display and we got the default display instead,
357 // fail.
358 if ($view->current_display == 'default') {
359 $form['markup'] = array('#markup' => t('Broken/missing/deleted view display.'));
360 return $form;
361 }
362
363 $allow = $view->display_handler->get_option('allow');
364
365 // Provide defaults for everything in order to prevent warnings.
366 views_content_views_panes_add_defaults($conf, $view);
367
368 $form['arguments']['#tree'] = TRUE;
369
370 foreach ($view->display_handler->get_argument_input() as $id => $argument) {
371 if ($argument['type'] == 'user') {
372 $form['arguments'][$id] = array(
373 '#type' => 'textfield',
374 '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
375 '#description' => t('You may use keywords for substitutions.'),
376 '#title' => check_plain($argument['label']),
377 );
378 }
379 }
380 if ($allow['link_to_view'] ) {
381 $form['link_to_view'] = array(
382 '#type' => 'checkbox',
383 '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
384 '#title' => t('Link title to page'),
385 );
386 }
387 if ($allow['more_link']) {
388 $form['more_link'] = array(
389 '#type' => 'checkbox',
390 '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
391 '#description' => t('The text of this link will be "@more". This setting can only be modified on the View configuration.', array('@more' => $view->display_handler->use_more_text())),
392 '#title' => t('Provide a "more" link.'),
393 );
394 }
395
396 if (!empty($allow['feed_icons'])) {
397 $form['feed_icons'] = array(
398 '#type' => 'checkbox',
399 '#default_value' => !empty($conf['feed_icons']),
400 '#title' => t('Display feed icons'),
401 );
402 }
403
404 $view->init_style();
405 if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
406 $form['fields_override'] = array(
407 '#type' => 'fieldset',
408 '#title' => 'Fields to display',
409 '#collapsible' => TRUE,
410 '#tree' => TRUE,
411 );
412 foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
413 $title = $handler->ui_name();
414 if ($handler->options['label']) {
415 $title .= ' (' . check_plain($handler->options['label']) . ')';
416 }
417
418 $form['fields_override'][$field] = array(
419 '#type' => 'checkbox',
420 '#title' => $title,
421 '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
422 );
423 }
424 }
425
426 ctools_include('dependent');
427 if ($allow['use_pager']) {
428 $form['use_pager'] = array(
429 '#type' => 'checkbox',
430 '#title' => t('Use pager'),
431 '#default_value' => $conf['use_pager'],
432 '#id' => 'use-pager-checkbox',
433 '#prefix' => '<div class="container-inline">',
434 );
435 $form['pager_id'] = array(
436 '#type' => 'textfield',
437 '#default_value' => $conf['pager_id'],
438 '#title' => t('Pager ID'),
439 '#size' => 4,
440 '#id' => 'use-pager-textfield',
441 '#dependency' => array('use-pager-checkbox' => array(1)),
442 '#suffix' => '</div>',
443 );
444 }
445 if ($allow['items_per_page']) {
446 $form['items_per_page'] = array(
447 '#type' => 'textfield',
448 '#default_value' => $conf['items_per_page'],
449 '#title' => t('Num items'),
450 '#size' => 4,
451 '#description' => t('Select the number of items to display, or 0 to display all results.'),
452 );
453 }
454 if ($allow['offset']) {
455 $form['offset'] = array(
456 '#type' => 'textfield',
457 '#default_value' => $conf['offset'],
458 '#title' => t('Offset'),
459 '#size' => 4,
460 '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
461 );
462 }
463 if ($allow['path_override']) {
464 $form['path'] = array(
465 '#type' => 'textfield',
466 '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
467 '#title' => t('Override path'),
468 '#size' => 30,
469 '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
470 );
471 if (!empty($contexts)) {
472 $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');
473
474 $form['contexts'] = array(
475 '#type' => 'fieldset',
476 '#title' => t('Substitutions'),
477 '#collapsible' => TRUE,
478 '#collapsed' => TRUE,
479 );
480
481 $rows = array();
482 foreach ($contexts as $context) {
483 foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
484 $rows[] = array(
485 check_plain($keyword),
486 t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
487 );
488 }
489 }
490
491 $header = array(t('Keyword'), t('Value'));
492 $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
493 }
494 }
495
496 if (empty($conf['exposed'])) {
497 $conf['exposed'] = array();
498 }
499
500 if ($allow['exposed_form']) {
501 // If the exposed form is part of pane configuration, get the exposed
502 // form re-tool it for our use.
503 $exposed_form_state = array(
504 'view' => &$view,
505 'display' => &$view->display[$display_id],
506 );
507
508 $view->set_exposed_input($conf['exposed']);
509
510 if (version_compare(views_api_version(), '3', '>=')) {
511 $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
512 }
513 $view->init_handlers();
514 $exposed_form = array();
515 $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);
516
517 $form['exposed'] = array(
518 '#tree' => TRUE,
519 );
520
521 foreach ($exposed_form['#info'] as $id => $info) {
522 $form['exposed'][$id] = array(
523 '#type' => 'item',
524 '#id' => 'views-exposed-pane',
525 );
526
527 if (!empty($info['label'])) {
528 $form['exposed'][$id]['#title'] = $info['label'];
529 }
530
531 if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
532 $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
533 $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
534 $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
535 }
536 $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
537 $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
538 $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
539 }
540 }
541
542 // The exposed sort stuff doesn't fall into $exposed_form['#info'] so we
543 // have to handle it separately.
544 if (isset($exposed_form['sort_by'])) {
545 $form['exposed']['sort_by'] = $exposed_form['sort_by'];
546 }
547
548 if (isset($exposed_form['sort_order'])) {
549 $form['exposed']['sort_order'] = $exposed_form['sort_order'];
550 }
551
552 // Add the view object to the form to allow additional customization
553 $form_state['view'] = $view;
554
555 return $form;
556 }
557
558 /**
559 * Store form values in $conf.
560 */
561 function views_content_views_panes_content_type_edit_form_submit(&$form, &$form_state) {
562 // Copy everything from our defaults.
563 $keys = array('link_to_view', 'more_link', 'feed_icons', 'use_pager',
564 'pager_id', 'items_per_page', 'offset', 'path_override', 'path', 'arguments', 'fields_override', 'exposed');
565
566 foreach ($keys as $key) {
567 if (isset($form_state['values'][$key])) {
568 $form_state['conf'][$key] = $form_state['values'][$key];
569 }
570 }
571 }
572
573
574 /**
575 * Returns the administrative title for a type.
576 */
577 function views_content_views_panes_content_type_admin_title($subtype, $conf, $contexts) {
578 list($name, $display) = explode('-', $subtype);
579 $view = views_get_view($name);
580 if (empty($view) || empty($view->display[$display])) {
581 return t('Deleted/missing view @view', array('@view' => $name));
582 }
583
584 $view->set_display($display);
585 views_content_views_panes_add_defaults($conf, $view);
586
587 $title = views_content_get_display_title($view, $display);
588
589 return check_plain($title);
590 }
591
592 /**
593 * Returns the administrative title for a type.
594 */
595 function views_content_views_panes_content_type_admin_info($subtype, $conf, $contexts) {
596 $info = array();
597
598 list($view_name, $display_name) = explode('-', $subtype);
599 $view = views_get_view($view_name);
600
601 if (empty($view) || empty($view->display[$display_name])) {
602 return;
603 }
604
605 $view->set_display($display_name);
606 views_content_views_panes_add_defaults($conf, $view);
607
608 // Add arguments first
609 if (!empty($conf['arguments'])) {
610 $keys = array_keys($conf['arguments']);
611 $values = array_values($conf['arguments']);
612 $argument_input = $view->display_handler->get_option('argument_input');
613
614 foreach ($conf['arguments'] as $key => $value) {
615 if (!empty($value)){
616 $label = $argument_input[$key]['label'];
617 $info[] = $label . ': ' . $value;
618 }
619 }
620 }
621
622 $block = new stdClass;
623 if ($info) {
624 $block->title = array_shift($info);
625
626 $info[] = $view->display_handler->get_option('pane_description');
627 $block->content = theme('item_list', array('items' => $info));
628 }
629 else {
630 $block->title = $view->display_handler->get_option('pane_description');
631 $block->content = '';
632 }
633 return $block;
634 }