plugins.inc 66.8 KB
Newer Older
1
<?php
2 3
// $Id$
/**
4 5
 * @file plugins.inc
 * Built in plugins for Views output handling.
6 7 8
 *
 */

9
/**
10 11 12
 * Implementation of hook_views_plugins
 */
function views_views_plugins() {
13
  $path = drupal_get_path('module', 'views') . '/js';
14 15 16
  return array(
    'module' => 'views', // This just tells our themes are elsewhere.
    'display' => array(
merlinofchaos's avatar
merlinofchaos committed
17 18 19
      'default' => array(
        'title' => t('Defaults'),
        'help' => t('Default settings for this view.'),
20
        'handler' => 'views_plugin_display_default',
merlinofchaos's avatar
merlinofchaos committed
21 22
        'no ui' => TRUE,
        'no remove' => TRUE,
23
        'js' => array('misc/collapse.js', 'misc/textarea.js', 'misc/tabledrag.js', "$path/dependent.js"),
merlinofchaos's avatar
merlinofchaos committed
24
        'use pager' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
25
      ),
26 27
      'page' => array(
        'title' => t('Page'),
merlinofchaos's avatar
merlinofchaos committed
28
        'help' => t('Display the view as a page, with a URL and menu links.'),
29
        'handler' => 'views_plugin_display_page',
30
        'uses hook menu' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
31
        'use pager' => TRUE,
32 33
      ),
      'block' => array(
34
        'title' => t('Block'),
merlinofchaos's avatar
merlinofchaos committed
35
        'help' => t('Display the view as a block.'),
36
        'handler' => 'views_plugin_display_block',
37
        'uses hook block' => TRUE,
38 39 40 41
      ),
    ),
    'style' => array(
      'default' => array(
42
        'title' => t('Unformatted'),
43
        'help' => t('Displays rows one after another.'),
44
        'handler' => 'views_plugin_style_default',
merlinofchaos's avatar
merlinofchaos committed
45
        'theme' => 'views_view_unformatted',
46
        'uses row plugin' => TRUE,
47 48 49 50
      ),
      'list' => array(
        'title' => t('List'),
        'help' => t('Displays rows as an HTML list.'),
51
        'handler' => 'views_plugin_style_list',
52
        'theme' => 'views_view_list',
53 54
        'uses row plugin' => TRUE,
        'uses options' => TRUE,
55 56 57 58
      ),
      'table' => array(
        'title' => t('Table'),
        'help' => t('Displays rows in a table.'),
59
        'handler' => 'views_plugin_style_table',
60
        'theme' => 'views_view_table',
61 62
        'uses row plugin' => FALSE,
        'uses fields' => TRUE,
63
        'uses options' => TRUE,
64
      ),
65 66 67
      'default_summary' => array(
        'title' => t('Default'),
        'help' => t('Displays the default summary view'),
68
        'handler' => 'views_plugin_style_summary',
69
        'theme' => 'views_view_summary',
70 71
        'summary' => TRUE, // only shows up as a summary style
        'uses options' => TRUE,
72
      ),
73 74 75 76 77
    ),
    'row' => array(
      'fields' => array(
        'title' => t('Fields'),
        'help' => t('Displays the fields with an optional template.'),
78
        'handler' => 'views_plugin_row',
merlinofchaos's avatar
merlinofchaos committed
79
        'theme' => 'views_view_fields',
80
        'uses fields' => TRUE,
81 82 83 84 85 86
      ),
    ),
  );

}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * Builds and return a list of all plugins available in the system.
 *
 * @return Nested array of plugins, grouped by type and
 */
function views_discover_plugins() {
  $cache = array('display' => array(), 'style' => array(), 'row' => array());
  // Get plugins from all mdoules.
  foreach (module_implements('views_plugins') as $module) {
    $function = $module . '_views_plugins';
    $result = $function();
    if (!is_array($result)) {
      continue;
    }

    $module_dir = isset($result['module']) ? $result['module'] : $module;
    // Setup automatic path/file finding for theme registration
    if ($module_dir == 'views') {
      $path = drupal_get_path('module', $module_dir) . '/theme';
      $file = 'theme.inc';
    }
    else {
      $path = drupal_get_path('module', $module_dir);
      $file = "$module.views.inc";
    }
    foreach ($result as $type => $info) {
      if ($type == 'module') {
        continue;
      }
      foreach ($info as $plugin => $def) {
        if (isset($def['theme']) && !isset($def['path'])) {
          $def['path'] = $path;
          $def['file'] = $file;
        }
        // merge the new data in
        $cache[$type][$plugin] = $def;
      }
    }
  }
  return $cache;
}

129
/**
130
 * @defgroup views_plugin_displays Views' display plugins
131 132 133 134 135 136 137 138 139
 * @{
 * Display plugins control how Views interact with the rest of Drupal.
 *
 * They can handle creating Views from a Drupal page hook; they can
 * handle creating Views from a Drupal block hook. They can also
 * handle creating Views from an external module source, such as
 * a Panels pane, or an insert view, or a CCK field type.
 */

140 141 142 143
/**
 * The default display plugin handler. Display plugins handle options and
 * basic mechanisms for different output methods.
 */
144
class views_plugin_display extends views_object {
145 146 147
  /**
   * Fill this plugin in with the view, display, etc.
   */
148
  function init(&$view, &$display) {
merlinofchaos's avatar
merlinofchaos committed
149 150
    $this->view = &$view;
    $this->display = &$display;
151 152
  }

153 154 155 156 157 158
  /**
   * Determine if this display is the 'default' display which contains
   * fallback settings
   */
  function is_default_display() { return FALSE; }

159 160 161 162
  /**
   * Determine if this display uses exposed filters, so the view
   * will know whether or not to build them.
   */
merlinofchaos's avatar
merlinofchaos committed
163
  function uses_exposed() {
merlinofchaos's avatar
merlinofchaos committed
164 165 166 167 168 169 170 171 172 173 174 175
    if (!isset($this->has_exposed)) {
      foreach (array('field', 'filter') as $type) {
        foreach ($this->view->$type as $key => $info) {
          if ($info['handler']->is_exposed()) {
            // one is all we need; if we find it, return true.
            $this->has_exposed = TRUE;
            return TRUE;
          }
        }
      }
      $this->has_exposed = FALSE;
    }
merlinofchaos's avatar
merlinofchaos committed
176

merlinofchaos's avatar
merlinofchaos committed
177 178
    return $this->has_exposed;
  }
179

merlinofchaos's avatar
merlinofchaos committed
180 181 182 183 184 185 186 187 188 189
  /**
   * Does the display have a pager enabled?
   */
  function use_pager() {
    if (!empty($this->definition['use pager'])) {
      return $this->get_option('use_pager');
    }
    return FALSE;
  }

190 191 192 193 194 195 196 197 198 199 200 201
  /**
   * Static member function to list which sections are defaultable
   * and what items each section contains.
   */
  function defaultable_sections($section = NULL) {
    $sections = array(
      'access' => array('access'),
      'title' => array('title'),
      'header' => array('header', 'header_format', 'header_empty'),
      'footer' => array('footer', 'footer_format', 'footer_empty'),
      'empty' => array('empty', 'empty_format'),
      'items_per_page' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
merlinofchaos's avatar
merlinofchaos committed
202
      'use_pager' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
merlinofchaos's avatar
merlinofchaos committed
203
      'link_display' => array('link_display'),
merlinofchaos's avatar
merlinofchaos committed
204 205

    // @todo
206 207 208 209 210 211 212 213
      'php_arg_code' => array('php_arg_code'),
      'exposed_options' => array('exposed_options'),

      // Force these to cascade properly.
      'style_plugin' => array('style_plugin', 'style_options', 'row_plugin', 'row_options'),
      'style_options' => array('style_plugin', 'style_options', 'row_plugin', 'row_options'),
      'row_plugin' => array('style_plugin', 'style_options', 'row_plugin', 'row_options'),
      'row_options' => array('style_plugin', 'style_options', 'row_plugin', 'row_options'),
214

215
      // These guys are special
216
      'relationships' => array('relationships'),
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
      'fields' => array('fields'),
      'sorts' => array('sorts'),
      'arguments' => array('arguments'),
      'filters' => array('filters'),
    );
    if ($section) {
      if (!empty($sections[$section])) {
        return $sections[$section];
      }
    }
    else {
      return $sections;
    }
  }

  /**
   * This is called when a new display is created that has never been
   * saved to the database. It provides appropriate defaults for the
   * display, if necessary.
   */
237
  function options(&$display) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    // Set the following options to use the default display, forcing
    // them to pass through unless we override.
    $display->display_options['defaults'] = array(
      'access' => TRUE,
      'title' => TRUE,
      'header' => TRUE,
      'header_format' => TRUE,
      'header_empty' => TRUE,
      'footer' => TRUE,
      'footer_format' => TRUE,
      'footer_empty' => TRUE,
      'empty' => TRUE,
      'empty_format' => TRUE,

      'items_per_page' => TRUE,
      'offset' => TRUE,
      'use_pager' => TRUE,
      'pager_element'  => TRUE,

      'link_display' => TRUE,
      'php_arg_code' => TRUE,
      'exposed_options' => TRUE,

      'style_plugin' => TRUE,
      'style_options' => TRUE,
      'row_plugin' => TRUE,
      'row_options' => TRUE,

266
      'relationships' => TRUE,
267 268 269 270 271
      'fields' => TRUE,
      'sorts' => TRUE,
      'arguments' => TRUE,
      'filters' => TRUE,
    );
272 273 274 275 276 277

    $display->display_options['relationships'] = array();
    $display->display_options['fields'] = array();
    $display->display_options['sorts'] = array();
    $display->display_options['arguments'] = array();
    $display->display_options['filters'] = array();
278 279
  }

merlinofchaos's avatar
merlinofchaos committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  /**
   * Check to see if the display has a 'path' field.
   *
   * This is a pure function and not just a setting on the definition
   * because some displays (such as a panel pane) may have a path based
   * upon configuration.
   *
   * By default, displays do not have a path.
   */
  function has_path() { return FALSE; }

  /**
   * Check to see which display to use when creating links within
   * a view using this display.
   */
  function get_link_display() {
    $display_id = $this->get_option('link_display');
    // If unknown, pick the first one.
    if (empty($display_id) || empty($this->view->display[$display_id])) {
      foreach ($this->view->display as $display_id => $display) {
        if ($display->handler->has_path()) {
          return $display_id;
        }
      }
    }
    else {
      return $display_id;
    }
    // fall-through returns NULL
  }

  /**
   * Return the base path to use for this display.
   *
   * This can be overridden for displays that do strange things
   * with the path.
   */
  function get_path() {
    if ($this->has_path()) {
      return $this->get_option('path');
    }

    $display_id = $this->get_link_display();
    if ($display_id && !empty($this->view->display[$display_id])) {
      return $this->view->display[$display_id]->handler->get_path();
    }
  }

  /**
   * Check to see if the display needs a breadcrumb
   *
   * By default, displays do not need breadcrumbs
   */
  function uses_breadcrumb() { return FALSE; }

335 336 337 338 339
  /**
   * Intelligently get an option either from this display or from the
   * default display, if directed to do so.
   */
  function get_option($option) {
340 341
    if (!$this->is_default_display() && !empty($this->default_display) && !empty($this->display->display_options['defaults'][$option])) {
      return $this->default_display->get_option($option);
342
    }
343 344 345

    if (array_key_exists($option, $this->display->display_options)) {
      return $this->display->display_options[$option];
346
    }
347 348
  }

349 350 351 352 353 354 355 356 357 358 359
  /**
   * Determine if the display's style uses fields.
   */
  function uses_fields() {
    $plugin = views_get_plugin('style', $this->get_option('style_plugin'));
    $plugin->init($this->view, $this->display);
    if ($plugin) {
      return $plugin->uses_fields();
    }
  }

360 361 362 363 364 365 366
  /**
   * Intelligently set an option either from this display or from the
   * default display, if directed to do so.
   */
  function set_option($option, $value) {
    if (!$this->is_default_display() && !empty($this->default_display) && !empty($this->display->display_options['defaults'][$option])) {
      return $this->default_display->set_option($option, $value);
367
    }
368 369

    return $this->display->display_options[$option] = $value;
370
  }
371

merlinofchaos's avatar
merlinofchaos committed
372 373 374 375
  /**
   * Because forms may be split up into sections, this provides
   * an easy URL to exactly the right section. Don't override this.
   */
376 377 378 379 380
  function option_link($text, $section, $class = '') {
    if (!empty($class)) {
      $text = '<span>' . $text . '</span>';
    }

381 382 383 384
    if (empty($text)) {
      $text = t('Broken field');
    }

385
    return l($text, 'admin/build/views/nojs/display/' . $this->view->name . '/' . $this->display->id . '/' . $section, array('attributes' => array('class' => 'views-ajax-link ' . $class), 'html' => TRUE));
merlinofchaos's avatar
merlinofchaos committed
386 387 388 389
  }

  /**
   * Provide the default summary for options in the views UI.
390 391
   *
   * This output is returned as an array.
merlinofchaos's avatar
merlinofchaos committed
392
   */
393 394 395 396 397 398 399 400 401 402
  function options_summary(&$categories, &$options) {
    $categories['basic'] = array(
      'title' => t('Basic settings'),
    );

    $options['display_title'] = array(
      'category' => 'basic',
      'title' => t('Name'),
      'value' => $this->display->display_title,
    );
403 404 405 406 407

    $title = $this->get_option('title');
    if (!$title) {
      $title = t('None');
    }
408 409 410 411 412 413

    $options['title'] = array(
      'category' => 'basic',
      'title' => t('Title'),
      'value' => $title,
    );
414 415 416 417 418

    $style_plugin = views_fetch_plugin_data('style', $this->get_option('style_plugin'));
    $style_title = empty($style_plugin['title']) ? t('Missing style plugin') : $style_plugin['title'];

    $style = '';
419 420 421 422 423 424 425 426

    $options['style_plugin'] = array(
      'category' => 'basic',
      'title' => t('Style'),
      'value' => $style_title,
    );

    // This adds a 'Settings' link to the style_options setting if the style has options.
427
    if (!empty($style_plugin['uses options'])) {
428
      $options['style_plugin']['links']['style_options'] = t('Settings');
429 430 431 432 433 434
    }

    if (!empty($style_plugin['uses row plugin'])) {
      $row_plugin = views_fetch_plugin_data('row', $this->get_option('row_plugin'));
      $row_title = empty($row_plugin['title']) ? t('Missing style plugin') : $row_plugin['title'];

435 436 437 438 439 440
      $options['row_plugin'] = array(
        'category' => 'basic',
        'title' => t('Row style'),
        'value' => $row_title,
      );
      // This adds a 'Settings' link to the row_options setting if the row style has options.
441
      if (!empty($row_plugin['uses options'])) {
442
        $options['row_plugin']['links']['row_options'] = t('Settings');
443 444 445
      }
    }

446 447 448 449 450 451 452 453 454 455 456 457 458
    if (!empty($this->definition['use pager'])) {
      $options['use_pager'] = array(
        'category' => 'basic',
        'title' => t('Use pager'),
        'value' => $this->get_option('use_pager') ? t('Yes') : t('No'),
      );
    }
    $options['items_per_page'] = array(
      'category' => 'basic',
      'title' => $this->use_pager() ? t('Items per page') : t('Items to display'),
      'value' => intval($this->get_option('items_per_page')),
    );

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    $access = $this->get_option('access');
    if (!is_array($access)) {
      $access = array('type' => 'none');
    }
    switch($access['type']) {
      case 'none':
      default:
        $access_str = t('Unrestricted');
        break;
      case 'perm':
        $access_str = $access['perm'];
        break;
      case 'role':
        $roles = array_keys(array_filter($access['role']));
        if (count($roles) > 1) {
          $access_str = t('Multiple roles');
        }
        else {
          $rids = views_ui_get_roles();
          $rid = array_shift($roles);
          $access_str = $rids[$rid];
        }
        break;
    }
483 484 485 486 487 488

    $options['access'] = array(
      'category' => 'basic',
      'title' => t('Access'),
      'value' => $access_str,
    );
489

merlinofchaos's avatar
merlinofchaos committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
    if (!$this->has_path()) {
      // Only show the 'link display' if there is more than one option.
      $count = 0;
      foreach ($this->view->display as $display_id => $display) {
        if ($display->handler->has_path()) {
          $count++;
        }
        if ($count > 1) {
          break;
        }
      }

      if ($count > 1) {
        $display_id = $this->get_link_display();
        $link_display = empty($this->view->display[$display_id]) ? t('None') : $this->view->display[$display_id]->display_title;
        $options['link_display'] = array(
          'category' => 'basic',
          'title' => t('Link display'),
          'value' => $link_display,
        );
      }
    }

513 514 515 516 517 518 519 520 521 522 523 524
    foreach (array('header' => t('Header'), 'footer' => t('Footer'), 'empty' => t('Empty text')) as $type => $name) {
      if (!$this->get_option($type)) {
        $field = t('None');
      }
      else {
        // A lot of code to get the name of the filter format.
        $fmt_string = $this->get_option($type . '_format');
        if (empty($fmt_string)) {
          $fmt_string = FILTER_FORMAT_DEFAULT;
        }
        $format_val = filter_resolve_format($fmt_string);
        $format = filter_formats($format_val);
525 526 527 528 529 530
        if ($format) {
          $field = $format->name;
        }
        else {
          $field = t('Unknown/missing filter');
        }
531 532 533
      }

      $output[] = t('!name: !field', array('!name' => $name, '!field' => $this->option_link($field, $type)));
534 535 536 537 538
      $options[$type] = array(
        'category' => 'basic',
        'title' => $name,
        'value' => $field,
      );
539
    }
merlinofchaos's avatar
merlinofchaos committed
540 541
  }

542 543 544
  /**
   * Provide the default form for setting options.
   */
merlinofchaos's avatar
merlinofchaos committed
545
  function options_form(&$form, &$form_state) {
546 547 548
    if ($this->defaultable_sections($form_state['section'])) {
      $this->add_override_button($form, $form_state['section']);
    }
549
    $form['#title'] = check_plain($this->display->display_title) . ': ';
550

551 552 553 554 555 556 557 558 559 560
    // Set the 'section' to hilite on the form.
    // If it's the item we're looking at is pulling from the default display,
    // reflect that.
    if (!empty($this->display->display_options['defaults'][$form_state['section']])) {
      $form['#section'] = 'default-' . $form_state['section'];
    }
    else {
      $form['#section'] = $this->display->id . '-' . $form_state['section'];
    }

merlinofchaos's avatar
merlinofchaos committed
561 562
    switch ($form_state['section']) {
      case 'display_title':
563
        $form['#title'] .= t('The name of this display');
merlinofchaos's avatar
merlinofchaos committed
564 565 566 567 568 569
        $form['display_title'] = array(
          '#type' => 'textfield',
          '#description' => t('This title will appear only in the administrative interface for the View.'),
          '#default_value' => $this->display->display_title,
        );
        break;
570
      case 'title':
571
        $form['#title'] .= t('The title of this view');
572 573 574 575 576 577
        $form['title'] = array(
          '#type' => 'textfield',
          '#description' => t('This title will be displayed with the view, wherever titles are normally displayed; i.e, as the page title, block title, etc.'),
          '#default_value' => $this->get_option('title'),
        );
        break;
merlinofchaos's avatar
merlinofchaos committed
578 579 580 581 582 583 584 585 586 587
      case 'use_pager':
        $form['#title'] .= t('Use a pager for this view');
        $form['use_pager'] = array(
          '#type' => 'radios',
          '#options' => array(1 => t('Yes'), 0 => t('No')),
          '#default_value' => $this->get_option('use_pager'),
        );
        $form['pager_element'] = array(
          '#type' => 'textfield',
          '#title' => t('Pager element'),
588
          '#description' => t('Unless you\'re experiencing problems with pagers related to this view, you should leave this at 0. If using multiple pagers on one page you may need to set this number to a higher value so as not to conflict within the ?page= array.  Large values will add a lot of commas to your URLs, so avoid if possible.'),
merlinofchaos's avatar
merlinofchaos committed
589 590 591 592
          '#default_value' => intval($this->get_option('pager_element')),
        );
        break;
      case 'items_per_page':
merlinofchaos's avatar
merlinofchaos committed
593
        $form['#title'] .= $this->use_pager() ? t('Items per page') : t('Items to display');
merlinofchaos's avatar
merlinofchaos committed
594

merlinofchaos's avatar
merlinofchaos committed
595 596 597 598 599 600 601 602 603 604 605 606
        $form['items_per_page'] = array(
          '#type' => 'textfield',
          '#description' => t('The number of items to display per page. Enter 0 for no limit.'),
          '#default_value' => intval($this->get_option('items_per_page')),
        );
        $form['offset'] = array(
          '#type' => 'textfield',
          '#title' => t('Offset'),
          '#description' => t('The number of items to skip. For example, if this field is 3, the first 3 items will be skipped and not displayed.'),
          '#default_value' => intval($this->get_option('offset')),
        );
        break;
607
      case 'access':
608
        $form['#title'] .= t('Access restrictions');
609 610 611 612 613 614 615 616 617 618 619
        $form['access'] = array(
          '#prefix' => '<div class="clear-block">',
          '#suffix' => '</div>',
          '#tree' => TRUE,
        );

        $access = $this->get_option('access');
        if (empty($access)) {
          $access = array('type' => 'none', 'role' => array(), 'perm' => '');
        }
        $form['access']['type'] = array(
merlinofchaos's avatar
merlinofchaos committed
620
          '#prefix' => '<div class="views-left-25">',
621 622 623 624 625 626 627
          '#suffix' => '</div>',
          '#title' => t('Type'),
          '#type' => 'radios',
          '#options' => array('none' => t('Unrestricted'), 'role' => t('By role'), 'perm' => t('By perm')),
          '#default_value' => $access['type'],
        );
        $form['access']['role'] = array(
628 629 630 631
          // Add an id to the surrounding div because checkboxes don't get ids
          // as a whole group. =(
          '#prefix' => '<div class="views-left-25"><div id="edit-access-role">',
          '#suffix' => '</div></div>',
632 633 634 635 636
          '#type' => 'checkboxes',
          '#title' => t('If by role'),
          '#default_value' => $access['role'],
          '#options' => views_ui_get_roles(),
          '#description' => t('Only the checked roles will be able to access this display.'),
637 638
          '#process' => array('expand_checkboxes', 'views_process_dependency'),
          '#dependency' => array('radio:access[type]' => array('role')),
639 640 641 642 643 644 645 646 647 648
        );

        $perms = array();
        // Get list of permissions
        foreach (module_list(FALSE, FALSE, TRUE) as $module) {
          if ($permissions = module_invoke($module, 'perm')) {
            $perms[$module] = drupal_map_assoc($permissions);
          }
        }
        $form['access']['perm'] = array(
649
          '#prefix' => '<div class="views-left-30">',
650 651 652 653 654 655
          '#suffix' => '</div>',
          '#type' => 'select',
          '#options' => $perms,
          '#title' => t('If by perm'),
          '#default_value' => $access['perm'],
          '#description' => t('Only users with the selected permission flag will be able to access this display.'),
656 657 658 659
          '#process' => array('views_process_dependency'),
          // Radios don't *get* ids in this system, so we use a special format
          // for the id.
          '#dependency' => array('radio:access[type]' => array('perm')),
660 661 662
        );
        break;
      case 'header':
663
        $form['#title'] .= t('Header');
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        $form['header_empty'] = array(
          '#type' => 'checkbox',
          '#title' => t('Display even if view has no result'),
          '#default_value' => $this->get_option('header_empty'),
        );
        $form['header'] = array(
          '#type' => 'textarea',
          '#default_value' => $this->get_option('header'),
          '#rows' => 6,
          '#description' => t('Text to display at the top of the view. May contain an explanation or links or whatever you like. Optional.'),
        );

        $form['header_format'] = filter_form($this->get_option('header_format'), NULL, array('header_format'));
        break;
      case 'footer':
679
        $form['#title'] .= t('Footer');
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
        $form['footer_empty'] = array(
          '#type' => 'checkbox',
          '#title' => t('Display even if view has no result'),
          '#default_value' => $this->get_option('header_empty'),
        );
        $form['footer'] = array(
          '#type' => 'textarea',
          '#default_value' => $this->get_option('footer'),
          '#rows' => 6,
          '#description' => t('Text to display beneath the view. May contain an explanation or links or whatever you like. Optional.'),
        );

        $form['footer_format'] = filter_form($this->get_option('footer_format'), NULL, array('footer_format'));
        break;
      case 'empty':
695
        $form['#title'] .= t('Empty text');
696 697 698 699 700 701 702 703 704 705
        $form['empty'] = array(
          '#type' => 'textarea',
          '#default_value' => $this->get_option('empty'),
          '#rows' => 6,
          '#description' => t('Text to display if the view has no results. Optional.'),
        );

        $form['empty_format'] = filter_form($this->get_option('empty_format'), NULL, array('empty_format'));
        break;
      case 'style_plugin':
706
        $form['#title'] .= t('How should this view be styled');
707 708 709 710 711 712 713
        $form['style_plugin'] =  array(
          '#type' => 'radios',
          '#options' => views_fetch_plugin_names('style', 'summary', FALSE),
          '#default_value' => $this->get_option('style_plugin'),
        );
        break;
      case 'style_options':
714
        $form['#title'] .= t('Style options');
715
        $style = TRUE;
716 717
        $type = 'style_plugin';

718 719
      case 'row_options':
        // if row, $style will be empty.
720 721
        if (empty($style)) {
          $form['#title'] .= t('Row style options');
722
          $type = 'row_plugin';
723
        }
724
        $plugin = views_get_plugin(empty($style) ? 'row' : 'style', $this->get_option($type));
725 726 727 728
        if ($plugin) {
          $form[$form_state['section']] = array(
            '#tree' => TRUE,
          );
729
          $plugin->init($this->view, $this->display);
730
          $plugin->options_form($form[$form_state['section']], $form_state);
731 732 733
        }
        break;
      case 'row_plugin':
734
        $form['#title'] .= t('How should each row in this view be styled');
735 736 737 738 739 740
        $form['row_plugin'] =  array(
          '#type' => 'radios',
          '#options' => views_fetch_plugin_names('row', 'summary', FALSE),
          '#default_value' => $this->get_option('row_plugin'),
        );
        break;
merlinofchaos's avatar
merlinofchaos committed
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
      case 'link_display':
        $form['#title'] .= t('Which display to use for path');
        foreach ($this->view->display as $display_id => $display) {
          if ($display->handler->has_path()) {
            $options[$display_id] = $display->display_title;
          }
        }
        $form['link_display'] = array(
          '#type' => 'radios',
          '#options' => $options,
          '#description' => t('Which display to use to get this display\'s path for things like summary links, rss feed links, more links, etc.'),
          '#default_value' => $this->get_link_display(),
        );
        break;

merlinofchaos's avatar
merlinofchaos committed
756 757
    }
  }
758

759 760 761
  /**
   * Validate the options form.
   */
762 763 764 765 766 767 768 769
  function options_validate($form, &$form_state) {
    switch ($form_state['section']) {
      case 'style_options':
        $style = TRUE;
      case 'row_options':
        // if row, $style will be empty.
        $plugin = views_get_plugin(empty($style) ? 'row' : 'style', $this->get_option(empty($style) ? 'row_plugin' : 'style_plugin'));
        if ($plugin) {
770
          $plugin->init($this->view, $this->display);
771 772 773
          $plugin->options_validate($form[$form_state['section']], $form_state);
        }
        break;
774 775 776 777 778
      case 'access':
        $access = $form_state['values']['access'];
        if ($access['type'] == 'role' && !array_filter($access['role'])) {
          form_error($form, t('You must select at least one role if type is "by role"'));
        }
779 780
    }
  }
781 782 783 784 785

  /**
   * Perform any necessary changes to the form values prior to storage.
   * There is no need for this function to actually store the data.
   */
merlinofchaos's avatar
merlinofchaos committed
786
  function options_submit($form, &$form_state) {
787 788
    $section = $form_state['section'];
    switch ($section) {
merlinofchaos's avatar
merlinofchaos committed
789 790 791
      case 'display_title':
        $this->display->display_title = $form_state['values']['display_title'];
        break;
792 793
      case 'title':
      case 'access':
merlinofchaos's avatar
merlinofchaos committed
794 795
      case 'link_display':
      case 'php_arg_code':
796 797
        $this->set_option($section, $form_state['values'][$section]);
        break;
merlinofchaos's avatar
merlinofchaos committed
798 799 800 801 802 803 804 805
      case 'use_pager':
        $this->set_option($section, $form_state['values'][$section]);
        $this->set_option('pager_element', intval($form_state['values']['pager_element']));
        break;
      case 'items_per_page':
        $this->set_option($section, intval($form_state['values'][$section]));
        $this->set_option('offset', intval($form_state['values']['offset']));
        break;
806
      case 'row_plugin':
807 808 809 810 811
        // This if prevents resetting options to default if they don't change
        // the plugin.
        if ($this->get_option($section) != $form_state['values'][$section]) {
          $this->set_option($section, $form_state['values'][$section]);
          $plugin = views_get_plugin('row', $form_state['values'][$section]);
812
          $this->set_option('row_options', $plugin->options($this->display));
813
        }
814 815
        break;
      case 'style_plugin':
816 817 818 819 820
        // This if prevents resetting options to default if they don't change
        // the plugin.
        if ($this->get_option($section) != $form_state['values'][$section]) {
          $this->set_option($section, $form_state['values'][$section]);
          $plugin = views_get_plugin('style', $form_state['values'][$section]);
821
          $this->set_option('style_options', $plugin->options($this->display));
822
        }
823 824 825 826 827 828 829
        break;
      case 'style_options':
        $style = TRUE;
      case 'row_options':
        // if row, $style will be empty.
        $plugin = views_get_plugin(empty($style) ? 'row' : 'style', $this->get_option('style_plugin'));
        if ($plugin) {
830
          $plugin->init($this->view, $this->display);
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
          $plugin->options_submit($form[$section], $form_state);
        }
        $this->set_option($section, $form_state['values'][$section]);
        break;
      case 'header':
      case 'footer':
      case 'empty':
        $this->set_option($section, $form_state['values'][$section]);
        $this->set_option($section . '_format', $form_state['values'][$section . '_format']);
        if ($section != 'empty') {
          $this->set_option($section . '_empty', $form_state['values'][$section . '_empty']);
        }
        break;
    }
  }

  /**
   * Add an override button for a given section, allowing the user to
   * change whether this info is stored on the default display or on
   * the current display.
   */
  function add_override_button(&$form, $section) {
    if ($this->is_default_display()) {
      return;
    }

    $form['override'] = array(
      '#prefix' => '<div class="views-override clear-block">',
      '#suffix' => '</div>',
    );
    if (!empty($this->display->display_options['defaults'][$section])) {
      $form['override']['button'] = array(
        '#type' => 'submit',
        '#value' => t('Override'),
        '#submit' => array('views_ui_edit_display_form_override'),
      );
      $form['override']['markup'] = array(
        '#prefix' => '<div class="description">',
        '#value' => t('This item is currently using default values; modifying this value will modify it for all displays.'),
        '#suffix' => '</div>',
      );
    }
    else {
      $form['override']['button'] = array(
        '#type' => 'submit',
        '#value' => t('Use default'),
        '#submit' => array('views_ui_edit_display_form_override'),
      );
      $form['override']['markup'] = array(
        '#prefix' => '<div class="description">',
        '#value' => t('This item is currently overriding default values; modifying this value will modify only for this display. Reverting it will remove current values and return to default values.'),
        '#suffix' => '</div>',
      );
    }
  }

  /**
   * If override/revert was clicked, perform the proper toggle.
   */
  function options_override($form, &$form_state) {
891 892 893 894 895 896 897
    $this->set_override($form_state['section']);
  }

  /**
   * Flip the override setting for the given section.
   */
  function set_override($section) {
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
    $options = $this->defaultable_sections($section);
    if (!$options) {
      return;
    }

    $new_state = empty($this->display->display_options['defaults'][$section]);

    // For each option that is part of this group, fix our settings.
    foreach ($options as $option) {
      if ($new_state) {
        // Revert to defaults.
        unset($this->display->display_options[$option]);
      }
      else {
        // copy existing values into our display.
        $this->display->display_options[$option] = $this->get_option($option);
      }
      $this->display->display_options['defaults'][$option] = $new_state;
merlinofchaos's avatar
merlinofchaos committed
916 917
    }
  }
918 919 920 921 922 923 924 925 926 927 928 929

  /**
   * Not all display plugins will support filtering
   */
  function render_filters() { }

  /**
   * Not all display plugins will have a 'more' link
   */
  function render_more_link() { }

  /**
930
   * Not all display plugins will have a feed icon.
931
   */
932 933 934 935
  function render_feed_icon() { }

  /**
   * Render the view's title for display
936
   * @todo Necessary? Hm.
937 938 939
   */
  function render_title() { }

940
  function render_textarea($area) {
merlinofchaos's avatar
merlinofchaos committed
941 942 943 944
    $value = $this->get_option($area);
    if ($value) {
      return check_markup($value, $this->get_option($area . '_format'));
    }
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
  }

  /**
   * Render the header of the view.
   */
  function render_header() { return $this->render_textarea('header'); }

  /**
   * Render the footer of the view.
   */
  function render_footer() { return $this->render_textarea('footer'); }

  /**
   * Render the empty text of the view.
   */
  function render_empty() { return $this->render_textarea('empty'); }
961 962 963
  /**
   * If this display creates a block, implement one of these.
   */
964
  function hook_block($op = 'list', $delta = 0, $edit = array()) { return array(); }
965 966 967 968

  /**
   * If this display creates a page with a menu item, implement it here.
   */
969
  function hook_menu() { return array(); }
970 971 972 973 974

  /**
   * Render this display.
   */
  function render() {
975 976
    $theme = views_theme_functions('views_view', $this->view, $this->display);
    return theme($theme, $this->view);
977 978
  }

979 980 981
  /**
   * Determine if the user has access to this display of the view.
   */
merlinofchaos's avatar
merlinofchaos committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995
  function access($account) {
    $access = $this->get_option('access');
    switch ($access['type']) {
      case 'role':
        $roles = array_keys($account->roles);
        $roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
        return array_intersect(array_filter($access['role']), $roles);
      case 'perm':
        return user_access($access['perm'], $account);
      case 'none':
      default:
        return TRUE;
    }
  }
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

  /**
   * Set up any variables on the view prior to execution. These are separated
   * from execute because they are extremely common and unlikely to be
   * overridden on an individual display.
   */
  function pre_execute() {
    // Copy pager information from the display.
    $this->view->set_use_pager($this->use_pager());
    $this->view->set_pager_element($this->get_option('pager_element'));
    $this->view->set_items_per_page($this->get_option('items_per_page'));
    $this->view->set_offset($this->get_option('offset'));
  }

1010
  /**
1011 1012
   * When used externally, this is how a view gets run and returns
   * data in the format required.
merlinofchaos's avatar
merlinofchaos committed
1013 1014
   *
   * The base class cannot be executed.
1015 1016
   */
  function execute() { }
1017 1018 1019 1020 1021 1022

  /**
   * Fully render the display for the purposes of a live preview or
   * some other AJAXy reason.
   */
  function preview() { return $this->view->render(); }
1023 1024
}

merlinofchaos's avatar
merlinofchaos committed
1025 1026 1027
/**
 * A plugin to handle defaults on a view.
 */
1028
class views_plugin_display_default extends views_plugin_display {
1029 1030 1031 1032 1033 1034
  /**
   * Determine if this display is the 'default' display which contains
   * fallback settings
   */
  function is_default_display() { return TRUE; }

1035
  function options(&$display) {
1036 1037 1038 1039 1040
    // Make sure the default display has a style plugin to start with.
    $display->display_options['style_plugin'] = 'default';
    $display->display_options['style_options'] = array();
    $display->display_options['row_plugin'] = 'fields';
    $display->display_options['row_options'] = array();
1041 1042 1043 1044 1045
    $display->display_options['relationships'] = array();
    $display->display_options['fields'] = array();
    $display->display_options['sorts'] = array();
    $display->display_options['arguments'] = array();
    $display->display_options['filters'] = array();
merlinofchaos's avatar
merlinofchaos committed
1046 1047

    $display->display_options['items_per_page'] = 10;
1048
  }
1049

merlinofchaos's avatar
merlinofchaos committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
  /**
   * The default execute handler fully renders the view.
   *
   * For the simplest use:
   * @code
   *   $output = $view->execute_display('default', $args);
   * @endcode
   *
   * For more complex usages, a view can be partially built:
   * @code
   *   $view->set_arguments($args);
   *   $view->build('default'); // Build the query
   *   $view->execute(); // Run the query
   *   $output = $view->render(); // Render the view
   * @endcode
   *
   * If short circuited at any point, look in $view->build_info for
   * information about the query. After execute, look in $view->result
merlinofchaos's avatar
merlinofchaos committed
1068
   * for the array of objects returned from db_query.
merlinofchaos's avatar
merlinofchaos committed
1069 1070 1071 1072 1073 1074 1075 1076 1077
   *
   * You can also do:
   * @code
   *   $view->set_arguments($args);
   *   $output = $view->render('default'); // Render the view
   * @endcode
   *
   * This illustrates that render is smart enough to call build and execute
   * if these items have not already been accomplished.
1078 1079 1080 1081
   *
   * Note that execute also must accomplish other tasks, such
   * as setting page titles, breadcrumbs, and generating exposed filter
   * data if necessary.
merlinofchaos's avatar
merlinofchaos committed
1082 1083 1084 1085 1086 1087
   */
  function execute() {
    return $this->view->render();
  }
}

1088 1089 1090
/**
 * The plugin that handles a full page.
 */
1091
class views_plugin_display_page extends views_plugin_display {
merlinofchaos's avatar
merlinofchaos committed
1092 1093 1094 1095 1096 1097 1098 1099 1100
  /**
   * The page display has a path.
   */
  function has_path() { return TRUE; }
  function uses_breadcrumb() { return TRUE; }

  /**
   * Add this display's path information to Drupal's menu system.
   */
1101 1102 1103 1104
  function execute_hook_menu() {
    $items = array();
    // Replace % with the link to our standard views argument loader
    // views_arg_load -- which lives in views.module
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118

    $bits = explode('/', $this->get_option('path'));
    $page_arguments = array($this->view->name, $this->display->id);

    // Replace % with %views_arg for menu autoloading and add to the
    // page arguments so the argument actually comes through.
    foreach($bits as $pos => $bit) {
      if ($bit == '%') {
        $bits[$pos] = '%views_arg';
        $page_arguments[] = $pos;
      }
    }

    $path = implode('/', $bits);
1119

1120 1121 1122 1123 1124 1125
    if ($path) {
      // NOTE: This is the very simple 'menu normal item' version. The
      // tab version needs to come later. Maybe it should be its own plugin.
      $items[$path] = array(
        // default views page entry
        'page callback' => 'views_page',
1126
        'page arguments' => $page_arguments,
1127 1128 1129 1130
        // Default access check (per display)
        'access callback' => 'views_access',
        'access arguments' => array(array($this->view->name, $this->display->id)),
        // Identify URL embedded arguments and correlate them to a handler
1131
        'load arguments'  => array($this->view->name, $this->display->id, '%index'),
1132
      );
1133 1134 1135 1136 1137 1138 1139 1140 1141
      $menu = $this->get_option('menu');
      if (empty($menu)) {
        $menu = array('type' => 'none');
      }
      // Set the title if we have one.
      if ($menu['type'] != 'none') {
        $items[$path]['title'] = $menu['title'];
      }

1142 1143 1144 1145
      if (isset($menu['weight'])) {
        $items[$path]['weight'] = intval($menu['weight']);
      }

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
      switch ($menu['type']) {
        case 'none':
        default:
          $items[$path]['type'] = MENU_CALLBACK;
          break;
        case 'normal':
          $items[$path]['type'] = MENU_NORMAL_ITEM;
          break;
        case 'tab':
          $items[$path]['type'] = MENU_LOCAL_TASK;
          break;
        case 'default tab':
          $items[$path]['type'] = MENU_LOCAL_TASK;
          break;
      }

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

          // we can't do this if they tried to make the last path bit variable.
          if ($bit != '%views_arg' && !empty($bits)) {
            $default_path = implode('/', $bits);
            $items[$default_path] = array(
              // default views page entry
              'page callback' => 'views_page',
              'page arguments' => $page_arguments,
              // Default access check (per display)
              'access callback' => 'views_access',
              'access arguments' => array(array($this->view->name, $this->display->id)),
              // Identify URL embedded arguments and correlate them to a handler
              'load arguments'  => array($this->view->name, $this->display->id, '%index'),
              'title' => $tab_options['title'],
            );
            switch ($tab_options['type']) {
              default:
              case 'normal':
                $items[$default_path]['type'] = MENU_NORMAL_ITEM;
                break;
              case 'tab':
                $items[$default_path]['type'] = MENU_LOCAL_TASK;
                break;
            }
          }
1195 1196 1197 1198
          if (isset($tab_options['weight'])) {
            $items[$default_path]['weight'] = intval($tab_options['weight']);
          }

1199 1200
        }
      }
1201
    }
merlinofchaos's avatar
merlinofchaos committed
1202

1203 1204 1205 1206 1207 1208 1209 1210 1211
    return $items;
  }

  /**
   * The display page handler returns a normal view, but it also does
   * a drupal_set_title for the page, and does a views_set_page_view
   * on the view.
   */
  function execute() {
1212
    // Let the world know that this is the page view we're using.
1213
    views_set_page_view($this);
1214

1215 1216 1217
    // Prior to this being called, the $view should already be set to this
    // display, and arguments should be set on the view.
    $this->view->build();
1218
    $this->view->get_breadcrumb(TRUE);
merlinofchaos's avatar
merlinofchaos committed