/[drupal]/contributions/modules/views/includes/view.inc
ViewVC logotype

Contents of /contributions/modules/views/includes/view.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.167 - (show annotations) (download) (as text)
Tue Sep 15 17:57:11 2009 UTC (2 months, 1 week ago) by merlinofchaos
Branch: MAIN
CVS Tags: DRUPAL-6--2-7, HEAD
Branch point for: DRUPAL-6--2, DRUPAL-7--3
Changes since 1.166: +2 -1 lines
File MIME type: text/x-php
#540122 by stella: Attachment displays should attach even if arguments caused the primary display to abort.
1 <?php
2 // $Id: view.inc,v 1.166 2009/09/15 16:59:57 merlinofchaos Exp $
3 /**
4 * @file view.inc
5 * Provides the view object type and associated methods.
6 */
7
8 /**
9 * @defgroup views_objects Objects that represent a View or part of a view.
10 * @{
11 * These objects are the core of Views do the bulk of the direction and
12 * storing of data. All database activity is in these objects.
13 */
14
15 /**
16 * An object to contain all of the data to generate a view, plus the member
17 * functions to build the view query, execute the query and render the output.
18 */
19 class view extends views_db_object {
20 var $db_table = 'views_view';
21 var $base_table = 'node';
22
23 // State variables
24 var $built = FALSE;
25 var $executed = FALSE;
26
27 var $args = array();
28 var $build_info = array();
29
30 var $use_ajax = FALSE;
31
32 // Where the results of a query will go.
33 var $result = array();
34
35 // pager variables
36 var $pager = array(
37 'use_pager' => FALSE,
38 'items_per_page' => 10,
39 'element' => 0,
40 'offset' => 0,
41 'current_page' => 0,
42 );
43
44 // Places to put attached renderings:
45 var $attachment_before = '';
46 var $attachment_after = '';
47
48 // Exposed widget input
49 var $exposed_data = array();
50 var $exposed_input = array();
51
52 // Used to store views that were previously running if we recurse.
53 var $old_view = array();
54 /**
55 * Constructor
56 */
57 function view() {
58 parent::init();
59 // Make sure all of our sub objects are arrays.
60 foreach ($this->db_objects() as $object) {
61 $this->$object = array();
62 }
63
64 $this->query = new stdClass();
65 }
66
67 /**
68 * Returns a list of the sub-object types used by this view. These types are
69 * stored on the display, and are used in the build process.
70 */
71 function display_objects() {
72 return array('argument', 'field', 'sort', 'filter', 'relationship');
73 }
74
75 /**
76 * Returns the complete list of dependent objects in a view, for the purpose
77 * of initialization and loading/saving to/from the database.
78 *
79 * Note: In PHP5 this should be static, but PHP4 doesn't support static
80 * methods.
81 */
82 function db_objects() {
83 return array('display');
84 }
85
86 /**
87 * Set the arguments that come to this view. Usually from the URL
88 * but possibly from elsewhere.
89 */
90 function set_arguments($args) {
91 $this->args = $args;
92 }
93
94 /**
95 * Set the page size for ranged or pager queries
96 */
97 function set_items_per_page($items_per_page) {
98 $this->pager['items_per_page'] = $items_per_page;
99 if (empty($items_per_page)) {
100 $this->pager['use_pager'] = FALSE;
101 }
102 }
103
104 /**
105 * Change/Set the current page for the pager.
106 */
107 function set_current_page($page) {
108 $this->pager['current_page'] = $page;
109 }
110
111 /**
112 * Whether or not the pager should be used.
113 */
114 function set_use_pager($use_pager) {
115 $this->pager['use_pager'] = $use_pager;
116 }
117
118 /**
119 * The pager element id to use if use_apger is on
120 */
121 function set_pager_element($pager_element) {
122 $this->pager['element'] = $pager_element;
123 }
124
125 /**
126 * How many records to skip. This does not function if use_pager is
127 * set.
128 */
129 function set_offset($offset) {
130 $this->pager['offset'] = $offset;
131 }
132
133 /**
134 * Whether or not AJAX should be used. If AJAX is used, paging,
135 * tablesorting and exposed filters will be fetched via an AJAX call
136 * rather than a page refresh.
137 */
138 function set_use_ajax($use_ajax) {
139 $this->use_ajax = $use_ajax;
140 }
141
142 /**
143 * Set the exposed filters input to an array. If unset they will be taken
144 * from $_GET when the time comes.
145 */
146 function set_exposed_input($filters) {
147 $this->exposed_input = $filters;
148 }
149
150 /**
151 * Figure out what the exposed input for this view is.
152 */
153 function get_exposed_input() {
154 // Fill our input either from $_GET or from something previously set on the
155 // view.
156 if (empty($this->exposed_input)) {
157 $this->exposed_input = $_GET;
158 // unset items that are definitely not our input:
159 foreach (array('page', 'q') as $key) {
160 if (isset($this->exposed_input[$key])) {
161 unset($this->exposed_input[$key]);
162 }
163 }
164
165 // If we have no input at all, check for remembered input via session.
166
167 // If filters are not overridden, store the 'remember' settings on the
168 // default display. If they are, store them on this display. This way,
169 // multiple displays in the same view can share the same filters and
170 // remember settings.
171 $display_id = ($this->display_handler->is_defaulted('filters')) ? 'default' : $this->current_display;
172
173 if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->name][$display_id])) {
174 $this->exposed_input = $_SESSION['views'][$this->name][$display_id];
175 }
176 }
177
178 return $this->exposed_input;
179 }
180
181 /**
182 * Set the display for this view and initialize the display handler.
183 */
184 function init_display($reset = FALSE) {
185 // The default display is always the first one in the list.
186 if (isset($this->current_display)) {
187 return TRUE;
188 }
189
190 // Instantiate all displays
191 foreach (array_keys($this->display) as $id) {
192 // Correct for shallow cloning
193 // Often we'll have a cloned view so we don't mess up each other's
194 // displays, but the clone is pretty shallow and doesn't necessarily
195 // clone the displays. We can tell this by looking to see if a handler
196 // has already been set; if it has, but $this->current_display is not
197 // set, then something is dreadfully wrong.
198 if (!empty($this->display[$id]->handler)) {
199 $this->display[$id] = drupal_clone($this->display[$id]);
200 unset($this->display[$id]->handler);
201 }
202 $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
203 if (!empty($this->display[$id]->handler)) {
204 // Initialize the new display handler with data.
205 $this->display[$id]->handler->init($this, $this->display[$id]);
206 // If this is NOT the default display handler, let it know which is
207 // since it may well utilize some data from the default.
208 // This assumes that the 'default' handler is always first. It always
209 // is. Make sure of it.
210 if ($id != 'default') {
211 $this->display[$id]->handler->default_display = &$this->display['default']->handler;
212 }
213 }
214 }
215
216 $this->current_display = 'default';
217 $this->display_handler = &$this->display['default']->handler;
218
219 return TRUE;
220 }
221
222 /**
223 * Get the first display that is accessible to the user.
224 *
225 * @param $displays
226 * Either a single display id or an array of display ids.
227 */
228 function choose_display($displays) {
229 if (!is_array($displays)) {
230 return $displays;
231 }
232
233 $this->init_display();
234
235 foreach ($displays as $display_id) {
236 if ($this->display[$display_id]->handler->access()) {
237 return $display_id;
238 }
239 }
240
241 return 'default';
242 }
243
244 /**
245 * Set the display as current.
246 *
247 * @param $display_id
248 * The id of the display to mark as current.
249 */
250 function set_display($display_id = NULL) {
251 // If we have not already initialized the display, do so. But be careful.
252 if (empty($this->current_display)) {
253 $this->init_display();
254
255 // If handlers were not initialized, and no argument was sent, set up
256 // to the default display.
257 if (empty($display_id)) {
258 $display_id = 'default';
259 }
260 }
261
262 $display_id = $this->choose_display($display_id);
263
264 // If no display id sent in and one wasn't chosen above, we're finished.
265 if (empty($display_id)) {
266 return TRUE;
267 }
268
269 // Ensure the requested display exists.
270 if (empty($this->display[$display_id])) {
271 $display_id = 'default';
272 if (empty($this->display[$display_id])) {
273 vpr(t('set_display() called with invalid display id @display.', array('@display' => $display_id)));
274 return FALSE;
275 }
276 }
277
278 // Set the current display.
279 $this->current_display = $display_id;
280
281 // Ensure requested display has a working handler.
282 if (empty($this->display[$display_id]->handler)) {
283 return FALSE;
284 }
285
286 // Set a shortcut
287 $this->display_handler = &$this->display[$display_id]->handler;
288
289 return TRUE;
290 }
291
292 /**
293 * Find and initialize the style plugin.
294 *
295 * Note that arguments may have changed which style plugin we use, so
296 * check the view object first, then ask the display handler.
297 */
298 function init_style() {
299 if (isset($this->style_plugin)) {
300 return is_object($this->style_plugin);
301 }
302
303 if (!isset($this->plugin_name)) {
304 $this->plugin_name = $this->display_handler->get_option('style_plugin');
305 $this->style_options = $this->display_handler->get_option('style_options');
306 }
307
308 $this->style_plugin = views_get_plugin('style', $this->plugin_name);
309
310 if (empty($this->style_plugin)) {
311 return FALSE;
312 }
313
314 // init the new display handler with data.
315 $this->style_plugin->init($this, $this->display[$this->current_display], $this->style_options);
316 return TRUE;
317 }
318
319 /**
320 * Acquire and attach all of the handlers.
321 */
322 function init_handlers() {
323 if (empty($this->inited)) {
324 foreach (views_object_types() as $key => $info) {
325 $this->_init_handler($key, $info);
326 }
327 $this->inited = TRUE;
328 }
329 }
330
331 /**
332 * Create a list of base tables eligible for this view. Used primarily
333 * for the UI. Display must be already initialized.
334 */
335 function get_base_tables() {
336 $base_tables = array(
337 $this->base_table => TRUE,
338 '#global' => TRUE,
339 );
340
341 foreach ($this->display_handler->get_handlers('relationship') as $handler) {
342 $base_tables[$handler->definition['base']] = TRUE;
343 }
344 return $base_tables;
345 }
346
347 /**
348 * Run the pre_query() on all active handlers.
349 */
350 function _pre_query() {
351 foreach (views_object_types() as $key => $info) {
352 $handlers = &$this->$key;
353 $position = 0;
354 foreach ($handlers as $id => $handler) {
355 $handlers[$id]->position = $position;
356 $handlers[$id]->pre_query();
357 $position++;
358 }
359 }
360 }
361
362 /**
363 * Attach all of the handlers for each type.
364 *
365 * @param $key
366 * One of 'argument', 'field', 'sort', 'filter', 'relationship'
367 * @param $info
368 * The $info from views_object_types for this object.
369 */
370 function _init_handler($key, $info) {
371 // Load the requested items from the display onto the object.
372 $this->$key = $this->display_handler->get_handlers($key);
373
374 // This reference deals with difficult PHP indirection.
375 $handlers = &$this->$key;
376
377 // Run through and test for accessibility.
378 foreach ($handlers as $id => $handler) {
379 if (!$handler->access()) {
380 unset($handlers[$id]);
381 }
382 }
383 }
384
385 /**
386 * Render the exposed filter form.
387 *
388 * This actually does more than that; because it's using FAPI, the form will
389 * also assign data to the appropriate handlers for use in building the
390 * query.
391 */
392 function render_exposed_form($block = FALSE) {
393 // Deal with any exposed filters we may have, before building.
394 $form_state = array(
395 'view' => &$this,
396 'display' => &$this->display_handler->display,
397 'method' => 'get',
398 'rerender' => TRUE,
399 'no_redirect' => TRUE,
400 );
401
402 // Some types of displays (eg. attachments) may wish to use the exposed
403 // filters of their parent displays instead of showing an additional
404 // exposed filter form for the attachment as well as that for the parent.
405 if (!$this->display_handler->displays_exposed() || (!$block && $this->display_handler->get_option('exposed_block'))) {
406 unset($form_state['rerender']);
407 }
408
409 if (!empty($this->ajax)) {
410 $form_state['ajax'] = TRUE;
411 }
412
413 $output = drupal_build_form('views_exposed_form', $form_state);
414 if (!empty($form_state['js settings'])) {
415 $this->js_settings = $form_state['js settings'];
416 }
417
418 return $output;
419 }
420
421 /**
422 * Build all the arguments.
423 */
424 function _build_arguments() {
425 // Initially, we want to build sorts and fields. This can change, though,
426 // if we get a summary view.
427 if (empty($this->argument)) {
428 return TRUE;
429 }
430
431 // build arguments.
432 $position = -1;
433
434 // Create a title for use in the breadcrumb trail.
435 $title = $this->display_handler->get_option('title');
436
437 $this->build_info['breadcrumb'] = array();
438 $breadcrumb_args = array();
439 $substitutions = array();
440
441 $status = TRUE;
442
443 // Iterate through each argument and process.
444 foreach ($this->argument as $id => $arg) {
445 $position++;
446 $argument = &$this->argument[$id];
447
448 if ($argument->broken()) {
449 continue;
450 }
451
452 $argument->set_relationship();
453
454 $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
455 $argument->position = $position;
456
457 if (isset($arg) || $argument->has_default_argument()) {
458 if (!isset($arg)) {
459 $arg = $argument->get_default_argument();
460 // make sure default args get put back.
461 if (isset($arg)) {
462 $this->args[$position] = $arg;
463 }
464 }
465
466 // Set the argument, which will also validate that the argument can be set.
467 if (!$argument->set_argument($arg)) {
468 $status = $argument->validate_fail($arg);
469 break;
470 }
471
472 if ($argument->is_wildcard()) {
473 $arg_title = $argument->wildcard_title();
474 }
475 else {
476 $arg_title = $argument->get_title();
477 $argument->query();
478 }
479
480 // Add this argument's substitution
481 $substitutions['%' . ($position + 1)] = $arg_title;
482
483 // Since we're really generating the breadcrumb for the item above us,
484 // check the default action of this argument.
485 if ($this->display_handler->uses_breadcrumb() && $argument->uses_breadcrumb()) {
486 $path = $this->get_url($breadcrumb_args);
487 if (strpos($path, '%') === FALSE) {
488 $breadcrumb = !empty($argument->options['breadcrumb'])? $argument->options['breadcrumb'] : $title;
489 $this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
490 }
491 }
492
493 // Allow the argument to muck with this breadcrumb.
494 $argument->set_breadcrumb($this->build_info['breadcrumb']);
495
496 // Test to see if we should use this argument's title
497 if (!empty($argument->options['title'])) {
498 $title = $argument->options['title'];
499 }
500
501 $breadcrumb_args[] = $arg;
502 }
503 else {
504 // determine default condition and handle.
505 $status = $argument->default_action();
506 break;
507 }
508
509 // Be safe with references and loops:
510 unset($argument);
511 }
512
513 // set the title in the build info.
514 if (!empty($title)) {
515 $this->build_info['title'] = str_replace(array_keys($substitutions), $substitutions, $title);
516 }
517
518 // Store the arguments for later use.
519 $this->build_info['substitutions'] = $substitutions;
520
521 return $status;
522 }
523
524 /**
525 * Do some common building initialization.
526 */
527 function init_query() {
528 // Create and initialize the query object.
529 $views_data = views_fetch_data($this->base_table);
530 $this->base_field = $views_data['table']['base']['field'];
531 if (!empty($views_data['table']['base']['database'])) {
532 $this->base_database = $views_data['table']['base']['database'];
533 }
534 views_include('query');
535 $this->query = new views_query($this->base_table, $this->base_field);
536 }
537
538 /**
539 * Build the query for the view.
540 */
541 function build($display_id = NULL) {
542 if (!empty($this->built)) {
543 return;
544 }
545
546 if (empty($this->current_display) || $display_id) {
547 if (!$this->set_display($display_id)) {
548 return FALSE;
549 }
550 }
551
552 // Let modules modify the view just prior to executing it.
553 foreach (module_implements('views_pre_build') as $module) {
554 $function = $module . '_views_pre_build';
555 $function($this);
556 }
557
558 // Attempt to load from cache.
559 // @todo Load a build_info from cache.
560
561 $start = views_microtime();
562 // If that fails, let's build!
563 $this->build_info = array(
564 'query' => '',
565 'count_query' => '',
566 'query_args' => array(),
567 );
568
569 $this->init_query();
570
571 // Call a module hook and see if it wants to present us with a
572 // pre-built query or instruct us not to build the query for
573 // some reason.
574 // @todo: Implement this. Use the same mechanism Panels uses.
575
576 // Run through our handlers and ensure they have necessary information.
577 $this->init_handlers();
578
579 // Let the handlers interact with each other if they really want.
580 $this->_pre_query();
581
582 if ($this->display_handler->uses_exposed()) {
583 $this->exposed_widgets = $this->render_exposed_form();
584 if (form_set_error() || !empty($this->build_info['abort'])) {
585 $this->built = TRUE;
586 return empty($this->build_info['fail']);
587 }
588 }
589
590 // Build all the relationships first thing.
591 $this->_build('relationship');
592
593 // Build all the filters.
594 $this->_build('filter');
595
596 $this->build_sort = TRUE;
597
598 // Arguments can, in fact, cause this whole thing to abort.
599 if (!$this->_build_arguments()) {
600 $this->build_time = views_microtime() - $start;
601 $this->attach_displays();
602 return $this->built;
603 }
604
605 // Initialize the style; arguments may have changed which style we use,
606 // so waiting as long as possible is important. But we need to know
607 // about the style when we go to build fields.
608 if (!$this->init_style()) {
609 $this->build_info['fail'] = TRUE;
610 return FALSE;
611 }
612
613 if ($this->style_plugin->uses_fields()) {
614 $this->_build('field');
615 }
616
617 // Build our sort criteria if we were instructed to do so.
618 if (!empty($this->build_sort)) {
619 // Allow the style handler to deal with sorting.
620 if ($this->style_plugin->build_sort()) {
621 $this->_build('sort');
622 }
623 // allow the plugin to build second sorts as well.
624 $this->style_plugin->build_sort_post();
625 }
626
627 // Allow display handler to affect the query:
628 $this->display_handler->query();
629
630 // Allow style handler to affect the query:
631 $this->style_plugin->query();
632
633 if (variable_get('views_sql_signature', FALSE)) {
634 $this->query->add_field(NULL, "'" . $this->name . ':' . $this->current_display . "'", 'view_name');
635 }
636
637 // Let modules modify the query just prior to finalizing it.
638 foreach (module_implements('views_query_alter') as $module) {
639 $function = $module . '_views_query_alter';
640 $function($this, $this->query);
641 }
642
643 $this->build_info['query'] = $this->query->query();
644 $this->build_info['count_query'] = $this->query->query(TRUE);
645 $this->build_info['query_args'] = $this->query->get_where_args();
646 $this->built = TRUE;
647 $this->build_time = views_microtime() - $start;
648
649 // Attach displays
650 $this->attach_displays();
651 return TRUE;
652 }
653
654 /**
655 * Internal method to build an individual set of handlers.
656 */
657 function _build($key) {
658 $handlers = &$this->$key;
659 foreach ($handlers as $id => $data) {
660 if (!empty($handlers[$id]) && is_object($handlers[$id])) {
661 // Give this handler access to the exposed filter input.
662 if (!empty($this->exposed_data)) {
663 $rc = $handlers[$id]->accept_exposed_input($this->exposed_data);
664 $handlers[$id]->store_exposed_input($this->exposed_data, $rc);
665 if (!$rc) {
666 continue;
667 }
668 }
669 $handlers[$id]->set_relationship();
670 $handlers[$id]->query();
671 }
672 }
673 }
674
675 /**
676 * Execute the view's query.
677 */
678 function execute($display_id = NULL) {
679 if (empty($this->built)) {
680 if (!$this->build($display_id)) {
681 return FALSE;
682 }
683 }
684
685 if (!empty($this->executed)) {
686 return TRUE;
687 }
688
689 // Let modules modify the view just prior to executing it.
690 foreach (module_implements('views_pre_execute') as $module) {
691 $function = $module . '_views_pre_execute';
692 $function($this);
693 }
694
695 $query = db_rewrite_sql($this->build_info['query'], $this->base_table, $this->base_field, array('view' => &$this));
696 $count_query = db_rewrite_sql($this->build_info['count_query'], $this->base_table, $this->base_field, array('view' => &$this));
697
698 $args = $this->build_info['query_args'];
699
700 vpr($query);
701
702
703 // Check for already-cached results.
704 if (!empty($this->live_preview)) {
705 $cache = FALSE;
706 }
707 else {
708 $cache = $this->display_handler->get_cache_plugin();
709 }
710 if ($cache && $cache->cache_get('results')) {
711 vpr('Used cached results');
712 }
713 else {
714 $items = array();
715 if ($query) {
716 $replacements = module_invoke_all('views_query_substitutions', $this);
717 $query = str_replace(array_keys($replacements), $replacements, $query);
718 $count_query = 'SELECT COUNT(*) FROM (' . str_replace(array_keys($replacements), $replacements, $count_query) . ') count_alias';
719
720 if (is_array($args)) {
721 foreach ($args as $id => $arg) {
722 $args[$id] = str_replace(array_keys($replacements), $replacements, $arg);
723 }
724 }
725
726 // Allow for a view to query an external database.
727 if (isset($this->base_database)) {
728 db_set_active($this->base_database);
729 $external = TRUE;
730 }
731
732 $start = views_microtime();
733 if (!empty($this->pager['items_per_page'])) {
734 // We no longer use pager_query() here because pager_query() does not
735 // support an offset. This is fine as we don't actually need pager
736 // query; we've already been doing most of what it does, and we
737 // just need to do a little more playing with globals.
738 if (!empty($this->pager['use_pager']) || !empty($this->get_total_rows)) {
739 $this->total_rows = db_result(db_query($count_query, $args)) - $this->pager['offset'];
740 }
741
742 if (!empty($this->pager['use_pager'])) {
743 // dump information about what we already know into the globals
744 global $pager_page_array, $pager_total, $pager_total_items;
745 // total rows in query
746 $pager_total_items[$this->pager['element']] = $this->total_rows;
747 // total pages
748 $pager_total[$this->pager['element']] = ceil($pager_total_items[$this->pager['element']] / $this->pager['items_per_page']);
749
750 // What page was requested:
751 $pager_page_array = isset($_GET['page']) ? explode(',', $_GET['page']) : array();
752
753 // If the requested page was within range. $this->pager['current_page']
754 // defaults to 0 so we don't need to set it in an out-of-range condition.
755 if (!empty($pager_page_array[$this->pager['element']])) {
756 $page = intval($pager_page_array[$this->pager['element']]);
757 if ($page > 0 && $page < $pager_total[$this->pager['element']]) {
758 $this->pager['current_page'] = $page;
759 }
760 }
761 $pager_page_array[$this->pager['element']] = $this->pager['current_page'];
762 }
763
764 $offset = $this->pager['current_page'] * $this->pager['items_per_page'] + $this->pager['offset'];
765 $result = db_query_range($query, $args, $offset, $this->pager['items_per_page']);
766
767 }
768 else {
769 $result = db_query($query, $args);
770 }
771
772 $this->result = array();
773 while ($item = db_fetch_object($result)) {
774 $this->result[] = $item;
775 }
776 if (!empty($external)) {
777 db_set_active();
778 }
779 $this->execute_time = views_microtime() - $start;
780 }
781 if ($cache) {
782 $cache->cache_set('results');
783 }
784 }
785
786 $this->executed = TRUE;
787 }
788
789 /**
790 * Render this view for display.
791 */
792 function render($display_id = NULL) {
793 $this->execute($display_id);
794
795 // Check to see if the build failed.
796 if (!empty($this->build_info['fail'])) {
797 return;
798 }
799
800 init_theme();
801
802 $start = views_microtime();
803 if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
804 $this->start_query_capture();
805 }
806
807 // Check for already-cached output.
808 if (!empty($this->live_preview)) {
809 $cache = FALSE;
810 }
811 else {
812 $cache = $this->display_handler->get_cache_plugin();
813 }
814 if ($cache && $cache->cache_get('output')) {
815 vpr('Used cached output');
816 }
817 else {
818 if ($cache) {
819 $cache->cache_start();
820 }
821
822 // Initialize the style plugin.
823 $this->init_style();
824
825 $this->style_plugin->pre_render($this->result);
826
827 // Let modules modify the view just prior to executing it.
828 foreach (module_implements('views_pre_render') as $module) {
829 $function = $module . '_views_pre_render';
830 $function($this);
831 }
832
833 // Let the theme play too, because pre render is a very themey thing.
834 $function = $GLOBALS['theme'] . '_views_pre_render';
835 if (function_exists($function)) {
836 $function($this, $this->display_handler->output, $cache);
837 }
838
839 // Give field handlers the opportunity to perform additional queries
840 // using the entire resultset prior to rendering.
841 if ($this->style_plugin->uses_fields()) {
842 foreach ($this->field as $id => $handler) {
843 if (!empty($this->field[$id])) {
844 $this->field[$id]->pre_render($this->result);
845 }
846 }
847 }
848 $this->display_handler->output = $this->display_handler->render();
849 if ($cache) {
850 $cache->cache_set('output');
851 }
852 }
853
854 if ($cache) {
855 $cache->post_render($this->display_handler->output);
856 }
857
858 // Let modules modify the view output after it is rendered.
859 foreach (module_implements('views_post_render') as $module) {
860 $function = $module . '_views_post_render';
861 $function($this, $this->display_handler->output, $cache);
862 }
863
864 // Let the theme play too, because post render is a very themey thing.
865 $function = $GLOBALS['theme'] . '_views_post_render';
866 if (function_exists($function)) {
867 $function($this, $this->display_handler->output, $cache);
868 }
869
870 if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
871 $this->end_query_capture();
872 }
873 $this->render_time = views_microtime() - $start;
874
875 return $this->display_handler->output;
876 }
877
878 /**
879 * Render a specific field via the field ID and the row #.
880 */
881 function render_field($field, $row) {
882 if (isset($this->field[$field]) && isset($this->result[$row])) {
883 return $this->field[$field]->advanced_render($this->result[$row]);
884 }
885 }
886
887 /**
888 * Execute the given display, with the given arguments.
889 * To be called externally by whatever mechanism invokes the view,
890 * such as a page callback, hook_block, etc.
891 *
892 * This function should NOT be used by anything external as this
893 * returns data in the format specified by the display. It can also
894 * have other side effects that are only intended for the 'proper'
895 * use of the display, such as setting page titles and breadcrumbs.
896 *
897 * If you simply want to view the display, use view::preview() instead.
898 */
899 function execute_display($display_id = NULL, $args = array()) {
900 if (empty($this->current_display) || $this->current_display != $this->choose_display($display_id)) {
901 if (!$this->set_display($display_id)) {
902 return FALSE;
903 }
904 }
905
906 $this->pre_execute($args);
907
908 // Execute the view
909 $output = $this->display_handler->execute();
910
911 $this->post_execute();
912 return $output;
913 }
914
915 /**
916 * Preview the given display, with the given arguments.
917 *
918 * To be called externally, probably by an AJAX handler of some flavor.
919 * Can also be called when views are embedded, as this guarantees
920 * normalized output.
921 */
922 function preview($display_id = NULL, $args = array()) {
923 if (empty($this->current_display) || $this->current_display != $display_id) {
924 if (!$this->set_display($display_id)) {
925 return FALSE;
926 }
927 }
928
929 $this->preview = TRUE;
930 $this->pre_execute($args);
931 // Preview the view.
932 $output = $this->display_handler->preview();
933
934 $this->post_execute();
935 return $output;
936 }
937
938 /**
939 * Run attachments and let the display do what it needs to do prior
940 * to running.
941 */
942 function pre_execute($args = array()) {
943 $this->old_view[] = views_get_current_view();
944 views_set_current_view($this);
945 $display_id = $this->current_display;
946
947 // Let modules modify the view just prior to executing it.
948 foreach (module_implements('views_pre_view') as $module) {
949 $function = $module . '_views_pre_view';
950 $function($this, $display_id, $args);
951 }
952
953 // Prepare the view with the information we have, but only if we were
954 // passed arguments, as they may have been set previously.
955 if ($args) {
956 $this->set_arguments($args);
957 }
958
959 // $this->attach_displays();
960
961 // Allow the display handler to set up for execution
962 $this->display_handler->pre_execute();
963 }
964
965 /**
966 * Unset the current view, mostly.
967 */
968 function post_execute() {
969 // unset current view so we can be properly destructed later on.
970 // Return the previous value in case we're an attachment.
971
972 if ($this->old_view) {
973 $old_view = array_pop($this->old_view);
974 }
975
976 views_set_current_view(isset($old_view) ? $old_view : FALSE);
977 }
978
979 /**
980 * Run attachment displays for the view.
981 */
982 function attach_displays() {
983 if (!empty($this->is_attachment)) {
984 return;
985 }
986
987 if (!$this->display_handler->accept_attachments()) {
988 return;
989 }
990
991 $this->is_attachment = TRUE;
992 // Give other displays an opportunity to attach to the view.
993 foreach ($this->display as $id => $display) {
994 if (!empty($this->display[$id]->handler)) {
995 $this->display[$id]->handler->attach_to($this->current_display);
996 }
997 }
998 $this->is_attachment = FALSE;
999 }
1000
1001 /**
1002 * Called to get hook_menu information from the view and the
1003 * named display handler.
1004 */
1005 function execute_hook_menu($display_id = NULL) {
1006 // Prepare the view with the information we have.
1007
1008 // This was probably already called, but it's good to be safe.
1009 if (!$this->set_display($display_id)) {
1010 return FALSE;
1011 }
1012
1013 // Execute the view
1014 if (isset($this->display_handler)) {
1015 return $this->display_handler->execute_hook_menu();
1016 }
1017 }
1018
1019 /**
1020 * Called to get hook_block information from the view and the
1021 * named display handler.
1022 */
1023 function execute_hook_block($display_id = NULL) {
1024 // Prepare the view with the information we have.
1025
1026 // This was probably already called, but it's good to be safe.
1027 if (!$this->set_display($display_id)) {
1028 return FALSE;
1029 }
1030
1031 // Execute the view
1032 if (isset($this->display_handler)) {
1033 return $this->display_handler->execute_hook_block();
1034 }
1035 }
1036
1037 /**
1038 * Determine if the given user has access to the view. Note that
1039 * this sets the display handler if it hasn't been.
1040 */
1041 function access($displays = NULL, $account = NULL) {
1042 if (!isset($this->current_display)) {
1043 $this->init_display();
1044 }
1045
1046 if (!$account) {
1047 $account = $GLOBALS['user'];
1048 }
1049
1050 // We can't use choose_display() here because that function
1051 // calls this one.
1052 $displays = (array)$displays;
1053 foreach ($displays as $display_id) {
1054 if (!empty($this->display[$display_id]->handler)) {
1055 if ($this->display[$display_id]->handler->access($account)) {
1056 return TRUE;
1057 }
1058 }
1059 }
1060
1061 return FALSE;
1062 }
1063
1064 /**
1065 * Get the view's current title. This can change depending upon how it
1066 * was built.
1067 */
1068 function get_title() {
1069 if (empty($this->display_handler)) {
1070 if (!$this->set_display('default')) {
1071 return FALSE;
1072 }
1073 }
1074
1075 // During building, we might find a title override. If so, use it.
1076 if (!empty($this->build_info['title'])) {
1077 $title = $this->build_info['title'];
1078 }
1079 else {
1080 $title = $this->display_handler->get_option('title');
1081 }
1082
1083 return $title;
1084 }
1085
1086 /**
1087 * Force the view to build a title.
1088 */
1089 function build_title() {
1090 $this->init_display();
1091 if (empty($this->built)) {
1092 $this->init_query();
1093 }
1094 $this->init_handlers();
1095
1096 $this->_build_arguments();
1097 }
1098
1099 /**
1100 * Get the URL for the current view.
1101 *
1102 * This URL will be adjusted for arguments.
1103 */
1104 function get_url($args = NULL, $path = NULL) {
1105 if (!isset($path)) {
1106 $path = $this->get_path();
1107 }
1108 if (!isset($args)) {
1109 $args = $this->args;
1110 }
1111 // Don't bother working if there's nothing to do:
1112 if (empty($path) || (empty($args) && strpos($path, '%') === FALSE)) {
1113 return $path;
1114 }
1115
1116 $pieces = array();
1117 $arguments = isset($arguments) ? $arguments : $this->display_handler->get_option('arguments');
1118 $argument_keys = isset($arguments) ? array_keys($arguments) : array();
1119 $id = current($argument_keys);
1120 foreach (explode('/', $path) as $piece) {
1121 if ($piece != '%') {
1122 $pieces[] = $piece;
1123 }
1124 else {
1125 if (empty($args)) {
1126 // Try to never put % in a url; use the wildcard instead.
1127 if ($id && !empty($arguments[$id]['wildcard'])) {
1128 $pieces[] = $arguments[$id]['wildcard'];
1129 }
1130 else {
1131 $pieces[] = '*'; // gotta put something if there just isn't one.
1132 }
1133
1134 }
1135 else {
1136 $pieces[] = array_shift($args);
1137 }
1138
1139 if ($id) {
1140 $id = next($argument_keys);
1141 }
1142 }
1143 }
1144
1145 if (!empty($args)) {
1146 $pieces = array_merge($pieces, $args);
1147 }
1148 return implode('/', $pieces);
1149 }
1150
1151 /**
1152 * Get the base path used for this view.
1153 */
1154 function get_path() {
1155 if (!empty($this->override_path)) {
1156 return $this->override_path;
1157 }
1158
1159 if (empty($this->display_handler)) {
1160 if (!$this->set_display('default')) {
1161 return FALSE;
1162 }
1163 }
1164 return $this->display_handler->get_path();
1165 }
1166
1167 /**
1168 * Get the breadcrumb used for this view.
1169 *
1170 * @param $set
1171 * If true, use drupal_set_breadcrumb() to install the breadcrumb.
1172 */
1173 function get_breadcrumb($set = FALSE) {
1174 // Now that we've built the view, extract the breadcrumb.
1175 $base = TRUE;
1176 $breadcrumb = array();
1177
1178 if (!empty($this->build_info['breadcrumb'])) {
1179 foreach ($this->build_info['breadcrumb'] as $path => $title) {
1180 // Check to see if the frontpage is in the breadcrumb trail; if it
1181 // is, we'll remove that from the actual breadcrumb later.
1182 if ($path == variable_get('site_frontpage', 'node')) {
1183 $base = FALSE;
1184 $title = t('Home');
1185 }
1186 if ($title) {
1187 $breadcrumb[] = l($title, $path, array('html' => true));
1188 }
1189 }
1190
1191 if ($set) {
1192 if ($base) {
1193 $breadcrumb = array_merge(drupal_get_breadcrumb(), $breadcrumb);
1194 }
1195 drupal_set_breadcrumb($breadcrumb);
1196 }
1197 }
1198 return $breadcrumb;
1199 }
1200
1201 /**
1202 * Is this view cacheable?
1203 */
1204 function is_cacheable() {
1205 return $this->is_cacheable;
1206 }
1207
1208 /**
1209 * Set up query capturing.
1210 *
1211 * db_query() stores the queries that it runs in global $queries,
1212 * bit only if dev_query is set to true. In this case, we want
1213 * to temporarily override that setting if it's not and we
1214 * can do that without forcing a db rewrite by just manipulating
1215 * $conf. This is kind of evil but it works.
1216 */
1217 function start_query_capture() {
1218 global $conf, $queries;
1219 if (empty($conf['dev_query'])) {
1220 $this->fix_dev_query = TRUE;
1221 $conf['dev_query'] = TRUE;
1222 }
1223
1224 // Record the last query key used; anything already run isn't
1225 // a query that we are interested in.
1226 $this->last_query_key = NULL;
1227
1228 if (!empty($queries)) {
1229 $keys = array_keys($queries);
1230 $this->last_query_key = array_pop($keys);
1231 }
1232 }
1233
1234 /**
1235 * Add the list of queries run during render to buildinfo.
1236 *
1237 * @see view::start_query_capture()
1238 */
1239 function end_query_capture() {
1240 global $conf, $queries;
1241 if (!empty($this->fix_dev_query)) {
1242 $conf['dev_query'] = FALSE;
1243 }
1244
1245 // make a copy of the array so we can manipulate it with array_splice.
1246 $temp = $queries;
1247
1248 // Scroll through the queries until we get to our last query key.
1249 // Unset anything in our temp array.
1250 if (isset($this->last_query_key)) {
1251 while (list($id, $query) = each($queries)) {
1252 if ($id == $this->last_query_key) {
1253 break;
1254 }
1255
1256 unset($temp[$id]);
1257 }
1258 }
1259
1260 $this->additional_queries = $temp;
1261 }
1262
1263 /**
1264 * Load a view from the database based upon either vid or name.
1265 *
1266 * This is a static factory method that implements internal caching for