view.inc 76 KB

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