Projet

Général

Profil

Paste
Télécharger (21 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_page.inc @ 6eb8d15f

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains the page display plugin.
6
 */
7

    
8
/**
9
 * The plugin that handles a full page.
10
 *
11
 * @ingroup views_display_plugins
12
 */
13
class views_plugin_display_page extends views_plugin_display {
14
  /**
15
   * The page display has a path.
16
   */
17
  function has_path() { return TRUE; }
18
  function uses_breadcrumb() { return TRUE; }
19

    
20
  function option_definition() {
21
    $options = parent::option_definition();
22

    
23
    $options['path'] = array('default' => '');
24
    $options['menu'] = array(
25
      'contains' => array(
26
        'type' => array('default' => 'none'),
27
        // Do not translate menu and title as menu system will.
28
        'title' => array('default' => '', 'translatable' => FALSE),
29
        'description' => array('default' => '', 'translatable' => FALSE),
30
        'weight' => array('default' => 0),
31
        'name' => array('default' => variable_get('menu_default_node_menu', 'navigation')),
32
        'context' => array('default' => ''),
33
        'context_only_inline' => array('default' => FALSE),
34
       ),
35
    );
36
    $options['tab_options'] = array(
37
      'contains' => array(
38
        'type' => array('default' => 'none'),
39
        // Do not translate menu and title as menu system will.
40
        'title' => array('default' => '', 'translatable' => FALSE),
41
        'description' => array('default' => '', 'translatable' => FALSE),
42
        'weight' => array('default' => 0),
43
        'name' => array('default' => 'navigation'),
44
       ),
45
    );
46

    
47
    return $options;
48
  }
49

    
50
  /**
51
   * Add this display's path information to Drupal's menu system.
52
   */
53
  function execute_hook_menu($callbacks) {
54
    $items = array();
55
    // Replace % with the link to our standard views argument loader
56
    // views_arg_load -- which lives in views.module
57

    
58
    $bits = explode('/', $this->get_option('path'));
59
    $page_arguments = array($this->view->name, $this->display->id);
60
    $this->view->init_handlers();
61
    $view_arguments = $this->view->argument;
62

    
63
    // Replace % with %views_arg for menu autoloading and add to the
64
    // page arguments so the argument actually comes through.
65
    foreach ($bits as $pos => $bit) {
66
      if ($bit == '%') {
67
        $argument = array_shift($view_arguments);
68
        if (!empty($argument->options['specify_validation']) && $argument->options['validate']['type'] != 'none') {
69
          $bits[$pos] = '%views_arg';
70
        }
71
        $page_arguments[] = $pos;
72
      }
73
    }
74

    
75
    $path = implode('/', $bits);
76

    
77
    $access_plugin = $this->get_plugin('access');
78
    if (!isset($access_plugin)) {
79
      $access_plugin = views_get_plugin('access', 'none');
80
    }
81

    
82
    // Get access callback might return an array of the callback + the dynamic arguments.
83
    $access_plugin_callback = $access_plugin->get_access_callback();
84

    
85
    if (is_array($access_plugin_callback)) {
86
      $access_arguments = array();
87

    
88
      // Find the plugin arguments.
89
      $access_plugin_method = array_shift($access_plugin_callback);
90
      $access_plugin_arguments = array_shift($access_plugin_callback);
91
      if (!is_array($access_plugin_arguments)) {
92
        $access_plugin_arguments = array();
93
      }
94

    
95
      $access_arguments[0] = array($access_plugin_method, &$access_plugin_arguments);
96

    
97
      // Move the plugin arguments to the access arguments array.
98
      $i = 1;
99
      foreach ($access_plugin_arguments as $key => $value) {
100
        if (is_int($value)) {
101
          $access_arguments[$i] = $value;
102
          $access_plugin_arguments[$key] = $i;
103
          $i++;
104
        }
105
      }
106
    }
107
    else {
108
      $access_arguments = array($access_plugin_callback);
109
    }
110

    
111
    if ($path) {
112
      $items[$path] = array(
113
        // default views page entry
114
        'page callback' => 'views_page',
115
        'page arguments' => $page_arguments,
116
        // Default access check (per display)
117
        'access callback' => 'views_access',
118
        'access arguments' => $access_arguments,
119
        // Identify URL embedded arguments and correlate them to a handler
120
        'load arguments'  => array($this->view->name, $this->display->id, '%index'),
121
      );
122
      $menu = $this->get_option('menu');
123
      if (empty($menu)) {
124
        $menu = array('type' => 'none');
125
      }
126
      // Set the title and description if we have one.
127
      if ($menu['type'] != 'none') {
128
        $items[$path]['title'] = $menu['title'];
129
        $items[$path]['description'] = $menu['description'];
130
      }
131

    
132
      if (isset($menu['weight'])) {
133
        $items[$path]['weight'] = intval($menu['weight']);
134
      }
135

    
136
      switch ($menu['type']) {
137
        case 'none':
138
        default:
139
          $items[$path]['type'] = MENU_CALLBACK;
140
          break;
141
        case 'normal':
142
          $items[$path]['type'] = MENU_NORMAL_ITEM;
143
          // Insert item into the proper menu
144
          $items[$path]['menu_name'] = $menu['name'];
145
          break;
146
        case 'tab':
147
          $items[$path]['type'] = MENU_LOCAL_TASK;
148
          break;
149
        case 'default tab':
150
          $items[$path]['type'] = MENU_DEFAULT_LOCAL_TASK;
151
          break;
152
      }
153

    
154
      // Add context for contextual links.
155
      // @see menu_contextual_links()
156
      if (!empty($menu['context'])) {
157
        $items[$path]['context'] = !empty($menu['context_only_inline']) ? MENU_CONTEXT_INLINE : (MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE);
158
      }
159

    
160
      // If this is a 'default' tab, check to see if we have to create teh
161
      // parent menu item.
162
      if ($menu['type'] == 'default tab') {
163
        $tab_options = $this->get_option('tab_options');
164
        if (!empty($tab_options['type']) && $tab_options['type'] != 'none') {
165
          $bits = explode('/', $path);
166
          // Remove the last piece.
167
          $bit = array_pop($bits);
168

    
169
          // we can't do this if they tried to make the last path bit variable.
170
          // @todo: We can validate this.
171
          if ($bit != '%views_arg' && !empty($bits)) {
172
            $default_path = implode('/', $bits);
173
            $items[$default_path] = array(
174
              // default views page entry
175
              'page callback' => 'views_page',
176
              'page arguments' => $page_arguments,
177
              // Default access check (per display)
178
              'access callback' => 'views_access',
179
              'access arguments' => $access_arguments,
180
              // Identify URL embedded arguments and correlate them to a handler
181
              'load arguments'  => array($this->view->name, $this->display->id, '%index'),
182
              'title' => $tab_options['title'],
183
              'description' => $tab_options['description'],
184
              'menu_name' => $tab_options['name'],
185
            );
186
            switch ($tab_options['type']) {
187
              default:
188
              case 'normal':
189
                $items[$default_path]['type'] = MENU_NORMAL_ITEM;
190
                break;
191
              case 'tab':
192
                $items[$default_path]['type'] = MENU_LOCAL_TASK;
193
                break;
194
            }
195
            if (isset($tab_options['weight'])) {
196
              $items[$default_path]['weight'] = intval($tab_options['weight']);
197
            }
198
          }
199
        }
200
      }
201
    }
202

    
203
    return $items;
204
  }
205

    
206
  /**
207
   * The display page handler returns a normal view, but it also does
208
   * a drupal_set_title for the page, and does a views_set_page_view
209
   * on the view.
210
   */
211
  function execute() {
212
    // Let the world know that this is the page view we're using.
213
    views_set_page_view($this->view);
214

    
215
    // Prior to this being called, the $view should already be set to this
216
    // display, and arguments should be set on the view.
217
    $this->view->build();
218
    if (!empty($this->view->build_info['fail'])) {
219
      return MENU_NOT_FOUND;
220
    }
221

    
222
    if (!empty($this->view->build_info['denied'])) {
223
      return MENU_ACCESS_DENIED;
224
    }
225

    
226
    $this->view->get_breadcrumb(TRUE);
227

    
228

    
229
    // And now render the view.
230
    $render = $this->view->render();
231

    
232
    // First execute the view so it's possible to get tokens for the title.
233
    // And the title, which is much easier.
234
    drupal_set_title(filter_xss_admin($this->view->get_title()), PASS_THROUGH);
235
    return $render;
236
  }
237

    
238
  /**
239
   * Provide the summary for page options in the views UI.
240
   *
241
   * This output is returned as an array.
242
   */
243
  function options_summary(&$categories, &$options) {
244
    // It is very important to call the parent function here:
245
    parent::options_summary($categories, $options);
246

    
247
    $categories['page'] = array(
248
      'title' => t('Page settings'),
249
      'column' => 'second',
250
      'build' => array(
251
        '#weight' => -10,
252
      ),
253
    );
254

    
255
    $path = strip_tags($this->get_option('path'));
256
    if (empty($path)) {
257
      $path = t('No path is set');
258
    }
259
    else {
260
      $path = '/' . $path;
261
    }
262

    
263
    $options['path'] = array(
264
      'category' => 'page',
265
      'title' => t('Path'),
266
      'value' => views_ui_truncate($path, 24),
267
    );
268

    
269
    $menu = $this->get_option('menu');
270
    if (!is_array($menu)) {
271
      $menu = array('type' => 'none');
272
    }
273
    switch($menu['type']) {
274
      case 'none':
275
      default:
276
        $menu_str = t('No menu');
277
        break;
278
      case 'normal':
279
        $menu_str = t('Normal: @title', array('@title' => $menu['title']));
280
        break;
281
      case 'tab':
282
      case 'default tab':
283
        $menu_str = t('Tab: @title', array('@title' => $menu['title']));
284
        break;
285
    }
286

    
287
    $options['menu'] = array(
288
      'category' => 'page',
289
      'title' => t('Menu'),
290
      'value' => views_ui_truncate($menu_str, 24),
291
    );
292

    
293
    // This adds a 'Settings' link to the style_options setting if the style has options.
294
    if ($menu['type'] == 'default tab') {
295
      $options['menu']['setting'] = t('Parent menu item');
296
      $options['menu']['links']['tab_options'] = t('Change settings for the parent menu');
297
    }
298
  }
299

    
300
  /**
301
   * Provide the default form for setting options.
302
   */
303
  function options_form(&$form, &$form_state) {
304
    // It is very important to call the parent function here:
305
    parent::options_form($form, $form_state);
306

    
307
    switch ($form_state['section']) {
308
      case 'path':
309
        $form['#title'] .= t('The menu path or URL of this view');
310
        $form['#help_topic'] = 'path';
311
        $form['path'] = array(
312
          '#type' => 'textfield',
313
          '#description' => t('This view will be displayed by visiting this path on your site. You may use "%" in your URL to represent values that will be used for contextual filters: For example, "node/%/feed".'),
314
          '#default_value' => $this->get_option('path'),
315
	  '#field_prefix' => '<span dir="ltr">' . url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
316
	  '#field_suffix' => '</span>&lrm;',
317
	  '#attributes' => array('dir'=>'ltr'),
318
        );
319
        break;
320
      case 'menu':
321
        $form['#title'] .= t('Menu item entry');
322
        $form['#help_topic'] = 'menu';
323
        $form['menu'] = array(
324
          '#prefix' => '<div class="clearfix">',
325
          '#suffix' => '</div>',
326
          '#tree' => TRUE,
327
        );
328
        $menu = $this->get_option('menu');
329
        if (empty($menu)) {
330
          $menu = array('type' => 'none', 'title' => '', 'weight' => 0);
331
        }
332
        $form['menu']['type'] = array(
333
          '#prefix' => '<div class="views-left-30">',
334
          '#suffix' => '</div>',
335
          '#title' => t('Type'),
336
          '#type' => 'radios',
337
          '#options' => array(
338
            'none' => t('No menu entry'),
339
            'normal' => t('Normal menu entry'),
340
            'tab' => t('Menu tab'),
341
            'default tab' => t('Default menu tab')
342
          ),
343
          '#default_value' => $menu['type'],
344
        );
345
        $form['menu']['title'] = array(
346
          '#prefix' => '<div class="views-left-50">',
347
          '#title' => t('Title'),
348
          '#type' => 'textfield',
349
          '#default_value' => $menu['title'],
350
          '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
351
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
352
        );
353
        $form['menu']['description'] = array(
354
          '#title' => t('Description'),
355
          '#type' => 'textfield',
356
          '#default_value' => $menu['description'],
357
          '#description' => t("If set to normal or tab, enter the text to use for the menu item's description."),
358
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
359
        );
360

    
361
        // Only display the menu selector if menu module is enabled.
362
        if (module_exists('menu')) {
363
          $form['menu']['name'] = array(
364
            '#title' => t('Menu'),
365
            '#type' => 'select',
366
            '#options' => menu_get_menus(),
367
            '#default_value' => $menu['name'],
368
            '#description' => t('Insert item into an available menu.'),
369
            '#dependency' => array('radio:menu[type]' => array('normal', 'tab')),
370
          );
371
        }
372
        else {
373
          $form['menu']['name'] = array(
374
            '#type' => 'value',
375
            '#value' => $menu['name'],
376
          );
377
          $form['menu']['markup'] = array(
378
            '#markup' => t('Menu selection requires the activation of menu module.'),
379
          );
380
        }
381
        $form['menu']['weight'] = array(
382
          '#title' => t('Weight'),
383
          '#type' => 'textfield',
384
          '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
385
          '#description' => t('The lower the weight the higher/further left it will appear.'),
386
          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
387
        );
388
        $form['menu']['context'] = array(
389
          '#title' => t('Context'),
390
          '#type' => 'checkbox',
391
          '#default_value' => !empty($menu['context']),
392
          '#description' => t('Displays the link in contextual links'),
393
          '#dependency' => array('radio:menu[type]' => array('tab')),
394
        );
395
        $form['menu']['context_only_inline'] = array(
396
          '#title' => t('Hide menu tab'),
397
          '#suffix' => '</div>',
398
          '#type' => 'checkbox',
399
          '#default_value' => !empty($menu['context_only_inline']),
400
          '#description' => t('Only display menu item entry in contextual links. Menu tab should not be displayed.'),
401
          '#dependency' => array(
402
            'radio:menu[type]' => array('tab'),
403
            'edit-menu-context' => array(1),
404
          ),
405
          '#dependency_count' => 2,
406
        );
407
        break;
408
      case 'tab_options':
409
        $form['#title'] .= t('Default tab options');
410
        $tab_options = $this->get_option('tab_options');
411
        if (empty($tab_options)) {
412
          $tab_options = array('type' => 'none', 'title' => '', 'weight' => 0);
413
        }
414

    
415
        $form['tab_markup'] = array(
416
          '#markup' => '<div class="form-item description">' . t('When providing a menu item as a tab, Drupal needs to know what the parent menu item of that tab will be. Sometimes the parent will already exist, but other times you will need to have one created. The path of a parent item will always be the same path with the last part left off. i.e, if the path to this view is <em>foo/bar/baz</em>, the parent path would be <em>foo/bar</em>.') . '</div>',
417
        );
418

    
419
        $form['tab_options'] = array(
420
          '#prefix' => '<div class="clearfix">',
421
          '#suffix' => '</div>',
422
          '#tree' => TRUE,
423
        );
424
        $form['tab_options']['type'] = array(
425
          '#prefix' => '<div class="views-left-25">',
426
          '#suffix' => '</div>',
427
          '#title' => t('Parent menu item'),
428
          '#type' => 'radios',
429
          '#options' => array('none' => t('Already exists'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
430
          '#default_value' => $tab_options['type'],
431
        );
432
        $form['tab_options']['title'] = array(
433
          '#prefix' => '<div class="views-left-75">',
434
          '#title' => t('Title'),
435
          '#type' => 'textfield',
436
          '#default_value' => $tab_options['title'],
437
          '#description' => t('If creating a parent menu item, enter the title of the item.'),
438
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
439
        );
440
        $form['tab_options']['description'] = array(
441
          '#title' => t('Description'),
442
          '#type' => 'textfield',
443
          '#default_value' => $tab_options['description'],
444
          '#description' => t('If creating a parent menu item, enter the description of the item.'),
445
          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
446
        );
447
        // Only display the menu selector if menu module is enabled.
448
        if (module_exists('menu')) {
449
          $form['tab_options']['name'] = array(
450
            '#title' => t('Menu'),
451
            '#type' => 'select',
452
            '#options' => menu_get_menus(),
453
            '#default_value' => $tab_options['name'],
454
            '#description' => t('Insert item into an available menu.'),
455
            '#dependency' => array('radio:tab_options[type]' => array('normal')),
456
          );
457
        }
458
        else {
459
          $form['tab_options']['name'] = array(
460
            '#type' => 'value',
461
            '#value' => $tab_options['name'],
462
          );
463
          $form['tab_options']['markup'] = array(
464
            '#markup' => t('Menu selection requires the activation of menu module.'),
465
          );
466
        }
467
        $form['tab_options']['weight'] = array(
468
          '#suffix' => '</div>',
469
          '#title' => t('Tab weight'),
470
          '#type' => 'textfield',
471
          '#default_value' => $tab_options['weight'],
472
          '#size' => 5,
473
          '#description' => t('If the parent menu item is a tab, enter the weight of the tab. The lower the number, the more to the left it will be.'),
474
          '#dependency' => array('radio:tab_options[type]' => array('tab')),
475
        );
476
        break;
477
    }
478
  }
479

    
480
  function options_validate(&$form, &$form_state) {
481
    // It is very important to call the parent function here:
482
    parent::options_validate($form, $form_state);
483
    switch ($form_state['section']) {
484
      case 'path':
485
        if (strpos($form_state['values']['path'], '$arg') !== FALSE) {
486
          form_error($form['path'], t('"$arg" is no longer supported. Use % instead.'));
487
        }
488

    
489
        if (strpos($form_state['values']['path'], '%') === 0) {
490
          form_error($form['path'], t('"%" may not be used for the first segment of a path.'));
491
        }
492

    
493
        // automatically remove '/' and trailing whitespace from path.
494
        $form_state['values']['path'] = trim($form_state['values']['path'], '/ ');
495
        break;
496
      case 'menu':
497
        $path = $this->get_option('path');
498
        if ($form_state['values']['menu']['type'] == 'normal' && strpos($path, '%') !== FALSE) {
499
          form_error($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
500
        }
501

    
502
        if ($form_state['values']['menu']['type'] == 'default tab' || $form_state['values']['menu']['type'] == 'tab') {
503
          $bits = explode('/', $path);
504
          $last = array_pop($bits);
505
          if ($last == '%') {
506
            form_error($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
507
          }
508
        }
509

    
510
        if ($form_state['values']['menu']['type'] != 'none' && empty($form_state['values']['menu']['title'])) {
511
          form_error($form['menu']['title'], t('Title is required for this menu type.'));
512
        }
513
        break;
514
    }
515
  }
516

    
517
  function options_submit(&$form, &$form_state) {
518
    // It is very important to call the parent function here:
519
    parent::options_submit($form, $form_state);
520
    switch ($form_state['section']) {
521
      case 'path':
522
        $this->set_option('path', $form_state['values']['path']);
523
        break;
524
      case 'menu':
525
        $this->set_option('menu', $form_state['values']['menu']);
526
        // send ajax form to options page if we use it.
527
        if ($form_state['values']['menu']['type'] == 'default tab') {
528
          views_ui_add_form_to_stack('display', $this->view, $this->display->id, array('tab_options'));
529
        }
530
        break;
531
      case 'tab_options':
532
        $this->set_option('tab_options', $form_state['values']['tab_options']);
533
        break;
534
    }
535
  }
536

    
537
  function validate() {
538
    $errors = parent::validate();
539

    
540
    $menu = $this->get_option('menu');
541
    if (!empty($menu['type']) && $menu['type'] != 'none' && empty($menu['title'])) {
542
      $errors[] = t('Display @display is set to use a menu but the menu link text is not set.', array('@display' => $this->display->display_title));
543
    }
544

    
545
    if ($menu['type'] == 'default tab') {
546
      $tab_options = $this->get_option('tab_options');
547
      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
548
        $errors[] = t('Display @display is set to use a parent menu but the parent menu link text is not set.', array('@display' => $this->display->display_title));
549
      }
550
    }
551

    
552
    return $errors;
553
  }
554

    
555
  function get_argument_text() {
556
    return array(
557
      'filter value not present' => t('When the filter value is <em>NOT</em> in the URL'),
558
      'filter value present' => t('When the filter value <em>IS</em> in the URL or a default is provided'),
559
      'description' => t('The contextual filter values is provided by the URL.'),
560
    );
561
  }
562

    
563
  function get_pager_text() {
564
    return array(
565
      'items per page title' => t('Items per page'),
566
      'items per page description' => t('The number of items to display per page. Enter 0 for no limit.')
567
    );
568
  }
569
}