plugins.inc 73.2 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', 'misc/autocomplete.js', "$path/dependent.js"),
24
        'use ajax' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
25
        'use pager' => TRUE,
26
        'use more' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
27
      ),
28 29
      'page' => array(
        'title' => t('Page'),
merlinofchaos's avatar
merlinofchaos committed
30
        'help' => t('Display the view as a page, with a URL and menu links.'),
31
        'handler' => 'views_plugin_display_page',
32
        'uses hook menu' => TRUE,
33
        'use ajax' => TRUE,
merlinofchaos's avatar
merlinofchaos committed
34
        'use pager' => TRUE,
35 36
      ),
      'block' => array(
37
        'title' => t('Block'),
merlinofchaos's avatar
merlinofchaos committed
38
        'help' => t('Display the view as a block.'),
39
        'handler' => 'views_plugin_display_block',
40
        'uses hook block' => TRUE,
41 42
        'use ajax' => TRUE,
        'use pager' => TRUE,
43
        'use more' => TRUE,
44 45 46 47
      ),
    ),
    'style' => array(
      'default' => array(
48
        'title' => t('Unformatted'),
49
        'help' => t('Displays rows one after another.'),
50
        'handler' => 'views_plugin_style_default',
merlinofchaos's avatar
merlinofchaos committed
51
        'theme' => 'views_view_unformatted',
52
        'uses row plugin' => TRUE,
53 54 55 56
      ),
      'list' => array(
        'title' => t('List'),
        'help' => t('Displays rows as an HTML list.'),
57
        'handler' => 'views_plugin_style_list',
58
        'theme' => 'views_view_list',
59 60
        'uses row plugin' => TRUE,
        'uses options' => TRUE,
61 62 63 64
      ),
      'table' => array(
        'title' => t('Table'),
        'help' => t('Displays rows in a table.'),
65
        'handler' => 'views_plugin_style_table',
66
        'theme' => 'views_view_table',
67 68
        'uses row plugin' => FALSE,
        'uses fields' => TRUE,
69
        'uses options' => TRUE,
70
      ),
71 72 73
      'default_summary' => array(
        'title' => t('Default'),
        'help' => t('Displays the default summary view'),
74
        'handler' => 'views_plugin_style_summary',
75
        'theme' => 'views_view_summary',
76 77
        'summary' => TRUE, // only shows up as a summary style
        'uses options' => TRUE,
78
      ),
79 80 81 82 83
    ),
    'row' => array(
      'fields' => array(
        'title' => t('Fields'),
        'help' => t('Displays the fields with an optional template.'),
84
        'handler' => 'views_plugin_row',
merlinofchaos's avatar
merlinofchaos committed
85
        'theme' => 'views_view_fields',
86
        'uses fields' => TRUE,
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 129 130 131 132 133 134
/**
 * 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;
}

135
/**
136
 * @defgroup views_plugin_displays Views' display plugins
137 138 139 140 141 142 143 144 145
 * @{
 * 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.
 */

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

159 160 161 162 163 164
  /**
   * Determine if this display is the 'default' display which contains
   * fallback settings
   */
  function is_default_display() { return FALSE; }

165 166 167 168
  /**
   * Determine if this display uses exposed filters, so the view
   * will know whether or not to build them.
   */
merlinofchaos's avatar
merlinofchaos committed
169
  function uses_exposed() {
merlinofchaos's avatar
merlinofchaos committed
170 171 172
    if (!isset($this->has_exposed)) {
      foreach (array('field', 'filter') as $type) {
        foreach ($this->view->$type as $key => $info) {
173
          if (!empty($info['handler']) && $info['handler']->is_exposed()) {
merlinofchaos's avatar
merlinofchaos committed
174 175 176 177 178 179 180 181
            // 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
182

merlinofchaos's avatar
merlinofchaos committed
183 184
    return $this->has_exposed;
  }
185

186 187 188 189 190 191 192 193 194 195
  /**
   * Does the display use AJAX?
   */
  function use_ajax() {
    if (!empty($this->definition['use ajax'])) {
      return $this->get_option('use_ajax');
    }
    return FALSE;
  }

merlinofchaos's avatar
merlinofchaos committed
196 197 198 199 200 201 202 203 204 205
  /**
   * Does the display have a pager enabled?
   */
  function use_pager() {
    if (!empty($this->definition['use pager'])) {
      return $this->get_option('use_pager');
    }
    return FALSE;
  }

206 207 208 209 210 211 212 213 214 215
  /**
   * Does the display have a more link enabled?
   */
  function use_more() {
    if (!empty($this->definition['use more'])) {
      return $this->get_option('use_more');
    }
    return FALSE;
  }

216 217 218 219 220 221 222 223 224 225 226
  /**
   * 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'),
227
      'use_ajax' => array('use_ajax'),
228
      'items_per_page' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
merlinofchaos's avatar
merlinofchaos committed
229
      'use_pager' => array('items_per_page', 'offset', 'use_pager', 'pager_element'),
230
      'use_more' => array('use_more'),
merlinofchaos's avatar
merlinofchaos committed
231
      'link_display' => array('link_display'),
232
      'distinct' => array('distinct'),
merlinofchaos's avatar
merlinofchaos committed
233 234

    // @todo
235 236 237 238 239 240 241 242
      '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'),
243

244
      // These guys are special
245
      'relationships' => array('relationships'),
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
      '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.
   */
266
  function options(&$display) {
267 268 269 270 271 272 273 274 275 276 277 278 279 280
    // 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,

281
      'use_ajax' => TRUE,
282 283 284 285
      'items_per_page' => TRUE,
      'offset' => TRUE,
      'use_pager' => TRUE,
      'pager_element'  => TRUE,
286
      'use_more' => TRUE,
287
      'distinct' => TRUE,
288 289 290 291 292 293 294 295 296 297

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

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

298
      'relationships' => TRUE,
299 300 301 302 303
      'fields' => TRUE,
      'sorts' => TRUE,
      'arguments' => TRUE,
      'filters' => TRUE,
    );
304 305 306 307 308 309

    $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();
310 311
  }

merlinofchaos's avatar
merlinofchaos committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  /**
   * 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; }

367 368 369 370 371
  /**
   * Intelligently get an option either from this display or from the
   * default display, if directed to do so.
   */
  function get_option($option) {
372 373
    if (!$this->is_default_display() && !empty($this->default_display) && !empty($this->display->display_options['defaults'][$option])) {
      return $this->default_display->get_option($option);
374
    }
375 376 377

    if (array_key_exists($option, $this->display->display_options)) {
      return $this->display->display_options[$option];
378
    }
379 380
  }

381 382 383 384 385 386 387 388 389 390 391
  /**
   * 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();
    }
  }

392 393 394 395 396 397 398
  /**
   * 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);
399
    }
400 401

    return $this->display->display_options[$option] = $value;
402
  }
403

merlinofchaos's avatar
merlinofchaos committed
404 405 406 407
  /**
   * Because forms may be split up into sections, this provides
   * an easy URL to exactly the right section. Don't override this.
   */
408
  function option_link($text, $section, $class = '', $title = '') {
409 410 411 412
    if (!empty($class)) {
      $text = '<span>' . $text . '</span>';
    }

413 414 415 416
    if (empty($text)) {
      $text = t('Broken field');
    }

417 418 419 420 421
    if (empty($title)) {
      $title = $text;
    }

    return l($text, 'admin/build/views/nojs/display/' . $this->view->name . '/' . $this->display->id . '/' . $section, array('attributes' => array('class' => 'views-ajax-link ' . $class, 'title' => $title), 'html' => TRUE));
merlinofchaos's avatar
merlinofchaos committed
422 423 424 425
  }

  /**
   * Provide the default summary for options in the views UI.
426 427
   *
   * This output is returned as an array.
merlinofchaos's avatar
merlinofchaos committed
428
   */
429 430 431 432 433 434 435 436 437
  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,
438
      'desc' => t('Change the name of this display.'),
439
    );
440 441 442 443 444

    $title = $this->get_option('title');
    if (!$title) {
      $title = t('None');
    }
445 446 447 448 449

    $options['title'] = array(
      'category' => 'basic',
      'title' => t('Title'),
      'value' => $title,
450
      'desc' => t('Change the title that this display will use.'),
451
    );
452 453 454 455 456

    $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 = '';
457 458 459 460 461

    $options['style_plugin'] = array(
      'category' => 'basic',
      'title' => t('Style'),
      'value' => $style_title,
462
      'desc' => t('Change the style plugin.'),
463 464 465
    );

    // This adds a 'Settings' link to the style_options setting if the style has options.
466
    if (!empty($style_plugin['uses options'])) {
467
      $options['style_plugin']['links']['style_options'] = t('Change settings for this style');
468 469 470 471 472 473
    }

    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'];

474 475 476 477
      $options['row_plugin'] = array(
        'category' => 'basic',
        'title' => t('Row style'),
        'value' => $row_title,
478
        'desc' => t('Change the row plugin.'),
479 480
      );
      // This adds a 'Settings' link to the row_options setting if the row style has options.
481
      if (!empty($row_plugin['uses options'])) {
482
        $options['row_plugin']['links']['row_options'] = t('Change settings for this style');
483 484
      }
    }
485 486 487 488 489
    if (!empty($this->definition['use ajax'])) {
      $options['use_ajax'] = array(
        'category' => 'basic',
        'title' => t('Use AJAX'),
        'value' => $this->get_option('use_ajax') ? t('Yes') : t('No'),
490
        'desc' => t('Change whether or not this display will use AJAX.'),
491 492
      );
    }
493

494 495 496 497
    if (!empty($this->definition['use pager'])) {
      $options['use_pager'] = array(
        'category' => 'basic',
        'title' => t('Use pager'),
498
        'value' => $this->get_option('use_pager') ? ($this->get_option('use_pager') == 'mini' ? t('Mini') : t('Yes')) : t('No'),
499
        'desc' => t("Change this display's pager setting."),
500 501
      );
    }
502

503 504 505 506
    $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')),
507
      'desc' => t('Change how many items to display.'),
508 509
    );

510 511 512 513 514
    if (!empty($this->definition['use more'])) {
      $options['use_more'] = array(
        'category' => 'basic',
        'title' => t('More link'),
        'value' => $this->get_option('use_more') ? t('Yes') : t('No'),
515
        'desc' => t('Specify whether this display will provide a "more" link.'),
516 517 518
      );
    }

519 520 521 522 523 524 525
    $options['distinct'] = array(
      'category' => 'basic',
      'title' => t('Distinct'),
      'value' => $this->get_option('distinct') ? t('Yes') : t('No'),
      'desc' => t('Display only distinct items, without duplicates.'),
    );

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    $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;
    }
550 551 552 553 554

    $options['access'] = array(
      'category' => 'basic',
      'title' => t('Access'),
      'value' => $access_str,
555
      'desc' => t('Specify access control settings for this display.'),
556
    );
557

merlinofchaos's avatar
merlinofchaos committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    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,
577
          'desc' => t('Specify which display this display will link to.'),
merlinofchaos's avatar
merlinofchaos committed
578 579 580 581
        );
      }
    }

582 583 584 585 586 587 588 589 590 591 592 593
    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);
594 595 596 597 598 599
        if ($format) {
          $field = $format->name;
        }
        else {
          $field = t('Unknown/missing filter');
        }
600 601
      }

602
//      $output[] = t('!name: !field', array('!name' => $name, '!field' => $this->option_link($field, $type)));
603 604 605 606
      $options[$type] = array(
        'category' => 'basic',
        'title' => $name,
        'value' => $field,
607
        'desc' => t("Change this display's !name.", array('!name' => strtolower($name))),
608
      );
609
    }
merlinofchaos's avatar
merlinofchaos committed
610 611
  }

612 613 614
  /**
   * Provide the default form for setting options.
   */
merlinofchaos's avatar
merlinofchaos committed
615
  function options_form(&$form, &$form_state) {
616 617 618
    if ($this->defaultable_sections($form_state['section'])) {
      $this->add_override_button($form, $form_state['section']);
    }
619
    $form['#title'] = check_plain($this->display->display_title) . ': ';
620

621 622 623 624 625 626 627 628 629 630
    // 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
631 632
    switch ($form_state['section']) {
      case 'display_title':
633
        $form['#title'] .= t('The name of this display');
merlinofchaos's avatar
merlinofchaos committed
634 635 636 637 638 639
        $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;
640
      case 'title':
641
        $form['#title'] .= t('The title of this view');
642 643 644 645 646 647
        $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;
648 649 650 651 652
      case 'use_ajax':
        $form['#title'] .= t('Use AJAX when available to load this view');
        $form['description'] = array(
          '#prefix' => '<div class="description form-item">',
          '#suffix' => '</div>',
653
          '#value' => t('If set, this view will use an AJAX mechanism for paging, table sorting and exposed filters. This means the entire page will not refresh. It is not recommended that you use this if this view is the main content of the page as it will prevent deep linking to specific pages, but it is very useful for side content.'),
654 655 656 657 658 659 660
        );
        $form['use_ajax'] = array(
          '#type' => 'radios',
          '#options' => array(1 => t('Yes'), 0 => t('No')),
          '#default_value' => $this->get_option('use_ajax'),
        );
        break;
merlinofchaos's avatar
merlinofchaos committed
661 662 663 664
      case 'use_pager':
        $form['#title'] .= t('Use a pager for this view');
        $form['use_pager'] = array(
          '#type' => 'radios',
665
          '#options' => array(1 => t('Full pager'), 'mini' => t('Mini pager'), 0 => t('No')),
merlinofchaos's avatar
merlinofchaos committed
666 667 668 669 670
          '#default_value' => $this->get_option('use_pager'),
        );
        $form['pager_element'] = array(
          '#type' => 'textfield',
          '#title' => t('Pager element'),
671
          '#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
672 673 674 675
          '#default_value' => intval($this->get_option('pager_element')),
        );
        break;
      case 'items_per_page':
merlinofchaos's avatar
merlinofchaos committed
676
        $form['#title'] .= $this->use_pager() ? t('Items per page') : t('Items to display');
merlinofchaos's avatar
merlinofchaos committed
677

merlinofchaos's avatar
merlinofchaos committed
678 679 680 681 682 683 684 685 686 687 688 689
        $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;
690 691 692 693 694
      case 'use_more':
        $form['#title'] .= t('Add a more link to the bottom of the display.');
        $form['use_more'] = array(
          '#type' => 'checkbox',
          '#title' => t('Create more link'),
695
          '#description' => t("This will add a more link to the bottom of this view, which will link to the page view. If you have more than one page view, the link will point to the display specified in 'Link display' above."),
696 697 698
          '#default_value' => $this->get_option('use_more'),
        );
        break;
699 700 701 702 703 704 705 706 707
      case 'distinct':
        $form['#title'] .= t('Display only distinct items, without duplicates.');
        $form['distinct'] = array(
          '#type' => 'checkbox',
          '#title' => t('Distinct'),
          '#description' => t('This will make the view display only distinct items. If there are multiple identical items, each will be displayed only once. You can use this to try and remove duplicates from a view, though it does not always work. Note that this can slow queries down, so use it with caution.'),
          '#default_value' => $this->get_option('distinct'),
        );
        break;
708
      case 'access':
709
        $form['#title'] .= t('Access restrictions');
710 711 712 713 714 715 716 717 718 719 720
        $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
721
          '#prefix' => '<div class="views-left-25">',
722 723 724 725 726 727 728
          '#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(
729 730 731 732
          // 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>',
733 734 735 736 737
          '#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.'),
738 739
          '#process' => array('expand_checkboxes', 'views_process_dependency'),
          '#dependency' => array('radio:access[type]' => array('role')),
740 741 742 743 744 745 746 747 748 749
        );

        $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(
750
          '#prefix' => '<div class="views-left-30">',
751 752 753 754 755 756
          '#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.'),
757 758 759 760
          '#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')),
761 762 763
        );
        break;
      case 'header':
764
        $form['#title'] .= t('Header');
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
        $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':
780
        $form['#title'] .= t('Footer');
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
        $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':
796
        $form['#title'] .= t('Empty text');
797 798 799 800 801 802 803 804 805 806
        $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':
807
        $form['#title'] .= t('How should this view be styled');
808 809 810 811
        $form['style_plugin'] =  array(
          '#type' => 'radios',
          '#options' => views_fetch_plugin_names('style', 'summary', FALSE),
          '#default_value' => $this->get_option('style_plugin'),
812
          '#description' => t('If the style you choose has settings, be sure to click the settings button that will appear next to it in the View summary.'),
813 814 815
        );
        break;
      case 'style_options':
816
        $form['#title'] .= t('Style options');
817
        $style = TRUE;
818 819
        $type = 'style_plugin';

820 821
      case 'row_options':
        // if row, $style will be empty.
822 823
        if (empty($style)) {
          $form['#title'] .= t('Row style options');
824
          $type = 'row_plugin';
825
        }
826
        $plugin = views_get_plugin(empty($style) ? 'row' : 'style', $this->get_option($type));
827 828 829 830
        if ($plugin) {
          $form[$form_state['section']] = array(
            '#tree' => TRUE,
          );
831
          $plugin->init($this->view, $this->display);
832
          $plugin->options_form($form[$form_state['section']], $form_state);
833 834 835
        }
        break;
      case 'row_plugin':
836
        $form['#title'] .= t('How should each row in this view be styled');
837 838 839 840 841 842
        $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
843 844 845 846 847 848 849 850 851 852
      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,
853
          '#description' => t("Which display to use to get this display's path for things like summary links, rss feed links, more links, etc."),
merlinofchaos's avatar
merlinofchaos committed
854 855 856 857
          '#default_value' => $this->get_link_display(),
        );
        break;

merlinofchaos's avatar
merlinofchaos committed
858 859
    }
  }
860

861 862 863
  /**
   * Validate the options form.
   */
864 865 866 867 868 869 870 871
  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) {
872
          $plugin->init($this->view, $this->display);
873 874 875
          $plugin->options_validate($form[$form_state['section']], $form_state);
        }
        break;
876 877 878 879 880
      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"'));
        }
881 882
    }
  }
883 884 885 886 887

  /**
   * 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
888
  function options_submit($form, &$form_state) {
889 890
    $section = $form_state['section'];
    switch ($section) {
merlinofchaos's avatar
merlinofchaos committed
891 892 893
      case 'display_title':
        $this->display->display_title = $form_state['values']['display_title'];
        break;
894 895
      case 'title':
      case 'access':
merlinofchaos's avatar
merlinofchaos committed
896 897
      case 'link_display':
      case 'php_arg_code':
898
      case 'use_ajax':
899 900
        $this->set_option($section, $form_state['values'][$section]);
        break;
merlinofchaos's avatar
merlinofchaos committed
901 902 903 904 905 906 907 908
      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;
909
      case 'use_more':
910
      case 'distinct':
911 912
        $this->set_option($section, $form_state['values'][$section]);
        break;
913
      case 'row_plugin':
914 915 916 917 918
        // 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]);
919
          $this->set_option('row_options', $plugin->options($this->display));
920
        }
921 922
        break;
      case 'style_plugin':
923 924 925 926 927
        // 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]);
928
          $this->set_option('style_options', $plugin->options($this->display));
929
        }
930 931 932 933 934 935 936
        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) {
937
          $plugin->init($this->view, $this->display);
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
          $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) {
998 999 1000 1001 1002 1003 1004
    $this->set_override($form_state['section']);
  }

  /**
   * Flip the override setting for the given section.
   */
  function set_override($section) {
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    $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
1023 1024
    }
  }
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  /**
   * Inject anything into the query that the display handler needs.
   */
  function query() {
    // Make the query distinct if the option was set.
    if ($this->get_option('distinct')) {
      $this->view->query->set_distinct();
    }
  }

1036 1037 1038 1039 1040 1041
  /**
   * Not all display plugins will support filtering
   */
  function render_filters() { }

  /**
1042
   * Render the 'more' link
1043
   */
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  function render_more_link() {
    if ($this->use_more()) {
      $path = $this->get_path();
      if ($path) {
        $theme = views_theme_functions('views_more', $this->view, $this->display);
        $path = check_url(url($path));
        return theme($theme, $path);
      }
    }
  }
1054 1055

  /**
1056
   * Not all display plugins will have a feed icon.
1057
   */
1058 1059 1060
  function render_feed_icon() { }

  /**
1061
   * Render a text area, using hte proper format.
1062
   */
1063
  function render_textarea($area) {
merlinofchaos's avatar
merlinofchaos committed
1064 1065
    $value = $this->get_option($area);
    if ($value) {
1066
      return check_markup($value, $this->get_option($area . '_format'), FALSE);
merlinofchaos's avatar
merlinofchaos committed
1067
    }
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
  }

  /**
   * 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'); }
1084 1085 1086
  /**
   * If this display creates a block, implement one of these.
   */
1087
  function hook_block($op = 'list', $delta = 0, $edit = array()) { return array(); }
1088 1089 1090 1091

  /**
   * If this display creates a page with a menu item, implement it here.
   */
1092
  function hook_menu() { return array(); }
1093 1094 1095 1096 1097

  /**
   * Render this display.
   */
  function render() {
1098 1099
    $theme = views_theme_functions('views_view', $this->view, $this->display);
    return theme($theme, $this->view);
1100 1101
  }

1102 1103 1104
  /**
   * Determine if the user has access to this display of the view.
   */
merlinofchaos's avatar
merlinofchaos committed
1105 1106 1107 1108 1109 1110 1111 1112 1113