source: sipes/modules_contrib/views/plugins/views_plugin_display_page.inc @ 65dadeb

stableversion-3.0
Last change on this file since 65dadeb was 59029b2, checked in by José Gregorio Puentes <jpuentes@…>, 8 años ago

se actualizo la version del modulo views

  • Propiedad mode establecida a 100644
File size: 20.1 KB
Línea 
1<?php
2/**
3 * @file
4 * Contains the page display plugin.
5 */
6
7/**
8 * The plugin that handles a full page.
9 *
10 * @ingroup views_display_plugins
11 */
12class views_plugin_display_page extends views_plugin_display {
13  /**
14   * The page display has a path.
15   */
16  function has_path() { return TRUE; }
17  function uses_breadcrumb() { return TRUE; }
18
19  function option_definition() {
20    $options = parent::option_definition();
21
22    $options['path'] = array('default' => '');
23    $options['menu'] = array(
24      'contains' => array(
25        'type' => array('default' => 'none'),
26        // Do not translate menu and title as menu system will.
27        'title' => array('default' => '', 'translatable' => FALSE),
28        'description' => array('default' => '', 'translatable' => FALSE),
29        'weight' => array('default' => 0),
30        'name' => array('default' => variable_get('menu_default_node_menu', 'navigation')),
31       ),
32    );
33    $options['tab_options'] = array(
34      'contains' => array(
35        'type' => array('default' => 'none'),
36        // Do not translate menu and title as menu system will.
37        'title' => array('default' => '', 'translatable' => FALSE),
38        'description' => array('default' => '', 'translatable' => FALSE),
39        'weight' => array('default' => 0),
40        'name' => array('default' => 'navigation'),
41       ),
42    );
43
44    return $options;
45  }
46
47  /**
48   * Add this display's path information to Drupal's menu system.
49   */
50  function execute_hook_menu($callbacks) {
51    $items = array();
52    // Replace % with the link to our standard views argument loader
53    // views_arg_load -- which lives in views.module
54
55    $bits = explode('/', $this->get_option('path'));
56    $page_arguments = array($this->view->name, $this->display->id);
57    $view_arguments = $this->get_option('arguments');
58
59    // Replace % with %views_arg for menu autoloading and add to the
60    // page arguments so the argument actually comes through.
61    foreach($bits as $pos => $bit) {
62      if ($bit == '%') {
63        $argument = array_shift($view_arguments);
64        if (isset($argument['validate_type']) && $argument['validate_type'] != 'none') {
65          $bits[$pos] = '%views_arg';
66        }
67        $page_arguments[] = $pos;
68      }
69    }
70
71    $path = implode('/', $bits);
72
73    $access_plugin = $this->get_plugin('access');
74    if (!isset($access_plugin)) {
75      $access_plugin = views_get_plugin('access', 'none');
76    }
77
78    // Get access callback might return an array of the callback + the dynamic arguments.
79    $access_plugin_callback = $access_plugin->get_access_callback();
80
81    if (is_array($access_plugin_callback)) {
82      $access_arguments = array();
83
84      // Find the plugin arguments.
85      $access_plugin_method = array_shift($access_plugin_callback);
86      $access_plugin_arguments = array_shift($access_plugin_callback);
87      if (!is_array($access_plugin_arguments)) {
88        $access_plugin_arguments = array();
89      }
90
91      $access_arguments[0] = array($access_plugin_method, &$access_plugin_arguments);
92
93      // Move the plugin arguments to the access arguments array.
94      $i = 1;
95      foreach ($access_plugin_arguments as $key => $value) {
96        if (is_int($value)) {
97          $access_arguments[$i] = $value;
98          $access_plugin_arguments[$key] = $i;
99          $i++;
100        }
101      }
102    }
103    else {
104      $access_arguments = array($access_plugin_callback);
105    }
106
107    if ($path) {
108      $items[$path] = array(
109        // default views page entry
110        'page callback' => 'views_page',
111        'page arguments' => $page_arguments,
112        // Default access check (per display)
113        'access callback' => 'views_access',
114        'access arguments' => $access_arguments,
115        // Identify URL embedded arguments and correlate them to a handler
116        'load arguments'  => array($this->view->name, $this->display->id, '%index'),
117      );
118      $menu = $this->get_option('menu');
119      if (empty($menu)) {
120        $menu = array('type' => 'none');
121      }
122      // Set the title and description if we have one.
123      if ($menu['type'] != 'none') {
124        $items[$path]['title'] = $menu['title'];
125        $items[$path]['description'] = $menu['description'];
126      }
127
128      if (isset($menu['weight'])) {
129        $items[$path]['weight'] = intval($menu['weight']);
130      }
131
132      switch ($menu['type']) {
133        case 'none':
134        default:
135          $items[$path]['type'] = MENU_CALLBACK;
136          break;
137        case 'normal':
138          $items[$path]['type'] = MENU_NORMAL_ITEM;
139          // Insert item into the proper menu
140          $items[$path]['menu_name'] = $menu['name'];
141          break;
142        case 'tab':
143          $items[$path]['type'] = MENU_LOCAL_TASK;
144          break;
145        case 'default tab':
146          $items[$path]['type'] = MENU_DEFAULT_LOCAL_TASK;
147          break;
148      }
149
150      // If this is a 'default' tab, check to see if we have to create teh
151      // parent menu item.
152      if ($menu['type'] == 'default tab') {
153        $tab_options = $this->get_option('tab_options');
154        if (!empty($tab_options['type']) && $tab_options['type'] != 'none') {
155          $bits = explode('/', $path);
156          // Remove the last piece.
157          $bit = array_pop($bits);
158
159          // we can't do this if they tried to make the last path bit variable.
160          // @todo: We can validate this.
161          if ($bit != '%views_arg' && !empty($bits)) {
162            $default_path = implode('/', $bits);
163            $items[$default_path] = array(
164              // default views page entry
165              'page callback' => 'views_page',
166              'page arguments' => $page_arguments,
167              // Default access check (per display)
168              'access callback' => 'views_access',
169              'access arguments' => $access_arguments,
170              // Identify URL embedded arguments and correlate them to a handler
171              'load arguments'  => array($this->view->name, $this->display->id, '%index'),
172              'title' => $tab_options['title'],
173              'description' => $tab_options['description'],
174              'menu_name' => $tab_options['name'],
175            );
176            switch ($tab_options['type']) {
177              default:
178              case 'normal':
179                $items[$default_path]['type'] = MENU_NORMAL_ITEM;
180                break;
181              case 'tab':
182                $items[$default_path]['type'] = MENU_LOCAL_TASK;
183                break;
184            }
185            if (isset($tab_options['weight'])) {
186              $items[$default_path]['weight'] = intval($tab_options['weight']);
187            }
188          }
189        }
190      }
191    }
192
193    return $items;
194  }
195
196  /**
197   * The display page handler returns a normal view, but it also does
198   * a drupal_set_title for the page, and does a views_set_page_view
199   * on the view.
200   */
201  function execute() {
202    // Let the world know that this is the page view we're using.
203    views_set_page_view($this);
204
205    // Prior to this being called, the $view should already be set to this
206    // display, and arguments should be set on the view.
207    $this->view->build();
208    if (!empty($this->view->build_info['fail'])) {
209      return drupal_not_found();
210    }
211
212    $this->view->get_breadcrumb(TRUE);
213
214    // And now render the view.
215    $render = $this->view->render();
216
217    // First execute the view so it's possible to get tokens for the title.
218    // And the title, which is much easier.
219    drupal_set_title(filter_xss_admin($this->view->get_title()));
220    return $render;
221  }
222
223  /**
224   * Provide the summary for page options in the views UI.
225   *
226   * This output is returned as an array.
227   */
228  function options_summary(&$categories, &$options) {
229    // It is very important to call the parent function here:
230    parent::options_summary($categories, $options);
231
232    $categories['page'] = array(
233      'title' => t('Page settings'),
234    );
235
236    $path = strip_tags($this->get_option('path'));
237    if (empty($path)) {
238      $path = t('None');
239    }
240
241    if (strlen($path) > 16) {
242      $path = substr($path, 0, 16) . '...';
243    }
244
245    $options['path'] = array(
246      'category' => 'page',
247      'title' => t('Path'),
248      'value' => $path,
249    );
250
251    $menu = $this->get_option('menu');
252    if (!is_array($menu)) {
253      $menu = array('type' => 'none');
254    }
255    switch($menu['type']) {
256      case 'none':
257      default:
258        $menu_str = t('No menu');
259        break;
260      case 'normal':
261        $menu_str = t('Normal: @title', array('@title' => $menu['title']));
262        break;
263      case 'tab':
264      case 'default tab':
265        $menu_str = t('Tab: @title', array('@title' => $menu['title']));
266        break;
267    }
268
269    if (strlen($menu_str) > 16) {
270      $menu_str = substr($menu_str, 0, 16) . '...';
271    }
272
273    $options['menu'] = array(
274      'category' => 'page',
275      'title' => t('Menu'),
276      'value' => $menu_str,
277    );
278
279    // This adds a 'Settings' link to the style_options setting if the style has options.
280    if ($menu['type'] == 'default tab') {
281      $options['menu']['links']['tab_options'] = t('Change settings for the parent menu');
282    }
283  }
284
285  /**
286   * Provide the default form for setting options.
287   */
288  function options_form(&$form, &$form_state) {
289    // It is very important to call the parent function here:
290    parent::options_form($form, $form_state);
291
292    switch ($form_state['section']) {
293      case 'path':
294        $form['#title'] .= t('The menu path or URL of this view');
295        $form['#help_topic'] = 'path';
296        $form['path'] = array(
297          '#type' => 'textfield',
298          '#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 arguments: For example, "node/%/feed".'),
299          '#default_value' => $this->get_option('path'),
300          '#field_prefix' => '<span dir="ltr">' . url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
301          '#field_suffix' => '</span>&lrm;',
302          '#attributes' => array('dir'=>'ltr'),
303        );
304        break;
305      case 'menu':
306        $form['#title'] .= t('Menu item entry');
307        $form['#help_topic'] = 'menu';
308        $form['menu'] = array(
309          '#prefix' => '<div class="clear-block">',
310          '#suffix' => '</div>',
311          '#tree' => TRUE,
312        );
313        $menu = $this->get_option('menu');
314        if (empty($menu)) {
315          $menu = array('type' => 'none', 'title' => '', 'weight' => 0);
316        }
317        $form['menu']['type'] = array(
318          '#prefix' => '<div class="views-left-30">',
319          '#suffix' => '</div>',
320          '#title' => t('Type'),
321          '#type' => 'radios',
322          '#options' => array(
323            'none' => t('No menu entry'),
324            'normal' => t('Normal menu entry'),
325            'tab' => t('Menu tab'),
326            'default tab' => t('Default menu tab')
327          ),
328          '#default_value' => $menu['type'],
329        );
330        $form['menu']['title'] = array(
331          '#prefix' => '<div class="views-left-50">',
332          '#title' => t('Title'),
333          '#type' => 'textfield',
334          '#default_value' => $menu['title'],
335          '#description' => t('If set to normal or tab, enter the text to use for the menu item.'),
336          '#process' => array('views_process_dependency'),
337          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
338        );
339        $form['menu']['description'] = array(
340          '#title' => t('Description'),
341          '#type' => 'textfield',
342          '#default_value' => $menu['description'],
343          '#description' => t("If set to normal or tab, enter the text to use for the menu item's description."),
344          '#process' => array('views_process_dependency'),
345          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
346        );
347        $form['menu']['name-warning'] = array(
348          '#type' => 'markup',
349          '#prefix' => '<div class="warning">',
350          '#value' => t("Warning: Changing this item's menu will not work reliably in Drupal 6.4 or earlier. Please upgrade your copy of Drupal at !url.", array('!url' => l('drupal.org', 'http://drupal.org/project/Drupal+project'))),
351          '#suffix' => '</div>',
352          '#process' => array('views_process_dependency'),
353          '#dependency' => array('radio:menu[type]' => array('normal')),
354          '#access' => version_compare(VERSION, '6.5', '<'),
355        );
356
357        // Only display the menu selector if menu module is enabled.
358        if (module_exists('menu')) {
359          $form['menu']['name'] = array(
360            '#title' => t('Menu'),
361            '#type' => 'select',
362            '#options' => menu_get_menus(),
363            '#default_value' => $menu['name'],
364            '#description' => t('Insert item into an available menu.'), //
365            '#process' => array('views_process_dependency'),
366            '#dependency' => array('radio:menu[type]' => array('normal')),
367          );
368        }
369        else {
370          $form['menu']['name'] = array(
371            '#type' => 'value',
372            '#value' => $menu['name'],
373          );
374          $form['menu']['markup'] = array(
375            '#value' => t('Menu selection requires the activation of menu module.'),
376          );
377        }
378        $form['menu']['weight'] = array(
379          '#suffix' => '</div>',
380          '#title' => t('Weight'),
381          '#type' => 'textfield',
382          '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0,
383          '#description' => t('The lower the weight the higher/further left it will appear.'),
384          '#process' => array('views_process_dependency'),
385          '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')),
386        );
387        break;
388      case 'tab_options':
389        $form['#title'] .= t('Default tab options');
390        $tab_options = $this->get_option('tab_options');
391        if (empty($tab_options)) {
392          $tab_options = array('type' => 'none', 'title' => '', 'weight' => 0);
393        }
394
395        $form['tab_markup'] = array(
396          '#prefix' => '<div class="form-item description">',
397          '#suffix' => '</div>',
398          '#value' => 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>.'),
399        );
400
401        $form['tab_options'] = array(
402          '#prefix' => '<div class="clear-block">',
403          '#suffix' => '</div>',
404          '#tree' => TRUE,
405        );
406        $form['tab_options']['type'] = array(
407          '#prefix' => '<div class="views-left-25">',
408          '#suffix' => '</div>',
409          '#title' => t('Parent menu item'),
410          '#type' => 'radios',
411          '#options' => array('none' => t('Already exists'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')),
412          '#default_value' => $tab_options['type'],
413        );
414        $form['tab_options']['title'] = array(
415          '#prefix' => '<div class="views-left-75">',
416          '#title' => t('Title'),
417          '#type' => 'textfield',
418          '#default_value' => $tab_options['title'],
419          '#description' => t('If creating a parent menu item, enter the title of the item.'),
420          '#process' => array('views_process_dependency'),
421          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
422        );
423        $form['tab_options']['description'] = array(
424          '#title' => t('Description'),
425          '#type' => 'textfield',
426          '#default_value' => $tab_options['description'],
427          '#description' => t('If creating a parent menu item, enter the description of the item.'),
428          '#process' => array('views_process_dependency'),
429          '#dependency' => array('radio:tab_options[type]' => array('normal', 'tab')),
430        );
431        // Only display the menu selector if menu module is enabled.
432        if (module_exists('menu')) {
433          $form['tab_options']['name'] = array(
434            '#title' => t('Menu'),
435            '#type' => 'select',
436            '#options' => menu_get_menus(),
437            '#default_value' => $tab_options['name'],
438            '#description' => t('Insert item into an available menu.'),
439            '#process' => array('views_process_dependency'),
440            '#dependency' => array('radio:tab_options[type]' => array('normal')),
441          );
442        }
443        else {
444          $form['tab_options']['name'] = array(
445            '#type' => 'value',
446            '#value' => $tab_options['name'],
447          );
448          $form['tab_options']['markup'] = array(
449            '#value' => t('Menu selection requires the activation of menu module.'),
450          );
451        }
452        $form['tab_options']['weight'] = array(
453          '#suffix' => '</div>',
454          '#title' => t('Tab weight'),
455          '#type' => 'textfield',
456          '#default_value' => $tab_options['weight'],
457          '#size' => 5,
458          '#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.'),
459          '#process' => array('views_process_dependency'),
460          '#dependency' => array('radio:tab_options[type]' => array('tab')),
461        );
462        break;
463    }
464  }
465
466  function options_validate(&$form, &$form_state) {
467    // It is very important to call the parent function here:
468    parent::options_validate($form, $form_state);
469    switch ($form_state['section']) {
470      case 'path':
471        if (strpos($form_state['values']['path'], '$arg') !== FALSE) {
472          form_error($form['path'], t('"$arg" is no longer supported. Use % instead.'));
473        }
474
475        if (strpos($form_state['values']['path'], '%') === 0) {
476          form_error($form['path'], t('"%" may not be used for the first segment of a path.'));
477        }
478
479        // automatically remove '/' from path.
480        $form_state['values']['path'] = trim($form_state['values']['path'], '/');
481
482        break;
483      case 'menu':
484        $path = $this->get_option('path');
485        if ($form_state['values']['menu']['type'] == 'normal' && strpos($path, '%') !== FALSE) {
486          form_error($form['menu']['type'], t('Views cannot create normal menu items for paths with a % in them.'));
487        }
488
489        if ($form_state['values']['menu']['type'] == 'default tab' || $form_state['values']['menu']['type'] == 'tab') {
490          $bits = explode('/', $path);
491          $last = array_pop($bits);
492          if ($last == '%') {
493            form_error($form['menu']['type'], t('A display whose path ends with a % cannot be a tab.'));
494          }
495        }
496
497        if ($form_state['values']['menu']['type'] != 'none' && empty($form_state['values']['menu']['title'])) {
498          form_error($form['menu']['title'], t('Title is required for this menu type.'));
499        }
500        break;
501    }
502  }
503
504  function options_submit($form, &$form_state) {
505    // It is very important to call the parent function here:
506    parent::options_submit($form, $form_state);
507    switch ($form_state['section']) {
508      case 'path':
509        $this->set_option('path', $form_state['values']['path']);
510        break;
511      case 'menu':
512        $this->set_option('menu', $form_state['values']['menu']);
513        // send ajax form to options page if we use it.
514        if ($form_state['values']['menu']['type'] == 'default tab') {
515          views_ui_add_form_to_stack('display', $this->view, $this->display->id, array('tab_options'));
516        }
517        break;
518      case 'tab_options':
519        $this->set_option('tab_options', $form_state['values']['tab_options']);
520        break;
521    }
522  }
523
524  function validate() {
525    $errors = parent::validate();
526
527    $menu = $this->get_option('menu');
528    if (!empty($menu['type']) && $menu['type'] != 'none' && empty($menu['title'])) {
529      $errors[] = t('Display @display is set to use a menu but the menu title is not set.', array('@display' => $this->display->display_title));
530    }
531
532    if ($menu['type'] == 'default tab') {
533      $tab_options = $this->get_option('tab_options');
534      if (!empty($tab_options['type']) && $tab_options['type'] != 'none' && empty($tab_options['title'])) {
535        $errors[] = t('Display @display is set to use a parent menu but the parent menu title is not set.', array('@display' => $this->display->display_title));
536      }
537    }
538
539    return $errors;
540  }
541}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.