PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/source/sites/all/modules/contrib/views/includes/view.inc

https://bitbucket.org/edutrul/sanitation
Pascal | 2649 lines | 1237 code | 252 blank | 1160 comment | 140 complexity | 945fd8f73b45bdf86a9fef17eeaf2c4c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @file
  4. * Provides the view object type and associated methods.
  5. */
  6. /**
  7. * @defgroup views_objects Objects that represent a View or part of a view
  8. * @{
  9. * These objects are the core of Views do the bulk of the direction and
  10. * storing of data. All database activity is in these objects.
  11. */
  12. /**
  13. * An object to contain all of the data to generate a view, plus the member
  14. * functions to build the view query, execute the query and render the output.
  15. */
  16. class view extends views_db_object {
  17. var $db_table = 'views_view';
  18. var $base_table = 'node';
  19. var $base_field = 'nid';
  20. /**
  21. * The name of the view.
  22. *
  23. * @var string
  24. */
  25. var $name = "";
  26. /**
  27. * The id of the view, which is used only for views in the database.
  28. *
  29. * @var number
  30. */
  31. var $vid;
  32. /**
  33. * The description of the view, which is used only in the interface.
  34. *
  35. * @var string
  36. */
  37. var $description;
  38. /**
  39. * The "tags" of a view.
  40. * The tags are stored as a single string, though it is used as multiple tags
  41. * for example in the views overview.
  42. *
  43. * @var string
  44. */
  45. var $tag;
  46. /**
  47. * The human readable name of the view.
  48. *
  49. * @var string
  50. */
  51. var $human_name;
  52. /**
  53. * The core version the view was created for.
  54. * @var int
  55. */
  56. var $core;
  57. /**
  58. * The views-api version this view was created by.
  59. *
  60. * Some examples of the variable are 3.0 or 3.0-alpha1
  61. *
  62. * @var string
  63. */
  64. var $api_version;
  65. /**
  66. * Is the view disabled.
  67. *
  68. * This value is used for exported view, to provide some default views which aren't enabled.
  69. *
  70. * @var bool
  71. */
  72. var $disabled;
  73. // State variables
  74. var $built = FALSE;
  75. var $executed = FALSE;
  76. var $editing = FALSE;
  77. var $args = array();
  78. var $build_info = array();
  79. var $use_ajax = FALSE;
  80. /**
  81. * Where the results of a query will go.
  82. *
  83. * The array must use a numeric index starting at 0.
  84. *
  85. * @var array
  86. */
  87. var $result = array();
  88. // May be used to override the current pager info.
  89. var $current_page = NULL;
  90. var $items_per_page = NULL;
  91. var $offset = NULL;
  92. var $total_rows = NULL;
  93. // Places to put attached renderings:
  94. var $attachment_before = '';
  95. var $attachment_after = '';
  96. // Exposed widget input
  97. var $exposed_data = array();
  98. var $exposed_input = array();
  99. // Exposed widget input directly from the $form_state['values'].
  100. var $exposed_raw_input = array();
  101. // Used to store views that were previously running if we recurse.
  102. var $old_view = array();
  103. // To avoid recursion in views embedded into areas.
  104. var $parent_views = array();
  105. // Is the current stored view runned as an attachment to another view.
  106. var $is_attachment = NULL;
  107. // Stores the next steps of form items to handle.
  108. // It's an array of stack items, which contain the form id, the type of form,
  109. // the view, the display and some additional arguments.
  110. // @see views_ui_add_form_to_stack()
  111. // var $stack;
  112. /**
  113. * Identifier of the current display.
  114. *
  115. * @var string
  116. */
  117. var $current_display;
  118. /**
  119. * Where the $query object will reside:
  120. *
  121. * @var views_plugin_query
  122. */
  123. var $query = NULL;
  124. /**
  125. * The current used display plugin.
  126. *
  127. * @var views_plugin_display
  128. */
  129. var $display_handler;
  130. /**
  131. * Stores all display handlers of this view.
  132. *
  133. * @var array[views_display]
  134. */
  135. var $display;
  136. /**
  137. * The current used style plugin.
  138. *
  139. * @var views_plugin_style
  140. */
  141. var $style_plugin;
  142. /**
  143. * Stored the changed options of the style plugin.
  144. *
  145. * @deprecated Better use $view->style_plugin->options
  146. * @var array
  147. */
  148. var $style_options;
  149. /**
  150. * Stores the current active row while rendering.
  151. *
  152. * @var int
  153. */
  154. var $row_index;
  155. /**
  156. * Allow to override the url of the current view.
  157. *
  158. * @var string
  159. */
  160. var $override_url = NULL;
  161. /**
  162. * Allow to override the path used for generated urls.
  163. *
  164. * @var string
  165. */
  166. var $override_path = NULL;
  167. /**
  168. * Allow to override the used database which is used for this query.
  169. */
  170. var $base_database = NULL;
  171. /**
  172. * Here comes a list of the possible handler which are active on this view.
  173. */
  174. /**
  175. * Stores the field handlers which are initialized on this view.
  176. * @var array[views_handler_field]
  177. */
  178. var $field;
  179. /**
  180. * Stores the argument handlers which are initialized on this view.
  181. * @var array[views_handler_argument]
  182. */
  183. var $argument;
  184. /**
  185. * Stores the sort handlers which are initialized on this view.
  186. * @var array[views_handler_sort]
  187. */
  188. var $sort;
  189. /**
  190. * Stores the filter handlers which are initialized on this view.
  191. * @var array[views_handler_filter]
  192. */
  193. var $filter;
  194. /**
  195. * Stores the relationship handlers which are initialized on this view.
  196. * @var array[views_handler_relationship]
  197. */
  198. var $relationship;
  199. /**
  200. * Stores the area handlers for the header which are initialized on this view.
  201. * @var array[views_handler_area]
  202. */
  203. var $header;
  204. /**
  205. * Stores the area handlers for the footer which are initialized on this view.
  206. * @var array[views_handler_area]
  207. */
  208. var $footer;
  209. /**
  210. * Stores the area handlers for the empty text which are initialized on this view.
  211. * @var array[views_handler_area]
  212. */
  213. var $empty;
  214. /**
  215. * Constructor
  216. */
  217. function __construct() {
  218. parent::init();
  219. // Make sure all of our sub objects are arrays.
  220. foreach ($this->db_objects() as $object) {
  221. $this->$object = array();
  222. }
  223. }
  224. /**
  225. * Perform automatic updates when loading or importing a view.
  226. *
  227. * Over time, some things about Views or Drupal data has changed.
  228. * this attempts to do some automatic updates that must happen
  229. * to ensure older views will at least try to work.
  230. */
  231. function update() {
  232. // When views are converted automatically the base_table should be renamed
  233. // to have a working query.
  234. $this->base_table = views_move_table($this->base_table);
  235. }
  236. /**
  237. * Returns a list of the sub-object types used by this view. These types are
  238. * stored on the display, and are used in the build process.
  239. */
  240. function display_objects() {
  241. return array('argument', 'field', 'sort', 'filter', 'relationship', 'header', 'footer', 'empty');
  242. }
  243. /**
  244. * Returns the complete list of dependent objects in a view, for the purpose
  245. * of initialization and loading/saving to/from the database.
  246. */
  247. static function db_objects() {
  248. return array('display');
  249. }
  250. /**
  251. * Set the arguments that come to this view. Usually from the URL
  252. * but possibly from elsewhere.
  253. */
  254. function set_arguments($args) {
  255. $this->args = $args;
  256. }
  257. /**
  258. * Change/Set the current page for the pager.
  259. */
  260. function set_current_page($page) {
  261. $this->current_page = $page;
  262. // If the pager is already initialized, pass it through to the pager.
  263. if (!empty($this->query->pager)) {
  264. return $this->query->pager->set_current_page($page);
  265. }
  266. }
  267. /**
  268. * Get the current page from the pager.
  269. */
  270. function get_current_page() {
  271. // If the pager is already initialized, pass it through to the pager.
  272. if (!empty($this->query->pager)) {
  273. return $this->query->pager->get_current_page();
  274. }
  275. if (isset($this->current_page)) {
  276. return $this->current_page;
  277. }
  278. }
  279. /**
  280. * Get the items per page from the pager.
  281. */
  282. function get_items_per_page() {
  283. // If the pager is already initialized, pass it through to the pager.
  284. if (!empty($this->query->pager)) {
  285. return $this->query->pager->get_items_per_page();
  286. }
  287. if (isset($this->items_per_page)) {
  288. return $this->items_per_page;
  289. }
  290. }
  291. /**
  292. * Set the items per page on the pager.
  293. */
  294. function set_items_per_page($items_per_page) {
  295. $this->items_per_page = $items_per_page;
  296. // If the pager is already initialized, pass it through to the pager.
  297. if (!empty($this->query->pager)) {
  298. $this->query->pager->set_items_per_page($items_per_page);
  299. }
  300. }
  301. /**
  302. * Get the pager offset from the pager.
  303. */
  304. function get_offset() {
  305. // If the pager is already initialized, pass it through to the pager.
  306. if (!empty($this->query->pager)) {
  307. return $this->query->pager->get_offset();
  308. }
  309. if (isset($this->offset)) {
  310. return $this->offset;
  311. }
  312. }
  313. /**
  314. * Set the offset on the pager.
  315. */
  316. function set_offset($offset) {
  317. $this->offset = $offset;
  318. // If the pager is already initialized, pass it through to the pager.
  319. if (!empty($this->query->pager)) {
  320. $this->query->pager->set_offset($offset);
  321. }
  322. }
  323. /**
  324. * Determine if the pager actually uses a pager.
  325. */
  326. function use_pager() {
  327. if (!empty($this->query->pager)) {
  328. return $this->query->pager->use_pager();
  329. }
  330. }
  331. /**
  332. * Whether or not AJAX should be used. If AJAX is used, paging,
  333. * tablesorting and exposed filters will be fetched via an AJAX call
  334. * rather than a page refresh.
  335. */
  336. function set_use_ajax($use_ajax) {
  337. $this->use_ajax = $use_ajax;
  338. }
  339. /**
  340. * Set the exposed filters input to an array. If unset they will be taken
  341. * from $_GET when the time comes.
  342. */
  343. function set_exposed_input($filters) {
  344. $this->exposed_input = $filters;
  345. }
  346. /**
  347. * Figure out what the exposed input for this view is.
  348. */
  349. function get_exposed_input() {
  350. // Fill our input either from $_GET or from something previously set on the
  351. // view.
  352. if (empty($this->exposed_input)) {
  353. $this->exposed_input = $_GET;
  354. // unset items that are definitely not our input:
  355. foreach (array('page', 'q') as $key) {
  356. if (isset($this->exposed_input[$key])) {
  357. unset($this->exposed_input[$key]);
  358. }
  359. }
  360. // If we have no input at all, check for remembered input via session.
  361. // If filters are not overridden, store the 'remember' settings on the
  362. // default display. If they are, store them on this display. This way,
  363. // multiple displays in the same view can share the same filters and
  364. // remember settings.
  365. $display_id = ($this->display_handler->is_defaulted('filters')) ? 'default' : $this->current_display;
  366. if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->name][$display_id])) {
  367. $this->exposed_input = $_SESSION['views'][$this->name][$display_id];
  368. }
  369. }
  370. return $this->exposed_input;
  371. }
  372. /**
  373. * Set the display for this view and initialize the display handler.
  374. */
  375. function init_display($reset = FALSE) {
  376. // The default display is always the first one in the list.
  377. if (isset($this->current_display)) {
  378. return TRUE;
  379. }
  380. // Instantiate all displays
  381. foreach (array_keys($this->display) as $id) {
  382. // Correct for shallow cloning
  383. // Often we'll have a cloned view so we don't mess up each other's
  384. // displays, but the clone is pretty shallow and doesn't necessarily
  385. // clone the displays. We can tell this by looking to see if a handler
  386. // has already been set; if it has, but $this->current_display is not
  387. // set, then something is dreadfully wrong.
  388. if (!empty($this->display[$id]->handler)) {
  389. $this->display[$id] = clone $this->display[$id];
  390. unset($this->display[$id]->handler);
  391. }
  392. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  393. if (!empty($this->display[$id]->handler)) {
  394. $this->display[$id]->handler->localization_keys = array($id);
  395. // Initialize the new display handler with data.
  396. $this->display[$id]->handler->init($this, $this->display[$id]);
  397. // If this is NOT the default display handler, let it know which is
  398. // since it may well utilize some data from the default.
  399. // This assumes that the 'default' handler is always first. It always
  400. // is. Make sure of it.
  401. if ($id != 'default') {
  402. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  403. }
  404. }
  405. }
  406. $this->current_display = 'default';
  407. $this->display_handler = &$this->display['default']->handler;
  408. return TRUE;
  409. }
  410. /**
  411. * Get the first display that is accessible to the user.
  412. *
  413. * @param $displays
  414. * Either a single display id or an array of display ids.
  415. */
  416. function choose_display($displays) {
  417. if (!is_array($displays)) {
  418. return $displays;
  419. }
  420. $this->init_display();
  421. foreach ($displays as $display_id) {
  422. if ($this->display[$display_id]->handler->access()) {
  423. return $display_id;
  424. }
  425. }
  426. return 'default';
  427. }
  428. /**
  429. * Set the display as current.
  430. *
  431. * @param $display_id
  432. * The id of the display to mark as current.
  433. */
  434. function set_display($display_id = NULL) {
  435. // If we have not already initialized the display, do so. But be careful.
  436. if (empty($this->current_display)) {
  437. $this->init_display();
  438. // If handlers were not initialized, and no argument was sent, set up
  439. // to the default display.
  440. if (empty($display_id)) {
  441. $display_id = 'default';
  442. }
  443. }
  444. $display_id = $this->choose_display($display_id);
  445. // If no display id sent in and one wasn't chosen above, we're finished.
  446. if (empty($display_id)) {
  447. return FALSE;
  448. }
  449. // Ensure the requested display exists.
  450. if (empty($this->display[$display_id])) {
  451. $display_id = 'default';
  452. if (empty($this->display[$display_id])) {
  453. vpr('set_display() called with invalid display id @display.', array('@display' => $display_id));
  454. return FALSE;
  455. }
  456. }
  457. // Set the current display.
  458. $this->current_display = $display_id;
  459. // Ensure requested display has a working handler.
  460. if (empty($this->display[$display_id]->handler)) {
  461. return FALSE;
  462. }
  463. // Set a shortcut
  464. $this->display_handler = &$this->display[$display_id]->handler;
  465. return TRUE;
  466. }
  467. /**
  468. * Find and initialize the style plugin.
  469. *
  470. * Note that arguments may have changed which style plugin we use, so
  471. * check the view object first, then ask the display handler.
  472. */
  473. function init_style() {
  474. if (isset($this->style_plugin)) {
  475. return is_object($this->style_plugin);
  476. }
  477. if (!isset($this->plugin_name)) {
  478. $this->plugin_name = $this->display_handler->get_option('style_plugin');
  479. $this->style_options = $this->display_handler->get_option('style_options');
  480. }
  481. $this->style_plugin = views_get_plugin('style', $this->plugin_name);
  482. if (empty($this->style_plugin)) {
  483. return FALSE;
  484. }
  485. // init the new style handler with data.
  486. $this->style_plugin->init($this, $this->display[$this->current_display], $this->style_options);
  487. return TRUE;
  488. }
  489. /**
  490. * Attempt to discover if the view has handlers missing relationships.
  491. *
  492. * This will try to add relationships automatically if it can, and will
  493. * remove the handlers if it cannot.
  494. */
  495. function fix_missing_relationships() {
  496. if (isset($this->relationships_fixed)) {
  497. return;
  498. }
  499. $this->relationships_fixed = TRUE;
  500. // Go through all of our handler types and test them to see if they
  501. // are missing relationships. Missing relationships can cause fatally
  502. // broken Views.
  503. $base_tables = array(
  504. $this->base_table => TRUE,
  505. '#global' => TRUE,
  506. );
  507. // For each relationship we have, make sure we mark the base it provides as
  508. // available.
  509. foreach ($this->display_handler->get_option('relationships') as $id => $options) {
  510. $options['table'] = views_move_table($options['table']);
  511. $data = views_fetch_data($options['table'], FALSE);
  512. if (isset($data[$options['field']]['relationship']['base'])) {
  513. $base_tables[$data[$options['field']]['relationship']['base']] = TRUE;
  514. }
  515. }
  516. $base_tables = array_keys($base_tables);
  517. $missing_base_tables = array();
  518. $types = views_object_types();
  519. foreach ($types as $key => $info) {
  520. foreach ($this->display_handler->get_option($info['plural']) as $id => $options) {
  521. $options['table'] = views_move_table($options['table']);
  522. $data = views_fetch_data($options['table'], FALSE);
  523. $valid_bases = array($options['table']);
  524. if (isset($data['table']['join'])) {
  525. $valid_bases = array_merge($valid_bases, array_keys($data['table']['join']));
  526. }
  527. // If the base table is missing, record it so we can try to fix it.
  528. if (!array_intersect($valid_bases, $base_tables)) {
  529. $missing_base_tables[$options['table']][] = array('type' => $key, 'id' => $id);
  530. }
  531. }
  532. }
  533. if (!empty($missing_base_tables)) {
  534. // This will change handlers, so make sure any existing handlers get
  535. // tossed.
  536. $this->display_handler->handlers = array();
  537. $this->relationships_changed = TRUE;
  538. $this->changed = TRUE;
  539. // Try to fix it.
  540. foreach ($missing_base_tables as $table => $handlers) {
  541. $data = views_fetch_data($table);
  542. $relationship = NULL;
  543. // Does the missing base table have a default relationship we can
  544. // throw in?
  545. if (isset($data['table']['default_relationship'][$this->base_table])) {
  546. // Create the relationship.
  547. $info = $data['table']['default_relationship'][$this->base_table];
  548. $relationship_options = isset($info['options']) ? $info['options'] : array();
  549. $relationship = $this->add_item($this->current_display, 'relationship', $info['table'], $info['field'], $relationship_options);
  550. }
  551. foreach ($handlers as $handler) {
  552. $options = $this->display_handler->get_option($types[$handler['type']]['plural']);
  553. if ($relationship) {
  554. $options[$handler['id']]['relationship'] = $relationship;
  555. }
  556. else {
  557. unset($options[$handler['id']]);
  558. }
  559. $this->display_handler->set_option($types[$handler['type']]['plural'], $options);
  560. }
  561. }
  562. }
  563. }
  564. /**
  565. * Acquire and attach all of the handlers.
  566. */
  567. function init_handlers() {
  568. if (empty($this->inited)) {
  569. $this->fix_missing_relationships();
  570. foreach (views_object_types() as $key => $info) {
  571. $this->_init_handler($key, $info);
  572. }
  573. $this->inited = TRUE;
  574. }
  575. }
  576. /**
  577. * Initialize the pager
  578. *
  579. * Like style initialization, pager initialization is held until late
  580. * to allow for overrides.
  581. */
  582. function init_pager() {
  583. if (empty($this->query->pager)) {
  584. $this->query->pager = $this->display_handler->get_plugin('pager');
  585. if ($this->query->pager->use_pager()) {
  586. $this->query->pager->set_current_page($this->current_page);
  587. }
  588. // These overrides may have been set earlier via $view->set_*
  589. // functions.
  590. if (isset($this->items_per_page)) {
  591. $this->query->pager->set_items_per_page($this->items_per_page);
  592. }
  593. if (isset($this->offset)) {
  594. $this->query->pager->set_offset($this->offset);
  595. }
  596. }
  597. }
  598. /**
  599. * Create a list of base tables eligible for this view. Used primarily
  600. * for the UI. Display must be already initialized.
  601. */
  602. function get_base_tables() {
  603. $base_tables = array(
  604. $this->base_table => TRUE,
  605. '#global' => TRUE,
  606. );
  607. foreach ($this->display_handler->get_handlers('relationship') as $handler) {
  608. $base_tables[$handler->definition['base']] = TRUE;
  609. }
  610. return $base_tables;
  611. }
  612. /**
  613. * Run the pre_query() on all active handlers.
  614. */
  615. function _pre_query() {
  616. foreach (views_object_types() as $key => $info) {
  617. $handlers = &$this->$key;
  618. $position = 0;
  619. foreach ($handlers as $id => $handler) {
  620. $handlers[$id]->position = $position;
  621. $handlers[$id]->pre_query();
  622. $position++;
  623. }
  624. }
  625. }
  626. /**
  627. * Run the post_execute() on all active handlers.
  628. */
  629. function _post_execute() {
  630. foreach (views_object_types() as $key => $info) {
  631. $handlers = &$this->$key;
  632. foreach ($handlers as $id => $handler) {
  633. $handlers[$id]->post_execute($this->result);
  634. }
  635. }
  636. }
  637. /**
  638. * Attach all of the handlers for each type.
  639. *
  640. * @param $key
  641. * One of 'argument', 'field', 'sort', 'filter', 'relationship'
  642. * @param $info
  643. * The $info from views_object_types for this object.
  644. */
  645. function _init_handler($key, $info) {
  646. // Load the requested items from the display onto the object.
  647. $this->$key = $this->display_handler->get_handlers($key);
  648. // This reference deals with difficult PHP indirection.
  649. $handlers = &$this->$key;
  650. // Run through and test for accessibility.
  651. foreach ($handlers as $id => $handler) {
  652. if (!$handler->access()) {
  653. unset($handlers[$id]);
  654. }
  655. }
  656. }
  657. /**
  658. * Build all the arguments.
  659. */
  660. function _build_arguments() {
  661. // Initially, we want to build sorts and fields. This can change, though,
  662. // if we get a summary view.
  663. if (empty($this->argument)) {
  664. return TRUE;
  665. }
  666. // build arguments.
  667. $position = -1;
  668. // Create a title for use in the breadcrumb trail.
  669. $title = $this->display_handler->get_option('title');
  670. $this->build_info['breadcrumb'] = array();
  671. $breadcrumb_args = array();
  672. $substitutions = array();
  673. $status = TRUE;
  674. // Iterate through each argument and process.
  675. foreach ($this->argument as $id => $arg) {
  676. $position++;
  677. $argument = &$this->argument[$id];
  678. if ($argument->broken()) {
  679. continue;
  680. }
  681. $argument->set_relationship();
  682. $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
  683. $argument->position = $position;
  684. if (isset($arg) || $argument->has_default_argument()) {
  685. if (!isset($arg)) {
  686. $arg = $argument->get_default_argument();
  687. // make sure default args get put back.
  688. if (isset($arg)) {
  689. $this->args[$position] = $arg;
  690. }
  691. // remember that this argument was computed, not passed on the URL.
  692. $argument->is_default = TRUE;
  693. }
  694. // Set the argument, which will also validate that the argument can be set.
  695. if (!$argument->set_argument($arg)) {
  696. $status = $argument->validate_fail($arg);
  697. break;
  698. }
  699. if ($argument->is_exception()) {
  700. $arg_title = $argument->exception_title();
  701. }
  702. else {
  703. $arg_title = $argument->get_title();
  704. $argument->query($this->display_handler->use_group_by());
  705. }
  706. // Add this argument's substitution
  707. $substitutions['%' . ($position + 1)] = $arg_title;
  708. $substitutions['!' . ($position + 1)] = strip_tags(decode_entities($arg));
  709. // Since we're really generating the breadcrumb for the item above us,
  710. // check the default action of this argument.
  711. if ($this->display_handler->uses_breadcrumb() && $argument->uses_breadcrumb()) {
  712. $path = $this->get_url($breadcrumb_args);
  713. if (strpos($path, '%') === FALSE) {
  714. if (!empty($argument->options['breadcrumb_enable']) && !empty($argument->options['breadcrumb'])) {
  715. $breadcrumb = $argument->options['breadcrumb'];
  716. }
  717. else {
  718. $breadcrumb = $title;
  719. }
  720. $this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
  721. }
  722. }
  723. // Allow the argument to muck with this breadcrumb.
  724. $argument->set_breadcrumb($this->build_info['breadcrumb']);
  725. // Test to see if we should use this argument's title
  726. if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
  727. $title = $argument->options['title'];
  728. }
  729. $breadcrumb_args[] = $arg;
  730. }
  731. else {
  732. // determine default condition and handle.
  733. $status = $argument->default_action();
  734. break;
  735. }
  736. // Be safe with references and loops:
  737. unset($argument);
  738. }
  739. // set the title in the build info.
  740. if (!empty($title)) {
  741. $this->build_info['title'] = $title;
  742. }
  743. // Store the arguments for later use.
  744. $this->build_info['substitutions'] = $substitutions;
  745. return $status;
  746. }
  747. /**
  748. * Do some common building initialization.
  749. */
  750. function init_query() {
  751. if (!empty($this->query)) {
  752. $class = get_class($this->query);
  753. if ($class && $class != 'stdClass') {
  754. // return if query is already initialized.
  755. return TRUE;
  756. }
  757. }
  758. // Create and initialize the query object.
  759. $views_data = views_fetch_data($this->base_table);
  760. $this->base_field = !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : '';
  761. if (!empty($views_data['table']['base']['database'])) {
  762. $this->base_database = $views_data['table']['base']['database'];
  763. }
  764. // Load the options.
  765. $query_options = $this->display_handler->get_option('query');
  766. // Create and initialize the query object.
  767. $plugin = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
  768. $this->query = views_get_plugin('query', $plugin);
  769. if (empty($this->query)) {
  770. return FALSE;
  771. }
  772. $this->query->init($this->base_table, $this->base_field, $query_options['options']);
  773. return TRUE;
  774. }
  775. /**
  776. * Build the query for the view.
  777. */
  778. function build($display_id = NULL) {
  779. if (!empty($this->built)) {
  780. return;
  781. }
  782. if (empty($this->current_display) || $display_id) {
  783. if (!$this->set_display($display_id)) {
  784. return FALSE;
  785. }
  786. }
  787. // Let modules modify the view just prior to building it.
  788. foreach (module_implements('views_pre_build') as $module) {
  789. $function = $module . '_views_pre_build';
  790. $function($this);
  791. }
  792. // Attempt to load from cache.
  793. // @todo Load a build_info from cache.
  794. $start = microtime(TRUE);
  795. // If that fails, let's build!
  796. $this->build_info = array(
  797. 'query' => '',
  798. 'count_query' => '',
  799. 'query_args' => array(),
  800. );
  801. $this->init_query();
  802. // Call a module hook and see if it wants to present us with a
  803. // pre-built query or instruct us not to build the query for
  804. // some reason.
  805. // @todo: Implement this. Use the same mechanism Panels uses.
  806. // Run through our handlers and ensure they have necessary information.
  807. $this->init_handlers();
  808. // Let the handlers interact with each other if they really want.
  809. $this->_pre_query();
  810. if ($this->display_handler->uses_exposed()) {
  811. $exposed_form = $this->display_handler->get_plugin('exposed_form');
  812. $this->exposed_widgets = $exposed_form->render_exposed_form();
  813. if (form_set_error() || !empty($this->build_info['abort'])) {
  814. $this->built = TRUE;
  815. // Don't execute the query, but rendering will still be executed to display the empty text.
  816. $this->executed = TRUE;
  817. return empty($this->build_info['fail']);
  818. }
  819. }
  820. // Build all the relationships first thing.
  821. $this->_build('relationship');
  822. // Set the filtering groups.
  823. if (!empty($this->filter)) {
  824. $filter_groups = $this->display_handler->get_option('filter_groups');
  825. if ($filter_groups) {
  826. $this->query->set_group_operator($filter_groups['operator']);
  827. foreach($filter_groups['groups'] as $id => $operator) {
  828. $this->query->set_where_group($operator, $id);
  829. }
  830. }
  831. }
  832. // Build all the filters.
  833. $this->_build('filter');
  834. $this->build_sort = TRUE;
  835. // Arguments can, in fact, cause this whole thing to abort.
  836. if (!$this->_build_arguments()) {
  837. $this->build_time = microtime(TRUE) - $start;
  838. $this->attach_displays();
  839. return $this->built;
  840. }
  841. // Initialize the style; arguments may have changed which style we use,
  842. // so waiting as long as possible is important. But we need to know
  843. // about the style when we go to build fields.
  844. if (!$this->init_style()) {
  845. $this->build_info['fail'] = TRUE;
  846. return FALSE;
  847. }
  848. if ($this->style_plugin->uses_fields()) {
  849. $this->_build('field');
  850. }
  851. // Build our sort criteria if we were instructed to do so.
  852. if (!empty($this->build_sort)) {
  853. // Allow the style handler to deal with sorting.
  854. if ($this->style_plugin->build_sort()) {
  855. $this->_build('sort');
  856. }
  857. // allow the plugin to build second sorts as well.
  858. $this->style_plugin->build_sort_post();
  859. }
  860. // Allow area handlers to affect the query.
  861. $this->_build('header');
  862. $this->_build('footer');
  863. $this->_build('empty');
  864. // Allow display handler to affect the query:
  865. $this->display_handler->query($this->display_handler->use_group_by());
  866. // Allow style handler to affect the query:
  867. $this->style_plugin->query($this->display_handler->use_group_by());
  868. // Allow exposed form to affect the query:
  869. if (isset($exposed_form)) {
  870. $exposed_form->query();
  871. }
  872. if (variable_get('views_sql_signature', FALSE)) {
  873. $this->query->add_signature($this);
  874. }
  875. // Let modules modify the query just prior to finalizing it.
  876. $this->query->alter($this);
  877. // Only build the query if we weren't interrupted.
  878. if (empty($this->built)) {
  879. // Build the necessary info to execute the query.
  880. $this->query->build($this);
  881. }
  882. $this->built = TRUE;
  883. $this->build_time = microtime(TRUE) - $start;
  884. // Attach displays
  885. $this->attach_displays();
  886. // Let modules modify the view just after building it.
  887. foreach (module_implements('views_post_build') as $module) {
  888. $function = $module . '_views_post_build';
  889. $function($this);
  890. }
  891. return TRUE;
  892. }
  893. /**
  894. * Internal method to build an individual set of handlers.
  895. *
  896. * @param string $key
  897. * The type of handlers (filter etc.) which should be iterated over to
  898. * build the relationship and query information.
  899. */
  900. function _build($key) {
  901. $handlers = &$this->$key;
  902. foreach ($handlers as $id => $data) {
  903. if (!empty($handlers[$id]) && is_object($handlers[$id])) {
  904. $multiple_exposed_input = array(0 => NULL);
  905. if ($handlers[$id]->multiple_exposed_input()) {
  906. $multiple_exposed_input = $handlers[$id]->group_multiple_exposed_input($this->exposed_data);
  907. }
  908. foreach ($multiple_exposed_input as $group_id) {
  909. // Give this handler access to the exposed filter input.
  910. if (!empty($this->exposed_data)) {
  911. $converted = FALSE;
  912. if ($handlers[$id]->is_a_group()) {
  913. $converted = $handlers[$id]->convert_exposed_input($this->exposed_data, $group_id);
  914. $handlers[$id]->store_group_input($this->exposed_data, $converted);
  915. if (!$converted) {
  916. continue;
  917. }
  918. }
  919. $rc = $handlers[$id]->accept_exposed_input($this->exposed_data);
  920. $handlers[$id]->store_exposed_input($this->exposed_data, $rc);
  921. if (!$rc) {
  922. continue;
  923. }
  924. }
  925. $handlers[$id]->set_relationship();
  926. $handlers[$id]->query($this->display_handler->use_group_by());
  927. }
  928. }
  929. }
  930. }
  931. /**
  932. * Execute the view's query.
  933. *
  934. * @param string $display_id
  935. * The machine name of the display, which should be executed.
  936. *
  937. * @return bool
  938. * Return whether the executing was successful, for example an argument
  939. * could stop the process.
  940. */
  941. function execute($display_id = NULL) {
  942. if (empty($this->built)) {
  943. if (!$this->build($display_id)) {
  944. return FALSE;
  945. }
  946. }
  947. if (!empty($this->executed)) {
  948. return TRUE;
  949. }
  950. // Don't allow to use deactivated displays, but display them on the live preview.
  951. if (!$this->display[$this->current_display]->handler->get_option('enabled') && empty($this->live_preview)) {
  952. $this->build_info['fail'] = TRUE;
  953. return FALSE;
  954. }
  955. // Let modules modify the view just prior to executing it.
  956. foreach (module_implements('views_pre_execute') as $module) {
  957. $function = $module . '_views_pre_execute';
  958. $function($this);
  959. }
  960. // Check for already-cached results.
  961. if (!empty($this->live_preview)) {
  962. $cache = FALSE;
  963. }
  964. else {
  965. $cache = $this->display_handler->get_plugin('cache');
  966. }
  967. if ($cache && $cache->cache_get('results')) {
  968. if($this->query->pager->use_pager()) {
  969. $this->query->pager->total_items = $this->total_rows;
  970. $this->query->pager->update_page_info();
  971. }
  972. vpr('Used cached results');
  973. }
  974. else {
  975. $this->query->execute($this);
  976. // Enforce the array key rule as documented in
  977. // views_plugin_query::execute().
  978. $this->result = array_values($this->result);
  979. $this->_post_execute();
  980. if ($cache) {
  981. $cache->cache_set('results');
  982. }
  983. }
  984. // Let modules modify the view just after executing it.
  985. foreach (module_implements('views_post_execute') as $module) {
  986. $function = $module . '_views_post_execute';
  987. $function($this);
  988. }
  989. $this->executed = TRUE;
  990. }
  991. /**
  992. * Render this view for a certain display.
  993. *
  994. * Note: You should better use just the preview function if you want to
  995. * render a view.
  996. *
  997. * @param string $display_id
  998. * The machine name of the display, which should be rendered.
  999. *
  1000. * @return (string|NULL)
  1001. * Return the output of the rendered view or NULL if something failed in the process.
  1002. */
  1003. function render($display_id = NULL) {
  1004. $this->execute($display_id);
  1005. // Check to see if the build failed.
  1006. if (!empty($this->build_info['fail'])) {
  1007. return;
  1008. }
  1009. if (!empty($this->view->build_info['denied'])) {
  1010. return;
  1011. }
  1012. drupal_theme_initialize();
  1013. $start = microtime(TRUE);
  1014. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1015. $this->start_query_capture();
  1016. }
  1017. $exposed_form = $this->display_handler->get_plugin('exposed_form');
  1018. $exposed_form->pre_render($this->result);
  1019. // Check for already-cached output.
  1020. if (!empty($this->live_preview)) {
  1021. $cache = FALSE;
  1022. }
  1023. else {
  1024. $cache = $this->display_handler->get_plugin('cache');
  1025. }
  1026. if ($cache && $cache->cache_get('output')) {
  1027. }
  1028. else {
  1029. if ($cache) {
  1030. $cache->cache_start();
  1031. }
  1032. // Run pre_render for the pager as it might change the result.
  1033. if (!empty($this->query->pager)) {
  1034. $this->query->pager->pre_render($this->result);
  1035. }
  1036. // Initialize the style plugin.
  1037. $this->init_style();
  1038. // Give field handlers the opportunity to perform additional queries
  1039. // using the entire resultset prior to rendering.
  1040. if ($this->style_plugin->uses_fields()) {
  1041. foreach ($this->field as $id => $handler) {
  1042. if (!empty($this->field[$id])) {
  1043. $this->field[$id]->pre_render($this->result);
  1044. }
  1045. }
  1046. }
  1047. $this->style_plugin->pre_render($this->result);
  1048. // Let modules modify the view just prior to rendering it.
  1049. foreach (module_implements('views_pre_render') as $module) {
  1050. $function = $module . '_views_pre_render';
  1051. $function($this);
  1052. }
  1053. // Let the themes play too, because pre render is a very themey thing.
  1054. foreach ($GLOBALS['base_theme_info'] as $base) {
  1055. $function = $base->name . '_views_pre_render';
  1056. if (function_exists($function)) {
  1057. $function($this);
  1058. }
  1059. }
  1060. $function = $GLOBALS['theme'] . '_views_pre_render';
  1061. if (function_exists($function)) {
  1062. $function($this);
  1063. }
  1064. $this->display_handler->output = $this->display_handler->render();
  1065. if ($cache) {
  1066. $cache->cache_set('output');
  1067. }
  1068. }
  1069. $exposed_form->post_render($this->display_handler->output);
  1070. if ($cache) {
  1071. $cache->post_render($this->display_handler->output);
  1072. }
  1073. // Let modules modify the view output after it is rendered.
  1074. foreach (module_implements('views_post_render') as $module) {
  1075. $function = $module . '_views_post_render';
  1076. $function($this, $this->display_handler->output, $cache);
  1077. }
  1078. // Let the themes play too, because post render is a very themey thing.
  1079. foreach ($GLOBALS['base_theme_info'] as $base) {
  1080. $function = $base->name . '_views_post_render';
  1081. if (function_exists($function)) {
  1082. $function($this);
  1083. }
  1084. }
  1085. $function = $GLOBALS['theme'] . '_views_post_render';
  1086. if (function_exists($function)) {
  1087. $function($this, $this->display_handler->output, $cache);
  1088. }
  1089. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1090. $this->end_query_capture();
  1091. }
  1092. $this->render_time = microtime(TRUE) - $start;
  1093. return $this->display_handler->output;
  1094. }
  1095. /**
  1096. * Render a specific field via the field ID and the row #
  1097. *
  1098. * Note: You might want to use views_plugin_style::render_fields as it
  1099. * caches the output for you.
  1100. *
  1101. * @param string $field
  1102. * The id of the field to be rendered.
  1103. *
  1104. * @param int $row
  1105. * The row number in the $view->result which is used for the rendering.
  1106. *
  1107. * @return string
  1108. * The rendered output of the field.
  1109. */
  1110. function render_field($field, $row) {
  1111. if (isset($this->field[$field]) && isset($this->result[$row])) {
  1112. return $this->field[$field]->advanced_render($this->result[$row]);
  1113. }
  1114. }
  1115. /**
  1116. * Execute the given display, with the given arguments.
  1117. * To be called externally by whatever mechanism invokes the view,
  1118. * such as a page callback, hook_block, etc.
  1119. *
  1120. * This function should NOT be used by anything external as this
  1121. * returns data in the format specified by the display. It can also
  1122. * have other side effects that are only intended for the 'proper'
  1123. * use of the display, such as setting page titles and breadcrumbs.
  1124. *
  1125. * If you simply want to view the display, use view::preview() instead.
  1126. */
  1127. function execute_display($display_id = NULL, $args = array()) {
  1128. if (empty($this->current_display) || $this->current_display != $this->choose_display($display_id)) {
  1129. if (!$this->set_display($display_id)) {
  1130. return FALSE;
  1131. }
  1132. }
  1133. $this->pre_execute($args);
  1134. // Execute the view
  1135. $output = $this->display_handler->execute();
  1136. $this->post_execute();
  1137. return $output;
  1138. }
  1139. /**
  1140. * Preview the given display, with the given arguments.
  1141. *
  1142. * To be called externally, probably by an AJAX handler of some flavor.
  1143. * Can also be called when views are embedded, as this guarantees
  1144. * normalized output.
  1145. */
  1146. function preview($display_id = NULL, $args = array()) {
  1147. if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) {
  1148. if (!$this->set_display($display_id)) {
  1149. return FALSE;
  1150. }
  1151. }
  1152. $this->preview = TRUE;
  1153. $this->pre_execute($args);
  1154. // Preview the view.
  1155. $output = $this->display_handler->preview();
  1156. $this->post_execute();
  1157. return $output;
  1158. }
  1159. /**
  1160. * Run attachments and let the display do what it needs to do prior
  1161. * to running.
  1162. */
  1163. function pre_execute($args = array()) {
  1164. $this->old_view[] = views_get_current_view();
  1165. views_set_current_view($this);
  1166. $display_id = $this->current_display;
  1167. // Prepare the view with the information we have, but only if we were
  1168. // passed arguments, as they may have been set previously.
  1169. if ($args) {
  1170. $this->set_arguments($args);
  1171. }
  1172. // Let modules modify the view just prior to executing it.
  1173. foreach (module_implements('views_pre_view') as $module) {
  1174. $function = $module . '_views_pre_view';
  1175. $function($this, $display_id, $this->args);
  1176. }
  1177. // Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
  1178. $this->dom_id = !empty($this->dom_id) ? $this->dom_id : md5($this->name . REQUEST_TIME . rand());
  1179. // Allow the display handler to set up for execution
  1180. $this->display_handler->pre_execute();
  1181. }
  1182. /**
  1183. * Unset the current view, mostly.
  1184. */
  1185. function post_execute() {
  1186. // unset current view so we can be properly destructed later on.
  1187. // Return the previous value in case we're an attachment.
  1188. if ($this->old_view) {
  1189. $old_view = array_pop($this->old_view);
  1190. }
  1191. views_set_current_view(isset($old_view) ? $old_view : FALSE);
  1192. }
  1193. /**
  1194. * Run attachment displays for the view.
  1195. */
  1196. function attach_displays() {
  1197. if (!empty($this->is_attachment)) {
  1198. return;
  1199. }
  1200. if (!$this->display_handler->accept_attachments()) {
  1201. return;
  1202. }
  1203. $this->is_attachment = TRUE;
  1204. // Give other displays an opportunity to attach to the view.
  1205. foreach ($this->display as $id => $display) {
  1206. if (!empty($this->display[$id]->handler)) {
  1207. $this->display[$id]->handler->attach_to($this->current_display);
  1208. }
  1209. }
  1210. $this->is_attachment = FALSE;
  1211. }
  1212. /**
  1213. * Called to get hook_menu() information from the view and the named display handler.
  1214. *
  1215. * @param $display_id
  1216. * A display id.
  1217. * @param $callbacks
  1218. * A menu callback array passed from views_menu_alter().
  1219. */
  1220. function execute_hook_menu($display_id = NULL, &$callbacks = array()) {
  1221. // Prepare the view with the information we have.
  1222. // This was probably already called, but it's good to be safe.
  1223. if (!$this->set_display($display_id)) {
  1224. return FALSE;
  1225. }
  1226. // Execute the view
  1227. if (isset($this->display_handler)) {
  1228. return $this->display_handler->execute_hook_menu($callbacks);
  1229. }
  1230. }
  1231. /**
  1232. * Called to get hook_block information from the view and the
  1233. * named display handler.
  1234. */
  1235. function execute_hook_block_list($display_id = NULL) {
  1236. // Prepare the view with the information we have.
  1237. // This was probably already called, but it's good to be safe.
  1238. if (!$this->set_display($display_id)) {
  1239. return FALSE;
  1240. }
  1241. // Execute the view
  1242. if (isset($this->display_handler)) {
  1243. return $this->display_handler->execute_hook_block_list();
  1244. }
  1245. }
  1246. /**
  1247. * Determine if the given user has access to the view. Note that
  1248. * this sets the display handler if it hasn't been.
  1249. */
  1250. function access($displays = NULL, $account = NULL) {
  1251. // Noone should have access to disabled views.
  1252. if (!empty($this->disabled)) {
  1253. return FALSE;
  1254. }
  1255. if (!isset($this->current_display)) {
  1256. $this->init_display();
  1257. }
  1258. if (!$account) {
  1259. $account = $GLOBALS['user'];
  1260. }
  1261. // We can't use choose_display() here because that function
  1262. // calls this one.
  1263. $displays = (array)$displays;
  1264. foreach ($displays as $display_id) {
  1265. if (!empty($this->display[$display_id]->handler)) {
  1266. if ($this->display[$display_id]->handler->access($account)) {
  1267. return TRUE;
  1268. }
  1269. }
  1270. }
  1271. return FALSE;
  1272. }
  1273. /**
  1274. * Get the view's current title. This can change depending upon how it
  1275. * was built.
  1276. */
  1277. function get_title() {
  1278. if (empty($this->display_handler)) {
  1279. if (!$this->set_display('default')) {
  1280. return FALSE;
  1281. }
  1282. }
  1283. // During building, we might find a title override. If so, use it.
  1284. if (!empty($this->build_info['title'])) {
  1285. $title = $this->build_info['title'];
  1286. }
  1287. else {
  1288. $title = $this->display_handler->get_option('title');
  1289. }
  1290. // Allow substitutions from the first row.
  1291. if ($this->init_style()) {
  1292. $title = $this->style_plugin->tokenize_value($title, 0);
  1293. }
  1294. return $title;
  1295. }
  1296. /**
  1297. * Override the view's current title.
  1298. *
  1299. * The tokens in the title get's replaced before rendering.
  1300. */
  1301. function set_title($title) {
  1302. $this->build_info['title'] = $title;
  1303. return TRUE;
  1304. }
  1305. /**
  1306. * Return the human readable name for a view.
  1307. *
  1308. * When a certain view doesn't have a human readable name return the machine readable name.
  1309. */
  1310. function get_human_name() {
  1311. if (!empty($this->human_name)) {
  1312. $human_name = $this->human_name;
  1313. }
  1314. else {
  1315. $human_name = $this->name;
  1316. }
  1317. return $human_name;
  1318. }
  1319. /**
  1320. * Force the view to build a title.
  1321. */
  1322. function build_title() {
  1323. $this->init_display();
  1324. if (empty($this->built)) {
  1325. $this->init_query();
  1326. }
  1327. $this->init_handlers();
  1328. $this->_build_arguments();
  1329. }
  1330. /**
  1331. * Get the URL for the current view.
  1332. *
  1333. * This URL will be adjusted for arguments.
  1334. */
  1335. function get_url($args = NULL, $path = NULL) {
  1336. if (!empty($this->override_url)) {
  1337. return $this->override_url;
  1338. }
  1339. if (!isset($path)) {
  1340. $path = $this->get_path();
  1341. }
  1342. if (!isset($args)) {
  1343. $args = $this->args;
  1344. // Exclude arguments that were computed, not passed on the URL.
  1345. $position = 0;
  1346. if (!empty($this->argument)) {
  1347. foreach ($this->argument as $argument_id => $argument) {
  1348. if (!empty($argument->is_default) && !empty($argument->options['default_argument_skip_url'])) {
  1349. unset($args[$position]);
  1350. }
  1351. $position++;
  1352. }
  1353. }
  1354. }
  1355. // Don't bother working if there's nothing to do:
  1356. if (empty($path) || (empty($args) && strpos($path, '%') === FALSE)) {
  1357. return $path;
  1358. }
  1359. $pieces = array();
  1360. $argument_keys = isset($this->argument) ? array_keys($this->argument) : array();
  1361. $id = current($argument_keys);
  1362. foreach (explode('/', $path) as $piece) {
  1363. if ($piece != '%') {
  1364. $pieces[] = $piece;
  1365. }
  1366. else {
  1367. if (empty($args)) {
  1368. // Try to never put % in a url; use the wildcard instead.
  1369. if ($id && !empty($this->argument[$id]->options['exception']['value'])) {
  1370. $pieces[] = $this->argument[$id]->options['exception']['value'];
  1371. }
  1372. else {
  1373. $pieces[] = '*'; // gotta put something if there just isn't one.
  1374. }
  1375. }
  1376. else {
  1377. $pieces[] = array_shift($args);
  1378. }
  1379. if ($id) {
  1380. $id = next($argument_keys);
  1381. }
  1382. }
  1383. }
  1384. if (!empty($args)) {
  1385. $pieces = array_merge($pieces, $args);
  1386. }
  1387. return implode('/', $pieces);
  1388. }
  1389. /**
  1390. * Get the base path used for this view.
  1391. */
  1392. function get_path() {
  1393. if (!empty($this->override_path)) {
  1394. return $this->override_path;
  1395. }
  1396. if (empty($this->display_handler)) {
  1397. if (!$this->set_display('default')) {
  1398. return FALSE;
  1399. }
  1400. }
  1401. return $this->display_handler->get_path();
  1402. }
  1403. /**
  1404. * Get the breadcrumb used for this view.
  1405. *
  1406. * @param $set
  1407. * If true, use drupal_set_breadcrumb() to install the breadcrumb.
  1408. */
  1409. function get_breadcrumb($set = FALSE) {
  1410. // Now that we've built the view, extract the breadcrumb.
  1411. $base = TRUE;
  1412. $breadcrumb = array();
  1413. if (!empty($this->build_info['breadcrumb'])) {
  1414. foreach ($this->build_info['breadcrumb'] as $path => $title) {
  1415. // Check to see if the frontpage is in the breadcrumb trail; if it
  1416. // is, we'll remove that from the actual breadcrumb later.
  1417. if ($path == variable_get('site_frontpage', 'node')) {
  1418. $base = FALSE;
  1419. $title = t('Home');
  1420. }
  1421. if ($title) {
  1422. $breadcrumb[] = l($title, $path, array('html' => TRUE));
  1423. }
  1424. }
  1425. if ($set) {
  1426. if ($base) {
  1427. $breadcrumb = array_merge(drupal_get_breadcrumb(), $breadcrumb);
  1428. }
  1429. drupal_set_breadcrumb($breadcrumb);
  1430. }
  1431. }
  1432. return $breadcrumb;
  1433. }
  1434. /**
  1435. * Is this view cacheable?
  1436. */
  1437. function is_cacheable() {
  1438. return $this->is_cacheable;
  1439. }
  1440. /**
  1441. * Set up query capturing.
  1442. *
  1443. * db_query() stores the queries that it runs in global $queries,
  1444. * bit only if dev_query is set to true. In this case, we want
  1445. * to temporarily override that setting if it's not and we
  1446. * can do that without forcing a db rewrite by just manipulating
  1447. * $conf. This is kind of evil but it works.
  1448. */
  1449. function start_query_capture() {
  1450. global $conf, $queries;
  1451. if (empty($conf['dev_query'])) {
  1452. $this->fix_dev_query = TRUE;
  1453. $conf['dev_query'] = TRUE;
  1454. }
  1455. // Record the last query key used; anything already run isn't
  1456. // a query that we are interested in.
  1457. $this->last_query_key = NULL;
  1458. if (!empty($queries)) {
  1459. $keys = array_keys($queries);
  1460. $this->last_query_key = array_pop($keys);
  1461. }
  1462. }
  1463. /**
  1464. * Add the list of queries run during render to buildinfo.
  1465. *
  1466. * @see view::start_query_capture()
  1467. */
  1468. function end_query_capture() {
  1469. global $conf, $queries;
  1470. if (!empty($this->fix_dev_query)) {
  1471. $conf['dev_query'] = FALSE;
  1472. }
  1473. // make a copy of the array so we can manipulate it with array_splice.
  1474. $temp = $queries;
  1475. // Scroll through the queries until we get to our last query key.
  1476. // Unset anything in our temp array.
  1477. if (isset($this->last_query_key)) {
  1478. while (list($id, $query) = each($queries)) {
  1479. if ($id == $this->last_query_key) {
  1480. break;
  1481. }
  1482. unset($temp[$id]);
  1483. }
  1484. }
  1485. $this->additional_queries = $temp;
  1486. }
  1487. /**
  1488. * Static factory method to load a list of views based upon a $where clause.
  1489. *
  1490. * Although this method could be implemented to simply iterate over views::load(),
  1491. * that would be very slow. Buiding the views externally from unified queries is
  1492. * much faster.
  1493. */
  1494. static function load_views() {
  1495. $result = db_query("SELECT DISTINCT v.* FROM {views_view} v");
  1496. $views = array();
  1497. // Load all the views.
  1498. foreach ($result as $data) {
  1499. $vi

Large files files are truncated, but you can click here to view the full file