view.inc 76 KB

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