Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / views_content / plugins / content_types / views_panes.inc @ 219d19c4

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
  elseif ($path = $view->display_handler->get_option('inherit_panels_path')) {
212
    if (drupal_is_front_page()) {
213
      $view->override_path = '';
214
    }
215
    else {
216
      $view->override_path = $_GET['q'];
217
    }
218
  }
219

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

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

    
229
  // More link.
230
  if ($allow['more_link']) {
231
    if (empty($conf['more_link'])) {
232
      $view->display_handler->set_option('use_more', FALSE);
233
    }
234
    else {
235
      $view->display_handler->set_option('use_more', TRUE);
236
      // Make sure the view runs the count query so we know whether or not the
237
      // more link applies.
238
      $view->get_total_rows = TRUE;
239
    }
240
  }
241

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

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

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

    
261
    if ($conf['use_pager']) {
262
      if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
263
        $pager['options']['id'] = (int) $conf['pager_id'];
264
      }
265
    }
266

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

    
270
  if ($allow['fields_override']) {
271
    if ($conf['fields_override']) {
272
      $fields = $view->get_items('field');
273
      foreach ($fields as $field => $field_display) {
274
        $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
275
      }
276
      $view->display_handler->set_option('fields', $fields);
277

    
278
    }
279
  }
280

    
281
  if ($allow['exposed_form'] && !empty($conf['exposed'])) {
282
    foreach ($conf['exposed'] as $filter_name => $filter_value) {
283
      if (!is_array($filter_value)) {
284
        $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
285
      }
286
    }
287
    $view->set_exposed_input($conf['exposed']);
288
  }
289

    
290
  $stored_feeds = drupal_add_feed();
291

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

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

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

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

    
308
  if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
309
      (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
310
    $new_feeds = drupal_add_feed();
311
    if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
312
      foreach ($diff as $url) {
313
        $block->feeds[$url] = $new_feeds[$url];
314
      }
315
    }
316
  }
317

    
318
  return $block;
319
}
320

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

    
329
  if (empty($conf)) {
330
    $conf = array();
331
  }
332

    
333
  $conf += array(
334
    'link_to_view' => $view->display_handler->get_option('link_to_view'),
335
    'more_link' => $view->display_handler->get_option('use_more'),
336
    'feed_icons' => FALSE,
337
    'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
338
    'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
339
    'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
340
    'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
341
    'path_override' => FALSE,
342
    'path' => $view->get_path(),
343
    'fields_override' => $view->display_handler->get_option('fields_override'),
344
  );
345
}
346

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

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

    
363
  $view->set_display($display_id);
364

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

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

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

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

    
379
  foreach ($view->display_handler->get_argument_input() as $id => $argument) {
380
    if ($argument['type'] == 'user') {
381
      $form['arguments'][$id] = array(
382
        '#type' => 'textfield',
383
        '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
384
        '#description' => t('You may use keywords for substitutions.'),
385
        '#title' => check_plain($argument['label']),
386
      );
387
    }
388
  }
389
  if ($allow['link_to_view']) {
390
    $form['link_to_view'] = array(
391
      '#type' => 'checkbox',
392
      '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
393
      '#title' => t('Link title to page'),
394
    );
395
  }
396
  if ($allow['more_link']) {
397
    $form['more_link'] = array(
398
      '#type' => 'checkbox',
399
      '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
400
      '#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())),
401
      '#title' => t('Provide a "more" link.'),
402
    );
403
  }
404

    
405
  if (!empty($allow['feed_icons'])) {
406
    $form['feed_icons'] = array(
407
      '#type' => 'checkbox',
408
      '#default_value' => !empty($conf['feed_icons']),
409
      '#title' => t('Display feed icons'),
410
    );
411
  }
412

    
413
  $view->init_style();
414
  if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
415
    $form['fields_override'] = array(
416
      '#type' => 'fieldset',
417
      '#title' => 'Fields to display',
418
      '#collapsible' => TRUE,
419
      '#tree' => TRUE,
420
    );
421
    foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
422
      $title = $handler->ui_name();
423
      if ($handler->options['label']) {
424
        $title .= ' (' . check_plain($handler->options['label']) . ')';
425
      }
426

    
427
      $form['fields_override'][$field] = array(
428
        '#type' => 'checkbox',
429
        '#title' => $title,
430
        '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
431
      );
432
    }
433
  }
434

    
435
  ctools_include('dependent');
436
  if ($allow['use_pager']) {
437
    $form['use_pager'] = array(
438
      '#type' => 'checkbox',
439
      '#title' => t('Use pager'),
440
      '#default_value' => $conf['use_pager'],
441
      '#id' => 'use-pager-checkbox',
442
      '#prefix' => '<div class="container-inline">',
443
    );
444
    $form['pager_id'] = array(
445
      '#type' => 'textfield',
446
      '#default_value' => $conf['pager_id'],
447
      '#title' => t('Pager ID'),
448
      '#size' => 4,
449
      '#id' => 'use-pager-textfield',
450
      '#dependency' => array('use-pager-checkbox' => array(1)),
451
      '#suffix' => '</div>',
452
    );
453
  }
454
  if ($allow['items_per_page']) {
455
    $form['items_per_page'] = array(
456
      '#type' => 'textfield',
457
      '#default_value' => $conf['items_per_page'],
458
      '#title' => t('Num items'),
459
      '#size' => 4,
460
      '#description' => t('Select the number of items to display, or 0 to display all results.'),
461
    );
462
  }
463
  if ($allow['offset']) {
464
    $form['offset'] = array(
465
      '#type' => 'textfield',
466
      '#default_value' => $conf['offset'],
467
      '#title' => t('Offset'),
468
      '#size' => 4,
469
      '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
470
    );
471
  }
472
  if ($allow['path_override']) {
473
    $form['path'] = array(
474
      '#type' => 'textfield',
475
      '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
476
      '#title' => t('Override path'),
477
      '#size' => 30,
478
      '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
479
    );
480
    if (!empty($contexts)) {
481
      $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');
482

    
483
      $form['contexts'] = array(
484
        '#type' => 'fieldset',
485
        '#title' => t('Substitutions'),
486
        '#collapsible' => TRUE,
487
        '#collapsed' => TRUE,
488
      );
489

    
490
      $rows = array();
491
      foreach ($contexts as $context) {
492
        foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
493
          $rows[] = array(
494
            check_plain($keyword),
495
            t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
496
          );
497
        }
498
      }
499

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

    
505
  if (empty($conf['exposed'])) {
506
    $conf['exposed'] = array();
507
  }
508

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

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

    
519
    if (version_compare(views_api_version(), '3', '>=')) {
520
      $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
521
    }
522
    $view->init_handlers();
523
    $exposed_form = array();
524
    $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);
525

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

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

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

    
540
      if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
541
        $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
542
        $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
543
        $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
544
      }
545
      $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
546
      $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
547
      $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
548
    }
549
  }
550

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

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

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

    
564
  return $form;
565
}
566

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

    
576
  foreach ($keys as $key) {
577
    if (isset($form_state['values'][$key])) {
578
      $form_state['conf'][$key] = $form_state['values'][$key];
579
    }
580
  }
581
}
582

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

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

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

    
598
  return check_plain($title);
599
}
600

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

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

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

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

    
617
  // Add arguments first.
618
  if (!empty($conf['arguments'])) {
619
    $keys = array_keys($conf['arguments']);
620
    $values = array_values($conf['arguments']);
621
    $argument_input = $view->display_handler->get_option('argument_input');
622

    
623
    foreach ($conf['arguments'] as $key => $value) {
624
      if (!empty($value)) {
625
        $label = $argument_input[$key]['label'];
626
        $info[] = $label . ': ' . $value;
627
      }
628
    }
629
  }
630

    
631
  $block = new stdClass();
632
  if ($info) {
633
    $block->title = array_shift($info);
634

    
635
    $info[] = $view->display_handler->get_option('pane_description');
636
    $block->content = theme('item_list', array('items' => $info));
637
  }
638
  else {
639
    $block->title = $view->display_handler->get_option('pane_description');
640
    $block->content = '';
641
  }
642
  return $block;
643
}