Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / plugins / views_plugin_display_page.inc @ 7547bb19

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
        // Make sure the menu router knows where views_page is.
122
        'module' => 'views',
123
      );
124
      $menu = $this->get_option('menu');
125
      if (empty($menu)) {
126
        $menu = array('type' => 'none');
127
      }
128
      // Set the title and description if we have one.
129
      if ($menu['type'] != 'none') {
130
        $items[$path]['title'] = $menu['title'];
131
        $items[$path]['description'] = $menu['description'];
132
      }
133

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

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

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

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

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

    
207
    return $items;
208
  }
209

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

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

    
226
    if (!empty($this->view->build_info['denied'])) {
227
      return MENU_ACCESS_DENIED;
228
    }
229

    
230
    $this->view->get_breadcrumb(TRUE);
231

    
232

    
233
    // And now render the view.
234
    $render = $this->view->render();
235

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

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

    
251
    $categories['page'] = array(
252
      'title' => t('Page settings'),
253
      'column' => 'second',
254
      'build' => array(
255
        '#weight' => -10,
256
      ),
257
    );
258

    
259
    $path = strip_tags($this->get_option('path'));
260
    if (empty($path)) {
261
      $path = t('No path is set');
262
    }
263
    else {
264
      $path = '/' . $path;
265
    }
266

    
267
    $options['path'] = array(
268
      'category' => 'page',
269
      'title' => t('Path'),
270
      'value' => views_ui_truncate($path, 24),
271
    );
272

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

    
291
    $options['menu'] = array(
292
      'category' => 'page',
293
      'title' => t('Menu'),
294
      'value' => views_ui_truncate($menu_str, 24),
295
    );
296

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

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

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

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

    
419
        $form['tab_markup'] = array(
420
          '#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>',
421
        );
422

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

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

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

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

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

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

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

    
541
  function validate() {
542
    $errors = parent::validate();
543

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

    
549
    if ($menu['type'] == 'default tab') {
550
      $tab_options = $this->get_option('tab_options');
551
      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
552
        $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));
553
      }
554
    }
555

    
556
    return $errors;
557
  }
558

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

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