ViewExecutable.php 60.3 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\views\ViewExecutable.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
9
namespace Drupal\views;

10
use Symfony\Component\HttpFoundation\Response;
11
use Drupal\views\Plugin\Core\Entity\View;
12

merlinofchaos's avatar
merlinofchaos committed
13
14
15
16
17
18
19
20
21
22
23
/**
 * @defgroup views_objects Objects that represent a View or part of a view
 * @{
 * These objects are the core of Views do the bulk of the direction and
 * storing of data. All database activity is in these objects.
 */

/**
 * An object to contain all of the data to generate a view, plus the member
 * functions to build the view query, execute the query and render the output.
 */
24
class ViewExecutable {
25

merlinofchaos's avatar
merlinofchaos committed
26
  /**
27
   * The config entity in which the view is stored.
merlinofchaos's avatar
merlinofchaos committed
28
   *
29
   * @var Drupal\views\Plugin\Core\Entity\View
merlinofchaos's avatar
merlinofchaos committed
30
   */
31
  public $storage;
merlinofchaos's avatar
merlinofchaos committed
32

33
34
35
  /**
   * Whether or not the view has been built.
   *
36
37
   * @todo Group with other static properties.
   *
38
39
40
41
42
43
44
   * @var bool
   */
  public $built = FALSE;

  /**
   * Whether the view has been executed/query has been run.
   *
45
46
   * @todo Group with other static properties.
   *
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
   * @var bool
   */
  public $executed = FALSE;

  /**
   * Any arguments that have been passed into the view.
   *
   * @var array
   */
  public $args = array();

  /**
   * An array of build info.
   *
   * @var array
   */
  public $build_info = array();

  /**
   * Whether this view uses AJAX.
   *
   * @var bool
   */
  public $use_ajax = FALSE;
merlinofchaos's avatar
merlinofchaos committed
71

72
73
74
75
76
77
78
  /**
   * Where the results of a query will go.
   *
   * The array must use a numeric index starting at 0.
   *
   * @var array
   */
79
  public $result = array();
merlinofchaos's avatar
merlinofchaos committed
80
81
82

  // May be used to override the current pager info.

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  /**
   * The current page. If the view uses pagination.
   *
   * @var int
   */
  public $current_page = NULL;

  /**
   * The number of items per page.
   *
   * @var int
   */
  public $items_per_page = NULL;

  /**
   * The pager offset.
   *
   * @var int
   */
  public $offset = NULL;

  /**
   * The total number of rows returned from the query.
   *
   * @var array
   */
  public $total_rows = NULL;

  /**
   * Rendered attachments to place before the view.
   *
   * @var string
   */
  public $attachment_before = '';

  /**
   * Rendered attachements to place after the view.
   *
   * @var string
   */
  public $attachment_after = '';
merlinofchaos's avatar
merlinofchaos committed
124
125
126

  // Exposed widget input

127
128
129
130
131
132
  /**
   * All the form data from $form_state['values'].
   *
   * @var array
   */
  public $exposed_data = array();
merlinofchaos's avatar
merlinofchaos committed
133

134
135
136
137
138
139
  /**
   * An array of input values from exposed forms.
   *
   * @var array
   */
  public $exposed_input = array();
merlinofchaos's avatar
merlinofchaos committed
140

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  /**
   * Exposed widget input directly from the $form_state['values'].
   *
   * @var array
   */
  public $exposed_raw_input = array();

  /**
   * Used to store views that were previously running if we recurse.
   *
   * @var array
   */
  public $old_view = array();

  /**
   * To avoid recursion in views embedded into areas.
   *
   * @var array
   */
  public $parent_views = array();

  /**
   * Whether this view is an attachment to another view.
   *
   * @var bool
   */
  public $is_attachment = NULL;
merlinofchaos's avatar
merlinofchaos committed
168
169
170
171
172
173

  /**
   * Identifier of the current display.
   *
   * @var string
   */
174
  public $current_display;
merlinofchaos's avatar
merlinofchaos committed
175
176

  /**
177
   * Where the $query object will reside.
merlinofchaos's avatar
merlinofchaos committed
178
   *
179
   * @var Drupal\views\Plugin\query\QueryInterface
merlinofchaos's avatar
merlinofchaos committed
180
   */
181
  public $query = NULL;
merlinofchaos's avatar
merlinofchaos committed
182

183
184
185
186
187
188
189
190
  /**
   * The used pager plugin used by the current executed view.
   *
   * @var Drupal\views\Plugin\views\pager\PagerPluginBase
   */
  public $pager = NULL;

  /**
merlinofchaos's avatar
merlinofchaos committed
191
192
   * The current used display plugin.
   *
193
   * @var Drupal\views\Plugin\views\display\DisplayPluginBase
merlinofchaos's avatar
merlinofchaos committed
194
   */
195
  public $display_handler;
merlinofchaos's avatar
merlinofchaos committed
196

197
198
199
200
201
202
203
204
205
206
  /**
   * The list of used displays of the view.
   *
   * An array containing Drupal\views\Plugin\views\display\DisplayPluginBase
   * objects.
   *
   * @var array
   */
  public $displayHandlers;

merlinofchaos's avatar
merlinofchaos committed
207
208
209
  /**
   * The current used style plugin.
   *
210
   * @var Drupal\views\Plugin\views\style\StylePluginBase
merlinofchaos's avatar
merlinofchaos committed
211
   */
212
  public $style_plugin;
merlinofchaos's avatar
merlinofchaos committed
213
214
215
216
217
218

  /**
   * Stores the current active row while rendering.
   *
   * @var int
   */
219
  public $row_index;
merlinofchaos's avatar
merlinofchaos committed
220
221
222
223
224
225

   /**
   * Allow to override the url of the current view.
   *
   * @var string
   */
226
  public $override_url = NULL;
merlinofchaos's avatar
merlinofchaos committed
227
228
229
230
231
232

  /**
   * Allow to override the path used for generated urls.
   *
   * @var string
   */
233
  public $override_path = NULL;
merlinofchaos's avatar
merlinofchaos committed
234
235
236

  /**
   * Allow to override the used database which is used for this query.
237
238
   *
   * @var bool
merlinofchaos's avatar
merlinofchaos committed
239
   */
240
  public $base_database = NULL;
merlinofchaos's avatar
merlinofchaos committed
241

242
  // Handlers which are active on this view.
merlinofchaos's avatar
merlinofchaos committed
243
244
245

  /**
   * Stores the field handlers which are initialized on this view.
246
247
248
249
250
   *
   * An array containing Drupal\views\Plugin\views\field\FieldPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
251
   */
252
  public $field;
merlinofchaos's avatar
merlinofchaos committed
253
254
255

  /**
   * Stores the argument handlers which are initialized on this view.
256
257
258
259
260
   *
   * An array containing Drupal\views\Plugin\views\argument\ArgumentPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
261
   */
262
  public $argument;
merlinofchaos's avatar
merlinofchaos committed
263
264
265

  /**
   * Stores the sort handlers which are initialized on this view.
266
267
268
269
   *
   * An array containing Drupal\views\Plugin\views\sort\SortPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
270
   */
271
  public $sort;
merlinofchaos's avatar
merlinofchaos committed
272
273
274

  /**
   * Stores the filter handlers which are initialized on this view.
275
276
277
278
279
   *
   * An array containing Drupal\views\Plugin\views\filter\FilterPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
280
   */
281
  public $filter;
merlinofchaos's avatar
merlinofchaos committed
282
283
284

  /**
   * Stores the relationship handlers which are initialized on this view.
285
286
287
288
289
   *
   * An array containing Drupal\views\Plugin\views\relationship\RelationshipPluginBase
   * objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
290
   */
291
  public $relationship;
merlinofchaos's avatar
merlinofchaos committed
292
293
294

  /**
   * Stores the area handlers for the header which are initialized on this view.
295
296
297
298
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
299
   */
300
  public $header;
merlinofchaos's avatar
merlinofchaos committed
301
302
303

  /**
   * Stores the area handlers for the footer which are initialized on this view.
304
305
306
307
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
308
   */
309
  public $footer;
merlinofchaos's avatar
merlinofchaos committed
310
311
312

  /**
   * Stores the area handlers for the empty text which are initialized on this view.
313
314
315
316
   *
   * An array containing Drupal\views\Plugin\views\area\AreaPluginBase objects.
   *
   * @var array
merlinofchaos's avatar
merlinofchaos committed
317
   */
318
  public $empty;
merlinofchaos's avatar
merlinofchaos committed
319

320
321
322
323
324
325
326
  /**
   * Stores the current response object.
   *
   * @var Symfony\Component\HttpFoundation\Response
   */
  protected $response = NULL;

327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  /**
   * Does this view already have loaded it's handlers.
   *
   * @todo Group with other static properties.
   *
   * @var bool
   */
  public $inited;

  /**
   * The name of the active style plugin of the view.
   *
   * @todo remove this and just use $this->style_plugin
   *
   * @var string
   */
  public $plugin_name;

  /**
   * The options used by the style plugin of this running view.
   *
   * @todo To be able to remove it, Drupal\views\Plugin\views\argument\ArgumentPluginBase::default_summary()
   *   should instantiate the style plugin.
   * @var array
   */
  public $style_options;

  /**
   * The rendered output of the exposed form.
   *
   * @var string
   */
  public $exposed_widgets;

  /**
   * If this view has been previewed.
   *
   * @var bool
   */
  public $preview;

  /**
   * Force the query to calculate the total number of results.
   *
   * @todo Move to the query.
   *
   * @var bool
   */
  public $get_total_rows;

  /**
   * Indicates if the sorts have been built.
   *
   * @todo Group with other static properties.
   *
   * @var bool
   */
  public $build_sort;

  /**
   * Stores the many-to-one tables for performance.
   *
   * @var array
   */
  public $many_to_one_tables;

  /**
   * A unique identifier which allows to update multiple views output via js.
   *
   * @var string
   */
  public $dom_id;

400
401
402
  /**
   * Constructs a new ViewExecutable object.
   *
403
   * @param Drupal\views\Plugin\Core\Entity\View $storage
404
405
   *   The view config entity the actual information is stored on.
   */
406
  public function __construct(View $storage) {
407
408
    // Reference the storage and the executable to each other.
    $this->storage = $storage;
409
    $this->storage->set('executable', $this);
410
411
412
  }

  /**
413
   * @todo.
414
   */
415
416
  public function save() {
    $this->storage->save();
merlinofchaos's avatar
merlinofchaos committed
417
418
419
420
421
422
  }

  /**
   * Returns a list of the sub-object types used by this view. These types are
   * stored on the display, and are used in the build process.
   */
423
  public function displayObjects() {
merlinofchaos's avatar
merlinofchaos committed
424
425
426
427
428
429
430
    return array('argument', 'field', 'sort', 'filter', 'relationship', 'header', 'footer', 'empty');
  }

  /**
   * Set the arguments that come to this view. Usually from the URL
   * but possibly from elsewhere.
   */
431
  public function setArguments($args) {
merlinofchaos's avatar
merlinofchaos committed
432
433
434
435
436
437
    $this->args = $args;
  }

  /**
   * Change/Set the current page for the pager.
   */
438
  public function setCurrentPage($page) {
merlinofchaos's avatar
merlinofchaos committed
439
    $this->current_page = $page;
440
441

    // If the pager is already initialized, pass it through to the pager.
442
443
    if (!empty($this->pager)) {
      return $this->pager->set_current_page($page);
444
    }
merlinofchaos's avatar
merlinofchaos committed
445
446
447
448
449
  }

  /**
   * Get the current page from the pager.
   */
450
  public function getCurrentPage() {
451
    // If the pager is already initialized, pass it through to the pager.
452
453
    if (!empty($this->pager)) {
      return $this->pager->get_current_page();
merlinofchaos's avatar
merlinofchaos committed
454
    }
455
456
457
458

    if (isset($this->current_page)) {
      return $this->current_page;
    }
merlinofchaos's avatar
merlinofchaos committed
459
460
461
462
463
  }

  /**
   * Get the items per page from the pager.
   */
464
  public function getItemsPerPage() {
465
    // If the pager is already initialized, pass it through to the pager.
466
467
    if (!empty($this->pager)) {
      return $this->pager->get_items_per_page();
merlinofchaos's avatar
merlinofchaos committed
468
    }
469
470
471
472

    if (isset($this->items_per_page)) {
      return $this->items_per_page;
    }
merlinofchaos's avatar
merlinofchaos committed
473
474
475
476
477
  }

  /**
   * Set the items per page on the pager.
   */
478
  public function setItemsPerPage($items_per_page) {
merlinofchaos's avatar
merlinofchaos committed
479
480
481
    $this->items_per_page = $items_per_page;

    // If the pager is already initialized, pass it through to the pager.
482
483
    if (!empty($this->pager)) {
      $this->pager->set_items_per_page($items_per_page);
merlinofchaos's avatar
merlinofchaos committed
484
485
486
487
488
489
    }
  }

  /**
   * Get the pager offset from the pager.
   */
490
  public function getOffset() {
491
    // If the pager is already initialized, pass it through to the pager.
492
493
    if (!empty($this->pager)) {
      return $this->pager->get_offset();
merlinofchaos's avatar
merlinofchaos committed
494
    }
495
496
497
498

    if (isset($this->offset)) {
      return $this->offset;
    }
merlinofchaos's avatar
merlinofchaos committed
499
500
501
502
503
  }

  /**
   * Set the offset on the pager.
   */
504
  public function setOffset($offset) {
merlinofchaos's avatar
merlinofchaos committed
505
506
507
    $this->offset = $offset;

    // If the pager is already initialized, pass it through to the pager.
508
509
    if (!empty($this->pager)) {
      $this->pager->set_offset($offset);
merlinofchaos's avatar
merlinofchaos committed
510
511
512
513
514
515
    }
  }

  /**
   * Determine if the pager actually uses a pager.
   */
516
  public function usePager() {
517
518
    if (!empty($this->pager)) {
      return $this->pager->use_pager();
merlinofchaos's avatar
merlinofchaos committed
519
520
521
522
523
524
525
526
    }
  }

  /**
   * Whether or not AJAX should be used. If AJAX is used, paging,
   * tablesorting and exposed filters will be fetched via an AJAX call
   * rather than a page refresh.
   */
527
  public function setUseAJAX($use_ajax) {
merlinofchaos's avatar
merlinofchaos committed
528
529
530
531
532
533
534
    $this->use_ajax = $use_ajax;
  }

  /**
   * Set the exposed filters input to an array. If unset they will be taken
   * from $_GET when the time comes.
   */
535
  public function setExposedInput($filters) {
merlinofchaos's avatar
merlinofchaos committed
536
537
538
539
540
541
    $this->exposed_input = $filters;
  }

  /**
   * Figure out what the exposed input for this view is.
   */
542
  public function getExposedInput() {
merlinofchaos's avatar
merlinofchaos committed
543
544
545
    // Fill our input either from $_GET or from something previously set on the
    // view.
    if (empty($this->exposed_input)) {
546
      $this->exposed_input = drupal_container()->get('request')->query->all();
merlinofchaos's avatar
merlinofchaos committed
547
548
549
550
551
552
553
554
555
556
557
558
559
      // unset items that are definitely not our input:
      foreach (array('page', 'q') as $key) {
        if (isset($this->exposed_input[$key])) {
          unset($this->exposed_input[$key]);
        }
      }

      // If we have no input at all, check for remembered input via session.

      // If filters are not overridden, store the 'remember' settings on the
      // default display. If they are, store them on this display. This way,
      // multiple displays in the same view can share the same filters and
      // remember settings.
560
      $display_id = ($this->display_handler->isDefaulted('filters')) ? 'default' : $this->current_display;
merlinofchaos's avatar
merlinofchaos committed
561

562
563
      if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->storage->get('name')][$display_id])) {
        $this->exposed_input = $_SESSION['views'][$this->storage->get('name')][$display_id];
merlinofchaos's avatar
merlinofchaos committed
564
565
566
567
568
569
570
571
572
      }
    }

    return $this->exposed_input;
  }

  /**
   * Set the display for this view and initialize the display handler.
   */
573
  public function initDisplay() {
merlinofchaos's avatar
merlinofchaos committed
574
575
576
577
578
    if (isset($this->current_display)) {
      return TRUE;
    }

    // Instantiate all displays
579
580
    foreach ($this->storage->get('display') as $id => $display) {
      $this->displayHandlers[$id] = views_get_plugin('display', $display['display_plugin']);
581
      if (!empty($this->displayHandlers[$id])) {
merlinofchaos's avatar
merlinofchaos committed
582
        // Initialize the new display handler with data.
583
584
        // @todo Refactor display to not need the handler data by reference.
        $this->displayHandlers[$id]->init($this, $this->storage->getDisplay($id));
merlinofchaos's avatar
merlinofchaos committed
585
586
587
588
589
        // If this is NOT the default display handler, let it know which is
        // since it may well utilize some data from the default.
        // This assumes that the 'default' handler is always first. It always
        // is. Make sure of it.
        if ($id != 'default') {
590
          $this->displayHandlers[$id]->default_display =& $this->displayHandlers['default'];
merlinofchaos's avatar
merlinofchaos committed
591
592
593
594
595
        }
      }
    }

    $this->current_display = 'default';
596
    $this->display_handler = $this->displayHandlers['default'];
merlinofchaos's avatar
merlinofchaos committed
597
598
599
600
601
602
603

    return TRUE;
  }

  /**
   * Get the first display that is accessible to the user.
   *
604
   * @param array|string $displays
merlinofchaos's avatar
merlinofchaos committed
605
   *   Either a single display id or an array of display ids.
606
607
608
   *
   * @return string
   *   The first accessible display id, at least default.
merlinofchaos's avatar
merlinofchaos committed
609
   */
610
  public function chooseDisplay($displays) {
merlinofchaos's avatar
merlinofchaos committed
611
612
613
614
    if (!is_array($displays)) {
      return $displays;
    }

615
    $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
616
617

    foreach ($displays as $display_id) {
618
      if ($this->displayHandlers[$display_id]->access()) {
merlinofchaos's avatar
merlinofchaos committed
619
620
621
622
623
624
625
626
627
628
629
630
631
        return $display_id;
      }
    }

    return 'default';
  }

  /**
   * Set the display as current.
   *
   * @param $display_id
   *   The id of the display to mark as current.
   */
632
  public function setDisplay($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
633
634
    // If we have not already initialized the display, do so. But be careful.
    if (empty($this->current_display)) {
635
      $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
636
637
638
639
640
641
642
643

      // If handlers were not initialized, and no argument was sent, set up
      // to the default display.
      if (empty($display_id)) {
        $display_id = 'default';
      }
    }

644
    $display_id = $this->chooseDisplay($display_id);
merlinofchaos's avatar
merlinofchaos committed
645
646
647
648
649
650
651

    // If no display id sent in and one wasn't chosen above, we're finished.
    if (empty($display_id)) {
      return FALSE;
    }

    // Ensure the requested display exists.
652
    if (empty($this->displayHandlers[$display_id])) {
merlinofchaos's avatar
merlinofchaos committed
653
      $display_id = 'default';
654
      if (empty($this->displayHandlers[$display_id])) {
655
        debug(format_string('set_display() called with invalid display ID @display.', array('@display' => $display_id)));
merlinofchaos's avatar
merlinofchaos committed
656
657
658
659
660
661
662
663
        return FALSE;
      }
    }

    // Set the current display.
    $this->current_display = $display_id;

    // Ensure requested display has a working handler.
664
    if (empty($this->displayHandlers[$display_id])) {
merlinofchaos's avatar
merlinofchaos committed
665
666
667
668
      return FALSE;
    }

    // Set a shortcut
669
    $this->display_handler = $this->displayHandlers[$display_id];
merlinofchaos's avatar
merlinofchaos committed
670
671
672
673
674
675
676
677
678
679

    return TRUE;
  }

  /**
   * Find and initialize the style plugin.
   *
   * Note that arguments may have changed which style plugin we use, so
   * check the view object first, then ask the display handler.
   */
680
  public function initStyle() {
merlinofchaos's avatar
merlinofchaos committed
681
682
683
684
685
    if (isset($this->style_plugin)) {
      return is_object($this->style_plugin);
    }

    if (!isset($this->plugin_name)) {
686
687
688
      $style = $this->display_handler->getOption('style');
      $this->plugin_name = $style['type'];
      $this->style_options = $style['options'];
merlinofchaos's avatar
merlinofchaos committed
689
690
    }

dawehner's avatar
dawehner committed
691
    $this->style_plugin = views_get_plugin('style', $this->plugin_name);
merlinofchaos's avatar
merlinofchaos committed
692
693
694
695
696
697

    if (empty($this->style_plugin)) {
      return FALSE;
    }

    // init the new style handler with data.
698
    $this->style_plugin->init($this, $this->display_handler, $this->style_options);
merlinofchaos's avatar
merlinofchaos committed
699
700
701
702
703
704
    return TRUE;
  }

  /**
   * Acquire and attach all of the handlers.
   */
705
  public function initHandlers() {
706
    $this->initDisplay();
merlinofchaos's avatar
merlinofchaos committed
707
    if (empty($this->inited)) {
708
      foreach ($this::viewsHandlerTypes() as $key => $info) {
709
        $this->_initHandler($key, $info);
merlinofchaos's avatar
merlinofchaos committed
710
711
712
713
714
715
716
717
718
719
720
      }
      $this->inited = TRUE;
    }
  }

  /**
   * Initialize the pager
   *
   * Like style initialization, pager initialization is held until late
   * to allow for overrides.
   */
721
  public function initPager() {
722
    if (!isset($this->pager)) {
723
      $this->pager = $this->display_handler->getPlugin('pager');
merlinofchaos's avatar
merlinofchaos committed
724

725
726
      if ($this->pager->use_pager()) {
        $this->pager->set_current_page($this->current_page);
merlinofchaos's avatar
merlinofchaos committed
727
728
729
730
731
      }

      // These overrides may have been set earlier via $view->set_*
      // functions.
      if (isset($this->items_per_page)) {
732
        $this->pager->set_items_per_page($this->items_per_page);
merlinofchaos's avatar
merlinofchaos committed
733
734
735
      }

      if (isset($this->offset)) {
736
        $this->pager->set_offset($this->offset);
merlinofchaos's avatar
merlinofchaos committed
737
738
739
740
      }
    }
  }

741
742
743
  /**
   * Render the pager, if necessary.
   */
744
  public function renderPager($exposed_input) {
745
746
747
748
749
750
751
    if (!empty($this->pager) && $this->pager->use_pager()) {
      return $this->pager->render($exposed_input);
    }

    return '';
  }

merlinofchaos's avatar
merlinofchaos committed
752
753
754
755
  /**
   * Create a list of base tables eligible for this view. Used primarily
   * for the UI. Display must be already initialized.
   */
756
  public function getBaseTables() {
merlinofchaos's avatar
merlinofchaos committed
757
    $base_tables = array(
758
      $this->storage->get('base_table') => TRUE,
merlinofchaos's avatar
merlinofchaos committed
759
760
761
      '#global' => TRUE,
    );

762
    foreach ($this->display_handler->getHandlers('relationship') as $handler) {
merlinofchaos's avatar
merlinofchaos committed
763
764
765
766
767
768
      $base_tables[$handler->definition['base']] = TRUE;
    }
    return $base_tables;
  }

  /**
769
   * Run the preQuery() on all active handlers.
merlinofchaos's avatar
merlinofchaos committed
770
   */
771
  protected function _preQuery() {
772
    foreach ($this::viewsHandlerTypes() as $key => $info) {
merlinofchaos's avatar
merlinofchaos committed
773
774
775
776
      $handlers = &$this->$key;
      $position = 0;
      foreach ($handlers as $id => $handler) {
        $handlers[$id]->position = $position;
777
        $handlers[$id]->preQuery();
merlinofchaos's avatar
merlinofchaos committed
778
779
780
781
782
783
        $position++;
      }
    }
  }

  /**
784
   * Run the postExecute() on all active handlers.
merlinofchaos's avatar
merlinofchaos committed
785
   */
786
  protected function _postExecute() {
787
    foreach ($this::viewsHandlerTypes() as $key => $info) {
merlinofchaos's avatar
merlinofchaos committed
788
789
      $handlers = &$this->$key;
      foreach ($handlers as $id => $handler) {
790
        $handlers[$id]->postExecute($this->result);
merlinofchaos's avatar
merlinofchaos committed
791
792
793
794
795
796
797
798
799
800
      }
    }
  }

  /**
   * Attach all of the handlers for each type.
   *
   * @param $key
   *   One of 'argument', 'field', 'sort', 'filter', 'relationship'
   * @param $info
801
   *   The $info from viewsHandlerTypes for this object.
merlinofchaos's avatar
merlinofchaos committed
802
   */
803
  protected function _initHandler($key, $info) {
merlinofchaos's avatar
merlinofchaos committed
804
    // Load the requested items from the display onto the object.
805
    $this->$key = $this->display_handler->getHandlers($key);
merlinofchaos's avatar
merlinofchaos committed
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820

    // This reference deals with difficult PHP indirection.
    $handlers = &$this->$key;

    // Run through and test for accessibility.
    foreach ($handlers as $id => $handler) {
      if (!$handler->access()) {
        unset($handlers[$id]);
      }
    }
  }

  /**
   * Build all the arguments.
   */
821
  protected function _buildArguments() {
merlinofchaos's avatar
merlinofchaos committed
822
823
824
825
826
827
828
829
830
831
    // Initially, we want to build sorts and fields. This can change, though,
    // if we get a summary view.
    if (empty($this->argument)) {
      return TRUE;
    }

    // build arguments.
    $position = -1;

    // Create a title for use in the breadcrumb trail.
832
    $title = $this->display_handler->getOption('title');
merlinofchaos's avatar
merlinofchaos committed
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848

    $this->build_info['breadcrumb'] = array();
    $breadcrumb_args = array();
    $substitutions = array();

    $status = TRUE;

    // Iterate through each argument and process.
    foreach ($this->argument as $id => $arg) {
      $position++;
      $argument = &$this->argument[$id];

      if ($argument->broken()) {
        continue;
      }

849
      $argument->setRelationship();
merlinofchaos's avatar
merlinofchaos committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866

      $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
      $argument->position = $position;

      if (isset($arg) || $argument->has_default_argument()) {
        if (!isset($arg)) {
          $arg = $argument->get_default_argument();
          // make sure default args get put back.
          if (isset($arg)) {
            $this->args[$position] = $arg;
          }
          // remember that this argument was computed, not passed on the URL.
          $argument->is_default = TRUE;
        }

        // Set the argument, which will also validate that the argument can be set.
        if (!$argument->set_argument($arg)) {
867
          $status = $argument->validateFail($arg);
merlinofchaos's avatar
merlinofchaos committed
868
869
870
871
872
873
874
875
          break;
        }

        if ($argument->is_exception()) {
          $arg_title = $argument->exception_title();
        }
        else {
          $arg_title = $argument->get_title();
876
          $argument->query($this->display_handler->useGroupBy());
merlinofchaos's avatar
merlinofchaos committed
877
878
879
880
881
882
883
884
        }

        // Add this argument's substitution
        $substitutions['%' . ($position + 1)] = $arg_title;
        $substitutions['!' . ($position + 1)] = strip_tags(decode_entities($arg));

        // Since we're really generating the breadcrumb for the item above us,
        // check the default action of this argument.
885
        if ($this->display_handler->usesBreadcrumb() && $argument->uses_breadcrumb()) {
886
          $path = $this->getUrl($breadcrumb_args);
merlinofchaos's avatar
merlinofchaos committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
          if (strpos($path, '%') === FALSE) {
            if (!empty($argument->options['breadcrumb_enable']) && !empty($argument->options['breadcrumb'])) {
              $breadcrumb = $argument->options['breadcrumb'];
            }
            else {
              $breadcrumb = $title;
            }
            $this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
          }
        }

        // Allow the argument to muck with this breadcrumb.
        $argument->set_breadcrumb($this->build_info['breadcrumb']);

        // Test to see if we should use this argument's title
        if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
          $title = $argument->options['title'];
        }

        $breadcrumb_args[] = $arg;
      }
      else {
        // determine default condition and handle.
        $status = $argument->default_action();
        break;
      }

      // Be safe with references and loops:
      unset($argument);
    }

    // set the title in the build info.
    if (!empty($title)) {
      $this->build_info['title'] = $title;
    }

    // Store the arguments for later use.
    $this->build_info['substitutions'] = $substitutions;

    return $status;
  }

  /**
   * Do some common building initialization.
   */
932
  public function initQuery() {
merlinofchaos's avatar
merlinofchaos committed
933
934
935
936
937
938
939
940
941
    if (!empty($this->query)) {
      $class = get_class($this->query);
      if ($class && $class != 'stdClass') {
        // return if query is already initialized.
        return TRUE;
      }
    }

    // Create and initialize the query object.
942
943
    $views_data = views_fetch_data($this->storage->get('base_table'));
    $this->storage->set('base_field', !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : '');
merlinofchaos's avatar
merlinofchaos committed
944
945
946
947
    if (!empty($views_data['table']['base']['database'])) {
      $this->base_database = $views_data['table']['base']['database'];
    }

948
    $this->query = $this->display_handler->getPlugin('query');
merlinofchaos's avatar
merlinofchaos committed
949
950
951
952
953
954
    return TRUE;
  }

  /**
   * Build the query for the view.
   */
955
  public function build($display_id = NULL) {
merlinofchaos's avatar
merlinofchaos committed
956
957
958
959
960
    if (!empty($this->built)) {
      return;
    }

    if (empty($this->current_display) || $display_id) {
961
      if (!$this->setDisplay($display_id)) {
merlinofchaos's avatar
merlinofchaos committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
        return FALSE;
      }
    }

    // Let modules modify the view just prior to building it.
    foreach (module_implements('views_pre_build') as $module) {
      $function = $module . '_views_pre_build';
      $function($this);
    }

    // Attempt to load from cache.
    // @todo Load a build_info from cache.

    $start = microtime(TRUE);
    // If that fails, let's build!
    $this->build_info = array(
      'query' => '',
      'count_query' => '',
      'query_args' => array(),
    );

983
    $this->initQuery();
merlinofchaos's avatar
merlinofchaos committed
984
985
986
987
988
989
990

    // Call a module hook and see if it wants to present us with a
    // pre-built query or instruct us not to build the query for
    // some reason.
    // @todo: Implement this. Use the same mechanism Panels uses.

    // Run through our handlers and ensure they have necessary information.
991
    $this->initHandlers();
merlinofchaos's avatar
merlinofchaos committed
992
993

    // Let the handlers interact with each other if they really want.
994
    $this->_preQuery();
merlinofchaos's avatar
merlinofchaos committed
995

996
997
    if ($this->display_handler->usesExposed()) {
      $exposed_form = $this->display_handler->getPlugin('exposed_form');
merlinofchaos's avatar
merlinofchaos committed
998
999
1000
      $this->exposed_widgets = $exposed_form->render_exposed_form();
      if (form_set_error() || !empty($this->build_info['abort'])) {
        $this->built = TRUE;