PageRenderTime 58ms CodeModel.GetById 21ms 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
  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. $view = new view;
  1500. $view->load_row($data);
  1501. $view->loaded = TRUE;
  1502. $view->type = t('Normal');
  1503. $views[$view->name] = $view;
  1504. $names[$view->vid] = $view->name;
  1505. }
  1506. // Stop if we didn't get any views.
  1507. if (!$views) {
  1508. return array();
  1509. }
  1510. // Now load all the subtables:
  1511. foreach (view::db_objects() as $key) {
  1512. $object_name = "views_$key";
  1513. $result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN (:vids) ORDER BY vid, position",
  1514. array(':vids' => array_keys($names)));
  1515. foreach ($result as $data) {
  1516. $object = new $object_name(FALSE);
  1517. $object->load_row($data);
  1518. // Because it can get complicated with this much indirection,
  1519. // make a shortcut reference.
  1520. $location = &$views[$names[$object->vid]]->$key;
  1521. // If we have a basic id field, load the item onto the view based on
  1522. // this ID, otherwise push it on.
  1523. if (!empty($object->id)) {
  1524. $location[$object->id] = $object;
  1525. }
  1526. else {
  1527. $location[] = $object;
  1528. }
  1529. }
  1530. }
  1531. return $views;
  1532. }
  1533. /**
  1534. * Save the view to the database. If the view does not already exist,
  1535. * A vid will be assigned to the view and also returned from this function.
  1536. */
  1537. function save() {
  1538. if ($this->vid == 'new') {
  1539. $this->vid = NULL;
  1540. }
  1541. // If there is no vid, check if a view with this machine name already exists.
  1542. elseif (empty($this->vid)) {
  1543. $vid = db_query("SELECT vid from {views_view} WHERE name = :name", array(':name' => $this->name))->fetchField();
  1544. $this->vid = $vid ? $vid : NULL;
  1545. }
  1546. $transaction = db_transaction();
  1547. try {
  1548. // If we have no vid or our vid is a string, this is a new view.
  1549. if (!empty($this->vid)) {
  1550. // remove existing table entries
  1551. foreach ($this->db_objects() as $key) {
  1552. db_delete('views_' . $key)
  1553. ->condition('vid', $this->vid)
  1554. ->execute();
  1555. }
  1556. }
  1557. $this->save_row(!empty($this->vid) ? 'vid' : FALSE);
  1558. // Save all of our subtables.
  1559. foreach ($this->db_objects() as $key) {
  1560. $this->_save_rows($key);
  1561. }
  1562. }
  1563. catch (Exception $e) {
  1564. $transaction->rollback();
  1565. watchdog_exception('views', $e);
  1566. throw $e;
  1567. }
  1568. $this->save_locale_strings();
  1569. // Clear caches.
  1570. views_invalidate_cache();
  1571. }
  1572. /**
  1573. * Save a row to the database for the given key, which is one of the
  1574. * keys from view::db_objects()
  1575. */
  1576. function _save_rows($key) {
  1577. $count = 0;
  1578. foreach ($this->$key as $position => $object) {
  1579. $object->position = ++$count;
  1580. $object->vid = $this->vid;
  1581. $object->save_row();
  1582. }
  1583. }
  1584. /**
  1585. * Delete the view from the database.
  1586. */
  1587. function delete($clear = TRUE) {
  1588. if (empty($this->vid)) {
  1589. return;
  1590. }
  1591. db_delete('views_view')
  1592. ->condition('vid', $this->vid)
  1593. ->execute();
  1594. // Delete from all of our subtables as well.
  1595. foreach ($this->db_objects() as $key) {
  1596. db_delete('views_'. $key)
  1597. ->condition('vid', $this->vid)
  1598. ->execute();
  1599. }
  1600. cache_clear_all('views_query:' . $this->name, 'cache_views');
  1601. if ($clear) {
  1602. // Clear caches.
  1603. views_invalidate_cache();
  1604. }
  1605. }
  1606. /**
  1607. * Export a view as PHP code.
  1608. */
  1609. function export($indent = '') {
  1610. $this->init_display();
  1611. $this->init_query();
  1612. $output = '';
  1613. $output .= $this->export_row('view', $indent);
  1614. // Set the API version
  1615. $output .= $indent . '$view->api_version = \'' . views_api_version() . "';\n";
  1616. $output .= $indent . '$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */' . "\n";
  1617. foreach ($this->display as $id => $display) {
  1618. $output .= "\n" . $indent . "/* Display: $display->display_title */\n";
  1619. $output .= $indent . '$handler = $view->new_display(' . ctools_var_export($display->display_plugin, $indent) . ', ' . ctools_var_export($display->display_title, $indent) . ', \'' . $id . "');\n";
  1620. if (empty($display->handler)) {
  1621. // @todo -- probably need a method of exporting broken displays as
  1622. // they may simply be broken because a module is not installed. That
  1623. // does not invalidate the display.
  1624. continue;
  1625. }
  1626. $output .= $display->handler->export_options($indent, '$handler->options');
  1627. }
  1628. // Give the localization system a chance to export translatables to code.
  1629. if ($this->init_localization()) {
  1630. $this->export_locale_strings('export');
  1631. $translatables = $this->localization_plugin->export_render($indent);
  1632. if (!empty($translatables)) {
  1633. $output .= $translatables;
  1634. }
  1635. }
  1636. return $output;
  1637. }
  1638. /**
  1639. * Make a copy of this view that has been sanitized of all database IDs
  1640. * and handlers and other stuff.
  1641. *
  1642. * I'd call this clone() but it's reserved.
  1643. */
  1644. function copy() {
  1645. $code = $this->export();
  1646. eval($code);
  1647. return $view;
  1648. }
  1649. /**
  1650. * Safely clone a view.
  1651. *
  1652. * Because views are complicated objects within objects, and PHP loves to
  1653. * do references to everything, if a View is not properly and safely
  1654. * cloned it will still have references to the original view, and can
  1655. * actually cause the original view to point to objects in the cloned
  1656. * view. This gets ugly fast.
  1657. *
  1658. * This will completely wipe a view clean so it can be considered fresh.
  1659. *
  1660. * @return view
  1661. * The cloned view.
  1662. */
  1663. function clone_view() {
  1664. $clone = version_compare(phpversion(), '5.0') < 0 ? $this : clone($this);
  1665. $keys = array('current_display', 'display_handler', 'build_info', 'built', 'executed', 'attachment_before', 'attachment_after', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'exposed_widgets', 'many_to_one_tables', 'feed_icon');
  1666. foreach ($keys as $key) {
  1667. if (isset($clone->$key)) {
  1668. unset($clone->$key);
  1669. }
  1670. }
  1671. $clone->built = $clone->executed = FALSE;
  1672. $clone->build_info = array();
  1673. $clone->attachment_before = '';
  1674. $clone->attachment_after = '';
  1675. $clone->result = array();
  1676. // shallow cloning means that all the display objects
  1677. // *were not cloned*. We must clone them ourselves.
  1678. $displays = array();
  1679. foreach ($clone->display as $id => $display) {
  1680. $displays[$id] = clone $display;
  1681. if (isset($displays[$id]->handler)) {
  1682. unset($displays[$id]->handler);
  1683. }
  1684. }
  1685. $clone->display = $displays;
  1686. return $clone;
  1687. }
  1688. /**
  1689. * Unset references so that a $view object may be properly garbage
  1690. * collected.
  1691. */
  1692. function destroy() {
  1693. foreach (array_keys($this->display) as $display_id) {
  1694. if (isset($this->display[$display_id]->handler)) {
  1695. $this->display[$display_id]->handler->destroy();
  1696. unset($this->display[$display_id]->handler);
  1697. }
  1698. }
  1699. foreach (views_object_types() as $type => $info) {
  1700. if (isset($this->$type)) {
  1701. $handlers = &$this->$type;
  1702. foreach ($handlers as $id => $item) {
  1703. $handlers[$id]->destroy();
  1704. }
  1705. unset($handlers);
  1706. }
  1707. }
  1708. if (isset($this->style_plugin)) {
  1709. $this->style_plugin->destroy();
  1710. unset($this->style_plugin);
  1711. }
  1712. // Clear these to make sure the view can be processed/used again.
  1713. if (isset($this->display_handler)) {
  1714. unset($this->display_handler);
  1715. }
  1716. if (isset($this->current_display)) {
  1717. unset($this->current_display);
  1718. }
  1719. if (isset($this->query)) {
  1720. unset($this->query);
  1721. }
  1722. $keys = array('current_display', 'display_handler', 'build_info', 'built', 'executed', 'attachment_before', 'attachment_after', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables');
  1723. foreach ($keys as $key) {
  1724. if (isset($this->$key)) {
  1725. unset($this->$key);
  1726. }
  1727. }
  1728. // These keys are checked by the next init, so instead of unsetting them,
  1729. // just set the default values.
  1730. $keys = array('items_per_page', 'offset', 'current_page');
  1731. foreach ($keys as $key) {
  1732. if (isset($this->$key)) {
  1733. $this->$key = NULL;
  1734. }
  1735. }
  1736. $this->built = $this->executed = FALSE;
  1737. $this->build_info = array();
  1738. $this->attachment_before = '';
  1739. $this->attachment_after = '';
  1740. }
  1741. /**
  1742. * Make sure the view is completely valid.
  1743. *
  1744. * @return
  1745. * TRUE if the view is valid; an array of error strings if it is not.
  1746. */
  1747. function validate() {
  1748. $this->init_display();
  1749. $errors = array();
  1750. $this->display_errors = NULL;
  1751. $current_display = $this->current_display;
  1752. foreach ($this->display as $id => $display) {
  1753. if ($display->handler) {
  1754. if (!empty($display->deleted)) {
  1755. continue;
  1756. }
  1757. $result = $this->display[$id]->handler->validate();
  1758. if (!empty($result) && is_array($result)) {
  1759. $errors = array_merge($errors, $result);
  1760. // Mark this display as having validation errors.
  1761. $this->display_errors[$id] = TRUE;
  1762. }
  1763. }
  1764. }
  1765. $this->set_display($current_display);
  1766. return $errors ? $errors : TRUE;
  1767. }
  1768. /**
  1769. * Find and initialize the localizer plugin.
  1770. */
  1771. function init_localization() {
  1772. if (isset($this->localization_plugin) && is_object($this->localization_plugin)) {
  1773. return TRUE;
  1774. }
  1775. $this->localization_plugin = views_get_plugin('localization', views_get_localization_plugin());
  1776. if (empty($this->localization_plugin)) {
  1777. $this->localization_plugin = views_get_plugin('localization', 'none');
  1778. return FALSE;
  1779. }
  1780. /**
  1781. * Figure out whether there should be options.
  1782. */
  1783. $this->localization_plugin->init($this);
  1784. return $this->localization_plugin->translate;
  1785. }
  1786. /**
  1787. * Determine whether a view supports admin string translation.
  1788. */
  1789. function is_translatable() {
  1790. // If the view is normal or overridden, use admin string translation.
  1791. // A newly created view won't have a type. Accept this.
  1792. return (!isset($this->type) || in_array($this->type, array(t('Normal'), t('Overridden')))) ? TRUE : FALSE;
  1793. }
  1794. /**
  1795. * Send strings for localization.
  1796. */
  1797. function save_locale_strings() {
  1798. $this->process_locale_strings('save');
  1799. }
  1800. /**
  1801. * Delete localized strings.
  1802. */
  1803. function delete_locale_strings() {
  1804. $this->process_locale_strings('delete');
  1805. }
  1806. /**
  1807. * Export localized strings.
  1808. */
  1809. function export_locale_strings() {
  1810. $this->process_locale_strings('export');
  1811. }
  1812. /**
  1813. * Process strings for localization, deletion or export to code.
  1814. */
  1815. function process_locale_strings($op) {
  1816. // Ensure this view supports translation, we have a display, and we
  1817. // have a localization plugin.
  1818. // @fixme Export does not init every handler.
  1819. if (($this->is_translatable() || $op == 'export') && $this->init_display() && $this->init_localization()) {
  1820. $this->localization_plugin->process_locale_strings($op);
  1821. }
  1822. }
  1823. }
  1824. /**
  1825. * Base class for views' database objects.
  1826. */
  1827. class views_db_object {
  1828. public $db_table;
  1829. /**
  1830. * Initialize this object, setting values from schema defaults.
  1831. *
  1832. * @param $init
  1833. * If an array, this is a set of values from db_fetch_object to
  1834. * load. Otherwse, if TRUE values will be filled in from schema
  1835. * defaults.
  1836. */
  1837. function init($init = TRUE) {
  1838. if (is_array($init)) {
  1839. return $this->load_row($init);
  1840. }
  1841. if (!$init) {
  1842. return;
  1843. }
  1844. $schema = drupal_get_schema($this->db_table);
  1845. if (!$schema) {
  1846. return;
  1847. }
  1848. // Go through our schema and build correlations.
  1849. foreach ($schema['fields'] as $field => $info) {
  1850. if ($info['type'] == 'serial') {
  1851. $this->$field = NULL;
  1852. }
  1853. if (!isset($this->$field)) {
  1854. if (!empty($info['serialize']) && isset($info['serialized default'])) {
  1855. $this->$field = unserialize($info['serialized default']);
  1856. }
  1857. elseif (isset($info['default'])) {
  1858. $this->$field = $info['default'];
  1859. }
  1860. else {
  1861. $this->$field = '';
  1862. }
  1863. }
  1864. }
  1865. }
  1866. /**
  1867. * Write the row to the database.
  1868. *
  1869. * @param $update
  1870. * If true this will be an UPDATE query. Otherwise it will be an INSERT.
  1871. */
  1872. function save_row($update = NULL) {
  1873. $fields = $defs = $values = $serials = array();
  1874. $schema = drupal_get_schema($this->db_table);
  1875. // Go through our schema and build correlations.
  1876. foreach ($schema['fields'] as $field => $info) {
  1877. // special case -- skip serial types if we are updating.
  1878. if ($info['type'] == 'serial') {
  1879. $serials[] = $field;
  1880. continue;
  1881. }
  1882. elseif ($info['type'] == 'int') {
  1883. $this->$field = (int) $this->$field;
  1884. }
  1885. $fields[$field] = empty($info['serialize']) ? $this->$field : serialize($this->$field);
  1886. }
  1887. if (!$update) {
  1888. $query = db_insert($this->db_table);
  1889. }
  1890. else {
  1891. $query = db_update($this->db_table)
  1892. ->condition($update, $this->$update);
  1893. }
  1894. $return = $query
  1895. ->fields($fields)
  1896. ->execute();
  1897. if ($serials && !$update) {
  1898. // get last insert ids and fill them in.
  1899. // Well, one ID.
  1900. foreach ($serials as $field) {
  1901. $this->$field = $return;
  1902. }
  1903. }
  1904. }
  1905. /**
  1906. * Load the object with a row from the database.
  1907. *
  1908. * This method is separate from the constructor in order to give us
  1909. * more flexibility in terms of how the view object is built in different
  1910. * contexts.
  1911. *
  1912. * @param $data
  1913. * An object from db_fetch_object. It should contain all of the fields
  1914. * that are in the schema.
  1915. */
  1916. function load_row($data) {
  1917. $schema = drupal_get_schema($this->db_table);
  1918. // Go through our schema and build correlations.
  1919. foreach ($schema['fields'] as $field => $info) {
  1920. $this->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
  1921. }
  1922. }
  1923. /**
  1924. * Export a loaded row, such as an argument, field or the view itself to PHP code.
  1925. *
  1926. * @param $identifier
  1927. * The variable to assign the PHP code for this object to.
  1928. * @param $indent
  1929. * An optional indentation for prettifying nested code.
  1930. */
  1931. function export_row($identifier = NULL, $indent = '') {
  1932. ctools_include('export');
  1933. if (!$identifier) {
  1934. $identifier = $this->db_table;
  1935. }
  1936. $schema = drupal_get_schema($this->db_table);
  1937. $output = $indent . '$' . $identifier . ' = new ' . get_class($this) . "();\n";
  1938. // Go through our schema and build correlations.
  1939. foreach ($schema['fields'] as $field => $info) {
  1940. if (!empty($info['no export'])) {
  1941. continue;
  1942. }
  1943. if (!isset($this->$field)) {
  1944. if (isset($info['default'])) {
  1945. $this->$field = $info['default'];
  1946. }
  1947. else {
  1948. $this->$field = '';
  1949. }
  1950. // serialized defaults must be set as serialized.
  1951. if (isset($info['serialize'])) {
  1952. $this->$field = unserialize($this->$field);
  1953. }
  1954. }
  1955. $value = $this->$field;
  1956. if ($info['type'] == 'int') {
  1957. if (isset($info['size']) && $info['size'] == 'tiny') {
  1958. $value = (bool) $value;
  1959. }
  1960. else {
  1961. $value = (int) $value;
  1962. }
  1963. }
  1964. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  1965. }
  1966. return $output;
  1967. }
  1968. /**
  1969. * Add a new display handler to the view, automatically creating an id.
  1970. *
  1971. * @param $type
  1972. * The plugin type from the views plugin data. Defaults to 'page'.
  1973. * @param $title
  1974. * The title of the display; optional, may be filled in from default.
  1975. * @param $id
  1976. * The id to use.
  1977. * @return
  1978. * The key to the display in $view->display, so that the new display
  1979. * can be easily located.
  1980. */
  1981. function add_display($type = 'page', $title = NULL, $id = NULL) {
  1982. if (empty($type)) {
  1983. return FALSE;
  1984. }
  1985. $plugin = views_fetch_plugin_data('display', $type);
  1986. if (empty($plugin)) {
  1987. $plugin['title'] = t('Broken');
  1988. }
  1989. if (empty($id)) {
  1990. $id = $this->generate_display_id($type);
  1991. if ($id !== 'default') {
  1992. preg_match("/[0-9]+/", $id, $count);
  1993. $count = $count[0];
  1994. }
  1995. else {
  1996. $count = '';
  1997. }
  1998. if (empty($title)) {
  1999. if ($count > 1) {
  2000. $title = $plugin['title'] . ' ' . $count;
  2001. }
  2002. else {
  2003. $title = $plugin['title'];
  2004. }
  2005. }
  2006. }
  2007. // Create the new display object
  2008. $display = new views_display;
  2009. $display->options($type, $id, $title);
  2010. // Add the new display object to the view.
  2011. $this->display[$id] = $display;
  2012. return $id;
  2013. }
  2014. /**
  2015. * Generate a display id of a certain plugin type.
  2016. *
  2017. * @param $type
  2018. * Which plugin should be used for the new display id.
  2019. */
  2020. function generate_display_id($type) {
  2021. // 'default' is singular and is unique, so just go with 'default'
  2022. // for it. For all others, start counting.
  2023. if ($type == 'default') {
  2024. return 'default';
  2025. }
  2026. // Initial id.
  2027. $id = $type . '_1';
  2028. $count = 1;
  2029. // Loop through IDs based upon our style plugin name until
  2030. // we find one that is unused.
  2031. while (!empty($this->display[$id])) {
  2032. $id = $type . '_' . ++$count;
  2033. }
  2034. return $id;
  2035. }
  2036. /**
  2037. * Generates a unique ID for an item.
  2038. *
  2039. * These items are typically fields, filters, sort criteria, or arguments.
  2040. *
  2041. * @param $requested_id
  2042. * The requested ID for the item.
  2043. * @param $existing_items
  2044. * An array of existing items, keyed by their IDs.
  2045. *
  2046. * @return
  2047. * A unique ID. This will be equal to $requested_id if no item with that ID
  2048. * already exists. Otherwise, it will be appended with an integer to make
  2049. * it unique, e.g. "{$requested_id}_1", "{$requested_id}_2", etc.
  2050. */
  2051. public static function generate_item_id($requested_id, $existing_items) {
  2052. $count = 0;
  2053. $id = $requested_id;
  2054. while (!empty($existing_items[$id])) {
  2055. $id = $requested_id . '_' . ++$count;
  2056. }
  2057. return $id;
  2058. }
  2059. /**
  2060. * Create a new display and a display handler for it.
  2061. * @param $type
  2062. * The plugin type from the views plugin data. Defaults to 'page'.
  2063. * @param $title
  2064. * The title of the display; optional, may be filled in from default.
  2065. * @param $id
  2066. * The id to use.
  2067. * @return views_plugin_display
  2068. * A reference to the new handler object.
  2069. */
  2070. function &new_display($type = 'page', $title = NULL, $id = NULL) {
  2071. $id = $this->add_display($type, $title, $id);
  2072. // Create a handler
  2073. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  2074. if (empty($this->display[$id]->handler)) {
  2075. // provide a 'default' handler as an emergency. This won't work well but
  2076. // it will keep things from crashing.
  2077. $this->display[$id]->handler = views_get_plugin('display', 'default');
  2078. }
  2079. if (!empty($this->display[$id]->handler)) {
  2080. // Initialize the new display handler with data.
  2081. $this->display[$id]->handler->init($this, $this->display[$id]);
  2082. // If this is NOT the default display handler, let it know which is
  2083. if ($id != 'default') {
  2084. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  2085. }
  2086. }
  2087. return $this->display[$id]->handler;
  2088. }
  2089. /**
  2090. * Add an item with a handler to the view.
  2091. *
  2092. * These items may be fields, filters, sort criteria, or arguments.
  2093. */
  2094. function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
  2095. $types = views_object_types();
  2096. $this->set_display($display_id);
  2097. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2098. if (empty($id)) {
  2099. $id = $this->generate_item_id($field, $fields);
  2100. }
  2101. $new_item = array(
  2102. 'id' => $id,
  2103. 'table' => $table,
  2104. 'field' => $field,
  2105. ) + $options;
  2106. if (!empty($types[$type]['type'])) {
  2107. $handler_type = $types[$type]['type'];
  2108. }
  2109. else {
  2110. $handler_type = $type;
  2111. }
  2112. $handler = views_get_handler($table, $field, $handler_type);
  2113. $fields[$id] = $new_item;
  2114. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2115. return $id;
  2116. }
  2117. /**
  2118. * Get an array of items for the current display.
  2119. */
  2120. function get_items($type, $display_id = NULL) {
  2121. $this->set_display($display_id);
  2122. if (!isset($display_id)) {
  2123. $display_id = $this->current_display;
  2124. }
  2125. // Get info about the types so we can get the right data.
  2126. $types = views_object_types();
  2127. return $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2128. }
  2129. /**
  2130. * Get the configuration of an item (field/sort/filter/etc) on a given
  2131. * display.
  2132. */
  2133. function get_item($display_id, $type, $id) {
  2134. // Get info about the types so we can get the right data.
  2135. $types = views_object_types();
  2136. // Initialize the display
  2137. $this->set_display($display_id);
  2138. // Get the existing configuration
  2139. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2140. return isset($fields[$id]) ? $fields[$id] : NULL;
  2141. }
  2142. /**
  2143. * Set the configuration of an item (field/sort/filter/etc) on a given
  2144. * display.
  2145. *
  2146. * Pass in NULL for the $item to remove an item.
  2147. */
  2148. function set_item($display_id, $type, $id, $item) {
  2149. // Get info about the types so we can get the right data.
  2150. $types = views_object_types();
  2151. // Initialize the display
  2152. $this->set_display($display_id);
  2153. // Get the existing configuration
  2154. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2155. if (isset($item)) {
  2156. $fields[$id] = $item;
  2157. }
  2158. else {
  2159. unset($fields[$id]);
  2160. }
  2161. // Store.
  2162. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2163. }
  2164. /**
  2165. * Set an option on an item.
  2166. *
  2167. * Use this only if you have just 1 or 2 options to set; if you have
  2168. * many, consider getting the item, adding the options and doing
  2169. * set_item yourself.
  2170. */
  2171. function set_item_option($display_id, $type, $id, $option, $value) {
  2172. $item = $this->get_item($display_id, $type, $id);
  2173. $item[$option] = $value;
  2174. $this->set_item($display_id, $type, $id, $item);
  2175. }
  2176. }
  2177. /**
  2178. * A display type in a view.
  2179. *
  2180. * This is just the database storage mechanism, and isn't terribly important
  2181. * to the behavior of the display at all.
  2182. */
  2183. class views_display extends views_db_object {
  2184. /**
  2185. * The display handler itself, which has all the methods.
  2186. *
  2187. * @var views_plugin_display
  2188. */
  2189. var $handler;
  2190. /**
  2191. * Stores all options of the display, like fields, filters etc.
  2192. *
  2193. * @var array
  2194. */
  2195. var $display_options;
  2196. var $db_table = 'views_display';
  2197. function views_display($init = TRUE) {
  2198. parent::init($init);
  2199. }
  2200. function options($type, $id, $title) {
  2201. $this->display_plugin = $type;
  2202. $this->id = $id;
  2203. $this->display_title = $title;
  2204. }
  2205. }
  2206. /**
  2207. * Provide a list of views object types used in a view, with some information
  2208. * about them.
  2209. */
  2210. function views_object_types() {
  2211. static $retval = NULL;
  2212. // statically cache this so t() doesn't run a bajillion times.
  2213. if (!isset($retval)) {
  2214. $retval = array(
  2215. 'field' => array(
  2216. 'title' => t('Fields'), // title
  2217. 'ltitle' => t('fields'), // lowercase title for mid-sentence
  2218. 'stitle' => t('Field'), // singular title
  2219. 'lstitle' => t('field'), // singular lowercase title for mid sentence
  2220. 'plural' => 'fields',
  2221. ),
  2222. 'argument' => array(
  2223. 'title' => t('Contextual filters'),
  2224. 'ltitle' => t('contextual filters'),
  2225. 'stitle' => t('Contextual filter'),
  2226. 'lstitle' => t('contextual filter'),
  2227. 'plural' => 'arguments',
  2228. ),
  2229. 'sort' => array(
  2230. 'title' => t('Sort criteria'),
  2231. 'ltitle' => t('sort criteria'),
  2232. 'stitle' => t('Sort criterion'),
  2233. 'lstitle' => t('sort criterion'),
  2234. 'plural' => 'sorts',
  2235. ),
  2236. 'filter' => array(
  2237. 'title' => t('Filter criteria'),
  2238. 'ltitle' => t('filter criteria'),
  2239. 'stitle' => t('Filter criterion'),
  2240. 'lstitle' => t('filter criterion'),
  2241. 'plural' => 'filters',
  2242. ),
  2243. 'relationship' => array(
  2244. 'title' => t('Relationships'),
  2245. 'ltitle' => t('relationships'),
  2246. 'stitle' => t('Relationship'),
  2247. 'lstitle' => t('Relationship'),
  2248. 'plural' => 'relationships',
  2249. ),
  2250. 'header' => array(
  2251. 'title' => t('Header'),
  2252. 'ltitle' => t('header'),
  2253. 'stitle' => t('Header'),
  2254. 'lstitle' => t('Header'),
  2255. 'plural' => 'header',
  2256. 'type' => 'area',
  2257. ),
  2258. 'footer' => array(
  2259. 'title' => t('Footer'),
  2260. 'ltitle' => t('footer'),
  2261. 'stitle' => t('Footer'),
  2262. 'lstitle' => t('Footer'),
  2263. 'plural' => 'footer',
  2264. 'type' => 'area',
  2265. ),
  2266. 'empty' => array(
  2267. 'title' => t('No results behavior'),
  2268. 'ltitle' => t('no results behavior'),
  2269. 'stitle' => t('No results behavior'),
  2270. 'lstitle' => t('No results behavior'),
  2271. 'plural' => 'empty',
  2272. 'type' => 'area',
  2273. ),
  2274. );
  2275. }
  2276. return $retval;
  2277. }
  2278. /**
  2279. * @}
  2280. */