views.module 14.6 KB
Newer Older
1
<?php
2
// $Id$
3

4 5
/**
 * @file views.module
6 7
 *
 * Primarily Drupal hooks and global API functions to manipulate views.
8 9
 */

10
/**
11
 * Implementation of hook_theme(). Register views theming functions.
12
 */
13 14 15 16 17 18
function views_theme() {
  $path = drupal_get_path('module', 'views');
  include_once "$path/theme/theme.inc";

  // Some quasi clever array merging here.
  $base = array(
merlinofchaos's avatar
merlinofchaos committed
19
    'file' => 'theme.inc',
20
    'path' => "$path/theme",
21
  );
22 23 24 25 26 27 28 29 30

  $arguments = array(
    'display' => array('view' => NULL),
    'style' => array('view' => NULL, 'rows' => NULL),
    'row' => array('view' => NULL, 'row' => NULL),
  );

  // Default view themes
  $hooks['views_view'] = $base + array(
merlinofchaos's avatar
merlinofchaos committed
31
    'template' => 'views-view',
32 33 34 35
    'pattern' => 'views_view__',
    'arguments' => $arguments['display'],
  );
  $hooks['views_view_rows'] = $base + array(
merlinofchaos's avatar
merlinofchaos committed
36
    'template' => 'views-view-rows',
37 38 39 40
    'pattern' => 'views_view_rows__',
    'arguments' => $arguments['style'],
  );
  $hooks['views_view_row'] = $base + array(
merlinofchaos's avatar
merlinofchaos committed
41
    'template' => 'views-view-row',
42 43
    'pattern' => 'views_view_row__',
    'arguments' => $arguments['row'],
44
  );
45 46 47
  $hooks['views_view_field'] = $base + array(
    'pattern' => 'views_view_field__',
    'arguments' => array('view' => NULL, 'field' => NULL, 'row' => NULL),
48 49
  );

50 51 52 53 54 55 56 57
  $plugins = views_fetch_plugin_data();

  // Register theme functions for all style plugins
  foreach ($plugins as $type => $info) {
    foreach ($info as $plugin => $def) {
      if (isset($def['theme'])) {
        $hooks[$def['theme']] = array(
          'pattern' => $def['theme'] . '__',
merlinofchaos's avatar
merlinofchaos committed
58
          'file' => $def['file'],
59 60 61 62
          'path' => $def['path'],
          'arguments' => $arguments[$type],
        );
        if (!function_exists('theme_' . $def['theme'])) {
merlinofchaos's avatar
merlinofchaos committed
63
          $hooks[$def['theme']]['template'] = views_css_safe($def['theme']);
64 65 66 67
        }
      }
    }
  }
68

69
  return $hooks;
70 71
}

72
/**
73
 * Implementation of hook_menu().
74 75 76 77 78 79 80 81 82 83 84 85 86
 *
 * This probably needs to actually be hook_menu_alter or something.
 */
function views_menu() {
  $items = array();
  foreach (views_get_page_views() as $data) {
    list($view, $display_id) = $data;
    $items += $view->execute_hook_menu($display_id);
  }
  return $items;
}

/**
87
 * Helper function for menu loading. This will automatically be
88 89 90 91 92 93 94 95 96
 * called in order to 'load' a views argument; primarily it
 * will be used to perform validation.
 *
 * @param $value
 *   The actual value passed.
 * @param $name
 *   The name of the view. This needs to be specified in the 'load function'
 *   of the menu entry.
 * @param $index
97
 *   The menu argument index. This counts from 1.
98 99 100
 */
function views_arg_load($value, $name, $index) {
  if ($view = views_get_view($name)) {
101
    $view->init_handlers();
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    if (isset($view->argument[$index - 1])) {
      return $view->argument[$index - 1]->handler->validate($value);
    }
  }
}

/**
 * Page callback entry point; requires a view and a display id, then
 * passes control to the display handler.
 */
function views_page() {
  $args = func_get_args();
  $name = array_shift($args);
  $display_id = array_shift($args);

  // Load the view
  if ($view = views_get_view($name)) {
    return $view->execute_display($display_id, $args);
  }

  // Fallback; if we get here no view was found or handler was not valid.
  return drupal_not_found();
}

/**
 * Implementation of hook_block
 */
function views_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $items = array();
      foreach (views_get_block_views() as $data) {
        list($view, $display_id) = $data;
        $items += $view->execute_hook_block($display_id);
      }
      return $items;
    case 'configure':
    case 'list':
    case 'view':
      list($name, $display_id) = explode('-', $delta);
      // Load the view
      if ($view = views_get_view($name)) {
        if ($op == 'view') {
          return $view->execute_display($display_id, $args);
        }
        else {
          return $view->execute_hook_block($display_id, $op, $delta, $edit);
        }
      }
      break;
  }
}

155 156 157 158 159 160 161 162 163 164
/**
 * Implementation of hook_devel_caches().
 *
 * When the devel cache is cleared, clear cached views, too.  That
 * makes development a bit easier.
 */
function views_devel_caches() {
  return array('cache_views');
}

165 166
/**
 * Determine if the given user has access to the view + display.
167 168 169 170 171 172
 *
 * @param $view
 *   May be a view object, or an array with the view name and the display ID,
 *   or a string to use as the view name.
 * @param $account
 *   An optional account to use; if left off, the current user will be used.
173 174 175 176 177 178 179 180
 */
function views_access($view, $account = NULL) {
  if (is_array($view)) {
    list($name, $display_id) = $view;
    $view = views_get_view($name);
    if (!$view) {
      return FALSE;
    }
merlinofchaos's avatar
merlinofchaos committed
181 182 183
    if (!$view->init_display($display_id)) {
      return FALSE;
    }
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  }

  if (is_string($view)) {
    $view = views_get_view($name);
    if (!$view) {
      return FALSE;
    }
  }

  return $view->access($account);
}

/**
 * Set the current 'page view' that is being displayed so that it is easy
 * for other modules or the theme to identify.
 */
function &views_set_page_view($view = NULL) {
  static $cache = NULL;
  if (isset($view)) {
    $cache = $view;
  }

  return $cache;
}

/**
 * Find out what, if any, page view is currently in use. Please note that
 * this returns a reference, so be careful! You can unintentionally modify the
 * $view object.
 */
function &views_get_page_view() {
  return views_set_page_view();
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/**
 * Set the current 'current view' that is being built/rendered so that it is
 * easy for other modules or items in drupal_eval to identify
 */
function &views_set_current_view($view = NULL) {
  static $cache = NULL;
  if (isset($view)) {
    $cache = $view;
  }

  return $cache;
}

/**
 * Find out what, if any, current view is currently in use. Please note that
 * this returns a reference, so be careful! You can unintentionally modify the
 * $view object.
 */
function &views_get_current_view() {
  return views_set_current_view();
}

240 241 242 243
/**
 * Include views .inc files as necessary.
 */
function views_include($file) {
244 245 246 247 248 249
  static $used = array();
  if (!isset($used[$file])) {
    require_once drupal_get_path('module', 'views') . "/includes/$file.inc";
  }

  $used[$file] = TRUE;
250 251
}

merlinofchaos's avatar
merlinofchaos committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/**
 * Include views .css files.
 */
function views_add_css($file) {
  drupal_add_css(drupal_get_path('module', 'views') . "/css/$file.css");
}

/**
 * Include views .js files.
 */
function views_add_js($file) {
  static $base = TRUE;
  if ($base) {
    drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js");
  }
  drupal_add_js(drupal_get_path('module', 'views') . "/js/$file.js");
}

merlinofchaos's avatar
merlinofchaos committed
270 271 272 273 274 275 276 277 278 279
/**
 * Prepare the specified string for use as a CSS identifier.
 */
function views_css_safe($string) {
  return str_replace('_', '-', $string);
}

// -----------------------------------------------------------------------
// Views handler functions

280 281 282 283 284 285 286 287 288
/**
 * Load views files on behalf of modules.
 */
function views_include_handlers() {
  static $finished = FALSE;
  // Ensure this only gets run once.
  if ($finished) {
    return;
  }
289

290
  views_include('handlers');
291 292
  views_include('cache');
  _views_include_handlers();
293
  $finished = TRUE;
294 295
}

296 297 298 299 300 301 302 303 304 305 306 307
/**
 * Load default views files on behalf of modules.
 */
function views_include_default_views() {
  static $finished = FALSE;
  // Ensure this only gets run once.
  if ($finished) {
    return;
  }

  // Default views hooks may be in the normal handler file,
  // or in a separate views_default file at the discretion of
308
  // the module author.
309
  views_include_handlers();
310

311
  _views_include_default_views();
312 313 314
  $finished = TRUE;
}

315 316 317 318 319 320 321 322 323
/**
 * Fetch a handler from the data cache.
 */
function views_get_handler($table, $field, $key) {
  $data = views_fetch_data($table);
  if (isset($data[$field][$key])) {
    return _views_prepare_handler($data[$field][$key], $data, $field);
  }
  // DEBUG -- identify missing handlers
324
  dsm("Missing handler: $table $field $key");
325
}
merlinofchaos's avatar
merlinofchaos committed
326

327
/**
328
 * Fetch Views' data from the cache
329
 */
330 331 332
function views_fetch_data($table = NULL) {
  views_include('cache');
  return _views_fetch_data($table);
333
}
merlinofchaos's avatar
merlinofchaos committed
334

merlinofchaos's avatar
merlinofchaos committed
335 336 337 338 339 340 341 342 343 344 345 346
/**
 * Fetch a list of all base tables available
 *
 * @return
 *   A keyed array of in the form of 'base_table' => 'Description'.
 */
function views_fetch_base_table_names() {
  static $base_tables = array();
  if (empty($base_tables)) {
    $data = views_fetch_data();
    foreach ($data as $table => $info) {
      if (!empty($info['table']['base'])) {
merlinofchaos's avatar
merlinofchaos committed
347
        $base_tables[$table] = $info['table']['base']['title'] . '<br /><div class="description">' . $info['table']['base']['help'] . "</div>\n";
merlinofchaos's avatar
merlinofchaos committed
348 349 350 351 352 353 354 355 356 357
      }
    }
  }

  return $base_tables;
}

// -----------------------------------------------------------------------
// Views plugin functions

358 359 360 361
/**
 * Fetch the plugin data from cache.
 */
function views_fetch_plugin_data($type = NULL, $plugin = NULL) {
362 363
  views_include('cache');
  return _views_fetch_plugin_data($type, $plugin);
364
}
365

366
/**
367
 * Get a handler for a plugin
368
 */
369 370 371 372 373
function views_get_plugin($type, $plugin) {
  $definition = views_fetch_plugin_data($type, $plugin);
  if (!empty($definition)) {
    return _views_create_handler($definition);
  }
374
}
merlinofchaos's avatar
merlinofchaos committed
375 376

/**
merlinofchaos's avatar
merlinofchaos committed
377 378 379 380
 * Fetch a list of all base tables available
 *
 * @return
 *   A keyed array of in the form of 'base_table' => 'Description'.
merlinofchaos's avatar
merlinofchaos committed
381
 */
merlinofchaos's avatar
merlinofchaos committed
382 383 384 385 386 387 388 389 390 391 392
function views_fetch_plugin_names($type) {
  static $plugins = array();
  if (empty($plugins)) {
    $data = views_fetch_plugin_data();

    foreach ($data as $plugin_type => $plugs) {
      // Skip some info data
      if (!is_array($plugs)) {
        continue;
      }
      foreach ($plugs as $id => $plugin) {
merlinofchaos's avatar
merlinofchaos committed
393 394 395
        if (empty($plugin['no ui'])) {
          $plugins[$plugin_type][$id] = $plugin['title'];
        }
merlinofchaos's avatar
merlinofchaos committed
396 397 398 399 400 401 402 403 404
      }
    }
  }

  if (!empty($plugins[$type])) {
    return $plugins[$type];
  }
  // fall-through
  return array();
merlinofchaos's avatar
merlinofchaos committed
405
}
406

merlinofchaos's avatar
merlinofchaos committed
407 408 409
// -----------------------------------------------------------------------
// Views database functions

410 411 412
/**
 * Get a view from the database or from default views.
 *
merlinofchaos's avatar
merlinofchaos committed
413 414 415
 * This function is just a static wrapper around views::load(). This function
 * isn't called 'views_load()' primarily because it might get a view
 * from the default views which aren't technically loaded from the database.
416
 *
417 418
 * @param $name
 *   The name of the view.
419 420 421 422 423 424 425
 * @param $reset
 *   If TRUE, reset this entry in the load cache.
 * @return &$view
 *   A reference to the $view object. Use $reset if you're sure you want
 *   a fresh one.
 */
function &views_get_view($name, $reset = FALSE) {
426
  views_include('view');
merlinofchaos's avatar
merlinofchaos committed
427 428 429 430 431 432 433
  $view = view::load($name, $reset);

  if (empty($view)) {
    $view = views_get_default_view($name);
  }

  return $view;
434 435 436 437
}

/**
 * Get a view from the default views defined by modules.
438 439
 *
 * Default views are cached per-language.  This function will rescan the
440 441 442
 * default_views hook if necessary.
 *
 * @param $view_name
merlinofchaos's avatar
merlinofchaos committed
443 444 445
 *   The name of the view to load.
 * @return
 *   A view object or NULL if it is not available.
446 447
 */
function &views_get_default_view($view_name) {
merlinofchaos's avatar
merlinofchaos committed
448
  $null = NULL;
449
  $cache = views_discover_default_views();
450

451 452
  if (isset($cache[$view_name])) {
    return $cache[$view_name];
453
  }
merlinofchaos's avatar
merlinofchaos committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467
  return $null;
}

/**
 * Create an empty view to work with.
 *
 * @return
 *   A fully formed, empty $view object. This object must be populated before
 *   it can be successfully saved.
 */
function views_new_view() {
  views_include('view');
  $view = new view();
  $view->vid = 'new';
merlinofchaos's avatar
merlinofchaos committed
468
  $view->add_display('default');
merlinofchaos's avatar
merlinofchaos committed
469 470

  return $view;
471 472 473 474 475 476 477 478
}

/**
 * Scan all modules for default views and rebuild the default views cache.
 *
 * @return An associative array of all known default views.
 */
function views_discover_default_views() {
479
  static $cache = array();
480

481
  if (empty($cache)) {
482 483
    views_include('cache');
    $cache = _views_discover_default_views();
484
  }
485 486 487 488
  return $cache;
}

/**
489 490 491 492
 * Get a list of all views and the display plugins that provide
 * page support to the Drupal menu system. Since views can appear
 * in this list multiple times, the return of this function is an
 * array of arrays.
493
 *
494 495 496 497 498 499
 * @return
 * @code
 * array(
 *   array($view, $display_id),
 *   array($view, $display_id),
 * );
merlinofchaos's avatar
merlinofchaos committed
500
 * @endcode
501
 */
502 503
function views_get_page_views() {
  return views_get_applicable_views('uses_hook_menu');
504 505 506
}

/**
507 508 509 510
 * Get a list of all views and the display plugins that provide
 * themselves to the Drupal block system. Since views can appear
 * in this list multiple times, the return of this function is an
 * array of arrays.
511
 *
512 513 514 515 516 517
 * @return
 * @code
 * array(
 *   array($view, $display_id),
 *   array($view, $display_id),
 * );
merlinofchaos's avatar
merlinofchaos committed
518
 * @endcode
519
 */
520 521
function views_get_block_views() {
  return views_get_applicable_views('uses_hook_block');
522 523 524
}

/**
525 526
 * Return a list of all views and display IDs that have a particular
 * setting in their display's plugin settings.
527 528
 *
 * @return
529
 * @code
530 531 532 533
 * array(
 *   array($view, $display_id),
 *   array($view, $display_id),
 * );
merlinofchaos's avatar
merlinofchaos committed
534
 * @endcode
535
 */
536 537 538
function views_get_applicable_views($type) {
  $result = array();
  $views = views_get_all_views();
539

540 541 542 543
  foreach ($views as $view) {
    // Skip disabled views.
    if (!empty($view->disabled)) {
      continue;
544 545
    }

546 547
    foreach ($view->display as $display) {
      $plugin = views_fetch_plugin_data('display', $display->display_plugin);
548
      if (!empty($plugin[$type])) {
549 550 551
        // This view uses hook menu. Clone it so that different handlers
        // don't trip over each other, and add it to the list.
        $v = drupal_clone($view);
merlinofchaos's avatar
merlinofchaos committed
552 553 554
        if ($v->init_display($display->id)) {
          $result[] = array($v, $display->id);
        }
555 556 557 558 559 560 561
      }
    }
  }
  return $result;
}

/**
562
 * Return an array of all views as fully loaded $view objects.
563
 */
564 565
function views_get_all_views() {
  static $views = array();
566

567 568 569 570 571
  if (empty($views)) {
    // First, get all applicable views.
    views_include('view');
    $views = view::load_views();

572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
    // Get all default views.
    $status = variable_get('panel_page_defaults', array());

    foreach (views_discover_default_views() as $view) {
      // Determine if default view is enabled or disabled.
      if (isset($status[$view->name])) {
        $view->disabled = $status[$view->name];
      }

      // If overridden, also say so.
      if (!empty($views[$view->name])) {
        $views[$view->name]->type = t('Overridden');
      }
      else {
        $view->type = t('Default');
        $views[$view->name] = $view;
      }
    }

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
  }
  return $views;
}

/**
 * Basic definition for many views objects
 */
class views_object {
  /**
   * Views handlers use a special construct function so that we can more
   * easily construct them with variable arguments.
   */
  function construct() { }

  /**
   * Let the handler know what its full definition is.
   */
  function set_definition($definition) {
    $this->definition = $definition;
    if (isset($definition['field'])) {
      $this->real_field = $definition['field'];
612
    }
613 614
  }
}