Projet

Général

Profil

Paste
Télécharger (16,2 ko) Statistiques
| Branche: | Révision:

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

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
  $conf['pane_contexts'] = $contexts;
147
  $conf['panel_args'] = $panel_args;
148
  $view->display_handler->set_pane_conf($conf);
149

    
150
  $allow = $view->display_handler->get_option('allow');
151
  if ($allow['path_override'] && !empty($conf['path'])) {
152
    $view->override_path = $conf['path'];
153
  }
154
  else if ($path = $view->display_handler->get_option('inherit_panels_path')) {
155
    $view->override_path = $_GET['q'];
156
  }
157

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

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

    
167
  $stored_feeds = drupal_add_feed();
168

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

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

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

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

    
185
  if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
186
      (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
187
    $new_feeds = drupal_add_feed();
188
    if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
189
      foreach ($diff as $url) {
190
        $block->feeds[$url] = $new_feeds[$url];
191
      }
192
    }
193
  }
194

    
195
  return $block;
196
}
197

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

    
206
  if (empty($conf)) {
207
    $conf = array();
208
  }
209

    
210
  $conf += array(
211
    'link_to_view' => $view->display_handler->get_option('link_to_view'),
212
    'more_link' => $view->display_handler->get_option('use_more'),
213
    'feed_icons' => FALSE,
214
    'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
215
    'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
216
    'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
217
    'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
218
    'path_override' => FALSE,
219
    'path' => $view->get_path(),
220
    'fields_override' => $view->display_handler->get_option('fields_override'),
221
  );
222
}
223

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

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

    
240
  $view->set_display($display_id);
241

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

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

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

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

    
256
  foreach ($view->display_handler->get_argument_input() as $id => $argument) {
257
    if ($argument['type'] == 'user') {
258
      $form['arguments'][$id] = array(
259
        '#type' => 'textfield',
260
        '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
261
        '#description' => t('You may use keywords for substitutions.'),
262
        '#title' => check_plain($argument['label']),
263
      );
264
    }
265
  }
266
  if ($allow['link_to_view'] ) {
267
    $form['link_to_view'] = array(
268
      '#type' => 'checkbox',
269
      '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
270
      '#title' => t('Link title to page'),
271
    );
272
  }
273
  if ($allow['more_link']) {
274
    $form['more_link'] = array(
275
      '#type' => 'checkbox',
276
      '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
277
      '#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())),
278
      '#title' => t('Provide a "more" link.'),
279
    );
280
  }
281

    
282
  if (!empty($allow['feed_icons'])) {
283
    $form['feed_icons'] = array(
284
      '#type' => 'checkbox',
285
      '#default_value' => !empty($conf['feed_icons']),
286
      '#title' => t('Display feed icons'),
287
    );
288
  }
289

    
290
  $view->init_style();
291
  if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
292
    $form['fields_override'] = array(
293
      '#type' => 'fieldset',
294
      '#title' => 'Fields to display',
295
      '#collapsible' => TRUE,
296
      '#tree' => TRUE,
297
    );
298
    foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
299
      $title = $handler->ui_name();
300
      if ($handler->options['label']) {
301
        $title .= ' (' . check_plain($handler->options['label']) . ')';
302
      }
303

    
304
      $form['fields_override'][$field] = array(
305
        '#type' => 'checkbox',
306
        '#title' => $title,
307
        '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
308
      );
309
    }
310
  }
311

    
312
  ctools_include('dependent');
313
  if ($allow['use_pager']) {
314
    $form['use_pager'] = array(
315
      '#type' => 'checkbox',
316
      '#title' => t('Use pager'),
317
        '#default_value' => $conf['use_pager'],
318
      '#id' => 'use-pager-checkbox',
319
      '#prefix' => '<div class="container-inline">',
320
    );
321
    $form['pager_id'] = array(
322
      '#type' => 'textfield',
323
      '#default_value' => $conf['pager_id'],
324
      '#title' => t('Pager ID'),
325
      '#size' => 4,
326
      '#id' => 'use-pager-textfield',
327
      '#dependency' => array('use-pager-checkbox' => array(1)),
328
      '#suffix' => '</div>',
329
    );
330
  }
331
  if ($allow['items_per_page']) {
332
    $form['items_per_page'] = array(
333
      '#type' => 'textfield',
334
      '#default_value' => $conf['items_per_page'],
335
      '#title' => t('Num items'),
336
      '#size' => 4,
337
      '#description' => t('Select the number of items to display, or 0 to display all results.'),
338
    );
339
  }
340
  if ($allow['offset']) {
341
    $form['offset'] = array(
342
      '#type' => 'textfield',
343
      '#default_value' => $conf['offset'],
344
      '#title' => t('Offset'),
345
      '#size' => 4,
346
      '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
347
    );
348
  }
349
  if ($allow['path_override']) {
350
    $form['path'] = array(
351
      '#type' => 'textfield',
352
      '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
353
      '#title' => t('Override path'),
354
      '#size' => 30,
355
      '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
356
    );
357
    if (!empty($contexts)) {
358
      $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');
359

    
360
      $form['contexts'] = array(
361
        '#type' => 'fieldset',
362
        '#title' => t('Substitutions'),
363
        '#collapsible' => TRUE,
364
        '#collapsed' => TRUE,
365
      );
366

    
367
      $rows = array();
368
      foreach ($contexts as $context) {
369
        foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
370
          $rows[] = array(
371
            check_plain($keyword),
372
            t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
373
          );
374
        }
375
      }
376

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

    
382
  if (empty($conf['exposed'])) {
383
    $conf['exposed'] = array();
384
  }
385

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

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

    
396
    if (version_compare(views_api_version(), '3', '>=')) {
397
      $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
398
    }
399
    $view->init_handlers();
400
    $exposed_form = array();
401
    $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);
402

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

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

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

    
417
      if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
418
        $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
419
        $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
420
        $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
421
      }
422
      $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
423
      $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
424
      $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
425
    }
426
  }
427

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

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

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

    
441
  return $form;
442
}
443

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

    
452
  foreach ($keys as $key) {
453
    if (isset($form_state['values'][$key])) {
454
      $form_state['conf'][$key] = $form_state['values'][$key];
455
    }
456
  }
457
}
458

    
459

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

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

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

    
475
  return check_plain($title);
476
}
477

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

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

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

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

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

    
500
    foreach ($conf['arguments'] as $key => $value) {
501
      if (!empty($value)){
502
        $label = $argument_input[$key]['label'];
503
        $info[] = $label . ': ' . $value;
504
      }
505
    }
506
  }
507

    
508
  $block = new stdClass;
509
  if ($info) {
510
    $block->title = array_shift($info);
511

    
512
    $info[] = $view->display_handler->get_option('pane_description');
513
    $block->content = theme('item_list', array('items' => $info));
514
  }
515
  else {
516
    $block->title = $view->display_handler->get_option('pane_description');
517
    $block->content = '';
518
  }
519
  return $block;
520
}