summaryrefslogtreecommitdiff
path: root/sites/all/modules/ctools/views_content/plugins/content_types/views_panes.inc
blob: e4dded23ae6858937219b32584d479826fe0ca15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php

/**
 * @file
 * Content type plugin to allow Views to be exposed as a display type,
 * leaving most of the configuration on the view.
 */

/**
 * Implements hook_ctools_content_types()
 */
function views_content_views_panes_ctools_content_types() {
  return array(
    'title' => t('View panes'),
    'admin settings' => 'views_content_admin_form',
    'all contexts' => TRUE,
  );
}

/**
 * Return all content types available.
 */
function views_content_views_panes_content_type_content_types($plugin) {
  $types = array();
  // It can be fairly intensive to calculate this, so let's cache this in the
  // cache_views table. The nice thing there is that if views ever change, that
  // table will always be cleared. Except for the occasional default view, so
  // we must use the Views caching functions in order to respect Views caching
  // settings.
  views_include('cache');
  $data = views_cache_get('views_content_panes', TRUE);
  if (!empty($data->data)) {
    $types = $data->data;
  }

  if (empty($types)) {
    $types = array();

    $views = views_get_all_views();

    foreach ($views as $view) {
      if (!empty($view->disabled)) {
        continue;
      }

      $view->init_display();

      foreach ($view->display as $id => $display) {
        if (empty($display->handler->panel_pane_display)) {
          continue;
        }
        $info = _views_content_panes_content_type($view, $display);
        if ($info) {
          $types[$view->name . '-' . $id] = $info;
        }
      }

      $view->destroy();
    }
    views_cache_set('views_content_panes', $types, TRUE);
  }

  return $types;
}

/**
 * Return a single content type.
 */
function views_content_views_panes_content_type_content_type($subtype, $plugin) {
  list($name, $display) = explode('-', $subtype);
  $view = views_get_view($name);
  if (empty($view)) {
    return;
  }

  $view->set_display($display);
  $retval = _views_content_panes_content_type($view, $view->display[$display]);

  $view->destroy();
  return $retval;
}

function _views_content_panes_content_type($view, $display) {
  // Ensure the handler is the right type, as Views will fall back to
  // the default display if something is broken:
  if (!is_a($display->handler, 'views_content_plugin_display_panel_pane')) {
    return;
  }

  $title = views_content_get_display_title($view, $display->id);

  $description = $display->handler->get_option('pane_description');
  if (!$description) {
    $description = $view->description;
  }

  $category = $display->handler->get_option('pane_category');
  if (!$category['name']) {
    $category['name'] = t('View panes');
  }

  $icon = 'icon_views_page.png';

  $contexts = array();

  $arguments = $display->handler->get_argument_input();
  ctools_include('views');
  foreach ($arguments as $argument) {
    $contexts[] = ctools_views_get_argument_context($argument);
  }

  $allow = $display->handler->get_option('allow');
  return array(
    'title' => $title,
    'icon' => $icon,
    'description' => filter_xss_admin($description),
    'required context' => $contexts,
    'category' => array($category['name'], $category['weight']),
    'no title override' => empty($allow['title_override']),
  );
}

/**
 * Output function for the 'views' content type.
 *
 * Outputs a view based on the module and delta supplied in the configuration.
 */
function views_content_views_panes_content_type_render($subtype, $conf, $panel_args, $contexts) {
  if (!is_array($contexts)) {
    $contexts = array($contexts);
  }

  list($name, $display) = explode('-', $subtype);
  $view = views_get_view($name);
  if (empty($view)) {
    return;
  }

  $view->set_display($display);
  views_content_views_panes_add_defaults($conf, $view);

  if (!$view->display_handler->access($GLOBALS['user']) || empty($view->display_handler->panel_pane_display)) {
    return;
  }

  $view->display_handler->set_pane_conf($conf);

  $args = array();
  $arguments = $view->display_handler->get_option('arguments');

  $context_keys = isset($conf['context']) ? $conf['context'] : array();
  foreach ($view->display_handler->get_argument_input() as $id => $argument) {
    switch ($argument['type']) {
      case 'context':
        $key = array_shift($context_keys);
        if (isset($contexts[$key])) {
          if (strpos($argument['context'], '.')) {
            list($context, $converter) = explode('.', $argument['context'], 2);
            $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
          }
          else {
            $args[] = $contexts[$key]->argument;
          }
        }
        else {
          $args[] = isset($arguments[$id]['exception']['value']) ? $arguments[$id]['exception']['value'] : 'all';
        }
        break;

      case 'fixed':
        $args[] = $argument['fixed'];
        break;

      case 'panel':
        $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
        break;

      case 'user':
        $args[] = (isset($conf['arguments'][$id])  && $conf['arguments'][$id] !== '') ? ctools_context_keyword_substitute($conf['arguments'][$id], array(), $contexts) : NULL;
        break;

     case 'wildcard':
        // Put in the wildcard.
       $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
       break;

     case 'none':
     default:
       // Put in NULL.
       // views.module knows what to do with NULL (or missing) arguments
       $args[] = NULL;
       break;
    }
  }

  // remove any trailing NULL arguments as these are non-args:
  while (count($args) && end($args) === NULL) {
    array_pop($args);
  }

  $view->set_arguments($args);

  $allow = $view->display_handler->get_option('allow');

  if (!empty($conf['path'])) {
    $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
  }
  if ($allow['path_override'] && !empty($conf['path'])) {
    $view->override_path = $conf['path'];
  }
  else if ($path = $view->display_handler->get_option('inherit_panels_path')) {
    $view->override_path = $_GET['q'];
  }

  $block = new stdClass();
  $block->module = 'views';
  $block->delta  = $view->name . '-' . $display;

  if (($allow['link_to_view'] && !empty($conf['link_to_view'])) ||
      (!$allow['link_to_view'] && $view->display_handler->get_option('link_to_view'))) {
    $block->title_link = $view->get_url();
  }

  // more link
  if ($allow['more_link']) {
    if (empty($conf['more_link'])) {
      $view->display_handler->set_option('use_more', FALSE);
    }
    else {
      $view->display_handler->set_option('use_more', TRUE);
      // make sure the view runs the count query so we know whether or not the
      // more link applies.
      $view->get_total_rows = TRUE;
    }
  }

  if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
    $view->set_items_per_page($conf['items_per_page']);
  }

  if ($allow['offset']) {
    $view->set_offset($conf['offset']);
  }

  if ($allow['use_pager']) {
    // Only set use_pager if they differ, this way we can avoid overwriting the
    // pager type that Views uses.
    $pager = $view->display_handler->get_option('pager');
    if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
      $pager['type'] = 'full';
    }
    elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
      $pager['type'] = $view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
    }

    if ($conf['use_pager']) {
      if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
        $pager['options']['id'] = (int) $conf['pager_id'];
      }
    }

    $view->display_handler->set_option('pager', $pager);
  }

  if ($allow['fields_override']) {
    if ($conf['fields_override']) {
      $fields = $view->get_items('field');
      foreach ($fields as $field => $field_display) {
        $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
      }
      $view->display_handler->set_option('fields', $fields);

    }
  }

  if ($allow['exposed_form'] && !empty($conf['exposed'])) {
    foreach ($conf['exposed'] as $filter_name => $filter_value) {
      if (!is_array($filter_value)) {
        $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
      }
    }
    $view->set_exposed_input($conf['exposed']);
  }

  $stored_feeds = drupal_add_feed();

  $block->content = $view->preview();
  if (empty($view->result) && !$view->display_handler->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
    return;
  }

  // Add contextual links to the output.
  $block = (array) $block;
  views_add_block_contextual_links($block, $view, $display, 'panel_pane');
  $block = (object) $block;

  $block->title = $view->get_title();

  if (empty($view->total_rows) || $view->total_rows <= $view->get_items_per_page()) {
    unset($block->more);
  }

  if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
      (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
    $new_feeds = drupal_add_feed();
    if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
      foreach ($diff as $url) {
        $block->feeds[$url] = $new_feeds[$url];
      }
    }
  }

  return $block;
}

/**
 * Add defaults to view pane settings.
 * This helps cover us if $allow settings changed and there are no actual
 * settings for a particular item.
 */
function views_content_views_panes_add_defaults(&$conf, $view) {
  $pager = $view->display_handler->get_option('pager');

  $conf += array(
    'link_to_view' => $view->display_handler->get_option('link_to_view'),
    'more_link' => $view->display_handler->get_option('use_more'),
    'feed_icons' => FALSE,
    'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
    'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
    'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
    'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
    'path_override' => FALSE,
    'path' => $view->get_path(),
    'fields_override' => $view->display_handler->get_option('fields_override'),
  );
}

/**
 * Returns an edit form for a block.
 */
function views_content_views_panes_content_type_edit_form($form, &$form_state) {
  $conf = $form_state['conf'];
  $contexts = $form_state['contexts'];
  // This allows older content to continue to work, where we used to embed
  // the display directly.
  list($name, $display_id) = explode('-', $form_state['subtype_name']);
  $view = views_get_view($name);

  if (empty($view)) {
    $form['markup'] = array('#markup' => t('Broken/missing/deleted view.'));
    return $form;
  }

  $view->set_display($display_id);

  // If it couldn't set the display and we got the default display instead,
  // fail.
  if ($view->current_display == 'default') {
    $form['markup'] = array('#markup' => t('Broken/missing/deleted view display.'));
    return $form;
  }

  $allow = $view->display_handler->get_option('allow');

  // Provide defaults for everything in order to prevent warnings.
  views_content_views_panes_add_defaults($conf, $view);

  $form['arguments']['#tree'] = TRUE;

  foreach ($view->display_handler->get_argument_input() as $id => $argument) {
    if ($argument['type'] == 'user') {
      $form['arguments'][$id] = array(
        '#type' => 'textfield',
        '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
        '#description' => t('You may use keywords for substitutions.'),
        '#title' => check_plain($argument['label']),
      );
    }
  }
  if ($allow['link_to_view'] ) {
    $form['link_to_view'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
      '#title' => t('Link title to page'),
    );
  }
  if ($allow['more_link']) {
    $form['more_link'] = array(
      '#type' => 'checkbox',
      '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
      '#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())),
      '#title' => t('Provide a "more" link.'),
    );
  }

  if (!empty($allow['feed_icons'])) {
    $form['feed_icons'] = array(
      '#type' => 'checkbox',
      '#default_value' => !empty($conf['feed_icons']),
      '#title' => t('Display feed icons'),
    );
  }

  $view->init_style();
  if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
    $form['fields_override'] = array(
      '#type' => 'fieldset',
      '#title' => 'Fields to display',
      '#collapsible' => TRUE,
      '#tree' => TRUE,
    );
    foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
      $title = $handler->ui_name();
      if ($handler->options['label']) {
        $title .= ' (' . check_plain($handler->options['label']) . ')';
      }

      $form['fields_override'][$field] = array(
        '#type' => 'checkbox',
        '#title' => $title,
        '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
      );
    }
  }

  ctools_include('dependent');
  if ($allow['use_pager']) {
    $form['use_pager'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use pager'),
        '#default_value' => $conf['use_pager'],
      '#id' => 'use-pager-checkbox',
      '#prefix' => '<div class="container-inline">',
    );
    $form['pager_id'] = array(
      '#type' => 'textfield',
      '#default_value' => $conf['pager_id'],
      '#title' => t('Pager ID'),
      '#size' => 4,
      '#id' => 'use-pager-textfield',
      '#dependency' => array('use-pager-checkbox' => array(1)),
      '#suffix' => '</div>',
    );
  }
  if ($allow['items_per_page']) {
    $form['items_per_page'] = array(
      '#type' => 'textfield',
      '#default_value' => $conf['items_per_page'],
      '#title' => t('Num items'),
      '#size' => 4,
      '#description' => t('Select the number of items to display, or 0 to display all results.'),
    );
  }
  if ($allow['offset']) {
    $form['offset'] = array(
      '#type' => 'textfield',
      '#default_value' => $conf['offset'],
      '#title' => t('Offset'),
      '#size' => 4,
      '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
    );
  }
  if ($allow['path_override']) {
    $form['path'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
      '#title' => t('Override path'),
      '#size' => 30,
      '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
    );
    if (!empty($contexts)) {
      $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');

      $form['contexts'] = array(
        '#type' => 'fieldset',
        '#title' => t('Substitutions'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      $rows = array();
      foreach ($contexts as $context) {
        foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
          $rows[] = array(
            check_plain($keyword),
            t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
          );
        }
      }

      $header = array(t('Keyword'), t('Value'));
      $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
    }
  }

  if (empty($conf['exposed'])) {
    $conf['exposed'] = array();
  }

  if ($allow['exposed_form']) {
    // If the exposed form is part of pane configuration, get the exposed
    // form re-tool it for our use.
    $exposed_form_state = array(
      'view' => &$view,
      'display' => &$view->display[$display_id],
    );

    $view->set_exposed_input($conf['exposed']);

    if (version_compare(views_api_version(), '3', '>=')) {
      $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
    }
    $view->init_handlers();
    $exposed_form = array();
    $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);

    $form['exposed'] = array(
      '#tree' => TRUE,
    );

    foreach ($exposed_form['#info'] as $id => $info) {
      $form['exposed'][$id] = array(
        '#type' => 'item',
        '#id' => 'views-exposed-pane',
      );

      if (!empty($info['label'])) {
        $form['exposed'][$id]['#title'] = $info['label'];
      }

      if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
        $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
        $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
        $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
      }
      $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
      $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
      $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
    }
  }

  // The exposed sort stuff doesn't fall into $exposed_form['#info'] so we
  // have to handle it separately.
  if (isset($exposed_form['sort_by'])) {
    $form['exposed']['sort_by'] = $exposed_form['sort_by'];
  }

  if (isset($exposed_form['sort_order'])) {
    $form['exposed']['sort_order'] = $exposed_form['sort_order'];
  }

  // Add the view object to the form to allow additional customization
  $form_state['view'] = $view;

  return $form;
}

/**
 * Store form values in $conf.
 */
function views_content_views_panes_content_type_edit_form_submit(&$form, &$form_state) {
  // Copy everything from our defaults.
  $keys = array('link_to_view', 'more_link', 'feed_icons', 'use_pager',
    'pager_id', 'items_per_page', 'offset', 'path_override', 'path', 'arguments', 'fields_override', 'exposed');

  foreach ($keys as $key) {
    if (isset($form_state['values'][$key])) {
      $form_state['conf'][$key] = $form_state['values'][$key];
    }
  }
}


/**
 * Returns the administrative title for a type.
 */
function views_content_views_panes_content_type_admin_title($subtype, $conf, $contexts) {
  list($name, $display) = explode('-', $subtype);
  $view = views_get_view($name);
  if (empty($view) || empty($view->display[$display])) {
    return t('Deleted/missing view @view', array('@view' => $name));
  }

  $view->set_display($display);
  views_content_views_panes_add_defaults($conf, $view);

  $title = views_content_get_display_title($view, $display);

  return check_plain($title);
}

/**
 * Returns the administrative title for a type.
 */
function views_content_views_panes_content_type_admin_info($subtype, $conf, $contexts) {
  $info = array();

  list($view_name, $display_name) = explode('-', $subtype);
  $view = views_get_view($view_name);

  if (empty($view) || empty($view->display[$display_name])) {
    return;
  }

  $view->set_display($display_name);
  views_content_views_panes_add_defaults($conf, $view);

  // Add arguments first
  if (!empty($conf['arguments'])) {
    $keys = array_keys($conf['arguments']);
    $values = array_values($conf['arguments']);
    $argument_input = $view->display_handler->get_option('argument_input');

    foreach ($conf['arguments'] as $key => $value) {
      if (!empty($value)){
        $label = $argument_input[$key]['label'];
        $info[] = $label . ': ' . $value;
      }
    }
  }

  $block = new stdClass;
  if ($info) {
    $block->title = array_shift($info);

    $info[] = $view->display_handler->get_option('pane_description');
    $block->content = theme('item_list', array('items' => $info));
  }
  else {
    $block->title = $view->display_handler->get_option('pane_description');
    $block->content = '';
  }
  return $block;
}