view.inc

  1. cis7 sites/all/modules/ulmus/views/includes/view.inc
  2. cis7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  3. cle7 sites/all/modules/ulmus/views/includes/view.inc
  4. cle7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  5. ecd7 sites/all/modules/ulmus/views/includes/view.inc
  6. ecd7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  7. elmsmedia7 sites/all/modules/ulmus/views/includes/view.inc
  8. elmsmedia7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  9. harmony7 sites/all/modules/ulmus/views/includes/view.inc
  10. harmony7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  11. icor7 sites/all/modules/ulmus/views/includes/view.inc
  12. icor7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  13. meedjum_blog7 sites/all/modules/ulmus/views/includes/view.inc
  14. meedjum_blog7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc
  15. mooc7 sites/all/modules/ulmus/views/includes/view.inc
  16. mooc7 sites/all/modules/ulmus/ctools/views_content/plugins/contexts/view.inc

Provides the view object type and associated methods.

Functions

Namesort descending Description
views_object_types Provide a list of views object types used in a view, with some information about them.

Classes

Namesort descending Description
view 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.
views_db_object Base class for views' database objects.
views_display A display type in a view.

File

sites/all/modules/ulmus/views/includes/view.inc
View source
  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. // (1) Record the errors before rendering the exposed form widgets.
  813. $errors_before = form_set_error();
  814. $this->exposed_widgets = $exposed_form->render_exposed_form();
  815. // (2) Record the errors after rendering the exposed form widgets.
  816. $errors_after = form_set_error();
  817. // Find out if the validation of any of the elements in the exposed form
  818. // has failed by comparing (1) and (2) above. Don't mess with the view
  819. // otherwise.
  820. $exposed_errors = count($errors_after) > count($errors_before);
  821. if ($exposed_errors || !empty($this->build_info['abort'])) {
  822. $this->built = TRUE;
  823. // Don't execute the query, but rendering will still be executed to display the empty text.
  824. $this->executed = TRUE;
  825. return empty($this->build_info['fail']);
  826. }
  827. }
  828. // Build all the relationships first thing.
  829. $this->_build('relationship');
  830. // Set the filtering groups.
  831. if (!empty($this->filter)) {
  832. $filter_groups = $this->display_handler->get_option('filter_groups');
  833. if ($filter_groups) {
  834. $this->query->set_group_operator($filter_groups['operator']);
  835. foreach($filter_groups['groups'] as $id => $operator) {
  836. $this->query->set_where_group($operator, $id);
  837. }
  838. }
  839. }
  840. // Build all the filters.
  841. $this->_build('filter');
  842. $this->build_sort = TRUE;
  843. // Arguments can, in fact, cause this whole thing to abort.
  844. if (!$this->_build_arguments()) {
  845. $this->build_time = microtime(TRUE) - $start;
  846. $this->attach_displays();
  847. return $this->built;
  848. }
  849. // Initialize the style; arguments may have changed which style we use,
  850. // so waiting as long as possible is important. But we need to know
  851. // about the style when we go to build fields.
  852. if (!$this->init_style()) {
  853. $this->build_info['fail'] = TRUE;
  854. return FALSE;
  855. }
  856. if ($this->style_plugin->uses_fields()) {
  857. $this->_build('field');
  858. }
  859. // Build our sort criteria if we were instructed to do so.
  860. if (!empty($this->build_sort)) {
  861. // Allow the style handler to deal with sorting.
  862. if ($this->style_plugin->build_sort()) {
  863. $this->_build('sort');
  864. }
  865. // allow the plugin to build second sorts as well.
  866. $this->style_plugin->build_sort_post();
  867. }
  868. // Allow area handlers to affect the query.
  869. $this->_build('header');
  870. $this->_build('footer');
  871. $this->_build('empty');
  872. // Allow display handler to affect the query:
  873. $this->display_handler->query($this->display_handler->use_group_by());
  874. // Allow style handler to affect the query:
  875. $this->style_plugin->query($this->display_handler->use_group_by());
  876. // Allow exposed form to affect the query:
  877. if (isset($exposed_form)) {
  878. $exposed_form->query();
  879. }
  880. if (variable_get('views_sql_signature', FALSE)) {
  881. $this->query->add_signature($this);
  882. }
  883. // Let modules modify the query just prior to finalizing it.
  884. $this->query->alter($this);
  885. // Only build the query if we weren't interrupted.
  886. if (empty($this->built)) {
  887. // Build the necessary info to execute the query.
  888. $this->query->build($this);
  889. }
  890. $this->built = TRUE;
  891. $this->build_time = microtime(TRUE) - $start;
  892. // Attach displays
  893. $this->attach_displays();
  894. // Let modules modify the view just after building it.
  895. foreach (module_implements('views_post_build') as $module) {
  896. $function = $module . '_views_post_build';
  897. $function($this);
  898. }
  899. return TRUE;
  900. }
  901. /**
  902. * Internal method to build an individual set of handlers.
  903. *
  904. * @param string $key
  905. * The type of handlers (filter etc.) which should be iterated over to
  906. * build the relationship and query information.
  907. */
  908. function _build($key) {
  909. $handlers = &$this->$key;
  910. foreach ($handlers as $id => $data) {
  911. if (!empty($handlers[$id]) && is_object($handlers[$id])) {
  912. $multiple_exposed_input = array(0 => NULL);
  913. if ($handlers[$id]->multiple_exposed_input()) {
  914. $multiple_exposed_input = $handlers[$id]->group_multiple_exposed_input($this->exposed_data);
  915. }
  916. foreach ($multiple_exposed_input as $group_id) {
  917. // Give this handler access to the exposed filter input.
  918. if (!empty($this->exposed_data)) {
  919. $converted = FALSE;
  920. if ($handlers[$id]->is_a_group()) {
  921. $converted = $handlers[$id]->convert_exposed_input($this->exposed_data, $group_id);
  922. $handlers[$id]->store_group_input($this->exposed_data, $converted);
  923. if (!$converted) {
  924. continue;
  925. }
  926. }
  927. $rc = $handlers[$id]->accept_exposed_input($this->exposed_data);
  928. $handlers[$id]->store_exposed_input($this->exposed_data, $rc);
  929. if (!$rc) {
  930. continue;
  931. }
  932. }
  933. $handlers[$id]->set_relationship();
  934. $handlers[$id]->query($this->display_handler->use_group_by());
  935. }
  936. }
  937. }
  938. }
  939. /**
  940. * Execute the view's query.
  941. *
  942. * @param string $display_id
  943. * The machine name of the display, which should be executed.
  944. *
  945. * @return bool
  946. * Return whether the executing was successful, for example an argument
  947. * could stop the process.
  948. */
  949. function execute($display_id = NULL) {
  950. if (empty($this->built)) {
  951. if (!$this->build($display_id)) {
  952. return FALSE;
  953. }
  954. }
  955. if (!empty($this->executed)) {
  956. return TRUE;
  957. }
  958. // Don't allow to use deactivated displays, but display them on the live preview.
  959. if (!$this->display[$this->current_display]->handler->get_option('enabled') && empty($this->live_preview)) {
  960. $this->build_info['fail'] = TRUE;
  961. return FALSE;
  962. }
  963. // Let modules modify the view just prior to executing it.
  964. foreach (module_implements('views_pre_execute') as $module) {
  965. $function = $module . '_views_pre_execute';
  966. $function($this);
  967. }
  968. // Check for already-cached results.
  969. if (!empty($this->live_preview)) {
  970. $cache = FALSE;
  971. }
  972. else {
  973. $cache = $this->display_handler->get_plugin('cache');
  974. }
  975. if ($cache && $cache->cache_get('results')) {
  976. if($this->query->pager->use_pager()) {
  977. $this->query->pager->total_items = $this->total_rows;
  978. $this->query->pager->update_page_info();
  979. }
  980. vpr('Used cached results');
  981. }
  982. else {
  983. $this->query->execute($this);
  984. // Enforce the array key rule as documented in
  985. // views_plugin_query::execute().
  986. $this->result = array_values($this->result);
  987. $this->_post_execute();
  988. if ($cache) {
  989. $cache->cache_set('results');
  990. }
  991. }
  992. // Let modules modify the view just after executing it.
  993. foreach (module_implements('views_post_execute') as $module) {
  994. $function = $module . '_views_post_execute';
  995. $function($this);
  996. }
  997. $this->executed = TRUE;
  998. }
  999. /**
  1000. * Render this view for a certain display.
  1001. *
  1002. * Note: You should better use just the preview function if you want to
  1003. * render a view.
  1004. *
  1005. * @param string $display_id
  1006. * The machine name of the display, which should be rendered.
  1007. *
  1008. * @return (string|NULL)
  1009. * Return the output of the rendered view or NULL if something failed in the process.
  1010. */
  1011. function render($display_id = NULL) {
  1012. $this->execute($display_id);
  1013. // Check to see if the build failed.
  1014. if (!empty($this->build_info['fail'])) {
  1015. return;
  1016. }
  1017. if (!empty($this->view->build_info['denied'])) {
  1018. return;
  1019. }
  1020. drupal_theme_initialize();
  1021. $start = microtime(TRUE);
  1022. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1023. $this->start_query_capture();
  1024. }
  1025. $exposed_form = $this->display_handler->get_plugin('exposed_form');
  1026. $exposed_form->pre_render($this->result);
  1027. // Check for already-cached output.
  1028. if (!empty($this->live_preview)) {
  1029. $cache = FALSE;
  1030. }
  1031. else {
  1032. $cache = $this->display_handler->get_plugin('cache');
  1033. }
  1034. if ($cache && $cache->cache_get('output')) {
  1035. }
  1036. else {
  1037. if ($cache) {
  1038. $cache->cache_start();
  1039. }
  1040. // Run pre_render for the pager as it might change the result.
  1041. if (!empty($this->query->pager)) {
  1042. $this->query->pager->pre_render($this->result);
  1043. }
  1044. // Initialize the style plugin.
  1045. $this->init_style();
  1046. // Give field handlers the opportunity to perform additional queries
  1047. // using the entire resultset prior to rendering.
  1048. if ($this->style_plugin->uses_fields()) {
  1049. foreach ($this->field as $id => $handler) {
  1050. if (!empty($this->field[$id])) {
  1051. $this->field[$id]->pre_render($this->result);
  1052. }
  1053. }
  1054. }
  1055. $this->style_plugin->pre_render($this->result);
  1056. // Let modules modify the view just prior to rendering it.
  1057. foreach (module_implements('views_pre_render') as $module) {
  1058. $function = $module . '_views_pre_render';
  1059. $function($this);
  1060. }
  1061. // Let the themes play too, because pre render is a very themey thing.
  1062. foreach ($GLOBALS['base_theme_info'] as $base) {
  1063. $function = $base->name . '_views_pre_render';
  1064. if (function_exists($function)) {
  1065. $function($this);
  1066. }
  1067. }
  1068. $function = $GLOBALS['theme'] . '_views_pre_render';
  1069. if (function_exists($function)) {
  1070. $function($this);
  1071. }
  1072. $this->display_handler->output = $this->display_handler->render();
  1073. if ($cache) {
  1074. $cache->cache_set('output');
  1075. }
  1076. }
  1077. $this->render_time = microtime(TRUE) - $start;
  1078. $exposed_form->post_render($this->display_handler->output);
  1079. if ($cache) {
  1080. $cache->post_render($this->display_handler->output);
  1081. }
  1082. // Let modules modify the view output after it is rendered.
  1083. foreach (module_implements('views_post_render') as $module) {
  1084. $function = $module . '_views_post_render';
  1085. $function($this, $this->display_handler->output, $cache);
  1086. }
  1087. // Let the themes play too, because post render is a very themey thing.
  1088. foreach ($GLOBALS['base_theme_info'] as $base) {
  1089. $function = $base->name . '_views_post_render';
  1090. if (function_exists($function)) {
  1091. $function($this);
  1092. }
  1093. }
  1094. $function = $GLOBALS['theme'] . '_views_post_render';
  1095. if (function_exists($function)) {
  1096. $function($this, $this->display_handler->output, $cache);
  1097. }
  1098. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1099. $this->end_query_capture();
  1100. }
  1101. return $this->display_handler->output;
  1102. }
  1103. /**
  1104. * Render a specific field via the field ID and the row #
  1105. *
  1106. * Note: You might want to use views_plugin_style::render_fields as it
  1107. * caches the output for you.
  1108. *
  1109. * @param string $field
  1110. * The id of the field to be rendered.
  1111. *
  1112. * @param int $row
  1113. * The row number in the $view->result which is used for the rendering.
  1114. *
  1115. * @return string
  1116. * The rendered output of the field.
  1117. */
  1118. function render_field($field, $row) {
  1119. if (isset($this->field[$field]) && isset($this->result[$row])) {
  1120. return $this->field[$field]->advanced_render($this->result[$row]);
  1121. }
  1122. }
  1123. /**
  1124. * Execute the given display, with the given arguments.
  1125. * To be called externally by whatever mechanism invokes the view,
  1126. * such as a page callback, hook_block, etc.
  1127. *
  1128. * This function should NOT be used by anything external as this
  1129. * returns data in the format specified by the display. It can also
  1130. * have other side effects that are only intended for the 'proper'
  1131. * use of the display, such as setting page titles and breadcrumbs.
  1132. *
  1133. * If you simply want to view the display, use view::preview() instead.
  1134. */
  1135. function execute_display($display_id = NULL, $args = array()) {
  1136. if (empty($this->current_display) || $this->current_display != $this->choose_display($display_id)) {
  1137. if (!$this->set_display($display_id)) {
  1138. return FALSE;
  1139. }
  1140. }
  1141. $this->pre_execute($args);
  1142. // Execute the view
  1143. $output = $this->display_handler->execute();
  1144. $this->post_execute();
  1145. return $output;
  1146. }
  1147. /**
  1148. * Preview the given display, with the given arguments.
  1149. *
  1150. * To be called externally, probably by an AJAX handler of some flavor.
  1151. * Can also be called when views are embedded, as this guarantees
  1152. * normalized output.
  1153. */
  1154. function preview($display_id = NULL, $args = array()) {
  1155. if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) {
  1156. if (!$this->set_display($display_id)) {
  1157. return FALSE;
  1158. }
  1159. }
  1160. $this->preview = TRUE;
  1161. $this->pre_execute($args);
  1162. // Preview the view.
  1163. $output = $this->display_handler->preview();
  1164. $this->post_execute();
  1165. return $output;
  1166. }
  1167. /**
  1168. * Run attachments and let the display do what it needs to do prior
  1169. * to running.
  1170. */
  1171. function pre_execute($args = array()) {
  1172. $this->old_view[] = views_get_current_view();
  1173. views_set_current_view($this);
  1174. $display_id = $this->current_display;
  1175. // Prepare the view with the information we have, but only if we were
  1176. // passed arguments, as they may have been set previously.
  1177. if ($args) {
  1178. $this->set_arguments($args);
  1179. }
  1180. // Let modules modify the view just prior to executing it.
  1181. foreach (module_implements('views_pre_view') as $module) {
  1182. $function = $module . '_views_pre_view';
  1183. $function($this, $display_id, $this->args);
  1184. }
  1185. // Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
  1186. $this->dom_id = !empty($this->dom_id) ? $this->dom_id : md5($this->name . REQUEST_TIME . rand());
  1187. // Allow the display handler to set up for execution
  1188. $this->display_handler->pre_execute();
  1189. }
  1190. /**
  1191. * Unset the current view, mostly.
  1192. */
  1193. function post_execute() {
  1194. // unset current view so we can be properly destructed later on.
  1195. // Return the previous value in case we're an attachment.
  1196. if ($this->old_view) {
  1197. $old_view = array_pop($this->old_view);
  1198. }
  1199. views_set_current_view(isset($old_view) ? $old_view : FALSE);
  1200. }
  1201. /**
  1202. * Run attachment displays for the view.
  1203. */
  1204. function attach_displays() {
  1205. if (!empty($this->is_attachment)) {
  1206. return;
  1207. }
  1208. if (!$this->display_handler->accept_attachments()) {
  1209. return;
  1210. }
  1211. $this->is_attachment = TRUE;
  1212. // Give other displays an opportunity to attach to the view.
  1213. foreach ($this->display as $id => $display) {
  1214. if (!empty($this->display[$id]->handler)) {
  1215. $this->display[$id]->handler->attach_to($this->current_display);
  1216. }
  1217. }
  1218. $this->is_attachment = FALSE;
  1219. }
  1220. /**
  1221. * Called to get hook_menu() information from the view and the named display handler.
  1222. *
  1223. * @param $display_id
  1224. * A display id.
  1225. * @param $callbacks
  1226. * A menu callback array passed from views_menu_alter().
  1227. */
  1228. function execute_hook_menu($display_id = NULL, &$callbacks = array()) {
  1229. // Prepare the view with the information we have.
  1230. // This was probably already called, but it's good to be safe.
  1231. if (!$this->set_display($display_id)) {
  1232. return FALSE;
  1233. }
  1234. // Execute the view
  1235. if (isset($this->display_handler)) {
  1236. return $this->display_handler->execute_hook_menu($callbacks);
  1237. }
  1238. }
  1239. /**
  1240. * Called to get hook_block information from the view and the
  1241. * named display handler.
  1242. */
  1243. function execute_hook_block_list($display_id = NULL) {
  1244. // Prepare the view with the information we have.
  1245. // This was probably already called, but it's good to be safe.
  1246. if (!$this->set_display($display_id)) {
  1247. return FALSE;
  1248. }
  1249. // Execute the view
  1250. if (isset($this->display_handler)) {
  1251. return $this->display_handler->execute_hook_block_list();
  1252. }
  1253. }
  1254. /**
  1255. * Determine if the given user has access to the view. Note that
  1256. * this sets the display handler if it hasn't been.
  1257. */
  1258. function access($displays = NULL, $account = NULL) {
  1259. // Noone should have access to disabled views.
  1260. if (!empty($this->disabled)) {
  1261. return FALSE;
  1262. }
  1263. if (!isset($this->current_display)) {
  1264. $this->init_display();
  1265. }
  1266. if (!$account) {
  1267. $account = $GLOBALS['user'];
  1268. }
  1269. // We can't use choose_display() here because that function
  1270. // calls this one.
  1271. $displays = (array)$displays;
  1272. foreach ($displays as $display_id) {
  1273. if (!empty($this->display[$display_id]->handler)) {
  1274. if ($this->display[$display_id]->handler->access($account)) {
  1275. return TRUE;
  1276. }
  1277. }
  1278. }
  1279. return FALSE;
  1280. }
  1281. /**
  1282. * Get the view's current title. This can change depending upon how it
  1283. * was built.
  1284. */
  1285. function get_title() {
  1286. if (empty($this->display_handler)) {
  1287. if (!$this->set_display('default')) {
  1288. return FALSE;
  1289. }
  1290. }
  1291. // During building, we might find a title override. If so, use it.
  1292. if (!empty($this->build_info['title'])) {
  1293. $title = $this->build_info['title'];
  1294. }
  1295. else {
  1296. $title = $this->display_handler->get_option('title');
  1297. }
  1298. // Allow substitutions from the first row.
  1299. if ($this->init_style()) {
  1300. $title = $this->style_plugin->tokenize_value($title, 0);
  1301. }
  1302. return $title;
  1303. }
  1304. /**
  1305. * Override the view's current title.
  1306. *
  1307. * The tokens in the title get's replaced before rendering.
  1308. */
  1309. function set_title($title) {
  1310. $this->build_info['title'] = $title;
  1311. return TRUE;
  1312. }
  1313. /**
  1314. * Return the human readable name for a view.
  1315. *
  1316. * When a certain view doesn't have a human readable name return the machine readable name.
  1317. */
  1318. function get_human_name() {
  1319. if (!empty($this->human_name)) {
  1320. $human_name = $this->human_name;
  1321. }
  1322. else {
  1323. $human_name = $this->name;
  1324. }
  1325. return $human_name;
  1326. }
  1327. /**
  1328. * Force the view to build a title.
  1329. */
  1330. function build_title() {
  1331. $this->init_display();
  1332. if (empty($this->built)) {
  1333. $this->init_query();
  1334. }
  1335. $this->init_handlers();
  1336. $this->_build_arguments();
  1337. }
  1338. /**
  1339. * Get the URL for the current view.
  1340. *
  1341. * This URL will be adjusted for arguments.
  1342. */
  1343. function get_url($args = NULL, $path = NULL) {
  1344. if (!empty($this->override_url)) {
  1345. return $this->override_url;
  1346. }
  1347. if (!isset($path)) {
  1348. $path = $this->get_path();
  1349. }
  1350. if (!isset($args)) {
  1351. $args = $this->args;
  1352. // Exclude arguments that were computed, not passed on the URL.
  1353. $position = 0;
  1354. if (!empty($this->argument)) {
  1355. foreach ($this->argument as $argument_id => $argument) {
  1356. if (!empty($argument->is_default) && !empty($argument->options['default_argument_skip_url'])) {
  1357. unset($args[$position]);
  1358. }
  1359. $position++;
  1360. }
  1361. }
  1362. }
  1363. // Don't bother working if there's nothing to do:
  1364. if (empty($path) || (empty($args) && strpos($path, '%') === FALSE)) {
  1365. return $path;
  1366. }
  1367. $pieces = array();
  1368. $argument_keys = isset($this->argument) ? array_keys($this->argument) : array();
  1369. $id = current($argument_keys);
  1370. foreach (explode('/', $path) as $piece) {
  1371. if ($piece != '%') {
  1372. $pieces[] = $piece;
  1373. }
  1374. else {
  1375. if (empty($args)) {
  1376. // Try to never put % in a url; use the wildcard instead.
  1377. if ($id && !empty($this->argument[$id]->options['exception']['value'])) {
  1378. $pieces[] = $this->argument[$id]->options['exception']['value'];
  1379. }
  1380. else {
  1381. $pieces[] = '*'; // gotta put something if there just isn't one.
  1382. }
  1383. }
  1384. else {
  1385. $pieces[] = array_shift($args);
  1386. }
  1387. if ($id) {
  1388. $id = next($argument_keys);
  1389. }
  1390. }
  1391. }
  1392. if (!empty($args)) {
  1393. $pieces = array_merge($pieces, $args);
  1394. }
  1395. return implode('/', $pieces);
  1396. }
  1397. /**
  1398. * Get the base path used for this view.
  1399. */
  1400. function get_path() {
  1401. if (!empty($this->override_path)) {
  1402. return $this->override_path;
  1403. }
  1404. if (empty($this->display_handler)) {
  1405. if (!$this->set_display('default')) {
  1406. return FALSE;
  1407. }
  1408. }
  1409. return $this->display_handler->get_path();
  1410. }
  1411. /**
  1412. * Get the breadcrumb used for this view.
  1413. *
  1414. * @param $set
  1415. * If true, use drupal_set_breadcrumb() to install the breadcrumb.
  1416. */
  1417. function get_breadcrumb($set = FALSE) {
  1418. // Now that we've built the view, extract the breadcrumb.
  1419. $base = TRUE;
  1420. $breadcrumb = array();
  1421. if (!empty($this->build_info['breadcrumb'])) {
  1422. foreach ($this->build_info['breadcrumb'] as $path => $title) {
  1423. // Check to see if the frontpage is in the breadcrumb trail; if it
  1424. // is, we'll remove that from the actual breadcrumb later.
  1425. if ($path == variable_get('site_frontpage', 'node')) {
  1426. $base = FALSE;
  1427. $title = t('Home');
  1428. }
  1429. if ($title) {
  1430. $breadcrumb[] = l($title, $path, array('html' => TRUE));
  1431. }
  1432. }
  1433. if ($set) {
  1434. if ($base) {
  1435. $breadcrumb = array_merge(drupal_get_breadcrumb(), $breadcrumb);
  1436. }
  1437. drupal_set_breadcrumb($breadcrumb);
  1438. }
  1439. }
  1440. return $breadcrumb;
  1441. }
  1442. /**
  1443. * Is this view cacheable?
  1444. */
  1445. function is_cacheable() {
  1446. return $this->is_cacheable;
  1447. }
  1448. /**
  1449. * Set up query capturing.
  1450. *
  1451. * db_query() stores the queries that it runs in global $queries,
  1452. * bit only if dev_query is set to true. In this case, we want
  1453. * to temporarily override that setting if it's not and we
  1454. * can do that without forcing a db rewrite by just manipulating
  1455. * $conf. This is kind of evil but it works.
  1456. */
  1457. function start_query_capture() {
  1458. global $conf, $queries;
  1459. if (empty($conf['dev_query'])) {
  1460. $this->fix_dev_query = TRUE;
  1461. $conf['dev_query'] = TRUE;
  1462. }
  1463. // Record the last query key used; anything already run isn't
  1464. // a query that we are interested in.
  1465. $this->last_query_key = NULL;
  1466. if (!empty($queries)) {
  1467. $keys = array_keys($queries);
  1468. $this->last_query_key = array_pop($keys);
  1469. }
  1470. }
  1471. /**
  1472. * Add the list of queries run during render to buildinfo.
  1473. *
  1474. * @see view::start_query_capture()
  1475. */
  1476. function end_query_capture() {
  1477. global $conf, $queries;
  1478. if (!empty($this->fix_dev_query)) {
  1479. $conf['dev_query'] = FALSE;
  1480. }
  1481. // make a copy of the array so we can manipulate it with array_splice.
  1482. $temp = $queries;
  1483. // Scroll through the queries until we get to our last query key.
  1484. // Unset anything in our temp array.
  1485. if (isset($this->last_query_key)) {
  1486. while (list($id, $query) = each($queries)) {
  1487. if ($id == $this->last_query_key) {
  1488. break;
  1489. }
  1490. unset($temp[$id]);
  1491. }
  1492. }
  1493. $this->additional_queries = $temp;
  1494. }
  1495. /**
  1496. * Static factory method to load a list of views based upon a $where clause.
  1497. *
  1498. * Although this method could be implemented to simply iterate over views::load(),
  1499. * that would be very slow. Buiding the views externally from unified queries is
  1500. * much faster.
  1501. */
  1502. static function load_views() {
  1503. $result = db_query("SELECT DISTINCT v.* FROM {views_view} v");
  1504. $views = array();
  1505. // Load all the views.
  1506. foreach ($result as $data) {
  1507. $view = new view;
  1508. $view->load_row($data);
  1509. $view->loaded = TRUE;
  1510. $view->type = t('Normal');
  1511. $views[$view->name] = $view;
  1512. $names[$view->vid] = $view->name;
  1513. }
  1514. // Stop if we didn't get any views.
  1515. if (!$views) {
  1516. return array();
  1517. }
  1518. // Now load all the subtables:
  1519. foreach (view::db_objects() as $key) {
  1520. $object_name = "views_$key";
  1521. $result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN (:vids) ORDER BY vid, position",
  1522. array(':vids' => array_keys($names)));
  1523. foreach ($result as $data) {
  1524. $object = new $object_name(FALSE);
  1525. $object->load_row($data);
  1526. // Because it can get complicated with this much indirection,
  1527. // make a shortcut reference.
  1528. $location = &$views[$names[$object->vid]]->$key;
  1529. // If we have a basic id field, load the item onto the view based on
  1530. // this ID, otherwise push it on.
  1531. if (!empty($object->id)) {
  1532. $location[$object->id] = $object;
  1533. }
  1534. else {
  1535. $location[] = $object;
  1536. }
  1537. }
  1538. }
  1539. return $views;
  1540. }
  1541. /**
  1542. * Save the view to the database. If the view does not already exist,
  1543. * A vid will be assigned to the view and also returned from this function.
  1544. */
  1545. function save() {
  1546. if ($this->vid == 'new') {
  1547. $this->vid = NULL;
  1548. }
  1549. // If there is no vid, check if a view with this machine name already exists.
  1550. elseif (empty($this->vid)) {
  1551. $vid = db_query("SELECT vid from {views_view} WHERE name = :name", array(':name' => $this->name))->fetchField();
  1552. $this->vid = $vid ? $vid : NULL;
  1553. }
  1554. $transaction = db_transaction();
  1555. try {
  1556. // If we have no vid or our vid is a string, this is a new view.
  1557. if (!empty($this->vid)) {
  1558. // remove existing table entries
  1559. foreach ($this->db_objects() as $key) {
  1560. db_delete('views_' . $key)
  1561. ->condition('vid', $this->vid)
  1562. ->execute();
  1563. }
  1564. }
  1565. $this->save_row(!empty($this->vid) ? 'vid' : FALSE);
  1566. // Save all of our subtables.
  1567. foreach ($this->db_objects() as $key) {
  1568. $this->_save_rows($key);
  1569. }
  1570. }
  1571. catch (Exception $e) {
  1572. $transaction->rollback();
  1573. watchdog_exception('views', $e);
  1574. throw $e;
  1575. }
  1576. $this->save_locale_strings();
  1577. // Clear caches.
  1578. views_invalidate_cache();
  1579. }
  1580. /**
  1581. * Save a row to the database for the given key, which is one of the
  1582. * keys from view::db_objects()
  1583. */
  1584. function _save_rows($key) {
  1585. $count = 0;
  1586. foreach ($this->$key as $position => $object) {
  1587. $object->position = ++$count;
  1588. $object->vid = $this->vid;
  1589. $object->save_row();
  1590. }
  1591. }
  1592. /**
  1593. * Delete the view from the database.
  1594. */
  1595. function delete($clear = TRUE) {
  1596. if (empty($this->vid)) {
  1597. return;
  1598. }
  1599. db_delete('views_view')
  1600. ->condition('vid', $this->vid)
  1601. ->execute();
  1602. // Delete from all of our subtables as well.
  1603. foreach ($this->db_objects() as $key) {
  1604. db_delete('views_'. $key)
  1605. ->condition('vid', $this->vid)
  1606. ->execute();
  1607. }
  1608. cache_clear_all('views_query:' . $this->name, 'cache_views');
  1609. if ($clear) {
  1610. // Clear caches.
  1611. views_invalidate_cache();
  1612. }
  1613. }
  1614. /**
  1615. * Export a view as PHP code.
  1616. */
  1617. function export($indent = '') {
  1618. $this->init_display();
  1619. $this->init_query();
  1620. $output = '';
  1621. $output .= $this->export_row('view', $indent);
  1622. // Set the API version
  1623. $output .= $indent . '$view->api_version = \'' . views_api_version() . "';\n";
  1624. $output .= $indent . '$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */' . "\n";
  1625. foreach ($this->display as $id => $display) {
  1626. $output .= "\n" . $indent . "/* Display: $display->display_title */\n";
  1627. $output .= $indent . '$handler = $view->new_display(' . ctools_var_export($display->display_plugin, $indent) . ', ' . ctools_var_export($display->display_title, $indent) . ', \'' . $id . "');\n";
  1628. if (empty($display->handler)) {
  1629. // @todo -- probably need a method of exporting broken displays as
  1630. // they may simply be broken because a module is not installed. That
  1631. // does not invalidate the display.
  1632. continue;
  1633. }
  1634. $output .= $display->handler->export_options($indent, '$handler->options');
  1635. }
  1636. // Give the localization system a chance to export translatables to code.
  1637. if ($this->init_localization()) {
  1638. $this->export_locale_strings('export');
  1639. $translatables = $this->localization_plugin->export_render($indent);
  1640. if (!empty($translatables)) {
  1641. $output .= $translatables;
  1642. }
  1643. }
  1644. return $output;
  1645. }
  1646. /**
  1647. * Make a copy of this view that has been sanitized of all database IDs
  1648. * and handlers and other stuff.
  1649. *
  1650. * I'd call this clone() but it's reserved.
  1651. */
  1652. function copy() {
  1653. $code = $this->export();
  1654. eval($code);
  1655. return $view;
  1656. }
  1657. /**
  1658. * Safely clone a view.
  1659. *
  1660. * Because views are complicated objects within objects, and PHP loves to
  1661. * do references to everything, if a View is not properly and safely
  1662. * cloned it will still have references to the original view, and can
  1663. * actually cause the original view to point to objects in the cloned
  1664. * view. This gets ugly fast.
  1665. *
  1666. * This will completely wipe a view clean so it can be considered fresh.
  1667. *
  1668. * @return view
  1669. * The cloned view.
  1670. */
  1671. function clone_view() {
  1672. $clone = version_compare(phpversion(), '5.0') < 0 ? $this : clone($this);
  1673. $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');
  1674. foreach ($keys as $key) {
  1675. if (isset($clone->$key)) {
  1676. unset($clone->$key);
  1677. }
  1678. }
  1679. $clone->built = $clone->executed = FALSE;
  1680. $clone->build_info = array();
  1681. $clone->attachment_before = '';
  1682. $clone->attachment_after = '';
  1683. $clone->result = array();
  1684. // shallow cloning means that all the display objects
  1685. // *were not cloned*. We must clone them ourselves.
  1686. $displays = array();
  1687. foreach ($clone->display as $id => $display) {
  1688. $displays[$id] = clone $display;
  1689. if (isset($displays[$id]->handler)) {
  1690. unset($displays[$id]->handler);
  1691. }
  1692. }
  1693. $clone->display = $displays;
  1694. return $clone;
  1695. }
  1696. /**
  1697. * Unset references so that a $view object may be properly garbage
  1698. * collected.
  1699. */
  1700. function destroy() {
  1701. foreach (array_keys($this->display) as $display_id) {
  1702. if (isset($this->display[$display_id]->handler)) {
  1703. $this->display[$display_id]->handler->destroy();
  1704. unset($this->display[$display_id]->handler);
  1705. }
  1706. }
  1707. foreach (views_object_types() as $type => $info) {
  1708. if (isset($this->$type)) {
  1709. $handlers = &$this->$type;
  1710. foreach ($handlers as $id => $item) {
  1711. $handlers[$id]->destroy();
  1712. }
  1713. unset($handlers);
  1714. }
  1715. }
  1716. if (isset($this->style_plugin)) {
  1717. $this->style_plugin->destroy();
  1718. unset($this->style_plugin);
  1719. }
  1720. // Clear these to make sure the view can be processed/used again.
  1721. if (isset($this->display_handler)) {
  1722. unset($this->display_handler);
  1723. }
  1724. if (isset($this->current_display)) {
  1725. unset($this->current_display);
  1726. }
  1727. if (isset($this->query)) {
  1728. unset($this->query);
  1729. }
  1730. $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');
  1731. foreach ($keys as $key) {
  1732. if (isset($this->$key)) {
  1733. unset($this->$key);
  1734. }
  1735. }
  1736. // These keys are checked by the next init, so instead of unsetting them,
  1737. // just set the default values.
  1738. $keys = array('items_per_page', 'offset', 'current_page');
  1739. foreach ($keys as $key) {
  1740. if (isset($this->$key)) {
  1741. $this->$key = NULL;
  1742. }
  1743. }
  1744. $this->built = $this->executed = FALSE;
  1745. $this->build_info = array();
  1746. $this->attachment_before = '';
  1747. $this->attachment_after = '';
  1748. }
  1749. /**
  1750. * Make sure the view is completely valid.
  1751. *
  1752. * @return
  1753. * TRUE if the view is valid; an array of error strings if it is not.
  1754. */
  1755. function validate() {
  1756. $this->init_display();
  1757. $errors = array();
  1758. $this->display_errors = NULL;
  1759. $current_display = $this->current_display;
  1760. foreach ($this->display as $id => $display) {
  1761. if ($display->handler) {
  1762. if (!empty($display->deleted)) {
  1763. continue;
  1764. }
  1765. $result = $this->display[$id]->handler->validate();
  1766. if (!empty($result) && is_array($result)) {
  1767. $errors = array_merge($errors, $result);
  1768. // Mark this display as having validation errors.
  1769. $this->display_errors[$id] = TRUE;
  1770. }
  1771. }
  1772. }
  1773. $this->set_display($current_display);
  1774. return $errors ? $errors : TRUE;
  1775. }
  1776. /**
  1777. * Find and initialize the localizer plugin.
  1778. */
  1779. function init_localization() {
  1780. if (isset($this->localization_plugin) && is_object($this->localization_plugin)) {
  1781. return TRUE;
  1782. }
  1783. $this->localization_plugin = views_get_plugin('localization', views_get_localization_plugin());
  1784. if (empty($this->localization_plugin)) {
  1785. $this->localization_plugin = views_get_plugin('localization', 'none');
  1786. return FALSE;
  1787. }
  1788. /**
  1789. * Figure out whether there should be options.
  1790. */
  1791. $this->localization_plugin->init($this);
  1792. return $this->localization_plugin->translate;
  1793. }
  1794. /**
  1795. * Determine whether a view supports admin string translation.
  1796. */
  1797. function is_translatable() {
  1798. // If the view is normal or overridden, use admin string translation.
  1799. // A newly created view won't have a type. Accept this.
  1800. return (!isset($this->type) || in_array($this->type, array(t('Normal'), t('Overridden')))) ? TRUE : FALSE;
  1801. }
  1802. /**
  1803. * Send strings for localization.
  1804. */
  1805. function save_locale_strings() {
  1806. $this->process_locale_strings('save');
  1807. }
  1808. /**
  1809. * Delete localized strings.
  1810. */
  1811. function delete_locale_strings() {
  1812. $this->process_locale_strings('delete');
  1813. }
  1814. /**
  1815. * Export localized strings.
  1816. */
  1817. function export_locale_strings() {
  1818. $this->process_locale_strings('export');
  1819. }
  1820. /**
  1821. * Process strings for localization, deletion or export to code.
  1822. */
  1823. function process_locale_strings($op) {
  1824. // Ensure this view supports translation, we have a display, and we
  1825. // have a localization plugin.
  1826. // @fixme Export does not init every handler.
  1827. if (($this->is_translatable() || $op == 'export') && $this->init_display() && $this->init_localization()) {
  1828. $this->localization_plugin->process_locale_strings($op);
  1829. }
  1830. }
  1831. }
  1832. /**
  1833. * Base class for views' database objects.
  1834. */
  1835. class views_db_object {
  1836. public $db_table;
  1837. /**
  1838. * Initialize this object, setting values from schema defaults.
  1839. *
  1840. * @param $init
  1841. * If an array, this is a set of values from db_fetch_object to
  1842. * load. Otherwse, if TRUE values will be filled in from schema
  1843. * defaults.
  1844. */
  1845. function init($init = TRUE) {
  1846. if (is_array($init)) {
  1847. return $this->load_row($init);
  1848. }
  1849. if (!$init) {
  1850. return;
  1851. }
  1852. $schema = drupal_get_schema($this->db_table);
  1853. if (!$schema) {
  1854. return;
  1855. }
  1856. // Go through our schema and build correlations.
  1857. foreach ($schema['fields'] as $field => $info) {
  1858. if ($info['type'] == 'serial') {
  1859. $this->$field = NULL;
  1860. }
  1861. if (!isset($this->$field)) {
  1862. if (!empty($info['serialize']) && isset($info['serialized default'])) {
  1863. $this->$field = unserialize($info['serialized default']);
  1864. }
  1865. elseif (isset($info['default'])) {
  1866. $this->$field = $info['default'];
  1867. }
  1868. else {
  1869. $this->$field = '';
  1870. }
  1871. }
  1872. }
  1873. }
  1874. /**
  1875. * Write the row to the database.
  1876. *
  1877. * @param $update
  1878. * If true this will be an UPDATE query. Otherwise it will be an INSERT.
  1879. */
  1880. function save_row($update = NULL) {
  1881. $fields = $defs = $values = $serials = array();
  1882. $schema = drupal_get_schema($this->db_table);
  1883. // Go through our schema and build correlations.
  1884. foreach ($schema['fields'] as $field => $info) {
  1885. // special case -- skip serial types if we are updating.
  1886. if ($info['type'] == 'serial') {
  1887. $serials[] = $field;
  1888. continue;
  1889. }
  1890. elseif ($info['type'] == 'int') {
  1891. $this->$field = (int) $this->$field;
  1892. }
  1893. $fields[$field] = empty($info['serialize']) ? $this->$field : serialize($this->$field);
  1894. }
  1895. if (!$update) {
  1896. $query = db_insert($this->db_table);
  1897. }
  1898. else {
  1899. $query = db_update($this->db_table)
  1900. ->condition($update, $this->$update);
  1901. }
  1902. $return = $query
  1903. ->fields($fields)
  1904. ->execute();
  1905. if ($serials && !$update) {
  1906. // get last insert ids and fill them in.
  1907. // Well, one ID.
  1908. foreach ($serials as $field) {
  1909. $this->$field = $return;
  1910. }
  1911. }
  1912. }
  1913. /**
  1914. * Load the object with a row from the database.
  1915. *
  1916. * This method is separate from the constructor in order to give us
  1917. * more flexibility in terms of how the view object is built in different
  1918. * contexts.
  1919. *
  1920. * @param $data
  1921. * An object from db_fetch_object. It should contain all of the fields
  1922. * that are in the schema.
  1923. */
  1924. function load_row($data) {
  1925. $schema = drupal_get_schema($this->db_table);
  1926. // Go through our schema and build correlations.
  1927. foreach ($schema['fields'] as $field => $info) {
  1928. $this->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
  1929. }
  1930. }
  1931. /**
  1932. * Export a loaded row, such as an argument, field or the view itself to PHP code.
  1933. *
  1934. * @param $identifier
  1935. * The variable to assign the PHP code for this object to.
  1936. * @param $indent
  1937. * An optional indentation for prettifying nested code.
  1938. */
  1939. function export_row($identifier = NULL, $indent = '') {
  1940. ctools_include('export');
  1941. if (!$identifier) {
  1942. $identifier = $this->db_table;
  1943. }
  1944. $schema = drupal_get_schema($this->db_table);
  1945. $output = $indent . '$' . $identifier . ' = new ' . get_class($this) . "();\n";
  1946. // Go through our schema and build correlations.
  1947. foreach ($schema['fields'] as $field => $info) {
  1948. if (!empty($info['no export'])) {
  1949. continue;
  1950. }
  1951. if (!isset($this->$field)) {
  1952. if (isset($info['default'])) {
  1953. $this->$field = $info['default'];
  1954. }
  1955. else {
  1956. $this->$field = '';
  1957. }
  1958. // serialized defaults must be set as serialized.
  1959. if (isset($info['serialize'])) {
  1960. $this->$field = unserialize($this->$field);
  1961. }
  1962. }
  1963. $value = $this->$field;
  1964. if ($info['type'] == 'int') {
  1965. if (isset($info['size']) && $info['size'] == 'tiny') {
  1966. $value = (bool) $value;
  1967. }
  1968. else {
  1969. $value = (int) $value;
  1970. }
  1971. }
  1972. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  1973. }
  1974. return $output;
  1975. }
  1976. /**
  1977. * Add a new display handler to the view, automatically creating an id.
  1978. *
  1979. * @param $type
  1980. * The plugin type from the views plugin data. Defaults to 'page'.
  1981. * @param $title
  1982. * The title of the display; optional, may be filled in from default.
  1983. * @param $id
  1984. * The id to use.
  1985. * @return
  1986. * The key to the display in $view->display, so that the new display
  1987. * can be easily located.
  1988. */
  1989. function add_display($type = 'page', $title = NULL, $id = NULL) {
  1990. if (empty($type)) {
  1991. return FALSE;
  1992. }
  1993. $plugin = views_fetch_plugin_data('display', $type);
  1994. if (empty($plugin)) {
  1995. $plugin['title'] = t('Broken');
  1996. }
  1997. if (empty($id)) {
  1998. $id = $this->generate_display_id($type);
  1999. if ($id !== 'default') {
  2000. preg_match("/[0-9]+/", $id, $count);
  2001. $count = $count[0];
  2002. }
  2003. else {
  2004. $count = '';
  2005. }
  2006. if (empty($title)) {
  2007. if ($count > 1) {
  2008. $title = $plugin['title'] . ' ' . $count;
  2009. }
  2010. else {
  2011. $title = $plugin['title'];
  2012. }
  2013. }
  2014. }
  2015. // Create the new display object
  2016. $display = new views_display;
  2017. $display->options($type, $id, $title);
  2018. // Add the new display object to the view.
  2019. $this->display[$id] = $display;
  2020. return $id;
  2021. }
  2022. /**
  2023. * Generate a display id of a certain plugin type.
  2024. *
  2025. * @param $type
  2026. * Which plugin should be used for the new display id.
  2027. */
  2028. function generate_display_id($type) {
  2029. // 'default' is singular and is unique, so just go with 'default'
  2030. // for it. For all others, start counting.
  2031. if ($type == 'default') {
  2032. return 'default';
  2033. }
  2034. // Initial id.
  2035. $id = $type . '_1';
  2036. $count = 1;
  2037. // Loop through IDs based upon our style plugin name until
  2038. // we find one that is unused.
  2039. while (!empty($this->display[$id])) {
  2040. $id = $type . '_' . ++$count;
  2041. }
  2042. return $id;
  2043. }
  2044. /**
  2045. * Generates a unique ID for an item.
  2046. *
  2047. * These items are typically fields, filters, sort criteria, or arguments.
  2048. *
  2049. * @param $requested_id
  2050. * The requested ID for the item.
  2051. * @param $existing_items
  2052. * An array of existing items, keyed by their IDs.
  2053. *
  2054. * @return
  2055. * A unique ID. This will be equal to $requested_id if no item with that ID
  2056. * already exists. Otherwise, it will be appended with an integer to make
  2057. * it unique, e.g. "{$requested_id}_1", "{$requested_id}_2", etc.
  2058. */
  2059. public static function generate_item_id($requested_id, $existing_items) {
  2060. $count = 0;
  2061. $id = $requested_id;
  2062. while (!empty($existing_items[$id])) {
  2063. $id = $requested_id . '_' . ++$count;
  2064. }
  2065. return $id;
  2066. }
  2067. /**
  2068. * Create a new display and a display handler for it.
  2069. * @param $type
  2070. * The plugin type from the views plugin data. Defaults to 'page'.
  2071. * @param $title
  2072. * The title of the display; optional, may be filled in from default.
  2073. * @param $id
  2074. * The id to use.
  2075. * @return views_plugin_display
  2076. * A reference to the new handler object.
  2077. */
  2078. function &new_display($type = 'page', $title = NULL, $id = NULL) {
  2079. $id = $this->add_display($type, $title, $id);
  2080. // Create a handler
  2081. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  2082. if (empty($this->display[$id]->handler)) {
  2083. // provide a 'default' handler as an emergency. This won't work well but
  2084. // it will keep things from crashing.
  2085. $this->display[$id]->handler = views_get_plugin('display', 'default');
  2086. }
  2087. if (!empty($this->display[$id]->handler)) {
  2088. // Initialize the new display handler with data.
  2089. $this->display[$id]->handler->init($this, $this->display[$id]);
  2090. // If this is NOT the default display handler, let it know which is
  2091. if ($id != 'default') {
  2092. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  2093. }
  2094. }
  2095. return $this->display[$id]->handler;
  2096. }
  2097. /**
  2098. * Add an item with a handler to the view.
  2099. *
  2100. * These items may be fields, filters, sort criteria, or arguments.
  2101. */
  2102. function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
  2103. $types = views_object_types();
  2104. $this->set_display($display_id);
  2105. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2106. if (empty($id)) {
  2107. $id = $this->generate_item_id($field, $fields);
  2108. }
  2109. $new_item = array(
  2110. 'id' => $id,
  2111. 'table' => $table,
  2112. 'field' => $field,
  2113. ) + $options;
  2114. if (!empty($types[$type]['type'])) {
  2115. $handler_type = $types[$type]['type'];
  2116. }
  2117. else {
  2118. $handler_type = $type;
  2119. }
  2120. $handler = views_get_handler($table, $field, $handler_type);
  2121. $fields[$id] = $new_item;
  2122. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2123. return $id;
  2124. }
  2125. /**
  2126. * Get an array of items for the current display.
  2127. */
  2128. function get_items($type, $display_id = NULL) {
  2129. $this->set_display($display_id);
  2130. if (!isset($display_id)) {
  2131. $display_id = $this->current_display;
  2132. }
  2133. // Get info about the types so we can get the right data.
  2134. $types = views_object_types();
  2135. return $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2136. }
  2137. /**
  2138. * Get the configuration of an item (field/sort/filter/etc) on a given
  2139. * display.
  2140. */
  2141. function get_item($display_id, $type, $id) {
  2142. // Get info about the types so we can get the right data.
  2143. $types = views_object_types();
  2144. // Initialize the display
  2145. $this->set_display($display_id);
  2146. // Get the existing configuration
  2147. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2148. return isset($fields[$id]) ? $fields[$id] : NULL;
  2149. }
  2150. /**
  2151. * Set the configuration of an item (field/sort/filter/etc) on a given
  2152. * display.
  2153. *
  2154. * Pass in NULL for the $item to remove an item.
  2155. */
  2156. function set_item($display_id, $type, $id, $item) {
  2157. // Get info about the types so we can get the right data.
  2158. $types = views_object_types();
  2159. // Initialize the display
  2160. $this->set_display($display_id);
  2161. // Get the existing configuration
  2162. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2163. if (isset($item)) {
  2164. $fields[$id] = $item;
  2165. }
  2166. else {
  2167. unset($fields[$id]);
  2168. }
  2169. // Store.
  2170. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2171. }
  2172. /**
  2173. * Set an option on an item.
  2174. *
  2175. * Use this only if you have just 1 or 2 options to set; if you have
  2176. * many, consider getting the item, adding the options and doing
  2177. * set_item yourself.
  2178. */
  2179. function set_item_option($display_id, $type, $id, $option, $value) {
  2180. $item = $this->get_item($display_id, $type, $id);
  2181. $item[$option] = $value;
  2182. $this->set_item($display_id, $type, $id, $item);
  2183. }
  2184. }
  2185. /**
  2186. * A display type in a view.
  2187. *
  2188. * This is just the database storage mechanism, and isn't terribly important
  2189. * to the behavior of the display at all.
  2190. */
  2191. class views_display extends views_db_object {
  2192. /**
  2193. * The display handler itself, which has all the methods.
  2194. *
  2195. * @var views_plugin_display
  2196. */
  2197. var $handler;
  2198. /**
  2199. * Stores all options of the display, like fields, filters etc.
  2200. *
  2201. * @var array
  2202. */
  2203. var $display_options;
  2204. var $db_table = 'views_display';
  2205. function views_display($init = TRUE) {
  2206. parent::init($init);
  2207. }
  2208. function options($type, $id, $title) {
  2209. $this->display_plugin = $type;
  2210. $this->id = $id;
  2211. $this->display_title = $title;
  2212. }
  2213. }
  2214. /**
  2215. * Provide a list of views object types used in a view, with some information
  2216. * about them.
  2217. */
  2218. function views_object_types() {
  2219. static $retval = NULL;
  2220. // statically cache this so t() doesn't run a bajillion times.
  2221. if (!isset($retval)) {
  2222. $retval = array(
  2223. 'field' => array(
  2224. 'title' => t('Fields'), // title
  2225. 'ltitle' => t('fields'), // lowercase title for mid-sentence
  2226. 'stitle' => t('Field'), // singular title
  2227. 'lstitle' => t('field'), // singular lowercase title for mid sentence
  2228. 'plural' => 'fields',
  2229. ),
  2230. 'argument' => array(
  2231. 'title' => t('Contextual filters'),
  2232. 'ltitle' => t('contextual filters'),
  2233. 'stitle' => t('Contextual filter'),
  2234. 'lstitle' => t('contextual filter'),
  2235. 'plural' => 'arguments',
  2236. ),
  2237. 'sort' => array(
  2238. 'title' => t('Sort criteria'),
  2239. 'ltitle' => t('sort criteria'),
  2240. 'stitle' => t('Sort criterion'),
  2241. 'lstitle' => t('sort criterion'),
  2242. 'plural' => 'sorts',
  2243. ),
  2244. 'filter' => array(
  2245. 'title' => t('Filter criteria'),
  2246. 'ltitle' => t('filter criteria'),
  2247. 'stitle' => t('Filter criterion'),
  2248. 'lstitle' => t('filter criterion'),
  2249. 'plural' => 'filters',
  2250. ),
  2251. 'relationship' => array(
  2252. 'title' => t('Relationships'),
  2253. 'ltitle' => t('relationships'),
  2254. 'stitle' => t('Relationship'),
  2255. 'lstitle' => t('Relationship'),
  2256. 'plural' => 'relationships',
  2257. ),
  2258. 'header' => array(
  2259. 'title' => t('Header'),
  2260. 'ltitle' => t('header'),
  2261. 'stitle' => t('Header'),
  2262. 'lstitle' => t('Header'),
  2263. 'plural' => 'header',
  2264. 'type' => 'area',
  2265. ),
  2266. 'footer' => array(
  2267. 'title' => t('Footer'),
  2268. 'ltitle' => t('footer'),
  2269. 'stitle' => t('Footer'),
  2270. 'lstitle' => t('Footer'),
  2271. 'plural' => 'footer',
  2272. 'type' => 'area',
  2273. ),
  2274. 'empty' => array(
  2275. 'title' => t('No results behavior'),
  2276. 'ltitle' => t('no results behavior'),
  2277. 'stitle' => t('No results behavior'),
  2278. 'lstitle' => t('No results behavior'),
  2279. 'plural' => 'empty',
  2280. 'type' => 'area',
  2281. ),
  2282. );
  2283. }
  2284. return $retval;
  2285. }
  2286. /**
  2287. * @}
  2288. */