view.inc 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  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. if (empty($this->exposed_input)) {
  351. $this->exposed_input = array();
  352. // If filters are not overridden, store the 'remember' settings on the
  353. // default display. If they are, store them on this display. This way,
  354. // multiple displays in the same view can share the same filters and
  355. // remember settings.
  356. $display_id = ($this->display_handler->is_defaulted('filters')) ? 'default' : $this->current_display;
  357. // Start with remembered input via session.
  358. if (!empty($_SESSION['views'][$this->name][$display_id])) {
  359. $this->exposed_input = $_SESSION['views'][$this->name][$display_id];
  360. }
  361. // Fetch exposed input values from $_GET. Overwrite if clashing.
  362. foreach ($_GET as $key => $value) {
  363. if (!in_array($key, array('page', 'q'))) {
  364. $this->exposed_input[$key] = $value;
  365. }
  366. }
  367. }
  368. return $this->exposed_input;
  369. }
  370. /**
  371. * Set the display for this view and initialize the display handler.
  372. */
  373. function init_display($reset = FALSE) {
  374. // The default display is always the first one in the list.
  375. if (isset($this->current_display)) {
  376. return TRUE;
  377. }
  378. // Instantiate all displays
  379. foreach (array_keys($this->display) as $id) {
  380. // Correct for shallow cloning
  381. // Often we'll have a cloned view so we don't mess up each other's
  382. // displays, but the clone is pretty shallow and doesn't necessarily
  383. // clone the displays. We can tell this by looking to see if a handler
  384. // has already been set; if it has, but $this->current_display is not
  385. // set, then something is dreadfully wrong.
  386. if (!empty($this->display[$id]->handler)) {
  387. $this->display[$id] = clone $this->display[$id];
  388. unset($this->display[$id]->handler);
  389. }
  390. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  391. if (!empty($this->display[$id]->handler)) {
  392. $this->display[$id]->handler->localization_keys = array($id);
  393. // Initialize the new display handler with data.
  394. $this->display[$id]->handler->init($this, $this->display[$id]);
  395. // If this is NOT the default display handler, let it know which is
  396. // since it may well utilize some data from the default.
  397. // This assumes that the 'default' handler is always first. It always
  398. // is. Make sure of it.
  399. if ($id != 'default') {
  400. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  401. }
  402. }
  403. }
  404. $this->current_display = 'default';
  405. $this->display_handler = &$this->display['default']->handler;
  406. return TRUE;
  407. }
  408. /**
  409. * Get the first display that is accessible to the user.
  410. *
  411. * @param $displays
  412. * Either a single display id or an array of display ids.
  413. */
  414. function choose_display($displays) {
  415. if (!is_array($displays)) {
  416. return $displays;
  417. }
  418. $this->init_display();
  419. foreach ($displays as $display_id) {
  420. if ($this->display[$display_id]->handler->access()) {
  421. return $display_id;
  422. }
  423. }
  424. return 'default';
  425. }
  426. /**
  427. * Set the display as current.
  428. *
  429. * @param $display_id
  430. * The id of the display to mark as current.
  431. */
  432. function set_display($display_id = NULL) {
  433. // If we have not already initialized the display, do so. But be careful.
  434. if (empty($this->current_display)) {
  435. $this->init_display();
  436. // If handlers were not initialized, and no argument was sent, set up
  437. // to the default display.
  438. if (empty($display_id)) {
  439. $display_id = 'default';
  440. }
  441. }
  442. $display_id = $this->choose_display($display_id);
  443. // If no display id sent in and one wasn't chosen above, we're finished.
  444. if (empty($display_id)) {
  445. return FALSE;
  446. }
  447. // Ensure the requested display exists.
  448. if (empty($this->display[$display_id])) {
  449. $display_id = 'default';
  450. if (empty($this->display[$display_id])) {
  451. vpr('set_display() called with invalid display id @display.', array('@display' => $display_id));
  452. return FALSE;
  453. }
  454. }
  455. // Set the current display.
  456. $this->current_display = $display_id;
  457. // Ensure requested display has a working handler.
  458. if (empty($this->display[$display_id]->handler)) {
  459. return FALSE;
  460. }
  461. // Set a shortcut
  462. $this->display_handler = &$this->display[$display_id]->handler;
  463. return TRUE;
  464. }
  465. /**
  466. * Find and initialize the style plugin.
  467. *
  468. * Note that arguments may have changed which style plugin we use, so
  469. * check the view object first, then ask the display handler.
  470. */
  471. function init_style() {
  472. if (isset($this->style_plugin)) {
  473. return is_object($this->style_plugin);
  474. }
  475. if (!isset($this->plugin_name)) {
  476. $this->plugin_name = $this->display_handler->get_option('style_plugin');
  477. $this->style_options = $this->display_handler->get_option('style_options');
  478. }
  479. $this->style_plugin = views_get_plugin('style', $this->plugin_name);
  480. if (empty($this->style_plugin)) {
  481. return FALSE;
  482. }
  483. // init the new style handler with data.
  484. $this->style_plugin->init($this, $this->display[$this->current_display], $this->style_options);
  485. return TRUE;
  486. }
  487. /**
  488. * Attempt to discover if the view has handlers missing relationships.
  489. *
  490. * This will try to add relationships automatically if it can, and will
  491. * remove the handlers if it cannot.
  492. */
  493. function fix_missing_relationships() {
  494. if (isset($this->relationships_fixed)) {
  495. return;
  496. }
  497. $this->relationships_fixed = TRUE;
  498. // Go through all of our handler types and test them to see if they
  499. // are missing relationships. Missing relationships can cause fatally
  500. // broken Views.
  501. $base_tables = array(
  502. $this->base_table => TRUE,
  503. '#global' => TRUE,
  504. );
  505. // For each relationship we have, make sure we mark the base it provides as
  506. // available.
  507. foreach ($this->display_handler->get_option('relationships') as $id => $options) {
  508. $options['table'] = views_move_table($options['table']);
  509. $data = views_fetch_data($options['table'], FALSE);
  510. if (isset($data[$options['field']]['relationship']['base'])) {
  511. $base_tables[$data[$options['field']]['relationship']['base']] = TRUE;
  512. }
  513. }
  514. $base_tables = array_keys($base_tables);
  515. $missing_base_tables = array();
  516. $types = views_object_types();
  517. foreach ($types as $key => $info) {
  518. foreach ($this->display_handler->get_option($info['plural']) as $id => $options) {
  519. $options['table'] = views_move_table($options['table']);
  520. $data = views_fetch_data($options['table'], FALSE);
  521. $valid_bases = array($options['table']);
  522. if (isset($data['table']['join'])) {
  523. $valid_bases = array_merge($valid_bases, array_keys($data['table']['join']));
  524. }
  525. // If the base table is missing, record it so we can try to fix it.
  526. if (!array_intersect($valid_bases, $base_tables)) {
  527. $missing_base_tables[$options['table']][] = array('type' => $key, 'id' => $id);
  528. }
  529. }
  530. }
  531. if (!empty($missing_base_tables)) {
  532. // This will change handlers, so make sure any existing handlers get
  533. // tossed.
  534. $this->display_handler->handlers = array();
  535. $this->relationships_changed = TRUE;
  536. $this->changed = TRUE;
  537. // Try to fix it.
  538. foreach ($missing_base_tables as $table => $handlers) {
  539. $data = views_fetch_data($table);
  540. $relationship = NULL;
  541. // Does the missing base table have a default relationship we can
  542. // throw in?
  543. if (isset($data['table']['default_relationship'][$this->base_table])) {
  544. // Create the relationship.
  545. $info = $data['table']['default_relationship'][$this->base_table];
  546. $relationship_options = isset($info['options']) ? $info['options'] : array();
  547. $relationship = $this->add_item($this->current_display, 'relationship', $info['table'], $info['field'], $relationship_options);
  548. }
  549. foreach ($handlers as $handler) {
  550. $options = $this->display_handler->get_option($types[$handler['type']]['plural']);
  551. if ($relationship) {
  552. $options[$handler['id']]['relationship'] = $relationship;
  553. }
  554. else {
  555. unset($options[$handler['id']]);
  556. }
  557. $this->display_handler->set_option($types[$handler['type']]['plural'], $options);
  558. }
  559. }
  560. }
  561. }
  562. /**
  563. * Acquire and attach all of the handlers.
  564. */
  565. function init_handlers() {
  566. if (empty($this->inited)) {
  567. $this->fix_missing_relationships();
  568. foreach (views_object_types() as $key => $info) {
  569. $this->_init_handler($key, $info);
  570. }
  571. $this->inited = TRUE;
  572. }
  573. }
  574. /**
  575. * Initialize the pager
  576. *
  577. * Like style initialization, pager initialization is held until late
  578. * to allow for overrides.
  579. */
  580. function init_pager() {
  581. if (empty($this->query->pager)) {
  582. // If the query doesn't exist, initialize it.
  583. if (empty($this->query)) {
  584. $this->init_query();
  585. }
  586. $this->query->pager = $this->display_handler->get_plugin('pager');
  587. if ($this->query->pager->use_pager()) {
  588. $this->query->pager->set_current_page($this->current_page);
  589. }
  590. // These overrides may have been set earlier via $view->set_*
  591. // functions.
  592. if (isset($this->items_per_page)) {
  593. $this->query->pager->set_items_per_page($this->items_per_page);
  594. }
  595. if (isset($this->offset)) {
  596. $this->query->pager->set_offset($this->offset);
  597. }
  598. }
  599. }
  600. /**
  601. * Create a list of base tables eligible for this view. Used primarily
  602. * for the UI. Display must be already initialized.
  603. */
  604. function get_base_tables() {
  605. $base_tables = array(
  606. $this->base_table => TRUE,
  607. '#global' => TRUE,
  608. );
  609. foreach ($this->display_handler->get_handlers('relationship') as $handler) {
  610. $base_tables[$handler->definition['base']] = TRUE;
  611. }
  612. return $base_tables;
  613. }
  614. /**
  615. * Run the pre_query() on all active handlers.
  616. */
  617. function _pre_query() {
  618. foreach (views_object_types() as $key => $info) {
  619. $handlers = &$this->$key;
  620. $position = 0;
  621. foreach ($handlers as $id => $handler) {
  622. $handlers[$id]->position = $position;
  623. $handlers[$id]->pre_query();
  624. $position++;
  625. }
  626. }
  627. }
  628. /**
  629. * Run the post_execute() on all active handlers.
  630. */
  631. function _post_execute() {
  632. foreach (views_object_types() as $key => $info) {
  633. $handlers = &$this->$key;
  634. foreach ($handlers as $id => $handler) {
  635. $handlers[$id]->post_execute($this->result);
  636. }
  637. }
  638. }
  639. /**
  640. * Attach all of the handlers for each type.
  641. *
  642. * @param $key
  643. * One of 'argument', 'field', 'sort', 'filter', 'relationship'
  644. * @param $info
  645. * The $info from views_object_types for this object.
  646. */
  647. function _init_handler($key, $info) {
  648. // Load the requested items from the display onto the object.
  649. $this->$key = &$this->display_handler->get_handlers($key);
  650. // This reference deals with difficult PHP indirection.
  651. $handlers = &$this->$key;
  652. // Run through and test for accessibility.
  653. foreach ($handlers as $id => $handler) {
  654. if (!$handler->access()) {
  655. unset($handlers[$id]);
  656. }
  657. }
  658. }
  659. /**
  660. * Build all the arguments.
  661. */
  662. function _build_arguments() {
  663. // Initially, we want to build sorts and fields. This can change, though,
  664. // if we get a summary view.
  665. if (empty($this->argument)) {
  666. return TRUE;
  667. }
  668. // build arguments.
  669. $position = -1;
  670. // Create a title for use in the breadcrumb trail.
  671. $title = $this->display_handler->get_option('title');
  672. $this->build_info['breadcrumb'] = array();
  673. $breadcrumb_args = array();
  674. $substitutions = array();
  675. $status = TRUE;
  676. // Iterate through each argument and process.
  677. foreach ($this->argument as $id => $arg) {
  678. $position++;
  679. $argument = &$this->argument[$id];
  680. if ($argument->broken()) {
  681. continue;
  682. }
  683. $argument->set_relationship();
  684. $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
  685. $argument->position = $position;
  686. if (isset($arg) || $argument->has_default_argument()) {
  687. if (!isset($arg)) {
  688. $arg = $argument->get_default_argument();
  689. // make sure default args get put back.
  690. if (isset($arg)) {
  691. $this->args[$position] = $arg;
  692. }
  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() || !empty($this->get_total_rows)) {
  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, $this->display_handler->output, $cache);
  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. // No one 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->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. // Let modules modify the view just prior to saving it.
  1555. module_invoke_all('views_view_presave', $this);
  1556. $transaction = db_transaction();
  1557. try {
  1558. // If we have no vid or our vid is a string, this is a new view.
  1559. if (!empty($this->vid)) {
  1560. // remove existing table entries
  1561. foreach ($this->db_objects() as $key) {
  1562. db_delete('views_' . $key)
  1563. ->condition('vid', $this->vid)
  1564. ->execute();
  1565. }
  1566. }
  1567. $this->save_row(!empty($this->vid) ? 'vid' : FALSE);
  1568. // Save all of our subtables.
  1569. foreach ($this->db_objects() as $key) {
  1570. $this->_save_rows($key);
  1571. }
  1572. }
  1573. catch (Exception $e) {
  1574. $transaction->rollback();
  1575. watchdog_exception('views', $e);
  1576. throw $e;
  1577. }
  1578. $this->save_locale_strings();
  1579. // Clear caches.
  1580. views_invalidate_cache();
  1581. // Notify modules that this view has been saved.
  1582. module_invoke_all('views_view_save', $this);
  1583. }
  1584. /**
  1585. * Save a row to the database for the given key, which is one of the
  1586. * keys from view::db_objects()
  1587. */
  1588. function _save_rows($key) {
  1589. $count = 0;
  1590. foreach ($this->$key as $position => $object) {
  1591. $object->position = ++$count;
  1592. $object->vid = $this->vid;
  1593. $object->save_row();
  1594. }
  1595. }
  1596. /**
  1597. * Delete the view from the database.
  1598. */
  1599. function delete($clear = TRUE) {
  1600. if (empty($this->vid)) {
  1601. return;
  1602. }
  1603. db_delete('views_view')
  1604. ->condition('vid', $this->vid)
  1605. ->execute();
  1606. // Delete from all of our subtables as well.
  1607. foreach ($this->db_objects() as $key) {
  1608. db_delete('views_'. $key)
  1609. ->condition('vid', $this->vid)
  1610. ->execute();
  1611. }
  1612. cache_clear_all('views_query:' . $this->name, 'cache_views');
  1613. if ($clear) {
  1614. // Clear caches.
  1615. views_invalidate_cache();
  1616. }
  1617. // Notify modules that this view has been deleted.
  1618. module_invoke_all('views_view_delete', $this);
  1619. }
  1620. /**
  1621. * Export a view as PHP code.
  1622. */
  1623. function export($indent = '') {
  1624. $this->init_display();
  1625. $this->init_query();
  1626. $output = '';
  1627. $output .= $this->export_row('view', $indent);
  1628. // Set the API version
  1629. $output .= $indent . '$view->api_version = \'' . views_api_version() . "';\n";
  1630. $output .= $indent . '$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */' . "\n";
  1631. foreach ($this->display as $id => $display) {
  1632. $output .= "\n" . $indent . "/* Display: $display->display_title */\n";
  1633. $output .= $indent . '$handler = $view->new_display(' . ctools_var_export($display->display_plugin, $indent) . ', ' . ctools_var_export($display->display_title, $indent) . ', \'' . $id . "');\n";
  1634. if (empty($display->handler)) {
  1635. // @todo -- probably need a method of exporting broken displays as
  1636. // they may simply be broken because a module is not installed. That
  1637. // does not invalidate the display.
  1638. continue;
  1639. }
  1640. $output .= $display->handler->export_options($indent, '$handler->options');
  1641. }
  1642. // Give the localization system a chance to export translatables to code.
  1643. if ($this->init_localization()) {
  1644. $this->export_locale_strings('export');
  1645. $translatables = $this->localization_plugin->export_render($indent);
  1646. if (!empty($translatables)) {
  1647. $output .= $translatables;
  1648. }
  1649. }
  1650. return $output;
  1651. }
  1652. /**
  1653. * Make a copy of this view that has been sanitized of all database IDs
  1654. * and handlers and other stuff.
  1655. *
  1656. * I'd call this clone() but it's reserved.
  1657. */
  1658. function copy() {
  1659. $code = $this->export();
  1660. eval($code);
  1661. return $view;
  1662. }
  1663. /**
  1664. * Safely clone a view.
  1665. *
  1666. * Because views are complicated objects within objects, and PHP loves to
  1667. * do references to everything, if a View is not properly and safely
  1668. * cloned it will still have references to the original view, and can
  1669. * actually cause the original view to point to objects in the cloned
  1670. * view. This gets ugly fast.
  1671. *
  1672. * This will completely wipe a view clean so it can be considered fresh.
  1673. *
  1674. * @return view
  1675. * The cloned view.
  1676. */
  1677. function clone_view() {
  1678. $clone = clone $this;
  1679. $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');
  1680. foreach ($keys as $key) {
  1681. if (isset($clone->{$key})) {
  1682. unset($clone->{$key});
  1683. }
  1684. }
  1685. $clone->built = $clone->executed = FALSE;
  1686. $clone->build_info = array();
  1687. $clone->attachment_before = '';
  1688. $clone->attachment_after = '';
  1689. $clone->result = array();
  1690. // shallow cloning means that all the display objects
  1691. // *were not cloned*. We must clone them ourselves.
  1692. $displays = array();
  1693. foreach ($clone->display as $id => $display) {
  1694. $displays[$id] = clone $display;
  1695. if (isset($displays[$id]->handler)) {
  1696. unset($displays[$id]->handler);
  1697. }
  1698. }
  1699. $clone->display = $displays;
  1700. return $clone;
  1701. }
  1702. /**
  1703. * Unset references so that a $view object may be properly garbage
  1704. * collected.
  1705. */
  1706. function destroy() {
  1707. foreach (array_keys($this->display) as $display_id) {
  1708. if (isset($this->display[$display_id]->handler) && is_object($this->display[$display_id]->handler)) {
  1709. $this->display[$display_id]->handler->destroy();
  1710. unset($this->display[$display_id]->handler);
  1711. }
  1712. }
  1713. foreach (views_object_types() as $type => $info) {
  1714. if (isset($this->$type)) {
  1715. $handlers = &$this->$type;
  1716. foreach ($handlers as $id => $item) {
  1717. $handlers[$id]->destroy();
  1718. }
  1719. unset($handlers);
  1720. }
  1721. }
  1722. if (isset($this->style_plugin)) {
  1723. $this->style_plugin->destroy();
  1724. unset($this->style_plugin);
  1725. }
  1726. // Clear these to make sure the view can be processed/used again.
  1727. if (isset($this->display_handler)) {
  1728. unset($this->display_handler);
  1729. }
  1730. if (isset($this->current_display)) {
  1731. unset($this->current_display);
  1732. }
  1733. if (isset($this->query)) {
  1734. unset($this->query);
  1735. }
  1736. $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');
  1737. foreach ($keys as $key) {
  1738. if (isset($this->$key)) {
  1739. unset($this->$key);
  1740. }
  1741. }
  1742. // These keys are checked by the next init, so instead of unsetting them,
  1743. // just set the default values.
  1744. $keys = array('items_per_page', 'offset', 'current_page');
  1745. foreach ($keys as $key) {
  1746. if (isset($this->$key)) {
  1747. $this->$key = NULL;
  1748. }
  1749. }
  1750. $this->built = $this->executed = FALSE;
  1751. $this->build_info = array();
  1752. $this->attachment_before = '';
  1753. $this->attachment_after = '';
  1754. }
  1755. /**
  1756. * Make sure the view is completely valid.
  1757. *
  1758. * @return
  1759. * TRUE if the view is valid; an array of error strings if it is not.
  1760. */
  1761. function validate() {
  1762. $this->init_display();
  1763. $errors = array();
  1764. $this->display_errors = NULL;
  1765. $current_display = $this->current_display;
  1766. foreach ($this->display as $id => $display) {
  1767. if ($display->handler) {
  1768. if (!empty($display->deleted)) {
  1769. continue;
  1770. }
  1771. $result = $this->display[$id]->handler->validate();
  1772. if (!empty($result) && is_array($result)) {
  1773. $errors = array_merge($errors, $result);
  1774. // Mark this display as having validation errors.
  1775. $this->display_errors[$id] = TRUE;
  1776. }
  1777. }
  1778. }
  1779. $this->set_display($current_display);
  1780. return $errors ? $errors : TRUE;
  1781. }
  1782. /**
  1783. * Find and initialize the localization plugin.
  1784. */
  1785. function init_localization() {
  1786. // If the translate attribute isn't set, init the localization plugin.
  1787. if (!isset($this->localization_plugin->translate)) {
  1788. $this->localization_plugin = views_get_plugin('localization', views_get_localization_plugin());
  1789. // If the plugin is still not set, turn off all localization by using the
  1790. // views_plugin_localization_none plugin. This plugin has the translate
  1791. // property set to FALSE, signifying localization should not occur.
  1792. if (empty($this->localization_plugin)) {
  1793. $this->localization_plugin = views_get_plugin('localization', 'none');
  1794. }
  1795. // Init the plugin.
  1796. $this->localization_plugin->init($this);
  1797. }
  1798. // Return the value of the translate property. This is set to FALSE if
  1799. // localization is off.
  1800. return $this->localization_plugin->translate;
  1801. }
  1802. /**
  1803. * Determine whether a view supports admin string translation.
  1804. */
  1805. function is_translatable() {
  1806. // Use translation no matter what type of view.
  1807. if (variable_get('views_localize_all', FALSE)) {
  1808. return TRUE;
  1809. }
  1810. // If the view is normal or overridden, use admin string translation.
  1811. // A newly created view won't have a type. Accept this.
  1812. return (!isset($this->type) || in_array($this->type, array(t('Normal'), t('Overridden')))) ? TRUE : FALSE;
  1813. }
  1814. /**
  1815. * Send strings for localization.
  1816. */
  1817. function save_locale_strings() {
  1818. $this->process_locale_strings('save');
  1819. }
  1820. /**
  1821. * Delete localized strings.
  1822. */
  1823. function delete_locale_strings() {
  1824. $this->process_locale_strings('delete');
  1825. }
  1826. /**
  1827. * Export localized strings.
  1828. */
  1829. function export_locale_strings() {
  1830. $this->process_locale_strings('export');
  1831. }
  1832. /**
  1833. * Process strings for localization, deletion or export to code.
  1834. */
  1835. function process_locale_strings($op) {
  1836. // Ensure this view supports translation, we have a display, and we
  1837. // have a localization plugin.
  1838. // @fixme Export does not init every handler.
  1839. if (($this->is_translatable() || $op == 'export') && $this->init_display() && $this->init_localization()) {
  1840. $this->localization_plugin->process_locale_strings($op);
  1841. }
  1842. }
  1843. }
  1844. /**
  1845. * Base class for views' database objects.
  1846. */
  1847. class views_db_object {
  1848. public $db_table;
  1849. /**
  1850. * Initialize this object, setting values from schema defaults.
  1851. *
  1852. * @param $init
  1853. * If an array, this is a set of values from db_fetch_object to
  1854. * load. Otherwse, if TRUE values will be filled in from schema
  1855. * defaults.
  1856. */
  1857. function init($init = TRUE) {
  1858. if (is_array($init)) {
  1859. return $this->load_row($init);
  1860. }
  1861. if (!$init) {
  1862. return;
  1863. }
  1864. $schema = drupal_get_schema($this->db_table);
  1865. if (!$schema) {
  1866. return;
  1867. }
  1868. // Go through our schema and build correlations.
  1869. foreach ($schema['fields'] as $field => $info) {
  1870. if ($info['type'] == 'serial') {
  1871. $this->$field = NULL;
  1872. }
  1873. if (!isset($this->$field)) {
  1874. if (!empty($info['serialize']) && isset($info['serialized default'])) {
  1875. $this->$field = unserialize($info['serialized default']);
  1876. }
  1877. elseif (isset($info['default'])) {
  1878. $this->$field = $info['default'];
  1879. }
  1880. else {
  1881. $this->$field = '';
  1882. }
  1883. }
  1884. }
  1885. }
  1886. /**
  1887. * Write the row to the database.
  1888. *
  1889. * @param $update
  1890. * If true this will be an UPDATE query. Otherwise it will be an INSERT.
  1891. */
  1892. function save_row($update = NULL) {
  1893. $fields = $defs = $values = $serials = array();
  1894. $schema = drupal_get_schema($this->db_table);
  1895. // Go through our schema and build correlations.
  1896. foreach ($schema['fields'] as $field => $info) {
  1897. // special case -- skip serial types if we are updating.
  1898. if ($info['type'] == 'serial') {
  1899. $serials[] = $field;
  1900. continue;
  1901. }
  1902. elseif ($info['type'] == 'int') {
  1903. $this->$field = (int) $this->$field;
  1904. }
  1905. $fields[$field] = empty($info['serialize']) ? $this->$field : serialize($this->$field);
  1906. }
  1907. if (!$update) {
  1908. $query = db_insert($this->db_table);
  1909. }
  1910. else {
  1911. $query = db_update($this->db_table)
  1912. ->condition($update, $this->$update);
  1913. }
  1914. $return = $query
  1915. ->fields($fields)
  1916. ->execute();
  1917. if ($serials && !$update) {
  1918. // get last insert ids and fill them in.
  1919. // Well, one ID.
  1920. foreach ($serials as $field) {
  1921. $this->$field = $return;
  1922. }
  1923. }
  1924. }
  1925. /**
  1926. * Load the object with a row from the database.
  1927. *
  1928. * This method is separate from the constructor in order to give us
  1929. * more flexibility in terms of how the view object is built in different
  1930. * contexts.
  1931. *
  1932. * @param $data
  1933. * An object from db_fetch_object. It should contain all of the fields
  1934. * that are in the schema.
  1935. */
  1936. function load_row($data) {
  1937. $schema = drupal_get_schema($this->db_table);
  1938. // Go through our schema and build correlations.
  1939. foreach ($schema['fields'] as $field => $info) {
  1940. $this->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
  1941. }
  1942. }
  1943. /**
  1944. * Export a loaded row, such as an argument, field or the view itself to PHP code.
  1945. *
  1946. * @param $identifier
  1947. * The variable to assign the PHP code for this object to.
  1948. * @param $indent
  1949. * An optional indentation for prettifying nested code.
  1950. */
  1951. function export_row($identifier = NULL, $indent = '') {
  1952. ctools_include('export');
  1953. if (!$identifier) {
  1954. $identifier = $this->db_table;
  1955. }
  1956. $schema = drupal_get_schema($this->db_table);
  1957. $output = $indent . '$' . $identifier . ' = new ' . get_class($this) . "();\n";
  1958. // Go through our schema and build correlations.
  1959. foreach ($schema['fields'] as $field => $info) {
  1960. if (!empty($info['no export'])) {
  1961. continue;
  1962. }
  1963. if (!isset($this->$field)) {
  1964. if (isset($info['default'])) {
  1965. $this->$field = $info['default'];
  1966. }
  1967. else {
  1968. $this->$field = '';
  1969. }
  1970. // serialized defaults must be set as serialized.
  1971. if (isset($info['serialize'])) {
  1972. $this->$field = unserialize($this->$field);
  1973. }
  1974. }
  1975. $value = $this->$field;
  1976. if ($info['type'] == 'int') {
  1977. if (isset($info['size']) && $info['size'] == 'tiny') {
  1978. $value = (bool) $value;
  1979. }
  1980. else {
  1981. $value = (int) $value;
  1982. }
  1983. }
  1984. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  1985. }
  1986. return $output;
  1987. }
  1988. /**
  1989. * Add a new display handler to the view, automatically creating an id.
  1990. *
  1991. * @param $type
  1992. * The plugin type from the views plugin data. Defaults to 'page'.
  1993. * @param $title
  1994. * The title of the display; optional, may be filled in from default.
  1995. * @param $id
  1996. * The id to use.
  1997. * @return
  1998. * The key to the display in $view->display, so that the new display
  1999. * can be easily located.
  2000. */
  2001. function add_display($type = 'page', $title = NULL, $id = NULL) {
  2002. if (empty($type)) {
  2003. return FALSE;
  2004. }
  2005. $plugin = views_fetch_plugin_data('display', $type);
  2006. if (empty($plugin)) {
  2007. $plugin['title'] = t('Broken');
  2008. }
  2009. if (empty($id)) {
  2010. $id = $this->generate_display_id($type);
  2011. if ($id !== 'default') {
  2012. preg_match("/[0-9]+/", $id, $count);
  2013. $count = $count[0];
  2014. }
  2015. else {
  2016. $count = '';
  2017. }
  2018. if (empty($title)) {
  2019. if ($count > 1) {
  2020. $title = $plugin['title'] . ' ' . $count;
  2021. }
  2022. else {
  2023. $title = $plugin['title'];
  2024. }
  2025. }
  2026. }
  2027. // Create the new display object
  2028. $display = new views_display;
  2029. $display->options($type, $id, $title);
  2030. // Add the new display object to the view.
  2031. $this->display[$id] = $display;
  2032. return $id;
  2033. }
  2034. /**
  2035. * Generate a display id of a certain plugin type.
  2036. *
  2037. * @param $type
  2038. * Which plugin should be used for the new display id.
  2039. */
  2040. function generate_display_id($type) {
  2041. // 'default' is singular and is unique, so just go with 'default'
  2042. // for it. For all others, start counting.
  2043. if ($type == 'default') {
  2044. return 'default';
  2045. }
  2046. // Initial id.
  2047. $id = $type . '_1';
  2048. $count = 1;
  2049. // Loop through IDs based upon our style plugin name until
  2050. // we find one that is unused.
  2051. while (!empty($this->display[$id])) {
  2052. $id = $type . '_' . ++$count;
  2053. }
  2054. return $id;
  2055. }
  2056. /**
  2057. * Generates a unique ID for an item.
  2058. *
  2059. * These items are typically fields, filters, sort criteria, or arguments.
  2060. *
  2061. * @param $requested_id
  2062. * The requested ID for the item.
  2063. * @param $existing_items
  2064. * An array of existing items, keyed by their IDs.
  2065. *
  2066. * @return
  2067. * A unique ID. This will be equal to $requested_id if no item with that ID
  2068. * already exists. Otherwise, it will be appended with an integer to make
  2069. * it unique, e.g. "{$requested_id}_1", "{$requested_id}_2", etc.
  2070. */
  2071. public static function generate_item_id($requested_id, $existing_items) {
  2072. $count = 0;
  2073. $id = $requested_id;
  2074. while (!empty($existing_items[$id])) {
  2075. $id = $requested_id . '_' . ++$count;
  2076. }
  2077. return $id;
  2078. }
  2079. /**
  2080. * Create a new display and a display handler for it.
  2081. * @param $type
  2082. * The plugin type from the views plugin data. Defaults to 'page'.
  2083. * @param $title
  2084. * The title of the display; optional, may be filled in from default.
  2085. * @param $id
  2086. * The id to use.
  2087. * @return views_plugin_display
  2088. * A reference to the new handler object.
  2089. */
  2090. function &new_display($type = 'page', $title = NULL, $id = NULL) {
  2091. $id = $this->add_display($type, $title, $id);
  2092. // Create a handler
  2093. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  2094. if (empty($this->display[$id]->handler)) {
  2095. // provide a 'default' handler as an emergency. This won't work well but
  2096. // it will keep things from crashing.
  2097. $this->display[$id]->handler = views_get_plugin('display', 'default');
  2098. }
  2099. if (!empty($this->display[$id]->handler)) {
  2100. // Initialize the new display handler with data.
  2101. $this->display[$id]->handler->init($this, $this->display[$id]);
  2102. // If this is NOT the default display handler, let it know which is
  2103. if ($id != 'default') {
  2104. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  2105. }
  2106. }
  2107. return $this->display[$id]->handler;
  2108. }
  2109. /**
  2110. * Add an item with a handler to the view.
  2111. *
  2112. * These items may be fields, filters, sort criteria, or arguments.
  2113. */
  2114. function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
  2115. $types = views_object_types();
  2116. $this->set_display($display_id);
  2117. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2118. if (empty($id)) {
  2119. $id = $this->generate_item_id($field, $fields);
  2120. }
  2121. $new_item = array(
  2122. 'id' => $id,
  2123. 'table' => $table,
  2124. 'field' => $field,
  2125. ) + $options;
  2126. if (!empty($types[$type]['type'])) {
  2127. $handler_type = $types[$type]['type'];
  2128. }
  2129. else {
  2130. $handler_type = $type;
  2131. }
  2132. $handler = views_get_handler($table, $field, $handler_type);
  2133. $fields[$id] = $new_item;
  2134. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2135. return $id;
  2136. }
  2137. /**
  2138. * Get an array of items for the current display.
  2139. */
  2140. function get_items($type, $display_id = NULL) {
  2141. $this->set_display($display_id);
  2142. if (!isset($display_id)) {
  2143. $display_id = $this->current_display;
  2144. }
  2145. // Get info about the types so we can get the right data.
  2146. $types = views_object_types();
  2147. return $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2148. }
  2149. /**
  2150. * Get the configuration of an item (field/sort/filter/etc) on a given
  2151. * display.
  2152. */
  2153. function get_item($display_id, $type, $id) {
  2154. // Get info about the types so we can get the right data.
  2155. $types = views_object_types();
  2156. // Initialize the display
  2157. $this->set_display($display_id);
  2158. // Get the existing configuration
  2159. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2160. return isset($fields[$id]) ? $fields[$id] : NULL;
  2161. }
  2162. /**
  2163. * Set the configuration of an item (field/sort/filter/etc) on a given
  2164. * display.
  2165. *
  2166. * Pass in NULL for the $item to remove an item.
  2167. */
  2168. function set_item($display_id, $type, $id, $item) {
  2169. // Get info about the types so we can get the right data.
  2170. $types = views_object_types();
  2171. // Initialize the display
  2172. $this->set_display($display_id);
  2173. // Get the existing configuration
  2174. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2175. if (isset($item)) {
  2176. $fields[$id] = $item;
  2177. }
  2178. else {
  2179. unset($fields[$id]);
  2180. }
  2181. // Store.
  2182. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2183. }
  2184. /**
  2185. * Set an option on an item.
  2186. *
  2187. * Use this only if you have just 1 or 2 options to set; if you have
  2188. * many, consider getting the item, adding the options and doing
  2189. * set_item yourself.
  2190. */
  2191. function set_item_option($display_id, $type, $id, $option, $value) {
  2192. $item = $this->get_item($display_id, $type, $id);
  2193. $item[$option] = $value;
  2194. $this->set_item($display_id, $type, $id, $item);
  2195. }
  2196. }
  2197. /**
  2198. * A display type in a view.
  2199. *
  2200. * This is just the database storage mechanism, and isn't terribly important
  2201. * to the behavior of the display at all.
  2202. */
  2203. class views_display extends views_db_object {
  2204. /**
  2205. * The display handler itself, which has all the methods.
  2206. *
  2207. * @var views_plugin_display
  2208. */
  2209. var $handler;
  2210. /**
  2211. * Stores all options of the display, like fields, filters etc.
  2212. *
  2213. * @var array
  2214. */
  2215. var $display_options;
  2216. var $db_table = 'views_display';
  2217. function __construct($init = TRUE) {
  2218. parent::init($init);
  2219. }
  2220. function options($type, $id, $title) {
  2221. $this->display_plugin = $type;
  2222. $this->id = $id;
  2223. $this->display_title = $title;
  2224. }
  2225. }
  2226. /**
  2227. * Provide a list of views object types used in a view, with some information
  2228. * about them.
  2229. */
  2230. function views_object_types() {
  2231. static $retval = NULL;
  2232. // statically cache this so t() doesn't run a bajillion times.
  2233. if (!isset($retval)) {
  2234. $retval = array(
  2235. 'field' => array(
  2236. 'title' => t('Fields'), // title
  2237. 'ltitle' => t('fields'), // lowercase title for mid-sentence
  2238. 'stitle' => t('Field'), // singular title
  2239. 'lstitle' => t('field'), // singular lowercase title for mid sentence
  2240. 'plural' => 'fields',
  2241. ),
  2242. 'argument' => array(
  2243. 'title' => t('Contextual filters'),
  2244. 'ltitle' => t('contextual filters'),
  2245. 'stitle' => t('Contextual filter'),
  2246. 'lstitle' => t('contextual filter'),
  2247. 'plural' => 'arguments',
  2248. ),
  2249. 'sort' => array(
  2250. 'title' => t('Sort criteria'),
  2251. 'ltitle' => t('sort criteria'),
  2252. 'stitle' => t('Sort criterion'),
  2253. 'lstitle' => t('sort criterion'),
  2254. 'plural' => 'sorts',
  2255. ),
  2256. 'filter' => array(
  2257. 'title' => t('Filter criteria'),
  2258. 'ltitle' => t('filter criteria'),
  2259. 'stitle' => t('Filter criterion'),
  2260. 'lstitle' => t('filter criterion'),
  2261. 'plural' => 'filters',
  2262. ),
  2263. 'relationship' => array(
  2264. 'title' => t('Relationships'),
  2265. 'ltitle' => t('relationships'),
  2266. 'stitle' => t('Relationship'),
  2267. 'lstitle' => t('Relationship'),
  2268. 'plural' => 'relationships',
  2269. ),
  2270. 'header' => array(
  2271. 'title' => t('Header'),
  2272. 'ltitle' => t('header'),
  2273. 'stitle' => t('Header'),
  2274. 'lstitle' => t('Header'),
  2275. 'plural' => 'header',
  2276. 'type' => 'area',
  2277. ),
  2278. 'footer' => array(
  2279. 'title' => t('Footer'),
  2280. 'ltitle' => t('footer'),
  2281. 'stitle' => t('Footer'),
  2282. 'lstitle' => t('Footer'),
  2283. 'plural' => 'footer',
  2284. 'type' => 'area',
  2285. ),
  2286. 'empty' => array(
  2287. 'title' => t('No results behavior'),
  2288. 'ltitle' => t('no results behavior'),
  2289. 'stitle' => t('No results behavior'),
  2290. 'lstitle' => t('No results behavior'),
  2291. 'plural' => 'empty',
  2292. 'type' => 'area',
  2293. ),
  2294. );
  2295. }
  2296. return $retval;
  2297. }
  2298. /**
  2299. * @}
  2300. */