view.inc 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. <?php
  2. /**
  3. * @file
  4. * Provides the view object type and associated methods.
  5. */
  6. /**
  7. * @defgroup views_objects Objects that represent a View or part of a view
  8. * @{
  9. * These objects are the core of Views do the bulk of the direction and
  10. * storing of data. All database activity is in these objects.
  11. */
  12. /**
  13. * An object to contain all of the data to generate a view, plus the member
  14. * functions to build the view query, execute the query and render the output.
  15. */
  16. class view extends views_db_object {
  17. var $db_table = 'views_view';
  18. var $base_table = 'node';
  19. var $base_field = 'nid';
  20. /**
  21. * The name of the view.
  22. *
  23. * @var string
  24. */
  25. var $name = "";
  26. /**
  27. * The id of the view, which is used only for views in the database.
  28. *
  29. * @var number
  30. */
  31. var $vid;
  32. /**
  33. * The description of the view, which is used only in the interface.
  34. *
  35. * @var string
  36. */
  37. var $description;
  38. /**
  39. * The "tags" of a view.
  40. * The tags are stored as a single string, though it is used as multiple tags
  41. * for example in the views overview.
  42. *
  43. * @var string
  44. */
  45. var $tag;
  46. /**
  47. * The human readable name of the view.
  48. *
  49. * @var string
  50. */
  51. var $human_name;
  52. /**
  53. * The core version the view was created for.
  54. * @var int
  55. */
  56. var $core;
  57. /**
  58. * The views-api version this view was created by.
  59. *
  60. * Some examples of the variable are 3.0 or 3.0-alpha1
  61. *
  62. * @var string
  63. */
  64. var $api_version;
  65. /**
  66. * Is the view disabled.
  67. *
  68. * This value is used for exported view, to provide some default views which aren't enabled.
  69. *
  70. * @var bool
  71. */
  72. var $disabled;
  73. // State variables
  74. var $built = FALSE;
  75. var $executed = FALSE;
  76. var $editing = FALSE;
  77. var $args = array();
  78. var $build_info = array();
  79. var $use_ajax = FALSE;
  80. /**
  81. * Where the results of a query will go.
  82. *
  83. * The array must use a numeric index starting at 0.
  84. *
  85. * @var array
  86. */
  87. var $result = array();
  88. // May be used to override the current pager info.
  89. var $current_page = NULL;
  90. var $items_per_page = NULL;
  91. var $offset = NULL;
  92. var $total_rows = NULL;
  93. // Places to put attached renderings:
  94. var $attachment_before = '';
  95. var $attachment_after = '';
  96. // Exposed widget input
  97. var $exposed_data = array();
  98. var $exposed_input = array();
  99. // Exposed widget input directly from the $form_state['values'].
  100. var $exposed_raw_input = array();
  101. // Used to store views that were previously running if we recurse.
  102. var $old_view = array();
  103. // To avoid recursion in views embedded into areas.
  104. var $parent_views = array();
  105. // Is the current stored view runned as an attachment to another view.
  106. var $is_attachment = NULL;
  107. // Stores the next steps of form items to handle.
  108. // It's an array of stack items, which contain the form id, the type of form,
  109. // the view, the display and some additional arguments.
  110. // @see views_ui_add_form_to_stack()
  111. // var $stack;
  112. /**
  113. * Identifier of the current display.
  114. *
  115. * @var string
  116. */
  117. var $current_display;
  118. /**
  119. * Where the $query object will reside:
  120. *
  121. * @var views_plugin_query
  122. */
  123. var $query = NULL;
  124. /**
  125. * The current used display plugin.
  126. *
  127. * @var views_plugin_display
  128. */
  129. var $display_handler;
  130. /**
  131. * Stores all display handlers of this view.
  132. *
  133. * @var array[views_display]
  134. */
  135. var $display;
  136. /**
  137. * The current used style plugin.
  138. *
  139. * @var views_plugin_style
  140. */
  141. var $style_plugin;
  142. /**
  143. * Stored the changed options of the style plugin.
  144. *
  145. * @deprecated Better use $view->style_plugin->options
  146. * @var array
  147. */
  148. var $style_options;
  149. /**
  150. * Stores the current active row while rendering.
  151. *
  152. * @var int
  153. */
  154. var $row_index;
  155. /**
  156. * Allow to override the url of the current view.
  157. *
  158. * @var string
  159. */
  160. var $override_url = NULL;
  161. /**
  162. * Allow to override the path used for generated urls.
  163. *
  164. * @var string
  165. */
  166. var $override_path = NULL;
  167. /**
  168. * Allow to override the used database which is used for this query.
  169. */
  170. var $base_database = NULL;
  171. /**
  172. * Here comes a list of the possible handler which are active on this view.
  173. */
  174. /**
  175. * Stores the field handlers which are initialized on this view.
  176. * @var array[views_handler_field]
  177. */
  178. var $field;
  179. /**
  180. * Stores the argument handlers which are initialized on this view.
  181. * @var array[views_handler_argument]
  182. */
  183. var $argument;
  184. /**
  185. * Stores the sort handlers which are initialized on this view.
  186. * @var array[views_handler_sort]
  187. */
  188. var $sort;
  189. /**
  190. * Stores the filter handlers which are initialized on this view.
  191. * @var array[views_handler_filter]
  192. */
  193. var $filter;
  194. /**
  195. * Stores the relationship handlers which are initialized on this view.
  196. * @var array[views_handler_relationship]
  197. */
  198. var $relationship;
  199. /**
  200. * Stores the area handlers for the header which are initialized on this view.
  201. * @var array[views_handler_area]
  202. */
  203. var $header;
  204. /**
  205. * Stores the area handlers for the footer which are initialized on this view.
  206. * @var array[views_handler_area]
  207. */
  208. var $footer;
  209. /**
  210. * Stores the area handlers for the empty text which are initialized on this view.
  211. * @var array[views_handler_area]
  212. */
  213. var $empty;
  214. /**
  215. * Constructor
  216. */
  217. function __construct() {
  218. parent::init();
  219. // Make sure all of our sub objects are arrays.
  220. foreach ($this->db_objects() as $object) {
  221. $this->$object = array();
  222. }
  223. }
  224. /**
  225. * Perform automatic updates when loading or importing a view.
  226. *
  227. * Over time, some things about Views or Drupal data has changed.
  228. * this attempts to do some automatic updates that must happen
  229. * to ensure older views will at least try to work.
  230. */
  231. function update() {
  232. // When views are converted automatically the base_table should be renamed
  233. // to have a working query.
  234. $this->base_table = views_move_table($this->base_table);
  235. }
  236. /**
  237. * Returns a list of the sub-object types used by this view. These types are
  238. * stored on the display, and are used in the build process.
  239. */
  240. function display_objects() {
  241. return array('argument', 'field', 'sort', 'filter', 'relationship', 'header', 'footer', 'empty');
  242. }
  243. /**
  244. * Returns the complete list of dependent objects in a view, for the purpose
  245. * of initialization and loading/saving to/from the database.
  246. */
  247. static function db_objects() {
  248. return array('display');
  249. }
  250. /**
  251. * Set the arguments that come to this view. Usually from the URL
  252. * but possibly from elsewhere.
  253. */
  254. function set_arguments($args) {
  255. $this->args = $args;
  256. }
  257. /**
  258. * Change/Set the current page for the pager.
  259. */
  260. function set_current_page($page) {
  261. $this->current_page = $page;
  262. // If the pager is already initialized, pass it through to the pager.
  263. if (!empty($this->query->pager)) {
  264. return $this->query->pager->set_current_page($page);
  265. }
  266. }
  267. /**
  268. * Get the current page from the pager.
  269. */
  270. function get_current_page() {
  271. // If the pager is already initialized, pass it through to the pager.
  272. if (!empty($this->query->pager)) {
  273. return $this->query->pager->get_current_page();
  274. }
  275. if (isset($this->current_page)) {
  276. return $this->current_page;
  277. }
  278. }
  279. /**
  280. * Get the items per page from the pager.
  281. */
  282. function get_items_per_page() {
  283. // If the pager is already initialized, pass it through to the pager.
  284. if (!empty($this->query->pager)) {
  285. return $this->query->pager->get_items_per_page();
  286. }
  287. if (isset($this->items_per_page)) {
  288. return $this->items_per_page;
  289. }
  290. }
  291. /**
  292. * Set the items per page on the pager.
  293. */
  294. function set_items_per_page($items_per_page) {
  295. $this->items_per_page = $items_per_page;
  296. // If the pager is already initialized, pass it through to the pager.
  297. if (!empty($this->query->pager)) {
  298. $this->query->pager->set_items_per_page($items_per_page);
  299. }
  300. }
  301. /**
  302. * Get the pager offset from the pager.
  303. */
  304. function get_offset() {
  305. // If the pager is already initialized, pass it through to the pager.
  306. if (!empty($this->query->pager)) {
  307. return $this->query->pager->get_offset();
  308. }
  309. if (isset($this->offset)) {
  310. return $this->offset;
  311. }
  312. }
  313. /**
  314. * Set the offset on the pager.
  315. */
  316. function set_offset($offset) {
  317. $this->offset = $offset;
  318. // If the pager is already initialized, pass it through to the pager.
  319. if (!empty($this->query->pager)) {
  320. $this->query->pager->set_offset($offset);
  321. }
  322. }
  323. /**
  324. * Determine if the pager actually uses a pager.
  325. */
  326. function use_pager() {
  327. if (!empty($this->query->pager)) {
  328. return $this->query->pager->use_pager();
  329. }
  330. }
  331. /**
  332. * Whether or not AJAX should be used. If AJAX is used, paging,
  333. * tablesorting and exposed filters will be fetched via an AJAX call
  334. * rather than a page refresh.
  335. */
  336. function set_use_ajax($use_ajax) {
  337. $this->use_ajax = $use_ajax;
  338. }
  339. /**
  340. * Set the exposed filters input to an array. If unset they will be taken
  341. * from $_GET when the time comes.
  342. */
  343. function set_exposed_input($filters) {
  344. $this->exposed_input = $filters;
  345. }
  346. /**
  347. * Figure out what the exposed input for this view is.
  348. */
  349. function get_exposed_input() {
  350. // Fill our input either from $_GET or from something previously set on the
  351. // view.
  352. if (empty($this->exposed_input)) {
  353. $this->exposed_input = $_GET;
  354. // unset items that are definitely not our input:
  355. foreach (array('page', 'q') as $key) {
  356. if (isset($this->exposed_input[$key])) {
  357. unset($this->exposed_input[$key]);
  358. }
  359. }
  360. // If we have no input at all, check for remembered input via session.
  361. // If filters are not overridden, store the 'remember' settings on the
  362. // default display. If they are, store them on this display. This way,
  363. // multiple displays in the same view can share the same filters and
  364. // remember settings.
  365. $display_id = ($this->display_handler->is_defaulted('filters')) ? 'default' : $this->current_display;
  366. if (empty($this->exposed_input) && !empty($_SESSION['views'][$this->name][$display_id])) {
  367. $this->exposed_input = $_SESSION['views'][$this->name][$display_id];
  368. }
  369. }
  370. return $this->exposed_input;
  371. }
  372. /**
  373. * Set the display for this view and initialize the display handler.
  374. */
  375. function init_display($reset = FALSE) {
  376. // The default display is always the first one in the list.
  377. if (isset($this->current_display)) {
  378. return TRUE;
  379. }
  380. // Instantiate all displays
  381. foreach (array_keys($this->display) as $id) {
  382. // Correct for shallow cloning
  383. // Often we'll have a cloned view so we don't mess up each other's
  384. // displays, but the clone is pretty shallow and doesn't necessarily
  385. // clone the displays. We can tell this by looking to see if a handler
  386. // has already been set; if it has, but $this->current_display is not
  387. // set, then something is dreadfully wrong.
  388. if (!empty($this->display[$id]->handler)) {
  389. $this->display[$id] = clone $this->display[$id];
  390. unset($this->display[$id]->handler);
  391. }
  392. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  393. if (!empty($this->display[$id]->handler)) {
  394. $this->display[$id]->handler->localization_keys = array($id);
  395. // Initialize the new display handler with data.
  396. $this->display[$id]->handler->init($this, $this->display[$id]);
  397. // If this is NOT the default display handler, let it know which is
  398. // since it may well utilize some data from the default.
  399. // This assumes that the 'default' handler is always first. It always
  400. // is. Make sure of it.
  401. if ($id != 'default') {
  402. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  403. }
  404. }
  405. }
  406. $this->current_display = 'default';
  407. $this->display_handler = &$this->display['default']->handler;
  408. return TRUE;
  409. }
  410. /**
  411. * Get the first display that is accessible to the user.
  412. *
  413. * @param $displays
  414. * Either a single display id or an array of display ids.
  415. */
  416. function choose_display($displays) {
  417. if (!is_array($displays)) {
  418. return $displays;
  419. }
  420. $this->init_display();
  421. foreach ($displays as $display_id) {
  422. if ($this->display[$display_id]->handler->access()) {
  423. return $display_id;
  424. }
  425. }
  426. return 'default';
  427. }
  428. /**
  429. * Set the display as current.
  430. *
  431. * @param $display_id
  432. * The id of the display to mark as current.
  433. */
  434. function set_display($display_id = NULL) {
  435. // If we have not already initialized the display, do so. But be careful.
  436. if (empty($this->current_display)) {
  437. $this->init_display();
  438. // If handlers were not initialized, and no argument was sent, set up
  439. // to the default display.
  440. if (empty($display_id)) {
  441. $display_id = 'default';
  442. }
  443. }
  444. $display_id = $this->choose_display($display_id);
  445. // If no display id sent in and one wasn't chosen above, we're finished.
  446. if (empty($display_id)) {
  447. return FALSE;
  448. }
  449. // Ensure the requested display exists.
  450. if (empty($this->display[$display_id])) {
  451. $display_id = 'default';
  452. if (empty($this->display[$display_id])) {
  453. vpr('set_display() called with invalid display id @display.', array('@display' => $display_id));
  454. return FALSE;
  455. }
  456. }
  457. // Set the current display.
  458. $this->current_display = $display_id;
  459. // Ensure requested display has a working handler.
  460. if (empty($this->display[$display_id]->handler)) {
  461. return FALSE;
  462. }
  463. // Set a shortcut
  464. $this->display_handler = &$this->display[$display_id]->handler;
  465. return TRUE;
  466. }
  467. /**
  468. * Find and initialize the style plugin.
  469. *
  470. * Note that arguments may have changed which style plugin we use, so
  471. * check the view object first, then ask the display handler.
  472. */
  473. function init_style() {
  474. if (isset($this->style_plugin)) {
  475. return is_object($this->style_plugin);
  476. }
  477. if (!isset($this->plugin_name)) {
  478. $this->plugin_name = $this->display_handler->get_option('style_plugin');
  479. $this->style_options = $this->display_handler->get_option('style_options');
  480. }
  481. $this->style_plugin = views_get_plugin('style', $this->plugin_name);
  482. if (empty($this->style_plugin)) {
  483. return FALSE;
  484. }
  485. // init the new style handler with data.
  486. $this->style_plugin->init($this, $this->display[$this->current_display], $this->style_options);
  487. return TRUE;
  488. }
  489. /**
  490. * Attempt to discover if the view has handlers missing relationships.
  491. *
  492. * This will try to add relationships automatically if it can, and will
  493. * remove the handlers if it cannot.
  494. */
  495. function fix_missing_relationships() {
  496. if (isset($this->relationships_fixed)) {
  497. return;
  498. }
  499. $this->relationships_fixed = TRUE;
  500. // Go through all of our handler types and test them to see if they
  501. // are missing relationships. Missing relationships can cause fatally
  502. // broken Views.
  503. $base_tables = array(
  504. $this->base_table => TRUE,
  505. '#global' => TRUE,
  506. );
  507. // For each relationship we have, make sure we mark the base it provides as
  508. // available.
  509. foreach ($this->display_handler->get_option('relationships') as $id => $options) {
  510. $options['table'] = views_move_table($options['table']);
  511. $data = views_fetch_data($options['table'], FALSE);
  512. if (isset($data[$options['field']]['relationship']['base'])) {
  513. $base_tables[$data[$options['field']]['relationship']['base']] = TRUE;
  514. }
  515. }
  516. $base_tables = array_keys($base_tables);
  517. $missing_base_tables = array();
  518. $types = views_object_types();
  519. foreach ($types as $key => $info) {
  520. foreach ($this->display_handler->get_option($info['plural']) as $id => $options) {
  521. $options['table'] = views_move_table($options['table']);
  522. $data = views_fetch_data($options['table'], FALSE);
  523. $valid_bases = array($options['table']);
  524. if (isset($data['table']['join'])) {
  525. $valid_bases = array_merge($valid_bases, array_keys($data['table']['join']));
  526. }
  527. // If the base table is missing, record it so we can try to fix it.
  528. if (!array_intersect($valid_bases, $base_tables)) {
  529. $missing_base_tables[$options['table']][] = array('type' => $key, 'id' => $id);
  530. }
  531. }
  532. }
  533. if (!empty($missing_base_tables)) {
  534. // This will change handlers, so make sure any existing handlers get
  535. // tossed.
  536. $this->display_handler->handlers = array();
  537. $this->relationships_changed = TRUE;
  538. $this->changed = TRUE;
  539. // Try to fix it.
  540. foreach ($missing_base_tables as $table => $handlers) {
  541. $data = views_fetch_data($table);
  542. $relationship = NULL;
  543. // Does the missing base table have a default relationship we can
  544. // throw in?
  545. if (isset($data['table']['default_relationship'][$this->base_table])) {
  546. // Create the relationship.
  547. $info = $data['table']['default_relationship'][$this->base_table];
  548. $relationship_options = isset($info['options']) ? $info['options'] : array();
  549. $relationship = $this->add_item($this->current_display, 'relationship', $info['table'], $info['field'], $relationship_options);
  550. }
  551. foreach ($handlers as $handler) {
  552. $options = $this->display_handler->get_option($types[$handler['type']]['plural']);
  553. if ($relationship) {
  554. $options[$handler['id']]['relationship'] = $relationship;
  555. }
  556. else {
  557. unset($options[$handler['id']]);
  558. }
  559. $this->display_handler->set_option($types[$handler['type']]['plural'], $options);
  560. }
  561. }
  562. }
  563. }
  564. /**
  565. * Acquire and attach all of the handlers.
  566. */
  567. function init_handlers() {
  568. if (empty($this->inited)) {
  569. $this->fix_missing_relationships();
  570. foreach (views_object_types() as $key => $info) {
  571. $this->_init_handler($key, $info);
  572. }
  573. $this->inited = TRUE;
  574. }
  575. }
  576. /**
  577. * Initialize the pager
  578. *
  579. * Like style initialization, pager initialization is held until late
  580. * to allow for overrides.
  581. */
  582. function init_pager() {
  583. if (empty($this->query->pager)) {
  584. $this->query->pager = $this->display_handler->get_plugin('pager');
  585. if ($this->query->pager->use_pager()) {
  586. $this->query->pager->set_current_page($this->current_page);
  587. }
  588. // These overrides may have been set earlier via $view->set_*
  589. // functions.
  590. if (isset($this->items_per_page)) {
  591. $this->query->pager->set_items_per_page($this->items_per_page);
  592. }
  593. if (isset($this->offset)) {
  594. $this->query->pager->set_offset($this->offset);
  595. }
  596. }
  597. }
  598. /**
  599. * Create a list of base tables eligible for this view. Used primarily
  600. * for the UI. Display must be already initialized.
  601. */
  602. function get_base_tables() {
  603. $base_tables = array(
  604. $this->base_table => TRUE,
  605. '#global' => TRUE,
  606. );
  607. foreach ($this->display_handler->get_handlers('relationship') as $handler) {
  608. $base_tables[$handler->definition['base']] = TRUE;
  609. }
  610. return $base_tables;
  611. }
  612. /**
  613. * Run the pre_query() on all active handlers.
  614. */
  615. function _pre_query() {
  616. foreach (views_object_types() as $key => $info) {
  617. $handlers = &$this->$key;
  618. $position = 0;
  619. foreach ($handlers as $id => $handler) {
  620. $handlers[$id]->position = $position;
  621. $handlers[$id]->pre_query();
  622. $position++;
  623. }
  624. }
  625. }
  626. /**
  627. * Run the post_execute() on all active handlers.
  628. */
  629. function _post_execute() {
  630. foreach (views_object_types() as $key => $info) {
  631. $handlers = &$this->$key;
  632. foreach ($handlers as $id => $handler) {
  633. $handlers[$id]->post_execute($this->result);
  634. }
  635. }
  636. }
  637. /**
  638. * Attach all of the handlers for each type.
  639. *
  640. * @param $key
  641. * One of 'argument', 'field', 'sort', 'filter', 'relationship'
  642. * @param $info
  643. * The $info from views_object_types for this object.
  644. */
  645. function _init_handler($key, $info) {
  646. // Load the requested items from the display onto the object.
  647. $this->$key = &$this->display_handler->get_handlers($key);
  648. // This reference deals with difficult PHP indirection.
  649. $handlers = &$this->$key;
  650. // Run through and test for accessibility.
  651. foreach ($handlers as $id => $handler) {
  652. if (!$handler->access()) {
  653. unset($handlers[$id]);
  654. }
  655. }
  656. }
  657. /**
  658. * Build all the arguments.
  659. */
  660. function _build_arguments() {
  661. // Initially, we want to build sorts and fields. This can change, though,
  662. // if we get a summary view.
  663. if (empty($this->argument)) {
  664. return TRUE;
  665. }
  666. // build arguments.
  667. $position = -1;
  668. // Create a title for use in the breadcrumb trail.
  669. $title = $this->display_handler->get_option('title');
  670. $this->build_info['breadcrumb'] = array();
  671. $breadcrumb_args = array();
  672. $substitutions = array();
  673. $status = TRUE;
  674. // Iterate through each argument and process.
  675. foreach ($this->argument as $id => $arg) {
  676. $position++;
  677. $argument = &$this->argument[$id];
  678. if ($argument->broken()) {
  679. continue;
  680. }
  681. $argument->set_relationship();
  682. $arg = isset($this->args[$position]) ? $this->args[$position] : NULL;
  683. $argument->position = $position;
  684. if (isset($arg) || $argument->has_default_argument()) {
  685. if (!isset($arg)) {
  686. $arg = $argument->get_default_argument();
  687. // make sure default args get put back.
  688. if (isset($arg)) {
  689. $this->args[$position] = $arg;
  690. }
  691. }
  692. // Set the argument, which will also validate that the argument can be set.
  693. if (!$argument->set_argument($arg)) {
  694. $status = $argument->validate_fail($arg);
  695. break;
  696. }
  697. if ($argument->is_exception()) {
  698. $arg_title = $argument->exception_title();
  699. }
  700. else {
  701. $arg_title = $argument->get_title();
  702. $argument->query($this->display_handler->use_group_by());
  703. }
  704. // Add this argument's substitution
  705. $substitutions['%' . ($position + 1)] = $arg_title;
  706. $substitutions['!' . ($position + 1)] = strip_tags(decode_entities($arg));
  707. // Since we're really generating the breadcrumb for the item above us,
  708. // check the default action of this argument.
  709. if ($this->display_handler->uses_breadcrumb() && $argument->uses_breadcrumb()) {
  710. $path = $this->get_url($breadcrumb_args);
  711. if (strpos($path, '%') === FALSE) {
  712. if (!empty($argument->options['breadcrumb_enable']) && !empty($argument->options['breadcrumb'])) {
  713. $breadcrumb = $argument->options['breadcrumb'];
  714. }
  715. else {
  716. $breadcrumb = $title;
  717. }
  718. $this->build_info['breadcrumb'][$path] = str_replace(array_keys($substitutions), $substitutions, $breadcrumb);
  719. }
  720. }
  721. // Allow the argument to muck with this breadcrumb.
  722. $argument->set_breadcrumb($this->build_info['breadcrumb']);
  723. // Test to see if we should use this argument's title
  724. if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
  725. $title = $argument->options['title'];
  726. }
  727. $breadcrumb_args[] = $arg;
  728. }
  729. else {
  730. // determine default condition and handle.
  731. $status = $argument->default_action();
  732. break;
  733. }
  734. // Be safe with references and loops:
  735. unset($argument);
  736. }
  737. // set the title in the build info.
  738. if (!empty($title)) {
  739. $this->build_info['title'] = $title;
  740. }
  741. // Store the arguments for later use.
  742. $this->build_info['substitutions'] = $substitutions;
  743. return $status;
  744. }
  745. /**
  746. * Do some common building initialization.
  747. */
  748. function init_query() {
  749. if (!empty($this->query)) {
  750. $class = get_class($this->query);
  751. if ($class && $class != 'stdClass') {
  752. // return if query is already initialized.
  753. return TRUE;
  754. }
  755. }
  756. // Create and initialize the query object.
  757. $views_data = views_fetch_data($this->base_table);
  758. $this->base_field = !empty($views_data['table']['base']['field']) ? $views_data['table']['base']['field'] : '';
  759. if (!empty($views_data['table']['base']['database'])) {
  760. $this->base_database = $views_data['table']['base']['database'];
  761. }
  762. // Load the options.
  763. $query_options = $this->display_handler->get_option('query');
  764. // Create and initialize the query object.
  765. $plugin = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
  766. $this->query = views_get_plugin('query', $plugin);
  767. if (empty($this->query)) {
  768. return FALSE;
  769. }
  770. $this->query->init($this->base_table, $this->base_field, $query_options['options']);
  771. return TRUE;
  772. }
  773. /**
  774. * Build the query for the view.
  775. */
  776. function build($display_id = NULL) {
  777. if (!empty($this->built)) {
  778. return;
  779. }
  780. if (empty($this->current_display) || $display_id) {
  781. if (!$this->set_display($display_id)) {
  782. return FALSE;
  783. }
  784. }
  785. // Let modules modify the view just prior to building it.
  786. foreach (module_implements('views_pre_build') as $module) {
  787. $function = $module . '_views_pre_build';
  788. $function($this);
  789. }
  790. // Attempt to load from cache.
  791. // @todo Load a build_info from cache.
  792. $start = microtime(TRUE);
  793. // If that fails, let's build!
  794. $this->build_info = array(
  795. 'query' => '',
  796. 'count_query' => '',
  797. 'query_args' => array(),
  798. );
  799. $this->init_query();
  800. // Call a module hook and see if it wants to present us with a
  801. // pre-built query or instruct us not to build the query for
  802. // some reason.
  803. // @todo: Implement this. Use the same mechanism Panels uses.
  804. // Run through our handlers and ensure they have necessary information.
  805. $this->init_handlers();
  806. // Let the handlers interact with each other if they really want.
  807. $this->_pre_query();
  808. if ($this->display_handler->uses_exposed()) {
  809. $exposed_form = $this->display_handler->get_plugin('exposed_form');
  810. // (1) Record the errors before rendering the exposed form widgets.
  811. $errors_before = form_set_error();
  812. $this->exposed_widgets = $exposed_form->render_exposed_form();
  813. // (2) Record the errors after rendering the exposed form widgets.
  814. $errors_after = form_set_error();
  815. // Find out if the validation of any of the elements in the exposed form
  816. // has failed by comparing (1) and (2) above. Don't mess with the view
  817. // otherwise.
  818. $exposed_errors = count($errors_after) > count($errors_before);
  819. if ($exposed_errors || !empty($this->build_info['abort'])) {
  820. $this->built = TRUE;
  821. // Don't execute the query, but rendering will still be executed to display the empty text.
  822. $this->executed = TRUE;
  823. return empty($this->build_info['fail']);
  824. }
  825. }
  826. // Build all the relationships first thing.
  827. $this->_build('relationship');
  828. // Set the filtering groups.
  829. if (!empty($this->filter)) {
  830. $filter_groups = $this->display_handler->get_option('filter_groups');
  831. if ($filter_groups) {
  832. $this->query->set_group_operator($filter_groups['operator']);
  833. foreach($filter_groups['groups'] as $id => $operator) {
  834. $this->query->set_where_group($operator, $id);
  835. }
  836. }
  837. }
  838. // Build all the filters.
  839. $this->_build('filter');
  840. $this->build_sort = TRUE;
  841. // Arguments can, in fact, cause this whole thing to abort.
  842. if (!$this->_build_arguments()) {
  843. $this->build_time = microtime(TRUE) - $start;
  844. $this->attach_displays();
  845. return $this->built;
  846. }
  847. // Initialize the style; arguments may have changed which style we use,
  848. // so waiting as long as possible is important. But we need to know
  849. // about the style when we go to build fields.
  850. if (!$this->init_style()) {
  851. $this->build_info['fail'] = TRUE;
  852. return FALSE;
  853. }
  854. if ($this->style_plugin->uses_fields()) {
  855. $this->_build('field');
  856. }
  857. // Build our sort criteria if we were instructed to do so.
  858. if (!empty($this->build_sort)) {
  859. // Allow the style handler to deal with sorting.
  860. if ($this->style_plugin->build_sort()) {
  861. $this->_build('sort');
  862. }
  863. // allow the plugin to build second sorts as well.
  864. $this->style_plugin->build_sort_post();
  865. }
  866. // Allow area handlers to affect the query.
  867. $this->_build('header');
  868. $this->_build('footer');
  869. $this->_build('empty');
  870. // Allow display handler to affect the query:
  871. $this->display_handler->query($this->display_handler->use_group_by());
  872. // Allow style handler to affect the query:
  873. $this->style_plugin->query($this->display_handler->use_group_by());
  874. // Allow exposed form to affect the query:
  875. if (isset($exposed_form)) {
  876. $exposed_form->query();
  877. }
  878. if (variable_get('views_sql_signature', FALSE)) {
  879. $this->query->add_signature($this);
  880. }
  881. // Let modules modify the query just prior to finalizing it.
  882. $this->query->alter($this);
  883. // Only build the query if we weren't interrupted.
  884. if (empty($this->built)) {
  885. // Build the necessary info to execute the query.
  886. $this->query->build($this);
  887. }
  888. $this->built = TRUE;
  889. $this->build_time = microtime(TRUE) - $start;
  890. // Attach displays
  891. $this->attach_displays();
  892. // Let modules modify the view just after building it.
  893. foreach (module_implements('views_post_build') as $module) {
  894. $function = $module . '_views_post_build';
  895. $function($this);
  896. }
  897. return TRUE;
  898. }
  899. /**
  900. * Internal method to build an individual set of handlers.
  901. *
  902. * @param string $key
  903. * The type of handlers (filter etc.) which should be iterated over to
  904. * build the relationship and query information.
  905. */
  906. function _build($key) {
  907. $handlers = &$this->$key;
  908. foreach ($handlers as $id => $data) {
  909. if (!empty($handlers[$id]) && is_object($handlers[$id])) {
  910. $multiple_exposed_input = array(0 => NULL);
  911. if ($handlers[$id]->multiple_exposed_input()) {
  912. $multiple_exposed_input = $handlers[$id]->group_multiple_exposed_input($this->exposed_data);
  913. }
  914. foreach ($multiple_exposed_input as $group_id) {
  915. // Give this handler access to the exposed filter input.
  916. if (!empty($this->exposed_data)) {
  917. $converted = FALSE;
  918. if ($handlers[$id]->is_a_group()) {
  919. $converted = $handlers[$id]->convert_exposed_input($this->exposed_data, $group_id);
  920. $handlers[$id]->store_group_input($this->exposed_data, $converted);
  921. if (!$converted) {
  922. continue;
  923. }
  924. }
  925. $rc = $handlers[$id]->accept_exposed_input($this->exposed_data);
  926. $handlers[$id]->store_exposed_input($this->exposed_data, $rc);
  927. if (!$rc) {
  928. continue;
  929. }
  930. }
  931. $handlers[$id]->set_relationship();
  932. $handlers[$id]->query($this->display_handler->use_group_by());
  933. }
  934. }
  935. }
  936. }
  937. /**
  938. * Execute the view's query.
  939. *
  940. * @param string $display_id
  941. * The machine name of the display, which should be executed.
  942. *
  943. * @return bool
  944. * Return whether the executing was successful, for example an argument
  945. * could stop the process.
  946. */
  947. function execute($display_id = NULL) {
  948. if (empty($this->built)) {
  949. if (!$this->build($display_id)) {
  950. return FALSE;
  951. }
  952. }
  953. if (!empty($this->executed)) {
  954. return TRUE;
  955. }
  956. // Don't allow to use deactivated displays, but display them on the live preview.
  957. if (!$this->display[$this->current_display]->handler->get_option('enabled') && empty($this->live_preview)) {
  958. $this->build_info['fail'] = TRUE;
  959. return FALSE;
  960. }
  961. // Let modules modify the view just prior to executing it.
  962. foreach (module_implements('views_pre_execute') as $module) {
  963. $function = $module . '_views_pre_execute';
  964. $function($this);
  965. }
  966. // Check for already-cached results.
  967. if (!empty($this->live_preview)) {
  968. $cache = FALSE;
  969. }
  970. else {
  971. $cache = $this->display_handler->get_plugin('cache');
  972. }
  973. if ($cache && $cache->cache_get('results')) {
  974. if($this->query->pager->use_pager() || !empty($this->get_total_rows)) {
  975. $this->query->pager->total_items = $this->total_rows;
  976. $this->query->pager->update_page_info();
  977. }
  978. vpr('Used cached results');
  979. }
  980. else {
  981. $this->query->execute($this);
  982. // Enforce the array key rule as documented in
  983. // views_plugin_query::execute().
  984. $this->result = array_values($this->result);
  985. $this->_post_execute();
  986. if ($cache) {
  987. $cache->cache_set('results');
  988. }
  989. }
  990. // Let modules modify the view just after executing it.
  991. foreach (module_implements('views_post_execute') as $module) {
  992. $function = $module . '_views_post_execute';
  993. $function($this);
  994. }
  995. $this->executed = TRUE;
  996. }
  997. /**
  998. * Render this view for a certain display.
  999. *
  1000. * Note: You should better use just the preview function if you want to
  1001. * render a view.
  1002. *
  1003. * @param string $display_id
  1004. * The machine name of the display, which should be rendered.
  1005. *
  1006. * @return (string|NULL)
  1007. * Return the output of the rendered view or NULL if something failed in the process.
  1008. */
  1009. function render($display_id = NULL) {
  1010. $this->execute($display_id);
  1011. // Check to see if the build failed.
  1012. if (!empty($this->build_info['fail'])) {
  1013. return;
  1014. }
  1015. if (!empty($this->view->build_info['denied'])) {
  1016. return;
  1017. }
  1018. drupal_theme_initialize();
  1019. $start = microtime(TRUE);
  1020. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1021. $this->start_query_capture();
  1022. }
  1023. $exposed_form = $this->display_handler->get_plugin('exposed_form');
  1024. $exposed_form->pre_render($this->result);
  1025. // Check for already-cached output.
  1026. if (!empty($this->live_preview)) {
  1027. $cache = FALSE;
  1028. }
  1029. else {
  1030. $cache = $this->display_handler->get_plugin('cache');
  1031. }
  1032. if ($cache && $cache->cache_get('output')) {
  1033. }
  1034. else {
  1035. if ($cache) {
  1036. $cache->cache_start();
  1037. }
  1038. // Run pre_render for the pager as it might change the result.
  1039. if (!empty($this->query->pager)) {
  1040. $this->query->pager->pre_render($this->result);
  1041. }
  1042. // Initialize the style plugin.
  1043. $this->init_style();
  1044. // Give field handlers the opportunity to perform additional queries
  1045. // using the entire resultset prior to rendering.
  1046. if ($this->style_plugin->uses_fields()) {
  1047. foreach ($this->field as $id => $handler) {
  1048. if (!empty($this->field[$id])) {
  1049. $this->field[$id]->pre_render($this->result);
  1050. }
  1051. }
  1052. }
  1053. $this->style_plugin->pre_render($this->result);
  1054. // Let modules modify the view just prior to rendering it.
  1055. foreach (module_implements('views_pre_render') as $module) {
  1056. $function = $module . '_views_pre_render';
  1057. $function($this);
  1058. }
  1059. // Let the themes play too, because pre render is a very themey thing.
  1060. foreach ($GLOBALS['base_theme_info'] as $base) {
  1061. $function = $base->name . '_views_pre_render';
  1062. if (function_exists($function)) {
  1063. $function($this);
  1064. }
  1065. }
  1066. $function = $GLOBALS['theme'] . '_views_pre_render';
  1067. if (function_exists($function)) {
  1068. $function($this);
  1069. }
  1070. $this->display_handler->output = $this->display_handler->render();
  1071. if ($cache) {
  1072. $cache->cache_set('output');
  1073. }
  1074. }
  1075. $this->render_time = microtime(TRUE) - $start;
  1076. $exposed_form->post_render($this->display_handler->output);
  1077. if ($cache) {
  1078. $cache->post_render($this->display_handler->output);
  1079. }
  1080. // Let modules modify the view output after it is rendered.
  1081. foreach (module_implements('views_post_render') as $module) {
  1082. $function = $module . '_views_post_render';
  1083. $function($this, $this->display_handler->output, $cache);
  1084. }
  1085. // Let the themes play too, because post render is a very themey thing.
  1086. foreach ($GLOBALS['base_theme_info'] as $base) {
  1087. $function = $base->name . '_views_post_render';
  1088. if (function_exists($function)) {
  1089. $function($this);
  1090. }
  1091. }
  1092. $function = $GLOBALS['theme'] . '_views_post_render';
  1093. if (function_exists($function)) {
  1094. $function($this, $this->display_handler->output, $cache);
  1095. }
  1096. if (!empty($this->live_preview) && variable_get('views_show_additional_queries', FALSE)) {
  1097. $this->end_query_capture();
  1098. }
  1099. return $this->display_handler->output;
  1100. }
  1101. /**
  1102. * Render a specific field via the field ID and the row #
  1103. *
  1104. * Note: You might want to use views_plugin_style::render_fields as it
  1105. * caches the output for you.
  1106. *
  1107. * @param string $field
  1108. * The id of the field to be rendered.
  1109. *
  1110. * @param int $row
  1111. * The row number in the $view->result which is used for the rendering.
  1112. *
  1113. * @return string
  1114. * The rendered output of the field.
  1115. */
  1116. function render_field($field, $row) {
  1117. if (isset($this->field[$field]) && isset($this->result[$row])) {
  1118. return $this->field[$field]->advanced_render($this->result[$row]);
  1119. }
  1120. }
  1121. /**
  1122. * Execute the given display, with the given arguments.
  1123. * To be called externally by whatever mechanism invokes the view,
  1124. * such as a page callback, hook_block, etc.
  1125. *
  1126. * This function should NOT be used by anything external as this
  1127. * returns data in the format specified by the display. It can also
  1128. * have other side effects that are only intended for the 'proper'
  1129. * use of the display, such as setting page titles and breadcrumbs.
  1130. *
  1131. * If you simply want to view the display, use view::preview() instead.
  1132. */
  1133. function execute_display($display_id = NULL, $args = array()) {
  1134. if (empty($this->current_display) || $this->current_display != $this->choose_display($display_id)) {
  1135. if (!$this->set_display($display_id)) {
  1136. return FALSE;
  1137. }
  1138. }
  1139. $this->pre_execute($args);
  1140. // Execute the view
  1141. $output = $this->display_handler->execute();
  1142. $this->post_execute();
  1143. return $output;
  1144. }
  1145. /**
  1146. * Preview the given display, with the given arguments.
  1147. *
  1148. * To be called externally, probably by an AJAX handler of some flavor.
  1149. * Can also be called when views are embedded, as this guarantees
  1150. * normalized output.
  1151. */
  1152. function preview($display_id = NULL, $args = array()) {
  1153. if (empty($this->current_display) || ((!empty($display_id)) && $this->current_display != $display_id)) {
  1154. if (!$this->set_display($display_id)) {
  1155. return FALSE;
  1156. }
  1157. }
  1158. $this->preview = TRUE;
  1159. $this->pre_execute($args);
  1160. // Preview the view.
  1161. $output = $this->display_handler->preview();
  1162. $this->post_execute();
  1163. return $output;
  1164. }
  1165. /**
  1166. * Run attachments and let the display do what it needs to do prior
  1167. * to running.
  1168. */
  1169. function pre_execute($args = array()) {
  1170. $this->old_view[] = views_get_current_view();
  1171. views_set_current_view($this);
  1172. $display_id = $this->current_display;
  1173. // Prepare the view with the information we have, but only if we were
  1174. // passed arguments, as they may have been set previously.
  1175. if ($args) {
  1176. $this->set_arguments($args);
  1177. }
  1178. // Let modules modify the view just prior to executing it.
  1179. foreach (module_implements('views_pre_view') as $module) {
  1180. $function = $module . '_views_pre_view';
  1181. $function($this, $display_id, $this->args);
  1182. }
  1183. // Allow hook_views_pre_view() to set the dom_id, then ensure it is set.
  1184. $this->dom_id = !empty($this->dom_id) ? $this->dom_id : md5($this->name . REQUEST_TIME . rand());
  1185. // Allow the display handler to set up for execution
  1186. $this->display_handler->pre_execute();
  1187. }
  1188. /**
  1189. * Unset the current view, mostly.
  1190. */
  1191. function post_execute() {
  1192. // unset current view so we can be properly destructed later on.
  1193. // Return the previous value in case we're an attachment.
  1194. if ($this->old_view) {
  1195. $old_view = array_pop($this->old_view);
  1196. }
  1197. views_set_current_view(isset($old_view) ? $old_view : FALSE);
  1198. }
  1199. /**
  1200. * Run attachment displays for the view.
  1201. */
  1202. function attach_displays() {
  1203. if (!empty($this->is_attachment)) {
  1204. return;
  1205. }
  1206. if (!$this->display_handler->accept_attachments()) {
  1207. return;
  1208. }
  1209. $this->is_attachment = TRUE;
  1210. // Give other displays an opportunity to attach to the view.
  1211. foreach ($this->display as $id => $display) {
  1212. if (!empty($this->display[$id]->handler)) {
  1213. $this->display[$id]->handler->attach_to($this->current_display);
  1214. }
  1215. }
  1216. $this->is_attachment = FALSE;
  1217. }
  1218. /**
  1219. * Called to get hook_menu() information from the view and the named display handler.
  1220. *
  1221. * @param $display_id
  1222. * A display id.
  1223. * @param $callbacks
  1224. * A menu callback array passed from views_menu_alter().
  1225. */
  1226. function execute_hook_menu($display_id = NULL, &$callbacks = array()) {
  1227. // Prepare the view with the information we have.
  1228. // This was probably already called, but it's good to be safe.
  1229. if (!$this->set_display($display_id)) {
  1230. return FALSE;
  1231. }
  1232. // Execute the view
  1233. if (isset($this->display_handler)) {
  1234. return $this->display_handler->execute_hook_menu($callbacks);
  1235. }
  1236. }
  1237. /**
  1238. * Called to get hook_block information from the view and the
  1239. * named display handler.
  1240. */
  1241. function execute_hook_block_list($display_id = NULL) {
  1242. // Prepare the view with the information we have.
  1243. // This was probably already called, but it's good to be safe.
  1244. if (!$this->set_display($display_id)) {
  1245. return FALSE;
  1246. }
  1247. // Execute the view
  1248. if (isset($this->display_handler)) {
  1249. return $this->display_handler->execute_hook_block_list();
  1250. }
  1251. }
  1252. /**
  1253. * Determine if the given user has access to the view. Note that
  1254. * this sets the display handler if it hasn't been.
  1255. */
  1256. function access($displays = NULL, $account = NULL) {
  1257. // Noone should have access to disabled views.
  1258. if (!empty($this->disabled)) {
  1259. return FALSE;
  1260. }
  1261. if (!isset($this->current_display)) {
  1262. $this->init_display();
  1263. }
  1264. if (!$account) {
  1265. $account = $GLOBALS['user'];
  1266. }
  1267. // We can't use choose_display() here because that function
  1268. // calls this one.
  1269. $displays = (array)$displays;
  1270. foreach ($displays as $display_id) {
  1271. if (!empty($this->display[$display_id]->handler)) {
  1272. if ($this->display[$display_id]->handler->access($account)) {
  1273. return TRUE;
  1274. }
  1275. }
  1276. }
  1277. return FALSE;
  1278. }
  1279. /**
  1280. * Get the view's current title. This can change depending upon how it
  1281. * was built.
  1282. */
  1283. function get_title() {
  1284. if (empty($this->display_handler)) {
  1285. if (!$this->set_display('default')) {
  1286. return FALSE;
  1287. }
  1288. }
  1289. // During building, we might find a title override. If so, use it.
  1290. if (!empty($this->build_info['title'])) {
  1291. $title = $this->build_info['title'];
  1292. }
  1293. else {
  1294. $title = $this->display_handler->get_option('title');
  1295. }
  1296. // Allow substitutions from the first row.
  1297. if ($this->init_style()) {
  1298. $title = $this->style_plugin->tokenize_value($title, 0);
  1299. }
  1300. return $title;
  1301. }
  1302. /**
  1303. * Override the view's current title.
  1304. *
  1305. * The tokens in the title get's replaced before rendering.
  1306. */
  1307. function set_title($title) {
  1308. $this->build_info['title'] = $title;
  1309. return TRUE;
  1310. }
  1311. /**
  1312. * Return the human readable name for a view.
  1313. *
  1314. * When a certain view doesn't have a human readable name return the machine readable name.
  1315. */
  1316. function get_human_name() {
  1317. if (!empty($this->human_name)) {
  1318. $human_name = $this->human_name;
  1319. }
  1320. else {
  1321. $human_name = $this->name;
  1322. }
  1323. return $human_name;
  1324. }
  1325. /**
  1326. * Force the view to build a title.
  1327. */
  1328. function build_title() {
  1329. $this->init_display();
  1330. if (empty($this->built)) {
  1331. $this->init_query();
  1332. }
  1333. $this->init_handlers();
  1334. $this->_build_arguments();
  1335. }
  1336. /**
  1337. * Get the URL for the current view.
  1338. *
  1339. * This URL will be adjusted for arguments.
  1340. */
  1341. function get_url($args = NULL, $path = NULL) {
  1342. if (!empty($this->override_url)) {
  1343. return $this->override_url;
  1344. }
  1345. if (!isset($path)) {
  1346. $path = $this->get_path();
  1347. }
  1348. if (!isset($args)) {
  1349. $args = $this->args;
  1350. // Exclude arguments that were computed, not passed on the URL.
  1351. $position = 0;
  1352. if (!empty($this->argument)) {
  1353. foreach ($this->argument as $argument_id => $argument) {
  1354. if (!empty($argument->options['default_argument_skip_url'])) {
  1355. unset($args[$position]);
  1356. }
  1357. $position++;
  1358. }
  1359. }
  1360. }
  1361. // Don't bother working if there's nothing to do:
  1362. if (empty($path) || (empty($args) && strpos($path, '%') === FALSE)) {
  1363. return $path;
  1364. }
  1365. $pieces = array();
  1366. $argument_keys = isset($this->argument) ? array_keys($this->argument) : array();
  1367. $id = current($argument_keys);
  1368. foreach (explode('/', $path) as $piece) {
  1369. if ($piece != '%') {
  1370. $pieces[] = $piece;
  1371. }
  1372. else {
  1373. if (empty($args)) {
  1374. // Try to never put % in a url; use the wildcard instead.
  1375. if ($id && !empty($this->argument[$id]->options['exception']['value'])) {
  1376. $pieces[] = $this->argument[$id]->options['exception']['value'];
  1377. }
  1378. else {
  1379. $pieces[] = '*'; // gotta put something if there just isn't one.
  1380. }
  1381. }
  1382. else {
  1383. $pieces[] = array_shift($args);
  1384. }
  1385. if ($id) {
  1386. $id = next($argument_keys);
  1387. }
  1388. }
  1389. }
  1390. if (!empty($args)) {
  1391. $pieces = array_merge($pieces, $args);
  1392. }
  1393. return implode('/', $pieces);
  1394. }
  1395. /**
  1396. * Get the base path used for this view.
  1397. */
  1398. function get_path() {
  1399. if (!empty($this->override_path)) {
  1400. return $this->override_path;
  1401. }
  1402. if (empty($this->display_handler)) {
  1403. if (!$this->set_display('default')) {
  1404. return FALSE;
  1405. }
  1406. }
  1407. return $this->display_handler->get_path();
  1408. }
  1409. /**
  1410. * Get the breadcrumb used for this view.
  1411. *
  1412. * @param $set
  1413. * If true, use drupal_set_breadcrumb() to install the breadcrumb.
  1414. */
  1415. function get_breadcrumb($set = FALSE) {
  1416. // Now that we've built the view, extract the breadcrumb.
  1417. $base = TRUE;
  1418. $breadcrumb = array();
  1419. if (!empty($this->build_info['breadcrumb'])) {
  1420. foreach ($this->build_info['breadcrumb'] as $path => $title) {
  1421. // Check to see if the frontpage is in the breadcrumb trail; if it
  1422. // is, we'll remove that from the actual breadcrumb later.
  1423. if ($path == variable_get('site_frontpage', 'node')) {
  1424. $base = FALSE;
  1425. $title = t('Home');
  1426. }
  1427. if ($title) {
  1428. $breadcrumb[] = l($title, $path, array('html' => TRUE));
  1429. }
  1430. }
  1431. if ($set) {
  1432. if ($base) {
  1433. $breadcrumb = array_merge(drupal_get_breadcrumb(), $breadcrumb);
  1434. }
  1435. drupal_set_breadcrumb($breadcrumb);
  1436. }
  1437. }
  1438. return $breadcrumb;
  1439. }
  1440. /**
  1441. * Is this view cacheable?
  1442. */
  1443. function is_cacheable() {
  1444. return $this->is_cacheable;
  1445. }
  1446. /**
  1447. * Set up query capturing.
  1448. *
  1449. * db_query() stores the queries that it runs in global $queries,
  1450. * bit only if dev_query is set to true. In this case, we want
  1451. * to temporarily override that setting if it's not and we
  1452. * can do that without forcing a db rewrite by just manipulating
  1453. * $conf. This is kind of evil but it works.
  1454. */
  1455. function start_query_capture() {
  1456. global $conf, $queries;
  1457. if (empty($conf['dev_query'])) {
  1458. $this->fix_dev_query = TRUE;
  1459. $conf['dev_query'] = TRUE;
  1460. }
  1461. // Record the last query key used; anything already run isn't
  1462. // a query that we are interested in.
  1463. $this->last_query_key = NULL;
  1464. if (!empty($queries)) {
  1465. $keys = array_keys($queries);
  1466. $this->last_query_key = array_pop($keys);
  1467. }
  1468. }
  1469. /**
  1470. * Add the list of queries run during render to buildinfo.
  1471. *
  1472. * @see view::start_query_capture()
  1473. */
  1474. function end_query_capture() {
  1475. global $conf, $queries;
  1476. if (!empty($this->fix_dev_query)) {
  1477. $conf['dev_query'] = FALSE;
  1478. }
  1479. // make a copy of the array so we can manipulate it with array_splice.
  1480. $temp = $queries;
  1481. // Scroll through the queries until we get to our last query key.
  1482. // Unset anything in our temp array.
  1483. if (isset($this->last_query_key)) {
  1484. while (list($id, $query) = each($queries)) {
  1485. if ($id == $this->last_query_key) {
  1486. break;
  1487. }
  1488. unset($temp[$id]);
  1489. }
  1490. }
  1491. $this->additional_queries = $temp;
  1492. }
  1493. /**
  1494. * Static factory method to load a list of views based upon a $where clause.
  1495. *
  1496. * Although this method could be implemented to simply iterate over views::load(),
  1497. * that would be very slow. Buiding the views externally from unified queries is
  1498. * much faster.
  1499. */
  1500. static function load_views() {
  1501. $result = db_query("SELECT DISTINCT v.* FROM {views_view} v");
  1502. $views = array();
  1503. // Load all the views.
  1504. foreach ($result as $data) {
  1505. $view = new view;
  1506. $view->load_row($data);
  1507. $view->loaded = TRUE;
  1508. $view->type = t('Normal');
  1509. $views[$view->name] = $view;
  1510. $names[$view->vid] = $view->name;
  1511. }
  1512. // Stop if we didn't get any views.
  1513. if (!$views) {
  1514. return array();
  1515. }
  1516. // Now load all the subtables:
  1517. foreach (view::db_objects() as $key) {
  1518. $object_name = "views_$key";
  1519. $result = db_query("SELECT * FROM {{$object_name}} WHERE vid IN (:vids) ORDER BY vid, position",
  1520. array(':vids' => array_keys($names)));
  1521. foreach ($result as $data) {
  1522. $object = new $object_name(FALSE);
  1523. $object->load_row($data);
  1524. // Because it can get complicated with this much indirection,
  1525. // make a shortcut reference.
  1526. $location = &$views[$names[$object->vid]]->$key;
  1527. // If we have a basic id field, load the item onto the view based on
  1528. // this ID, otherwise push it on.
  1529. if (!empty($object->id)) {
  1530. $location[$object->id] = $object;
  1531. }
  1532. else {
  1533. $location[] = $object;
  1534. }
  1535. }
  1536. }
  1537. return $views;
  1538. }
  1539. /**
  1540. * Save the view to the database. If the view does not already exist,
  1541. * A vid will be assigned to the view and also returned from this function.
  1542. */
  1543. function save() {
  1544. if ($this->vid == 'new') {
  1545. $this->vid = NULL;
  1546. }
  1547. // If there is no vid, check if a view with this machine name already exists.
  1548. elseif (empty($this->vid)) {
  1549. $vid = db_query("SELECT vid from {views_view} WHERE name = :name", array(':name' => $this->name))->fetchField();
  1550. $this->vid = $vid ? $vid : NULL;
  1551. }
  1552. // Let modules modify the view just prior to saving it.
  1553. module_invoke_all('views_view_presave', $this);
  1554. $transaction = db_transaction();
  1555. try {
  1556. // If we have no vid or our vid is a string, this is a new view.
  1557. if (!empty($this->vid)) {
  1558. // remove existing table entries
  1559. foreach ($this->db_objects() as $key) {
  1560. db_delete('views_' . $key)
  1561. ->condition('vid', $this->vid)
  1562. ->execute();
  1563. }
  1564. }
  1565. $this->save_row(!empty($this->vid) ? 'vid' : FALSE);
  1566. // Save all of our subtables.
  1567. foreach ($this->db_objects() as $key) {
  1568. $this->_save_rows($key);
  1569. }
  1570. }
  1571. catch (Exception $e) {
  1572. $transaction->rollback();
  1573. watchdog_exception('views', $e);
  1574. throw $e;
  1575. }
  1576. $this->save_locale_strings();
  1577. // Clear caches.
  1578. views_invalidate_cache();
  1579. // Notify modules that this view has been saved.
  1580. module_invoke_all('views_view_save', $this);
  1581. }
  1582. /**
  1583. * Save a row to the database for the given key, which is one of the
  1584. * keys from view::db_objects()
  1585. */
  1586. function _save_rows($key) {
  1587. $count = 0;
  1588. foreach ($this->$key as $position => $object) {
  1589. $object->position = ++$count;
  1590. $object->vid = $this->vid;
  1591. $object->save_row();
  1592. }
  1593. }
  1594. /**
  1595. * Delete the view from the database.
  1596. */
  1597. function delete($clear = TRUE) {
  1598. if (empty($this->vid)) {
  1599. return;
  1600. }
  1601. db_delete('views_view')
  1602. ->condition('vid', $this->vid)
  1603. ->execute();
  1604. // Delete from all of our subtables as well.
  1605. foreach ($this->db_objects() as $key) {
  1606. db_delete('views_'. $key)
  1607. ->condition('vid', $this->vid)
  1608. ->execute();
  1609. }
  1610. cache_clear_all('views_query:' . $this->name, 'cache_views');
  1611. if ($clear) {
  1612. // Clear caches.
  1613. views_invalidate_cache();
  1614. }
  1615. // Notify modules that this view has been deleted.
  1616. module_invoke_all('views_view_delete', $this);
  1617. }
  1618. /**
  1619. * Export a view as PHP code.
  1620. */
  1621. function export($indent = '') {
  1622. $this->init_display();
  1623. $this->init_query();
  1624. $output = '';
  1625. $output .= $this->export_row('view', $indent);
  1626. // Set the API version
  1627. $output .= $indent . '$view->api_version = \'' . views_api_version() . "';\n";
  1628. $output .= $indent . '$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */' . "\n";
  1629. foreach ($this->display as $id => $display) {
  1630. $output .= "\n" . $indent . "/* Display: $display->display_title */\n";
  1631. $output .= $indent . '$handler = $view->new_display(' . ctools_var_export($display->display_plugin, $indent) . ', ' . ctools_var_export($display->display_title, $indent) . ', \'' . $id . "');\n";
  1632. if (empty($display->handler)) {
  1633. // @todo -- probably need a method of exporting broken displays as
  1634. // they may simply be broken because a module is not installed. That
  1635. // does not invalidate the display.
  1636. continue;
  1637. }
  1638. $output .= $display->handler->export_options($indent, '$handler->options');
  1639. }
  1640. // Give the localization system a chance to export translatables to code.
  1641. if ($this->init_localization()) {
  1642. $this->export_locale_strings('export');
  1643. $translatables = $this->localization_plugin->export_render($indent);
  1644. if (!empty($translatables)) {
  1645. $output .= $translatables;
  1646. }
  1647. }
  1648. return $output;
  1649. }
  1650. /**
  1651. * Make a copy of this view that has been sanitized of all database IDs
  1652. * and handlers and other stuff.
  1653. *
  1654. * I'd call this clone() but it's reserved.
  1655. */
  1656. function copy() {
  1657. $code = $this->export();
  1658. eval($code);
  1659. return $view;
  1660. }
  1661. /**
  1662. * Safely clone a view.
  1663. *
  1664. * Because views are complicated objects within objects, and PHP loves to
  1665. * do references to everything, if a View is not properly and safely
  1666. * cloned it will still have references to the original view, and can
  1667. * actually cause the original view to point to objects in the cloned
  1668. * view. This gets ugly fast.
  1669. *
  1670. * This will completely wipe a view clean so it can be considered fresh.
  1671. *
  1672. * @return view
  1673. * The cloned view.
  1674. */
  1675. function clone_view() {
  1676. $clone = version_compare(phpversion(), '5.0') < 0 ? $this : clone($this);
  1677. $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');
  1678. foreach ($keys as $key) {
  1679. if (isset($clone->$key)) {
  1680. unset($clone->$key);
  1681. }
  1682. }
  1683. $clone->built = $clone->executed = FALSE;
  1684. $clone->build_info = array();
  1685. $clone->attachment_before = '';
  1686. $clone->attachment_after = '';
  1687. $clone->result = array();
  1688. // shallow cloning means that all the display objects
  1689. // *were not cloned*. We must clone them ourselves.
  1690. $displays = array();
  1691. foreach ($clone->display as $id => $display) {
  1692. $displays[$id] = clone $display;
  1693. if (isset($displays[$id]->handler)) {
  1694. unset($displays[$id]->handler);
  1695. }
  1696. }
  1697. $clone->display = $displays;
  1698. return $clone;
  1699. }
  1700. /**
  1701. * Unset references so that a $view object may be properly garbage
  1702. * collected.
  1703. */
  1704. function destroy() {
  1705. foreach (array_keys($this->display) as $display_id) {
  1706. if (isset($this->display[$display_id]->handler)) {
  1707. $this->display[$display_id]->handler->destroy();
  1708. unset($this->display[$display_id]->handler);
  1709. }
  1710. }
  1711. foreach (views_object_types() as $type => $info) {
  1712. if (isset($this->$type)) {
  1713. $handlers = &$this->$type;
  1714. foreach ($handlers as $id => $item) {
  1715. $handlers[$id]->destroy();
  1716. }
  1717. unset($handlers);
  1718. }
  1719. }
  1720. if (isset($this->style_plugin)) {
  1721. $this->style_plugin->destroy();
  1722. unset($this->style_plugin);
  1723. }
  1724. // Clear these to make sure the view can be processed/used again.
  1725. if (isset($this->display_handler)) {
  1726. unset($this->display_handler);
  1727. }
  1728. if (isset($this->current_display)) {
  1729. unset($this->current_display);
  1730. }
  1731. if (isset($this->query)) {
  1732. unset($this->query);
  1733. }
  1734. $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');
  1735. foreach ($keys as $key) {
  1736. if (isset($this->$key)) {
  1737. unset($this->$key);
  1738. }
  1739. }
  1740. // These keys are checked by the next init, so instead of unsetting them,
  1741. // just set the default values.
  1742. $keys = array('items_per_page', 'offset', 'current_page');
  1743. foreach ($keys as $key) {
  1744. if (isset($this->$key)) {
  1745. $this->$key = NULL;
  1746. }
  1747. }
  1748. $this->built = $this->executed = FALSE;
  1749. $this->build_info = array();
  1750. $this->attachment_before = '';
  1751. $this->attachment_after = '';
  1752. }
  1753. /**
  1754. * Make sure the view is completely valid.
  1755. *
  1756. * @return
  1757. * TRUE if the view is valid; an array of error strings if it is not.
  1758. */
  1759. function validate() {
  1760. $this->init_display();
  1761. $errors = array();
  1762. $this->display_errors = NULL;
  1763. $current_display = $this->current_display;
  1764. foreach ($this->display as $id => $display) {
  1765. if ($display->handler) {
  1766. if (!empty($display->deleted)) {
  1767. continue;
  1768. }
  1769. $result = $this->display[$id]->handler->validate();
  1770. if (!empty($result) && is_array($result)) {
  1771. $errors = array_merge($errors, $result);
  1772. // Mark this display as having validation errors.
  1773. $this->display_errors[$id] = TRUE;
  1774. }
  1775. }
  1776. }
  1777. $this->set_display($current_display);
  1778. return $errors ? $errors : TRUE;
  1779. }
  1780. /**
  1781. * Find and initialize the localization plugin.
  1782. */
  1783. function init_localization() {
  1784. // If the translate attribute isn't set, init the localization plugin.
  1785. if (!isset($this->localization_plugin->translate)) {
  1786. $this->localization_plugin = views_get_plugin('localization', views_get_localization_plugin());
  1787. // If the plugin is still not set, turn off all localization by using the
  1788. // views_plugin_localization_none plugin. This plugin has the translate
  1789. // property set to FALSE, signifying localization should not occur.
  1790. if (empty($this->localization_plugin)) {
  1791. $this->localization_plugin = views_get_plugin('localization', 'none');
  1792. }
  1793. // Init the plugin.
  1794. $this->localization_plugin->init($this);
  1795. }
  1796. // Return the value of the translate property. This is set to FALSE if
  1797. // localization is off.
  1798. return $this->localization_plugin->translate;
  1799. }
  1800. /**
  1801. * Determine whether a view supports admin string translation.
  1802. */
  1803. function is_translatable() {
  1804. // Use translation no matter what type of view.
  1805. if (variable_get('views_localize_all', FALSE)) {
  1806. return TRUE;
  1807. }
  1808. // If the view is normal or overridden, use admin string translation.
  1809. // A newly created view won't have a type. Accept this.
  1810. return (!isset($this->type) || in_array($this->type, array(t('Normal'), t('Overridden')))) ? TRUE : FALSE;
  1811. }
  1812. /**
  1813. * Send strings for localization.
  1814. */
  1815. function save_locale_strings() {
  1816. $this->process_locale_strings('save');
  1817. }
  1818. /**
  1819. * Delete localized strings.
  1820. */
  1821. function delete_locale_strings() {
  1822. $this->process_locale_strings('delete');
  1823. }
  1824. /**
  1825. * Export localized strings.
  1826. */
  1827. function export_locale_strings() {
  1828. $this->process_locale_strings('export');
  1829. }
  1830. /**
  1831. * Process strings for localization, deletion or export to code.
  1832. */
  1833. function process_locale_strings($op) {
  1834. // Ensure this view supports translation, we have a display, and we
  1835. // have a localization plugin.
  1836. // @fixme Export does not init every handler.
  1837. if (($this->is_translatable() || $op == 'export') && $this->init_display() && $this->init_localization()) {
  1838. $this->localization_plugin->process_locale_strings($op);
  1839. }
  1840. }
  1841. }
  1842. /**
  1843. * Base class for views' database objects.
  1844. */
  1845. class views_db_object {
  1846. public $db_table;
  1847. /**
  1848. * Initialize this object, setting values from schema defaults.
  1849. *
  1850. * @param $init
  1851. * If an array, this is a set of values from db_fetch_object to
  1852. * load. Otherwse, if TRUE values will be filled in from schema
  1853. * defaults.
  1854. */
  1855. function init($init = TRUE) {
  1856. if (is_array($init)) {
  1857. return $this->load_row($init);
  1858. }
  1859. if (!$init) {
  1860. return;
  1861. }
  1862. $schema = drupal_get_schema($this->db_table);
  1863. if (!$schema) {
  1864. return;
  1865. }
  1866. // Go through our schema and build correlations.
  1867. foreach ($schema['fields'] as $field => $info) {
  1868. if ($info['type'] == 'serial') {
  1869. $this->$field = NULL;
  1870. }
  1871. if (!isset($this->$field)) {
  1872. if (!empty($info['serialize']) && isset($info['serialized default'])) {
  1873. $this->$field = unserialize($info['serialized default']);
  1874. }
  1875. elseif (isset($info['default'])) {
  1876. $this->$field = $info['default'];
  1877. }
  1878. else {
  1879. $this->$field = '';
  1880. }
  1881. }
  1882. }
  1883. }
  1884. /**
  1885. * Write the row to the database.
  1886. *
  1887. * @param $update
  1888. * If true this will be an UPDATE query. Otherwise it will be an INSERT.
  1889. */
  1890. function save_row($update = NULL) {
  1891. $fields = $defs = $values = $serials = array();
  1892. $schema = drupal_get_schema($this->db_table);
  1893. // Go through our schema and build correlations.
  1894. foreach ($schema['fields'] as $field => $info) {
  1895. // special case -- skip serial types if we are updating.
  1896. if ($info['type'] == 'serial') {
  1897. $serials[] = $field;
  1898. continue;
  1899. }
  1900. elseif ($info['type'] == 'int') {
  1901. $this->$field = (int) $this->$field;
  1902. }
  1903. $fields[$field] = empty($info['serialize']) ? $this->$field : serialize($this->$field);
  1904. }
  1905. if (!$update) {
  1906. $query = db_insert($this->db_table);
  1907. }
  1908. else {
  1909. $query = db_update($this->db_table)
  1910. ->condition($update, $this->$update);
  1911. }
  1912. $return = $query
  1913. ->fields($fields)
  1914. ->execute();
  1915. if ($serials && !$update) {
  1916. // get last insert ids and fill them in.
  1917. // Well, one ID.
  1918. foreach ($serials as $field) {
  1919. $this->$field = $return;
  1920. }
  1921. }
  1922. }
  1923. /**
  1924. * Load the object with a row from the database.
  1925. *
  1926. * This method is separate from the constructor in order to give us
  1927. * more flexibility in terms of how the view object is built in different
  1928. * contexts.
  1929. *
  1930. * @param $data
  1931. * An object from db_fetch_object. It should contain all of the fields
  1932. * that are in the schema.
  1933. */
  1934. function load_row($data) {
  1935. $schema = drupal_get_schema($this->db_table);
  1936. // Go through our schema and build correlations.
  1937. foreach ($schema['fields'] as $field => $info) {
  1938. $this->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
  1939. }
  1940. }
  1941. /**
  1942. * Export a loaded row, such as an argument, field or the view itself to PHP code.
  1943. *
  1944. * @param $identifier
  1945. * The variable to assign the PHP code for this object to.
  1946. * @param $indent
  1947. * An optional indentation for prettifying nested code.
  1948. */
  1949. function export_row($identifier = NULL, $indent = '') {
  1950. ctools_include('export');
  1951. if (!$identifier) {
  1952. $identifier = $this->db_table;
  1953. }
  1954. $schema = drupal_get_schema($this->db_table);
  1955. $output = $indent . '$' . $identifier . ' = new ' . get_class($this) . "();\n";
  1956. // Go through our schema and build correlations.
  1957. foreach ($schema['fields'] as $field => $info) {
  1958. if (!empty($info['no export'])) {
  1959. continue;
  1960. }
  1961. if (!isset($this->$field)) {
  1962. if (isset($info['default'])) {
  1963. $this->$field = $info['default'];
  1964. }
  1965. else {
  1966. $this->$field = '';
  1967. }
  1968. // serialized defaults must be set as serialized.
  1969. if (isset($info['serialize'])) {
  1970. $this->$field = unserialize($this->$field);
  1971. }
  1972. }
  1973. $value = $this->$field;
  1974. if ($info['type'] == 'int') {
  1975. if (isset($info['size']) && $info['size'] == 'tiny') {
  1976. $value = (bool) $value;
  1977. }
  1978. else {
  1979. $value = (int) $value;
  1980. }
  1981. }
  1982. $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  1983. }
  1984. return $output;
  1985. }
  1986. /**
  1987. * Add a new display handler to the view, automatically creating an id.
  1988. *
  1989. * @param $type
  1990. * The plugin type from the views plugin data. Defaults to 'page'.
  1991. * @param $title
  1992. * The title of the display; optional, may be filled in from default.
  1993. * @param $id
  1994. * The id to use.
  1995. * @return
  1996. * The key to the display in $view->display, so that the new display
  1997. * can be easily located.
  1998. */
  1999. function add_display($type = 'page', $title = NULL, $id = NULL) {
  2000. if (empty($type)) {
  2001. return FALSE;
  2002. }
  2003. $plugin = views_fetch_plugin_data('display', $type);
  2004. if (empty($plugin)) {
  2005. $plugin['title'] = t('Broken');
  2006. }
  2007. if (empty($id)) {
  2008. $id = $this->generate_display_id($type);
  2009. if ($id !== 'default') {
  2010. preg_match("/[0-9]+/", $id, $count);
  2011. $count = $count[0];
  2012. }
  2013. else {
  2014. $count = '';
  2015. }
  2016. if (empty($title)) {
  2017. if ($count > 1) {
  2018. $title = $plugin['title'] . ' ' . $count;
  2019. }
  2020. else {
  2021. $title = $plugin['title'];
  2022. }
  2023. }
  2024. }
  2025. // Create the new display object
  2026. $display = new views_display;
  2027. $display->options($type, $id, $title);
  2028. // Add the new display object to the view.
  2029. $this->display[$id] = $display;
  2030. return $id;
  2031. }
  2032. /**
  2033. * Generate a display id of a certain plugin type.
  2034. *
  2035. * @param $type
  2036. * Which plugin should be used for the new display id.
  2037. */
  2038. function generate_display_id($type) {
  2039. // 'default' is singular and is unique, so just go with 'default'
  2040. // for it. For all others, start counting.
  2041. if ($type == 'default') {
  2042. return 'default';
  2043. }
  2044. // Initial id.
  2045. $id = $type . '_1';
  2046. $count = 1;
  2047. // Loop through IDs based upon our style plugin name until
  2048. // we find one that is unused.
  2049. while (!empty($this->display[$id])) {
  2050. $id = $type . '_' . ++$count;
  2051. }
  2052. return $id;
  2053. }
  2054. /**
  2055. * Generates a unique ID for an item.
  2056. *
  2057. * These items are typically fields, filters, sort criteria, or arguments.
  2058. *
  2059. * @param $requested_id
  2060. * The requested ID for the item.
  2061. * @param $existing_items
  2062. * An array of existing items, keyed by their IDs.
  2063. *
  2064. * @return
  2065. * A unique ID. This will be equal to $requested_id if no item with that ID
  2066. * already exists. Otherwise, it will be appended with an integer to make
  2067. * it unique, e.g. "{$requested_id}_1", "{$requested_id}_2", etc.
  2068. */
  2069. public static function generate_item_id($requested_id, $existing_items) {
  2070. $count = 0;
  2071. $id = $requested_id;
  2072. while (!empty($existing_items[$id])) {
  2073. $id = $requested_id . '_' . ++$count;
  2074. }
  2075. return $id;
  2076. }
  2077. /**
  2078. * Create a new display and a display handler for it.
  2079. * @param $type
  2080. * The plugin type from the views plugin data. Defaults to 'page'.
  2081. * @param $title
  2082. * The title of the display; optional, may be filled in from default.
  2083. * @param $id
  2084. * The id to use.
  2085. * @return views_plugin_display
  2086. * A reference to the new handler object.
  2087. */
  2088. function &new_display($type = 'page', $title = NULL, $id = NULL) {
  2089. $id = $this->add_display($type, $title, $id);
  2090. // Create a handler
  2091. $this->display[$id]->handler = views_get_plugin('display', $this->display[$id]->display_plugin);
  2092. if (empty($this->display[$id]->handler)) {
  2093. // provide a 'default' handler as an emergency. This won't work well but
  2094. // it will keep things from crashing.
  2095. $this->display[$id]->handler = views_get_plugin('display', 'default');
  2096. }
  2097. if (!empty($this->display[$id]->handler)) {
  2098. // Initialize the new display handler with data.
  2099. $this->display[$id]->handler->init($this, $this->display[$id]);
  2100. // If this is NOT the default display handler, let it know which is
  2101. if ($id != 'default') {
  2102. $this->display[$id]->handler->default_display = &$this->display['default']->handler;
  2103. }
  2104. }
  2105. return $this->display[$id]->handler;
  2106. }
  2107. /**
  2108. * Add an item with a handler to the view.
  2109. *
  2110. * These items may be fields, filters, sort criteria, or arguments.
  2111. */
  2112. function add_item($display_id, $type, $table, $field, $options = array(), $id = NULL) {
  2113. $types = views_object_types();
  2114. $this->set_display($display_id);
  2115. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2116. if (empty($id)) {
  2117. $id = $this->generate_item_id($field, $fields);
  2118. }
  2119. $new_item = array(
  2120. 'id' => $id,
  2121. 'table' => $table,
  2122. 'field' => $field,
  2123. ) + $options;
  2124. if (!empty($types[$type]['type'])) {
  2125. $handler_type = $types[$type]['type'];
  2126. }
  2127. else {
  2128. $handler_type = $type;
  2129. }
  2130. $handler = views_get_handler($table, $field, $handler_type);
  2131. $fields[$id] = $new_item;
  2132. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2133. return $id;
  2134. }
  2135. /**
  2136. * Get an array of items for the current display.
  2137. */
  2138. function get_items($type, $display_id = NULL) {
  2139. $this->set_display($display_id);
  2140. if (!isset($display_id)) {
  2141. $display_id = $this->current_display;
  2142. }
  2143. // Get info about the types so we can get the right data.
  2144. $types = views_object_types();
  2145. return $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2146. }
  2147. /**
  2148. * Get the configuration of an item (field/sort/filter/etc) on a given
  2149. * display.
  2150. */
  2151. function get_item($display_id, $type, $id) {
  2152. // Get info about the types so we can get the right data.
  2153. $types = views_object_types();
  2154. // Initialize the display
  2155. $this->set_display($display_id);
  2156. // Get the existing configuration
  2157. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2158. return isset($fields[$id]) ? $fields[$id] : NULL;
  2159. }
  2160. /**
  2161. * Set the configuration of an item (field/sort/filter/etc) on a given
  2162. * display.
  2163. *
  2164. * Pass in NULL for the $item to remove an item.
  2165. */
  2166. function set_item($display_id, $type, $id, $item) {
  2167. // Get info about the types so we can get the right data.
  2168. $types = views_object_types();
  2169. // Initialize the display
  2170. $this->set_display($display_id);
  2171. // Get the existing configuration
  2172. $fields = $this->display[$display_id]->handler->get_option($types[$type]['plural']);
  2173. if (isset($item)) {
  2174. $fields[$id] = $item;
  2175. }
  2176. else {
  2177. unset($fields[$id]);
  2178. }
  2179. // Store.
  2180. $this->display[$display_id]->handler->set_option($types[$type]['plural'], $fields);
  2181. }
  2182. /**
  2183. * Set an option on an item.
  2184. *
  2185. * Use this only if you have just 1 or 2 options to set; if you have
  2186. * many, consider getting the item, adding the options and doing
  2187. * set_item yourself.
  2188. */
  2189. function set_item_option($display_id, $type, $id, $option, $value) {
  2190. $item = $this->get_item($display_id, $type, $id);
  2191. $item[$option] = $value;
  2192. $this->set_item($display_id, $type, $id, $item);
  2193. }
  2194. }
  2195. /**
  2196. * A display type in a view.
  2197. *
  2198. * This is just the database storage mechanism, and isn't terribly important
  2199. * to the behavior of the display at all.
  2200. */
  2201. class views_display extends views_db_object {
  2202. /**
  2203. * The display handler itself, which has all the methods.
  2204. *
  2205. * @var views_plugin_display
  2206. */
  2207. var $handler;
  2208. /**
  2209. * Stores all options of the display, like fields, filters etc.
  2210. *
  2211. * @var array
  2212. */
  2213. var $display_options;
  2214. var $db_table = 'views_display';
  2215. function __construct($init = TRUE) {
  2216. parent::init($init);
  2217. }
  2218. function options($type, $id, $title) {
  2219. $this->display_plugin = $type;
  2220. $this->id = $id;
  2221. $this->display_title = $title;
  2222. }
  2223. }
  2224. /**
  2225. * Provide a list of views object types used in a view, with some information
  2226. * about them.
  2227. */
  2228. function views_object_types() {
  2229. static $retval = NULL;
  2230. // statically cache this so t() doesn't run a bajillion times.
  2231. if (!isset($retval)) {
  2232. $retval = array(
  2233. 'field' => array(
  2234. 'title' => t('Fields'), // title
  2235. 'ltitle' => t('fields'), // lowercase title for mid-sentence
  2236. 'stitle' => t('Field'), // singular title
  2237. 'lstitle' => t('field'), // singular lowercase title for mid sentence
  2238. 'plural' => 'fields',
  2239. ),
  2240. 'argument' => array(
  2241. 'title' => t('Contextual filters'),
  2242. 'ltitle' => t('contextual filters'),
  2243. 'stitle' => t('Contextual filter'),
  2244. 'lstitle' => t('contextual filter'),
  2245. 'plural' => 'arguments',
  2246. ),
  2247. 'sort' => array(
  2248. 'title' => t('Sort criteria'),
  2249. 'ltitle' => t('sort criteria'),
  2250. 'stitle' => t('Sort criterion'),
  2251. 'lstitle' => t('sort criterion'),
  2252. 'plural' => 'sorts',
  2253. ),
  2254. 'filter' => array(
  2255. 'title' => t('Filter criteria'),
  2256. 'ltitle' => t('filter criteria'),
  2257. 'stitle' => t('Filter criterion'),
  2258. 'lstitle' => t('filter criterion'),
  2259. 'plural' => 'filters',
  2260. ),
  2261. 'relationship' => array(
  2262. 'title' => t('Relationships'),
  2263. 'ltitle' => t('relationships'),
  2264. 'stitle' => t('Relationship'),
  2265. 'lstitle' => t('Relationship'),
  2266. 'plural' => 'relationships',
  2267. ),
  2268. 'header' => array(
  2269. 'title' => t('Header'),
  2270. 'ltitle' => t('header'),
  2271. 'stitle' => t('Header'),
  2272. 'lstitle' => t('Header'),
  2273. 'plural' => 'header',
  2274. 'type' => 'area',
  2275. ),
  2276. 'footer' => array(
  2277. 'title' => t('Footer'),
  2278. 'ltitle' => t('footer'),
  2279. 'stitle' => t('Footer'),
  2280. 'lstitle' => t('Footer'),
  2281. 'plural' => 'footer',
  2282. 'type' => 'area',
  2283. ),
  2284. 'empty' => array(
  2285. 'title' => t('No results behavior'),
  2286. 'ltitle' => t('no results behavior'),
  2287. 'stitle' => t('No results behavior'),
  2288. 'lstitle' => t('No results behavior'),
  2289. 'plural' => 'empty',
  2290. 'type' => 'area',
  2291. ),
  2292. );
  2293. }
  2294. return $retval;
  2295. }
  2296. /**
  2297. * @}
  2298. */