Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / views_content / plugins / views / views_content_plugin_display_panel_pane.inc @ 651307cd

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * The plugin that handles a panel_pane.
5
 */
6
class views_content_plugin_display_panel_pane extends views_plugin_display {
7
  /**
8
   * If this variable is true, this display counts as a panel pane. We use
9
   * this variable so that other modules can create alternate pane displays.
10
   */
11
  var $panel_pane_display = TRUE;
12
  var $has_pane_conf = NULL;
13
14
  function option_definition() {
15
    $options = parent::option_definition();
16
17
    $options['pane_title'] = array('default' => '', 'translatable' => TRUE);
18
    $options['pane_description'] = array('default' => '', 'translatable' => TRUE);
19
    $options['pane_category'] = array(
20
      'contains' => array(
21
        'name' => array('default' => 'View panes', 'translatable' => TRUE),
22
        'weight' => array('default' => 0),
23
      ),
24
    );
25
26
    $options['allow'] = array(
27
      'contains' => array(
28
        'use_pager'       => array('default' => FALSE),
29
        'items_per_page'  => array('default' => FALSE),
30
        'offset'          => array('default' => FALSE),
31
        'link_to_view'    => array('default' => FALSE),
32
        'more_link'       => array('default' => FALSE),
33
        'path_override'   => array('default' => FALSE),
34
        'title_override'  => array('default' => FALSE),
35
        'exposed_form'    => array('default' => FALSE),
36
        'fields_override' => array('default' => FALSE),
37
       ),
38
    );
39
40
    $options['argument_input'] = array('default' => array());
41
    $options['link_to_view'] = array('default' => 0);
42
    $options['inherit_panels_path'] = array('default' => 0);
43
44
    return $options;
45
  }
46
47
  function has_pane_conf() {
48
    return isset($this->has_pane_conf);
49
  }
50
51 08475715 Assos Assos
  function set_pane_conf($conf = array(), $set_cache = TRUE) {
52 85ad3d82 Assos Assos
    $this->set_option('pane_conf', $conf);
53 08475715 Assos Assos
    $this->view->dom_id = !empty($this->view->dom_id) ? $this->view->dom_id : md5($this->view->name . REQUEST_TIME . rand());
54
    if ($set_cache) {
55
      cache_set('view_panel_pane_' . $this->view->dom_id, $conf);
56
    }
57 85ad3d82 Assos Assos
    $this->has_pane_conf = TRUE;
58
  }
59
60
  /**
61
   * Provide the summary for page options in the views UI.
62
   *
63
   * This output is returned as an array.
64
   */
65
  function options_summary(&$categories, &$options) {
66
    // It is very important to call the parent function here:
67
    parent::options_summary($categories, $options);
68
69
    $categories['panel_pane'] = array(
70
      'title' => t('Pane settings'),
71
      'column' => 'second',
72
      'build' => array(
73
        '#weight' => -10,
74
      ),
75
    );
76
77
    $pane_title = $this->get_option('pane_title');
78
    if (empty($pane_title)) {
79
      $pane_title = t('Use view name');
80
    }
81
82
    if (drupal_strlen($pane_title) > 16) {
83
      $pane_title = drupal_substr($pane_title, 0, 16) . '...';
84
    }
85
86
    $options['pane_title'] = array(
87
      'category' => 'panel_pane',
88
      'title' => t('Admin title'),
89
      'value' => $pane_title,
90
    );
91
92
    $pane_description = $this->get_option('pane_description');
93
    if (empty($pane_description)) {
94
      $pane_description = t('Use view description');
95
    }
96
97
    if (drupal_strlen($pane_description) > 16) {
98
      $pane_description = drupal_substr($pane_description, 0, 16) . '...';
99
    }
100
101
    $options['pane_description'] = array(
102
      'category' => 'panel_pane',
103
      'title' => t('Admin desc'),
104
      'value' => $pane_description,
105
    );
106
107
    $category = $this->get_option('pane_category');
108
    $pane_category = $category['name'];
109
    if (empty($pane_category)) {
110
      $pane_category = t('View panes');
111
    }
112
113
    if (drupal_strlen($pane_category) > 16) {
114
      $pane_category = drupal_substr($pane_category, 0, 16) . '...';
115
    }
116
117
    $options['pane_category'] = array(
118
      'category' => 'panel_pane',
119
      'title' => t('Category'),
120
      'value' => $pane_category,
121
    );
122
123
    $options['link_to_view'] = array(
124
      'category' => 'panel_pane',
125
      'title' => t('Link to view'),
126
      'value' => $this->get_option('link_to_view') ? t('Yes') : t('No'),
127
    );
128
129
    $options['inherit_panels_path'] = array(
130
      'category' => 'panel_pane',
131
      'title' => t('Use Panel path'),
132
      'value' => $this->get_option('inherit_panels_path') ? t('Yes') : t('No'),
133
    );
134
135
    $options['argument_input'] = array(
136
      'category' => 'panel_pane',
137
      'title' => t('Argument input'),
138
      'value' => t('Edit'),
139
    );
140
141
    $allow = $this->get_option('allow');
142
    $filtered_allow = array_filter($allow);
143
144
    $options['allow'] = array(
145
      'category' => 'panel_pane',
146
      'title' => t('Allow settings'),
147
      'value' => empty($filtered_allow) ? t('None') : ($allow === $filtered_allow ? t('All') : t('Some')),
148
    );
149
  }
150
151
  /**
152
   * Provide the default form for setting options.
153
   */
154
  function options_form(&$form, &$form_state) {
155
    // It is very important to call the parent function here:
156
    parent::options_form($form, $form_state);
157
158
    switch ($form_state['section']) {
159
      case 'allow':
160
        $form['#title'] .= t('Allow settings');
161
        $form['description'] = array(
162
          '#value' => '<div class="form-item description">' . t('Checked settings will be available in the panel pane config dialog for modification by the panels user. Unchecked settings will not be available and will only use the settings in this display.') . '</div>',
163
        );
164
165
        $options = array(
166
          'use_pager' => t('Use pager'),
167
          'items_per_page' => t('Items per page'),
168
          'offset' => t('Pager offset'),
169
          'link_to_view' => t('Link to view'),
170
          'more_link' => t('More link'),
171
          'path_override' => t('Path override'),
172
          'title_override' => t('Title override'),
173
          'exposed_form' => t('Use exposed widgets form as pane configuration'),
174
          'fields_override' => t('Fields override'),
175
        );
176
177
        $allow = array_filter($this->get_option('allow'));
178
        $form['allow'] = array(
179
          '#type' => 'checkboxes',
180
          '#default_value' => $allow,
181
          '#options' => $options,
182
        );
183
        break;
184
      case 'pane_title':
185
        $form['#title'] .= t('Administrative title');
186
187
        $form['pane_title'] = array(
188
          '#type' => 'textfield',
189
          '#default_value' => $this->get_option('pane_title'),
190
          '#description' => t('This is the title that will appear for this view pane in the add content dialog. If left blank, the view name will be used.'),
191
        );
192
        break;
193
194
      case 'pane_description':
195
        $form['#title'] .= t('Administrative description');
196
197
        $form['pane_description'] = array(
198
          '#type' => 'textfield',
199
          '#default_value' => $this->get_option('pane_description'),
200
          '#description' => t('This is text that will be displayed when the user mouses over the pane in the add content dialog. If blank the view description will be used.'),
201
        );
202
        break;
203
204
      case 'pane_category':
205
        $form['#title'] .= t('Administrative description');
206
207
        $cat = $this->get_option('pane_category');
208
        $form['pane_category']['#tree'] = TRUE;
209
        $form['pane_category']['name'] = array(
210
          '#type' => 'textfield',
211
          '#default_value' => $cat['name'],
212
          '#description' => t('This is category the pane will appear in on the add content dialog.'),
213
        );
214
        $form['pane_category']['weight'] = array(
215
          '#title' => t('Weight'),
216
          '#type' => 'textfield',
217
          '#default_value' => $cat['weight'],
218
          '#description' => t('This is the default weight of the category. Note that if the weight of a category is defined in multiple places, only the first one Panels sees will get that definition, so if the weight does not appear to be working, check other places that the weight might be set.'),
219
        );
220
        break;
221
222
      case 'link_to_view':
223
        $form['#title'] .= t('Link pane title to view');
224
225
        $form['link_to_view'] = array(
226
          '#type' => 'select',
227
          '#options' => array(1 => t('Yes'), 0 => t('No')),
228
          '#default_value' => $this->get_option('link_to_view'),
229
        );
230
        break;
231
232
      case 'inherit_panels_path':
233
        $form['#title'] .= t('Inherit path from panel display');
234
235
        $form['inherit_panels_path'] = array(
236
          '#type' => 'select',
237
          '#options' => array(1 => t('Yes'), 0 => t('No')),
238
          '#default_value' => $this->get_option('inherit_panels_path'),
239
          '#description' => t('If yes, all links generated by Views, such as more links, summary links, and exposed input links will go to the panels display path, not the view, if the display has a path.'),
240
        );
241
        break;
242
243
      case 'argument_input':
244
        $form['#title'] .= t('Choose the data source for view arguments');
245
        $argument_input = $this->get_argument_input();
246
        ctools_include('context');
247
        ctools_include('dependent');
248
        $form['argument_input']['#tree'] = TRUE;
249
250
        $converters = ctools_context_get_all_converters();
251
        ksort($converters);
252
253
        foreach ($argument_input as $id => $argument) {
254
          $form['argument_input'][$id] = array(
255
            '#tree' => TRUE,
256
          );
257
258
          $safe = str_replace(array('][', '_', ' '), '-', $id);
259
          $type_id = 'edit-argument-input-' . $safe;
260
261
          $form['argument_input'][$id]['type'] = array(
262
            '#type' => 'select',
263
            '#options' => array(
264
              'none' => t('No argument'),
265
              'wildcard' => t('Argument wildcard'),
266
              'context' => t('From context'),
267
              'panel' => t('From panel argument'),
268
              'fixed' => t('Fixed'),
269
              'user' => t('Input on pane config'),
270
            ),
271
            '#id' => $type_id,
272
            '#title' => t('@arg source', array('@arg' => $argument['name'])),
273
            '#default_value' => $argument['type'],
274
          );
275
          $form['argument_input'][$id]['context'] = array(
276
            '#type' => 'select',
277
            '#title' => t('Required context'),
278
            '#description' => t('If "From context" is selected, which type of context to use.'),
279
            '#default_value' => $argument['context'],
280
            '#options' => $converters,
281
            '#dependency' => array($type_id => array('context')),
282
          );
283
284
          $form['argument_input'][$id]['context_optional'] = array(
285
            '#type' => 'checkbox',
286
            '#title' => t('Context is optional'),
287
            '#description' => t('This context need not be present for the pane to function. If you plan to use this, ensure that the argument handler can handle empty values gracefully.'),
288
            '#default_value' => $argument['context_optional'],
289
            '#dependency' => array($type_id => array('context')),
290
          );
291
292
          $form['argument_input'][$id]['panel'] = array(
293
            '#type' => 'select',
294
            '#title' => t('Panel argument'),
295
            '#description' => t('If "From panel argument" is selected, which panel argument to use.'),
296
            '#default_value' => $argument['panel'],
297
            '#options' => array(0 => t('First'), 1 => t('Second'), 2 => t('Third'), 3 => t('Fourth'), 4 => t('Fifth'), 5 => t('Sixth')),
298
            '#dependency' => array($type_id => array('panel')),
299
          );
300
301
          $form['argument_input'][$id]['fixed'] = array(
302
            '#type' => 'textfield',
303
            '#title' => t('Fixed argument'),
304
            '#description' => t('If "Fixed" is selected, what to use as an argument.'),
305
            '#default_value' => $argument['fixed'],
306
            '#dependency' => array($type_id => array('fixed')),
307
          );
308
309
          $form['argument_input'][$id]['label'] = array(
310
            '#type' => 'textfield',
311
            '#title' => t('Label'),
312
            '#description' => t('If this argument is presented to the panels user, what label to apply to it.'),
313
            '#default_value' => empty($argument['label']) ? $argument['name'] : $argument['label'],
314
            '#dependency' => array($type_id => array('user')),
315
          );
316
        }
317
        break;
318
    }
319
  }
320
321
  /**
322
   * Perform any necessary changes to the form values prior to storage.
323
   * There is no need for this function to actually store the data.
324
   */
325
  function options_submit(&$form, &$form_state) {
326
    // It is very important to call the parent function here:
327
    parent::options_submit($form, $form_state);
328
    switch ($form_state['section']) {
329
      case 'allow':
330
      case 'argument_input':
331
      case 'link_to_view':
332
      case 'inherit_panels_path':
333
      case 'pane_title':
334
      case 'pane_description':
335
      case 'pane_category':
336
        $this->set_option($form_state['section'], $form_state['values'][$form_state['section']]);
337
        break;
338
    }
339
  }
340
341
  /**
342
   * Adjust the array of argument input to match the current list of
343
   * arguments available for this display. This ensures that changing
344
   * the arguments doesn't cause the argument input field to just
345
   * break.
346
   */
347
  function get_argument_input() {
348
    $arguments = $this->get_option('argument_input');
349
    $handlers = $this->get_handlers('argument');
350
351
    // We use a separate output so as to seamlessly discard info for
352
    // arguments that no longer exist.
353
    $output = array();
354
355
    foreach ($handlers as $id => $handler) {
356
      if (empty($arguments[$id])) {
357
        $output[$id] = array(
358
          'type' => 'none',
359
          'context' => 'any',
360
          'context_optional' => FALSE,
361
          'panel' => 0,
362
          'fixed' => '',
363
          'name' => $handler->ui_name(),
364
        );
365
      }
366
      else {
367
        $output[$id] = $arguments[$id];
368
        $output[$id]['name'] = $handler->ui_name();
369
      }
370
    }
371
372
    return $output;
373
  }
374
375
  function use_more() {
376
    $allow = $this->get_option('allow');
377
    if (!$allow['more_link'] || !$this->has_pane_conf()) {
378
      return parent::use_more();
379
    }
380
    $conf = $this->get_option('pane_conf');
381
    return (bool) $conf['more_link'];
382
  }
383
384
  function get_path() {
385
    if (empty($this->view->override_path)) {
386
      return parent::get_path();
387
    }
388
    return $this->view->override_path;
389
  }
390
391
  function get_url() {
392
    if ($this->get_option('inherit_panels_path')) {
393
      return $this->get_path();
394
    }
395
    return parent::get_url();
396
  }
397
398
  function uses_exposed_form_in_block() {
399
    // We'll always allow the exposed form in a block, regardless of path.
400
    return TRUE;
401
  }
402
403
  /**
404
   * Determine if this display should display the exposed
405
   * filters widgets, so the view will know whether or not
406
   * to render them.
407
   *
408
   * Regardless of what this function
409
   * returns, exposed filters will not be used nor
410
   * displayed unless uses_exposed() returns TRUE.
411
   */
412
  function displays_exposed() {
413
    $conf = $this->get_option('allow');
414
    // If this is set, the exposed form is part of pane configuration, not
415
    // rendered normally.
416
    return empty($conf['exposed_form']);
417
  }
418
419 08475715 Assos Assos
  /**
420
   * Set the pane configuration options
421
   * Done here in handler for reuse between views_content_views_panes_content_type_render
422
   * and views_content_views_pre_view
423
   */
424
  function pane_process_conf() {
425
    ctools_include('context');
426
    $conf = $this->get_option('pane_conf');
427
    if (!$conf) {
428
      // See if dom id has been cached and get conf from there.
429
      if (!empty($this->view->dom_id) && ($cache = cache_get('view_panel_pane_' . $this->view->dom_id))) {
430
        $conf = $cache->data;
431
      }
432
      if (!$conf) {
433
        return;
434
      }
435
      $this->set_pane_conf($conf);
436
    }
437
    $contexts = $conf['pane_contexts'];
438
    $panel_args = $conf['panel_args'];
439
    $args = array();
440
    $arguments = $this->get_option('arguments');
441
442
    $context_keys = isset($conf['context']) ? $conf['context'] : array();
443
    foreach ($this->get_argument_input() as $id => $argument) {
444
      switch ($argument['type']) {
445
        case 'context':
446
          $key = array_shift($context_keys);
447
          if (isset($contexts[$key])) {
448
            if (strpos($argument['context'], '.')) {
449
              list($context, $converter) = explode('.', $argument['context'], 2);
450
              $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
451
            }
452
            else {
453
              $args[] = $contexts[$key]->argument;
454
            }
455
          }
456
          else {
457
            $args[] = isset($arguments[$id]['exception']['value']) ? $arguments[$id]['exception']['value'] : 'all';
458
          }
459
          break;
460
461
        case 'fixed':
462
          $args[] = $argument['fixed'];
463
          break;
464
465
        case 'panel':
466
          $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
467
          break;
468
469
        case 'user':
470
          $args[] = (isset($conf['arguments'][$id])  && $conf['arguments'][$id] !== '') ? ctools_context_keyword_substitute($conf['arguments'][$id], array(), $contexts) : NULL;
471
          break;
472
473
       case 'wildcard':
474
          // Put in the wildcard.
475
         $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
476
         break;
477
478
       case 'none':
479
       default:
480
         // Put in NULL.
481
         // views.module knows what to do with NULL (or missing) arguments
482
         $args[] = NULL;
483
         break;
484
      }
485
    }
486
487
    // remove any trailing NULL arguments as these are non-args:
488
    while (count($args) && end($args) === NULL) {
489
      array_pop($args);
490
    }
491
492
    $this->view->set_arguments($args);
493
    $allow = $this->get_option('allow');
494
495
    if (!empty($conf['path'])) {
496
      $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
497
    }
498
    if ($allow['path_override'] && !empty($conf['path'])) {
499
      $this->view->override_path = $conf['path'];
500
    }
501
    else if ($path = $this->get_option('inherit_panels_path')) {
502
      $this->view->override_path = $_GET['q'];
503
    }
504
505
    // more link
506
    if ($allow['more_link']) {
507
      if (empty($conf['more_link'])) {
508
        $this->set_option('use_more', FALSE);
509
      }
510
      else {
511
        $this->set_option('use_more', TRUE);
512
        // make sure the view runs the count query so we know whether or not the
513
        // more link applies.
514
        $this->view->get_total_rows = TRUE;
515
      }
516
    }
517
518
    if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
519
      $this->view->set_items_per_page($conf['items_per_page']);
520
    }
521
522
    if ($allow['offset']) {
523
      $this->view->set_offset($conf['offset']);
524
    }
525
526
    if ($allow['use_pager']) {
527
      // Only set use_pager if they differ, this way we can avoid overwriting the
528
      // pager type that Views uses.
529
      $pager = $this->get_option('pager');
530
      if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
531
        $pager['type'] = 'full';
532
      }
533
      elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
534
        $pager['type'] = $this->view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
535
      }
536
537
      if ($conf['use_pager']) {
538
        if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
539
          $pager['options']['id'] = (int) $conf['pager_id'];
540
        }
541
      }
542
543
      $this->set_option('pager', $pager);
544
    }
545
546
    if ($allow['fields_override']) {
547
      if ($conf['fields_override']) {
548
        $fields = $this->view->get_items('field');
549
        foreach ($fields as $field => $field_display) {
550
          $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
551
        }
552
        $this->set_option('fields', $fields);
553
554
      }
555
    }
556
557
    if ($allow['exposed_form'] && !empty($conf['exposed'])) {
558
      foreach ($conf['exposed'] as $filter_name => $filter_value) {
559
        if (!is_array($filter_value)) {
560
          $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
561
        }
562
      }
563
      $this->view->set_exposed_input($conf['exposed']);
564
    }
565
  }
566
567 85ad3d82 Assos Assos
}