tripal_views.api.inc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. <?php
  2. /**
  3. * @file
  4. * API functions for Tripal Views Integration
  5. */
  6. /**
  7. * @defgroup tripal_views_api Tripal Views Module API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides functions to help extension modules add their own tripal views integrations
  11. * as well as functions for managing default views.
  12. *
  13. * Managing Default Views:
  14. *
  15. * When you create administrative default views (or really any default view) you really
  16. * have no way to ensure that the site administrator keeps these views enabled. In some
  17. * cases this is a good thing and provides the site administrator the ability to disable
  18. * views that don't apply to their particular installation or to replace your default view
  19. * with a custom view of their own. But in other cases, particularily for administration
  20. * views, you want to gaurd against accidental disabling of your views.
  21. *
  22. * One way to do this is to add a landing page using a heook_menu() definition to a custom
  23. * callback returning HTML. You can use this page to render the view if it is enabled but
  24. * provide logic to display a message if the view is disabled. Furthermore, it's often
  25. * helpful to provide a link allowing site administrators to enable the view from your
  26. * page rather than expecting them to go to the views administration UI. This is done for
  27. * all the administration views provided by Tripal. The following is an example of how to
  28. * achomplish this functionality for your own module:
  29. * @code
  30. function mymodule_menu() {
  31. $items = array();
  32. // Create the landing page
  33. $items['admin/tripal/<PATH-TO-YOUR-LANDING-PAGE>'] = array(
  34. 'title' => 'MyModule Administration',
  35. 'description' => 'Administration of my module.',
  36. 'page callback' => 'mymodule_admin_landing_page',
  37. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  38. 'type' => MENU_NORMAL_ITEM,
  39. );
  40. // Create one of these for each of your default views
  41. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable'] = array(
  42. 'title' => 'Enable <VIEW-HUMAN-READABLE-NAME>',
  43. 'page callback' => 'tripal_views_admin_enable_view',
  44. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  45. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  46. 'type' => MENU_CALLBACK,
  47. );
  48. return $items;
  49. }
  50. function mymodule_admin_landing_page() {
  51. // Get the View Embed Code
  52. // This function will return FALSE if your view is not enabled
  53. $view_code = views_embed_view('<VIEW-MACHINE-NAME>', '<DISPLAY-MACHINE-NAME');
  54. // If your view is enabled then embed it in this page by returning the embed code
  55. if (isset($view_code)) {
  56. $output .= $view_code;
  57. }
  58. else {
  59. // Provide the landing page with links to the menu item created in hook_menu to
  60. // to enable your view
  61. $output .= '<p>The My Module module uses primarily views to provide an '
  62. . 'administrative interface. Currently one or more views needed for this '
  63. . 'administrative interface are disabled. <strong>Click each of the following links to '
  64. . 'enable the pertinent views</strong>:</p>';
  65. $output .= '<ul>';
  66. // NOTE: <URL-FROM-MENU-TO-ENABLE-VIEW> is
  67. // admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable
  68. // from above hook_menu().
  69. $output .= '<li>' . l('<VIEW-HUMAN-RADABLE-NAME>', '<URL-FROM-MENU-TO-ENABLE-VIEW>') . '</li>';
  70. $output .= '</ul>';
  71. }
  72. return $output;
  73. }
  74. * @endcode
  75. *
  76. * Adding your own Tripal Views Integrations:
  77. *
  78. * One of the main ways the Tripal View API is likely to be used is if your module needs
  79. * to create it's own tripal views integration. You might need to do this for any number of
  80. * reasons but the following examples are thought to be the most common:
  81. *
  82. * 1) Your module wants to add handlers with better functionality to fields it has more
  83. * knowledge of than the general integration for all tables
  84. * @code
  85. mymodule_views_data() {
  86. // First check that your integration has not already been added
  87. // When selecting a priority, do not choose -10 or -9 and make sure it is below 0
  88. // so that individual sites still have the ability to override it, if needed
  89. $table_name = 'my_chado_table';
  90. $priority = -5;
  91. if (!tripal_views_is_integrated($table_name, $priority)) {
  92. // If you really only need to tweak an existing integration you can clone it
  93. // If you want to clone the integration that is currently taking priority
  94. // then use tripal_views_get_table_lightest_priority() as below
  95. $lightest_priority = tripal_views_get_table_lightest_priority($table_name);
  96. $setup_id = tripal_views_clone_integration($table_name, $priority, $lightest_priority);
  97. // And then make a few changes
  98. // First get the definition array created via the clone above
  99. $defn_array = tripal_views_integration_export_entry($setup_id);
  100. // Then make some changes to the array here
  101. // And finally save the changes to the integration
  102. tripal_views_integration_update_entry($setup_id, $defn_array);
  103. }
  104. }
  105. * @endcode
  106. * 2) Your module creates a chado table that is not already integrated.
  107. * @code
  108. mymodule_views_data() {
  109. // First check that your integration has not already been added
  110. // When selecting a priority, do not choose 10 or 9 and make sure it is below 0
  111. // so that individual sites still have the ability to override it, if needed
  112. $table_name = 'my_chado_table';
  113. $priority = 5;
  114. if (!tripal_views_is_integrated($table_name, $priority)) {
  115. // Describe your table using a large array as specified by tripal_views_integration_add_entry().
  116. $defn_array = array(
  117. 'table' => $table_name, //tablename or materialized view name
  118. 'name' => 'My Chado Table', // Human readable name
  119. 'type' => 'chado', //either chado or mview depending on tablename
  120. 'description' => 'Create a listing from my chado table.', //description seen when creating a view of this type
  121. 'priority' => $priority, //For Base tripal modules: 10; custom modules: 9 to 0;
  122. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  123. 'fields' => array(
  124. 'feature_id' => array(
  125. 'name' => 'feature_id', //field name in database
  126. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  127. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  128. 'type' => 'int', // the type of field
  129. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  130. 'field' => array(
  131. 'name' => 'chado_views_handler_numeric' //name of handler
  132. ),
  133. 'filter' => array( ... ),
  134. ...
  135. ),
  136. 'join' => array( //describe a table that joins to this one via this field
  137. 'table' => 'featureprop', //table to join to
  138. 'field' => 'feature_id', //field in above table (featureprop)
  139. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  140. ),
  141. )
  142. ),
  143. );
  144. // Actually create the entry
  145. tripal_views_integration_add_entry($defn_array);
  146. }
  147. }
  148. * @endcode
  149. * @}
  150. */
  151. /**
  152. * Programatically enable view
  153. *
  154. * This should be used in a hook_menu definition as the callback to provide a link
  155. * to enable the view (first example). It can also be called directly if needed (second example).
  156. * @code
  157. // Create a URL that when the user navigates there, a given view will be enabled.
  158. // You will still need to provide a link to this menu item somewhere appropriate (ie: an admin landing page).
  159. function mymodule_menu() {
  160. $items = array();
  161. // Create one of these for each of your default views
  162. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable'] = array(
  163. 'title' => 'Enable <VIEW-HUMAN-READABLE-NAME>',
  164. 'page callback' => 'tripal_views_admin_enable_view',
  165. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  166. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  167. 'type' => MENU_CALLBACK,
  168. );
  169. return $items;
  170. }
  171. // Call this function directly to disable a view
  172. // The example shows enabling your own default view when your module is enabled.
  173. // This might be useful if you disable your view when your module is disabled.
  174. function mymodule_enable() {
  175. $view_name = '<VIEW-MACHINE-NAME>';
  176. tripal_views_admin_enable_view($view_name);
  177. }
  178. * @endcode
  179. *
  180. * @param $view_name
  181. * The machine-name of the view to be enabled
  182. * @param $redirect_link
  183. * The path to redirect to. FALSE if no redirect needed
  184. *
  185. * @ingroup tripal_views_api
  186. */
  187. function tripal_views_admin_enable_view($view_name, $redirect_link = FALSE) {
  188. $status = variable_get('views_defaults', array());
  189. if (isset($status[$view_name])) {
  190. $status[$view_name] = FALSE;
  191. variable_set('views_defaults', $status);
  192. drupal_set_message("Successfully Enabled $view_name");
  193. }
  194. else {
  195. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to enable this view.",'error');
  196. }
  197. if ($redirect_link) {
  198. drupal_goto($redirect_link);
  199. }
  200. }
  201. /**
  202. * Programatically disable view.
  203. *
  204. * This should be used in a hook_menu definition as the callback to provide a link
  205. * to disable the view (first example). It can also be called directly if needed (second example).
  206. * @code
  207. // Create a URL that when the user navigates there, a given view will be disabled.
  208. // You will still need to provide a link to this menu item somewhere appropriate.
  209. function mymodule_menu() {
  210. $items = array();
  211. // Create one of these for each of your default views
  212. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/disable'] = array(
  213. 'title' => 'Disable <VIEW-HUMAN-READABLE-NAME>',
  214. 'page callback' => 'tripal_views_admin_disable_view',
  215. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  216. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  217. 'type' => MENU_CALLBACK,
  218. );
  219. return $items;
  220. }
  221. // Call this function directly to disable a view
  222. // The example shows disabling your own default view when your module is uninstalled
  223. function mymodule_uninstall() {
  224. $view_name = '<VIEW-MACHINE-NAME>';
  225. tripal_views_admin_disable_view($view_name);
  226. }
  227. * @endcode
  228. *
  229. * @param $view_name
  230. * The machine-name of the view to be enabled
  231. * @param $redirect_link
  232. * The path to redirect to. FALSE if no redirect needed
  233. *
  234. * @ingroup tripal_views_api
  235. */
  236. function tripal_views_admin_disable_view($view_name, $redirect_link = FALSE) {
  237. $status = variable_get('views_defaults', array());
  238. if (isset($status[$view_name])) {
  239. $status[$view_name] = TRUE;
  240. variable_set('views_defaults', $status);
  241. drupal_set_message("Disabled $view_name");
  242. }
  243. else {
  244. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to disable this view.",'error');
  245. }
  246. if ($redirect_link) {
  247. drupal_goto($redirect_link);
  248. }
  249. }
  250. /**
  251. * Remove any drupal fields from a chado-based default view definition if chado is external.
  252. * This ensures compatibility with an external chado database.
  253. *
  254. * You should be calling this function in your hook_views_default_views(). This function
  255. * will only remove drupal tables if chado is external; thus you do not need to worry about
  256. * checking yourself. For example, the following is a good hook_views_default_views():
  257. * @code
  258. function mymodule_views_default_views() {
  259. $views = array();
  260. // NOTE: <VIEW-TYPE> describes the type of view:
  261. // - 'admin' for views used for administration of your module
  262. // - 'search' for views used to search data
  263. // - 'list' for views used primarily as data listings
  264. // <VIEW-HUMAN-READABLE-NAME>
  265. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  266. $view = tripal_views_make_view_compatible_with_external($view);
  267. $views[$view->name] = $view;
  268. // <VIEW-HUMAN-READABLE-NAME>
  269. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  270. $view = tripal_views_make_view_compatible_with_external($view);
  271. $views[$view->name] = $view;
  272. return $views;
  273. }
  274. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  275. // PASTE VIEWS EXPORT CODE HERE
  276. return $view;
  277. }
  278. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  279. // PASTE VIEWS EXPORT CODE HERE
  280. return $view;
  281. }
  282. * @endcode
  283. * Notice that the actual views export code is in a separate function. This makes your
  284. * hook_views_default_views() more readable.
  285. *
  286. * NOTE: Currently assumes all tables not in the tripal views integration tables are Drupal tables.
  287. *
  288. * @param $view
  289. * The default view definition object
  290. * @return
  291. * The default view with all relationships, fields, filters, sorts, arguements for
  292. * Drupal tables removed.
  293. */
  294. function tripal_views_make_view_compatible_with_external($view) {
  295. $remove_table = array();
  296. // First check that the base table for the view is a chado table
  297. // If it's not then don't do any filtering
  298. $setup_id = tripal_views_is_integrated($view->base_table);
  299. if (!$setup_id) {
  300. return $view;
  301. }
  302. // IF chado is external then remove all config relating to drupal tables
  303. if (!chado_is_local()) {
  304. // Iterate through all displays
  305. foreach ($view->display as $display_name => $display) {
  306. $display_options = $display->handler->display->display_options;
  307. $sections = array('fields', 'filters', 'sorts', 'relationships');
  308. foreach ($sections as $section) {
  309. $display_options[$section] = (isset($display_options[$section])) ? $display_options[$section] : array();
  310. foreach ($display_options[$section] as $key => $defn) {
  311. // If the table has not already been encountered; check if it's in tripal_views
  312. if (!isset($remove_table[ $defn['table'] ])) {
  313. // If the table is view then this is a general handler; thus keep
  314. if ($defn['table'] == 'views') {
  315. $remove_table[ $defn['table'] ] = FALSE;
  316. }
  317. // If this table is integrated then it is chado; thus keep
  318. $setup_id = tripal_views_is_integrated($defn['table']);
  319. if ($setup_id) {
  320. $remove_table[ $defn['table'] ] = FALSE;
  321. }
  322. else {
  323. $remove_table[ $defn['table'] ] = TRUE;
  324. }
  325. }
  326. // Based on the $remove_table array, unset this field if its from a drupal table
  327. if ($remove_table[ $defn['table'] ]) {
  328. unset($view->display[$display_name]->handler->display->display_options[$section][$key]);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. return $view;
  335. }
  336. /**
  337. * Retrieve the priority of the lightest priority for a given table.
  338. *
  339. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  340. * and -10 is of highest priority.
  341. *
  342. * @param $table_name
  343. * The name of the table to retrieve the setup ID for. This can be either a materialized
  344. * view or a chado table
  345. *
  346. * @return
  347. * returns the lowest priority. If the table has not been integrated, a priority of 10
  348. * is returned.
  349. *
  350. * @ingroup tripal_views_api
  351. */
  352. function tripal_views_get_table_lightest_priority($table_name) {
  353. // D7 TODO: Check DBTNG changes work
  354. $sql = "SELECT priority FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  355. $setup = db_query($sql, array(':table' => $table_name));
  356. $setup = $setup->fetchObject();
  357. if ($setup) {
  358. return $setup->priority;
  359. }
  360. else {
  361. // default priority is 10
  362. return 10;
  363. }
  364. }
  365. /**
  366. * Retrieve the views integration setup_id with the lightest priority for a given table
  367. *
  368. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  369. * and -10 is of highest priority.
  370. *
  371. * @param $table_name
  372. * The name of the table to retrieve the setup ID for. This can be either a materialized
  373. * view or a chado table
  374. *
  375. * @return
  376. * On success, the setup_id to use for integration of this table; otherwise FALSE
  377. *
  378. * @ingroup tripal_views_api
  379. */
  380. function tripal_views_get_lightest_priority_setup($table_name) {
  381. // D7 TODO: Check DBTNG changes work
  382. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  383. $setup = db_query($sql, array(':table' => $table_name));
  384. $setup = $setup->fetchObject();
  385. if ($setup) {
  386. return $setup->setup_id;
  387. }
  388. else {
  389. return FALSE;
  390. }
  391. }
  392. /**
  393. * Retrieve the views integration setup_id with the given priority/table combination.
  394. *
  395. * @param $table_name
  396. * The name of the table to retrieve the setup ID for. This can be either a materialized
  397. * view or a chado table
  398. * @param $priority
  399. * The priority of the integration to retrieve the setup_id for
  400. *
  401. * @return
  402. * On success, the setup_id to use for integration of this table; otherwise FALSE
  403. *
  404. * @ingroup tripal_views_api
  405. */
  406. function tripal_views_get_setup_id($table_name, $priority) {
  407. // D7 TODO: Check DBTNG changes work
  408. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority ORDER BY priority ASC";
  409. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  410. $setup = $setup->fetchObject();
  411. if ($setup) {
  412. return $setup->setup_id;
  413. }
  414. else {
  415. return FALSE;
  416. }
  417. }
  418. /**
  419. * Check to see if this table already has an integration record with the given priority.
  420. *
  421. * @param $table_name
  422. * The name of the table to check for integration
  423. * @param $priority (optional)
  424. * The priority of record to check for
  425. *
  426. * @return
  427. * If the table is already integrated, the setup_id of the existing integration
  428. * record is returned (If priority is not specified this will be the lightest record);
  429. * Otherwise the table is not already integrated and FALSE is returned.
  430. *
  431. * @ingroup tripal_views_api
  432. */
  433. function tripal_views_is_integrated($table_name, $priority = NULL) {
  434. if ($priority) {
  435. // D7 TODO: Check DBTNG changes work
  436. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority";
  437. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  438. $setup = $setup->fetchObject();
  439. }
  440. else {
  441. // D7 TODO: Check DBTNG changes work
  442. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  443. $setup = db_query($sql, array(':table' => $table_name));
  444. $setup = $setup->fetchObject();
  445. }
  446. if ($setup) {
  447. return $setup->setup_id;
  448. }
  449. else {
  450. return FALSE;
  451. }
  452. }
  453. /**
  454. * Checks if you are dealing with the lightest priority setup for a given table. This is a
  455. * good way to determine whether your modules integration is being used by views.
  456. *
  457. * @param $setup_id
  458. * The ID of the setup to check (is this setup the lightest one?)
  459. * @param $table_name
  460. * The name of the table associated with this setup
  461. *
  462. * @return TRUE is this is the lightest priority; FALSE otherwise
  463. *
  464. * @ingroup tripal_views_api
  465. */
  466. function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
  467. $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  468. if ($lightest_priority_setup_id == $setup_id) {
  469. return TRUE;
  470. }
  471. else {
  472. return FALSE;
  473. }
  474. }
  475. /**
  476. * Rebuilds all the default integrations.
  477. *
  478. * This essentially clears the cache in case you mess up the integrations in your site.
  479. * This should not be used during module development since it really only rebuilds the
  480. * integrations described by all enabled modules in the site and if $delete_first is
  481. * TRUE it can delete custom integrations created by site administrators which will not
  482. * make your module popular.
  483. *
  484. * @param $delete_first
  485. * If TRUE then all integrations are first deleted.
  486. *
  487. * @ingroup tripal_views_api
  488. */
  489. function tripal_views_rebuild_views_integrations($delete_first = FALSE) {
  490. if ($delete_first) {
  491. tripal_views_delete_all_integrations();
  492. }
  493. tripal_views_integrate_all_chado_tables();
  494. drupal_set_message('Successfully rebuilt default Tripal Views Integrations');
  495. }
  496. /**
  497. * Add views integration records into the tripal_views* tables.
  498. *
  499. * This is the programatic way to add your own integrations to the tripal views integrations
  500. * list. Keep in mind that the priority set in your $defn_array needs to be lighter than
  501. * any existing integrations to be used by views and that it should still be below 0
  502. * in order to allow site administrators to override it should they need to.
  503. *
  504. * @param $defn_array
  505. * An array describing the structure and fields of the table
  506. *
  507. * @return
  508. * True/False if completed successfully/not
  509. *
  510. * Example usage (in hook_install()):
  511. * @code
  512. $defn_array = array(
  513. 'table' => 'feature', //tablename or materialized view name
  514. 'name' => 'Sequence Features', // Human readable name
  515. 'type' => 'chado', //either chado or mview depending on tablename
  516. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  517. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  518. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  519. 'fields' => array(
  520. 'feature_id' => array(
  521. 'name' => 'feature_id', //field name in database
  522. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  523. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  524. 'type' => 'int', // the type of field
  525. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  526. 'field' => array(
  527. 'name' => 'chado_views_handler_numeric' //name of handler
  528. ),
  529. 'filter' => array( ... ),
  530. ...
  531. ),
  532. 'join' => array( //describe a table that joins to this one via this field
  533. 'table' => 'featureprop', //table to join to
  534. 'field' => 'feature_id', //field in above table (featureprop)
  535. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  536. ),
  537. )
  538. ),
  539. );
  540. tripal_views_integration_add_entry($defn_array);
  541. * @endcode
  542. *
  543. * @ingroup tripal_views_api
  544. */
  545. function tripal_views_integration_add_entry($defn_array) {
  546. $no_errors = TRUE;
  547. if (empty($defn_array['table'])) {
  548. tripal_report_error('tripal_views', TRIPAL_WARNING, 'Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array,TRUE)));
  549. $no_errors = FALSE;
  550. return $no_errors;
  551. }
  552. // First insert into tripal_views
  553. $view_record = array(
  554. 'table_name' => $defn_array['table'],
  555. 'name' => $defn_array['name'],
  556. 'comment' => $defn_array['description'],
  557. 'priority' => $defn_array['priority'],
  558. 'base_table' => $defn_array['base_table'],
  559. );
  560. if ($defn_array['type'] == 'mview') {
  561. // D7 TODO: Check DBTNG changes work
  562. $mview = db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table=:table", array(':table' => $defn_array['table']));
  563. $mview = $mview->fetchObject();
  564. $view_record['mview_id'] = $mview->mview_id;
  565. if (!$mview->mview_id) {
  566. return FALSE;
  567. }
  568. }
  569. if ($view_record['name']) { // && $view_record['comment']) { # SPF: commented out 9/24/2012 .. It's not required on the form
  570. if (isset($defn_array['additional_content'])) {
  571. // D7 TODO: Check DBTNG changes work
  572. $setup = db_query(
  573. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  574. array(':table' => $view_record['table_name'], ':priority' => $view_record['priority'])
  575. );
  576. $setup = $setup->fetchObject();
  577. if (empty($setup->setup_id)) {
  578. $status = drupal_write_record('tripal_views', $view_record);
  579. }
  580. else {
  581. $view_record['setup_id'] = $setup->setup_id;
  582. $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
  583. }
  584. }
  585. else {
  586. $status = drupal_write_record('tripal_views', $view_record);
  587. }
  588. }
  589. else {
  590. $status = FALSE;
  591. drupal_set_message(t('Unable to integrate "%table" table due to a missing name field.', array('%table' => $defn_array['table'])), 'error');
  592. }
  593. if ($status) {
  594. // Need to update the tripal_views record so base_table can be false
  595. // this is a fix because drupal_write_record() puts in defaults if !isset()
  596. // and a variable is considered not set if it's null!
  597. // D7 TODO: Check DBTNG changes work
  598. db_query(
  599. "UPDATE {tripal_views} SET base_table=:base WHERE table_name=:table AND priority=:priority",
  600. array(
  601. ':base' => $defn_array['base_table'],
  602. ':table' => $defn_array['table'],
  603. ':priority' => $defn_array['priority']
  604. )
  605. );
  606. // Insert Field Definitions
  607. foreach ($defn_array['fields'] as $field) {
  608. $field_record = array(
  609. 'setup_id' => $view_record['setup_id'],
  610. 'column_name' => $field['name'],
  611. 'name' => $field['title'],
  612. 'description' => $field['description'],
  613. 'type' => $field['type'],
  614. );
  615. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  616. if (isset($defn_array['additional_content'])) {
  617. // D7 TODO: Check DBTNG changes work
  618. $is = db_query(
  619. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  620. array(
  621. ':column' => $field_record['column_name'],
  622. ':setup' => $field_record['setup_id']
  623. )
  624. );
  625. $is = $is->fetchObject();
  626. if (!$is->present) {
  627. $status = drupal_write_record('tripal_views_field', $field_record);
  628. }
  629. else {
  630. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  631. }
  632. }
  633. else {
  634. $status = drupal_write_record('tripal_views_field', $field_record);
  635. }
  636. }
  637. else {
  638. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  639. $status = FALSE;
  640. }
  641. if ($status) {
  642. // Insert Handler Definitions
  643. foreach ($field['handlers'] as $handler_type => $handler) {
  644. $handler_record = array(
  645. 'setup_id' => $view_record['setup_id'],
  646. 'column_name' => $field['name'],
  647. 'handler_type' => $handler_type,
  648. 'handler_name' => $handler['name'],
  649. 'arguments' => serialize($handler)
  650. );
  651. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  652. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  653. }
  654. else {
  655. $status = FALSE;
  656. }
  657. if (!$status) {
  658. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  659. $no_errors = FALSE;
  660. }
  661. }
  662. // Insert Joins
  663. if (!is_array($field['joins'])) {
  664. $field['joins'] = array();
  665. }
  666. foreach ($field['joins'] as $join) {
  667. $join_record = array(
  668. 'setup_id' => $view_record['setup_id'],
  669. 'base_table' => $defn_array['table'],
  670. 'base_field' => $field['name'],
  671. 'left_table' => $join['table'],
  672. 'left_field' => $join['field'],
  673. );
  674. $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
  675. $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
  676. $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;
  677. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  678. $status = drupal_write_record('tripal_views_join', $join_record);
  679. }
  680. else {
  681. $status = FALSE;
  682. }
  683. if (!$status) {
  684. drupal_set_message(
  685. t(
  686. 'Unable to join %left_table.%left_field with %table.%field',
  687. array(
  688. '%left_table' => $join['table'],
  689. '%left_field' => $join['field'],
  690. '%table' => $defn_array['table'],
  691. '%field' => $field['name']
  692. )
  693. ),
  694. 'error'
  695. );
  696. $no_errors = FALSE;
  697. }
  698. }
  699. }
  700. else {
  701. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  702. $no_errors = FALSE;
  703. }
  704. }
  705. }
  706. else {
  707. drupal_set_message(t('Unable to set default tripal views integration'), 'error');
  708. $no_errors = FALSE;
  709. }
  710. return $no_errors;
  711. }
  712. /**
  713. * Export Views integration records.
  714. *
  715. * This is a great way to create your own integration since it returns an already defined
  716. * integration in array form that you can modify. After modifications simply set the
  717. * priority to something lighter (but still below 0) than any existing integrations
  718. * and use tripal_views_integration_add_entry() to add it to the list of integrations.
  719. *
  720. * @param $setup_id
  721. * The unique setup id of the tripal views integration
  722. *
  723. * @return
  724. * A views integration definition array as used by tripal_views_integration_add_entry()
  725. *
  726. * @ingroup tripal_views_api
  727. */
  728. function tripal_views_integration_export_entry($setup_id) {
  729. // Main setup details
  730. // D7 TODO: Check DBTNG changes work
  731. $r = db_query("SELECT * FROM {tripal_views} WHERE setup_id=:setup", array(':setup' => $setup_id));
  732. $r = $r->fetchObject();
  733. $defn_array = array(
  734. 'table' => $r->table_name,
  735. 'name' => $r->name,
  736. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  737. 'description' => $r->comment,
  738. 'priority' => $r->priority,
  739. 'base_table' => $r->base_table,
  740. 'fields' => array(),
  741. );
  742. // Add fields
  743. // D7 TODO: Check DBTNG changes work
  744. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=:setup", array(':setup' => $setup_id));
  745. foreach ($resource as $r) {
  746. $defn_array['fields'][ $r->column_name ] = array(
  747. 'name' => $r->column_name,
  748. 'title' => $r->name,
  749. 'description' => $r->description,
  750. 'type' => $r->type,
  751. 'handlers' => array(),
  752. 'joins' => array()
  753. );
  754. }
  755. // Add handlers
  756. // D7 TODO: Check DBTNG changes work
  757. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=:setup", array(':setup' => $setup_id));
  758. foreach ($resource as $r) {
  759. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  760. 'name' => $r->handler_name
  761. );
  762. }
  763. // Add joins
  764. // D7 TODO: Check DBTNG changes work
  765. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=:setup", array(':setup' => $setup_id));
  766. foreach ($resource as $r) {
  767. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ] = array(
  768. 'table' => $r->left_table,
  769. 'field' => $r->left_field,
  770. 'handler' => $r->handler,
  771. );
  772. }
  773. return $defn_array;
  774. }
  775. /**
  776. * Removes a View Integration Entry when you only know the table the integration was
  777. * created for and the priority.
  778. *
  779. * This should only be used to remove integrations created by your own module (possibly
  780. * on uninstall of your module). To override existing integrations simply create your own
  781. * integration with a lighter priority using tripal_views_clone_integration() or
  782. * tripal_views_integration_export_entry() to create a template.
  783. *
  784. * @param $table_name
  785. * The name of the table to remove a views integration entry for
  786. * @param $priority
  787. * The priority of the of views integration entry
  788. *
  789. * @return
  790. * TRUE on Success; FALSE otherwise
  791. *
  792. * @ingroup tripal_views_api
  793. */
  794. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  795. // D7 TODO: Check DBTNG changes work
  796. $views = db_query(
  797. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  798. array(
  799. ':table' => $table_name,
  800. ':priority' => $priority
  801. )
  802. );
  803. $views = $views->fetchObject();
  804. if ($views->setup_id) {
  805. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  806. return TRUE;
  807. }
  808. else {
  809. return FALSE;
  810. }
  811. }
  812. /**
  813. * Removes a View Integration Entry when you know the setup_id
  814. *
  815. * This should only be used to remove integrations created by your own module (possibly
  816. * on uninstall of your module). To override existing integrations simply create your own
  817. * integration with a lighter priority using tripal_views_clone_integration() or
  818. * tripal_views_integration_export_entry() to create a template.
  819. *
  820. * @param $setup_id
  821. * The setup ID of the views integration entry to remove
  822. *
  823. * @ingroup tripal_views_api
  824. */
  825. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  826. // D7 TODO: Check DBTNG changes work
  827. db_query('DELETE FROM {tripal_views} WHERE setup_id=:setup', array(':setup' => $setup_id));
  828. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=:setup', array(':setup' => $setup_id));
  829. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup', array(':setup' => $setup_id));
  830. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=:setup', array(':setup' => $setup_id));
  831. }
  832. /**
  833. * Clone an integration. This is often a great way to create your own module-specific
  834. * integration while still benifiting from an existing (or even the lightest priority)
  835. * integration.
  836. *
  837. * @param $table_name
  838. * The table for which you'd like to clone an integration
  839. * @param $new_priority
  840. * The priority of the clone; this is the integration which will be created.
  841. * If no priority is supplied then one lighter then the $template_priority will be used.
  842. * @param $template_priority
  843. * The priority of the template to be used for the clone; this is an existing integration.
  844. * If no priority is supplied then the lightest priority will be used.
  845. *
  846. * @ingroup tripal_views_api
  847. */
  848. function tripal_views_clone_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  849. if (empty($template_priority)) {
  850. $template_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  851. }
  852. else {
  853. $template_setup_id = tripal_views_get_setup_id($table_name, $template_priority);
  854. }
  855. $defn_array = tripal_views_integration_export_entry($template_setup_id);
  856. if (empty($new_priority)) {
  857. $defn_array['priority'] = $new_priority;
  858. }
  859. else {
  860. $new_priority = $defn_array['priority'] - 1;
  861. $defn_array['priority'] = $defn_array['priority'] - 1;
  862. }
  863. // D7 TODO: Check DBTNG changes work
  864. tripal_views_integration_add_entry($defn_array);
  865. $setup_id = db_query(
  866. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  867. array(
  868. ':table' => $table_name,
  869. ':priority' => $new_priority
  870. )
  871. );
  872. $setup_id = $setup_id->fetchObject();
  873. if (empty($setup_id)) {
  874. tripal_report_error('tripal_views', TRIPAL_ERROR, 'Unable to clone the setup for %table in order to add the following field to the integration: %field.',
  875. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)));
  876. return FALSE;
  877. }
  878. else {
  879. return $setup_id;
  880. }
  881. }
  882. /**
  883. * Adds the given field to an existing or cloned integration. In the case of a cloned
  884. * integration, the lightest integration is used as the template for the clone.
  885. *
  886. * NOTE: If that field already exists in the specified integration then it will first be
  887. * deleted and the new one added.
  888. *
  889. * @param $table_name
  890. * The name of the table the integration is for
  891. * @param $priority
  892. * The priority of the integration to use; pass NULL to use the lightest integration
  893. * @param $field
  894. * An array describing the field ot add; uses the same format as the $defn_array
  895. *
  896. * @return
  897. * TRUE if the field was added successfully; FALSE otherwise
  898. *
  899. * @ingroup tripal_views_api
  900. */
  901. function tripal_views_add_field_to_integration($table_name, $priority, $field) {
  902. $no_errors = TRUE;
  903. // If no priority is supplied then add the field to the lightest integration
  904. if (empty($priority)) {
  905. $priority = tripal_views_get_table_lightest_priority($table_name);
  906. }
  907. // First get the setup_id
  908. // D7 TODO: Check DBTNG changes work
  909. $setup_id = db_query(
  910. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  911. array(
  912. ':table' => $table_name,
  913. ':priority' => $priority
  914. )
  915. );
  916. $setup_id = $setup_id->fetchObject();
  917. // If there isn't an integration matching that table/priority combination
  918. // then clone the lightest priority integration
  919. if (empty($setup_id)) {
  920. $setup_id = tripal_views_clone_integration($table_name, $priority);
  921. }
  922. // Now delete any existing field
  923. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  924. array(':setup' => $setup_id, 'column' => $field['name'])
  925. );
  926. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  927. array(':setup' => $setup_id, 'column' => $field['name'])
  928. );
  929. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  930. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  931. );
  932. // Now we need to add/update the field
  933. $field_record = array(
  934. 'setup_id' => $setup_id,
  935. 'column_name' => $field['name'],
  936. 'name' => $field['title'],
  937. 'description' => $field['description'],
  938. 'type' => $field['type'],
  939. );
  940. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  941. if ($defn_array['additional_content']) {
  942. // D7 TODO: Check DBTNG changes work
  943. $is = db_query(
  944. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  945. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  946. );
  947. $is = $is->fetchObject();
  948. if (!$is->present) {
  949. $status = drupal_write_record('tripal_views_field', $field_record);
  950. }
  951. else {
  952. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  953. }
  954. }
  955. else {
  956. $status = drupal_write_record('tripal_views_field', $field_record);
  957. }
  958. }
  959. else {
  960. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  961. $status = FALSE;
  962. }
  963. if ($status) {
  964. // Insert Handler Definitions
  965. foreach ($field['handlers'] as $handler_type => $handler) {
  966. $handler_record = array(
  967. 'setup_id' => $setup_id,
  968. 'column_name' => $field['name'],
  969. 'handler_type' => $handler_type,
  970. 'handler_name' => $handler['name'],
  971. 'arguments' => serialize($handler)
  972. );
  973. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  974. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  975. }
  976. else {
  977. $status = FALSE;
  978. }
  979. if (!$status) {
  980. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  981. $no_errors = FALSE;
  982. }
  983. }
  984. // Insert Joins
  985. if (!is_array($field['joins'])) {
  986. $field['joins'] = array();
  987. }
  988. foreach ($field['joins'] as $join) {
  989. $join_record = array(
  990. 'setup_id' => $setup_id,
  991. 'base_table' => $defn_array['table'],
  992. 'base_field' => $field['name'],
  993. 'left_table' => $join['table'],
  994. 'left_field' => $join['field'],
  995. );
  996. if (!empty($join['handler'])) {
  997. $join_record['handler'] = $join['handler'];
  998. }
  999. else {
  1000. $join_record['handler'] = 'views_join';
  1001. }
  1002. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  1003. $status = drupal_write_record('tripal_views_join', $join_record);
  1004. }
  1005. else {
  1006. $status = FALSE;
  1007. }
  1008. if (!$status) {
  1009. drupal_set_message(
  1010. t(
  1011. 'Unable to join %left_table.%left_field with %table.%field',
  1012. array(
  1013. '%left_table' => $join['table'],
  1014. '%left_field' => $join['field'],
  1015. '%table' => $defn_array['table'],
  1016. '%field' => $field['name']
  1017. )
  1018. ),
  1019. 'error'
  1020. );
  1021. $no_errors = FALSE;
  1022. }
  1023. }
  1024. }
  1025. else {
  1026. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  1027. $no_errors = FALSE;
  1028. }
  1029. return $no_errors;
  1030. }
  1031. /**
  1032. * Remove a join from an integration. This is usually done after cloning an existing
  1033. * integration using tripal_views_clone_integration().
  1034. *
  1035. * @param $setup_id
  1036. * The setup_id of the integration to delete the join from
  1037. * @param $base_table
  1038. * The name of the base table involved the join
  1039. * @param $base_field
  1040. * The field from the base table involved in the join
  1041. * @param $left_table
  1042. * The name of the other table involved in the join
  1043. * @param $left_field
  1044. * The name of the field from the other table involved in the join
  1045. *
  1046. * @ingroup tripal_views_api
  1047. */
  1048. function tripal_views_remove_join_from_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  1049. db_query(
  1050. "DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:base-table AND base_field=:base-field AND left_table=:left-table AND left_field=:left-field",
  1051. array(
  1052. ':setup' => $setup_id,
  1053. ':base-table' => $base_table,
  1054. ':base-field' => $base_field,
  1055. ':left-table' => $left_table,
  1056. ':left-field' => $left_field
  1057. )
  1058. );
  1059. }