tripal_views.api.inc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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_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_is_table_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_get_lightest_views_integration_priority() as below
  95. $lightest_priority = tripal_get_lightest_views_integration_priority($table_name);
  96. $setup_id = tripal_clone_views_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_export_views_integration($setup_id);
  100. // Then make some changes to the array here
  101. // And finally save the changes to the integration
  102. tripal_update_views_integration($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_is_table_integrated($table_name, $priority)) {
  115. // Describe your table using a large array as specified by tripal_add_views_integration().
  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. // Describe any joins involving this field.
  137. // Note: you can include both foreign keys (feature.type_id => cvterm.cvterm_id)
  138. // and referring tables (ie: feature.feature_id <= feature_relationship.subject_id)
  139. 'joins' => array(
  140. 'feature_relationship' => array( //table to join to.
  141. 'subject_id' => array( //field in above table (feature_relationship)
  142. 'table' => 'featureprop', //table to join to
  143. 'field' => 'feature_id', //field in above table (feature_relationship)
  144. 'handler' => 'views_join', //handler to use for joining
  145. 'relationship_handler' => 'views_handler_relationship', //handler to use when a relationship is added.
  146. 'relationship_only' => FALSE, //whether to join automatically (FALSE) or not (TRUE)
  147. ),
  148. ...
  149. ),
  150. ...
  151. ),
  152. )
  153. ),
  154. );
  155. // Actually create the entry
  156. tripal_add_views_integration($defn_array);
  157. }
  158. }
  159. * @endcode
  160. * @}
  161. */
  162. /**
  163. * Programatically enable view
  164. *
  165. * This should be used in a hook_menu definition as the callback to provide a link
  166. * to enable the view (first example). It can also be called directly if needed (second example).
  167. * @code
  168. // Create a URL that when the user navigates there, a given view will be enabled.
  169. // You will still need to provide a link to this menu item somewhere appropriate (ie: an admin landing page).
  170. function mymodule_menu() {
  171. $items = array();
  172. // Create one of these for each of your default views
  173. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable'] = array(
  174. 'title' => 'Enable <VIEW-HUMAN-READABLE-NAME>',
  175. 'page callback' => 'tripal_enable_view',
  176. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  177. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  178. 'type' => MENU_CALLBACK,
  179. );
  180. return $items;
  181. }
  182. // Call this function directly to disable a view
  183. // The example shows enabling your own default view when your module is enabled.
  184. // This might be useful if you disable your view when your module is disabled.
  185. function mymodule_enable() {
  186. $view_name = '<VIEW-MACHINE-NAME>';
  187. tripal_enable_view($view_name);
  188. }
  189. * @endcode
  190. *
  191. * @param $view_name
  192. * The machine-name of the view to be enabled
  193. * @param $redirect_link
  194. * The path to redirect to. FALSE if no redirect needed
  195. *
  196. * @ingroup tripal_views_api
  197. */
  198. function tripal_enable_view($view_name, $redirect_link = FALSE) {
  199. $status = variable_get('views_defaults', array());
  200. if (isset($status[$view_name])) {
  201. $status[$view_name] = FALSE;
  202. variable_set('views_defaults', $status);
  203. drupal_set_message("Successfully Enabled $view_name");
  204. }
  205. else {
  206. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to enable this view.",'error');
  207. }
  208. if ($redirect_link) {
  209. drupal_goto($redirect_link);
  210. }
  211. }
  212. /**
  213. * Programatically disable view.
  214. *
  215. * This should be used in a hook_menu definition as the callback to provide a link
  216. * to disable the view (first example). It can also be called directly if needed (second example).
  217. * @code
  218. // Create a URL that when the user navigates there, a given view will be disabled.
  219. // You will still need to provide a link to this menu item somewhere appropriate.
  220. function mymodule_menu() {
  221. $items = array();
  222. // Create one of these for each of your default views
  223. $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/disable'] = array(
  224. 'title' => 'Disable <VIEW-HUMAN-READABLE-NAME>',
  225. 'page callback' => 'tripal_disable_view',
  226. 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  227. 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  228. 'type' => MENU_CALLBACK,
  229. );
  230. return $items;
  231. }
  232. // Call this function directly to disable a view
  233. // The example shows disabling your own default view when your module is uninstalled
  234. function mymodule_uninstall() {
  235. $view_name = '<VIEW-MACHINE-NAME>';
  236. tripal_disable_view($view_name);
  237. }
  238. * @endcode
  239. *
  240. * @param $view_name
  241. * The machine-name of the view to be enabled
  242. * @param $redirect_link
  243. * The path to redirect to. FALSE if no redirect needed
  244. *
  245. * @ingroup tripal_views_api
  246. */
  247. function tripal_disable_view($view_name, $redirect_link = FALSE) {
  248. $status = variable_get('views_defaults', array());
  249. if (isset($status[$view_name])) {
  250. $status[$view_name] = TRUE;
  251. variable_set('views_defaults', $status);
  252. drupal_set_message("Disabled $view_name");
  253. }
  254. else {
  255. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to disable this view.",'error');
  256. }
  257. if ($redirect_link) {
  258. drupal_goto($redirect_link);
  259. }
  260. }
  261. /**
  262. * Remove any drupal fields from a chado-based default view definition if chado is external.
  263. * This ensures compatibility with an external chado database.
  264. *
  265. * You should be calling this function in your hook_views_default_views(). This function
  266. * will only remove drupal tables if chado is external; thus you do not need to worry about
  267. * checking yourself. For example, the following is a good hook_views_default_views():
  268. * @code
  269. function mymodule_views_default_views() {
  270. $views = array();
  271. // NOTE: <VIEW-TYPE> describes the type of view:
  272. // - 'admin' for views used for administration of your module
  273. // - 'search' for views used to search data
  274. // - 'list' for views used primarily as data listings
  275. // <VIEW-HUMAN-READABLE-NAME>
  276. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  277. $view = tripal_make_view_compatible_with_external($view);
  278. $views[$view->name] = $view;
  279. // <VIEW-HUMAN-READABLE-NAME>
  280. $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  281. $view = tripal_make_view_compatible_with_external($view);
  282. $views[$view->name] = $view;
  283. return $views;
  284. }
  285. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  286. // PASTE VIEWS EXPORT CODE HERE
  287. return $view;
  288. }
  289. function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  290. // PASTE VIEWS EXPORT CODE HERE
  291. return $view;
  292. }
  293. * @endcode
  294. * Notice that the actual views export code is in a separate function. This makes your
  295. * hook_views_default_views() more readable.
  296. *
  297. * NOTE: Currently assumes all tables not in the tripal views integration tables are Drupal tables.
  298. *
  299. * @param $view
  300. * The default view definition object
  301. * @return
  302. * The default view with all relationships, fields, filters, sorts, arguements for
  303. * Drupal tables removed.
  304. */
  305. function tripal_make_view_compatible_with_external($view) {
  306. $remove_table = array();
  307. // First check that the base table for the view is a chado table
  308. // If it's not then don't do any filtering
  309. $setup_id = tripal_is_table_integrated($view->base_table);
  310. if (!$setup_id) {
  311. return $view;
  312. }
  313. // IF chado is external then remove all config relating to drupal tables
  314. if (!chado_is_local()) {
  315. // Iterate through all displays
  316. foreach ($view->display as $display_name => $display) {
  317. $display_options = $display->handler->display->display_options;
  318. $sections = array('fields', 'filters', 'sorts', 'relationships');
  319. foreach ($sections as $section) {
  320. $display_options[$section] = (isset($display_options[$section])) ? $display_options[$section] : array();
  321. foreach ($display_options[$section] as $key => $defn) {
  322. // If the table has not already been encountered; check if it's in tripal_views
  323. if (!isset($remove_table[ $defn['table'] ])) {
  324. // If the table is view then this is a general handler; thus keep
  325. if ($defn['table'] == 'views') {
  326. $remove_table[ $defn['table'] ] = FALSE;
  327. }
  328. // If this table is integrated then it is chado; thus keep
  329. $setup_id = tripal_is_table_integrated($defn['table']);
  330. if ($setup_id) {
  331. $remove_table[ $defn['table'] ] = FALSE;
  332. }
  333. else {
  334. $remove_table[ $defn['table'] ] = TRUE;
  335. }
  336. }
  337. // Based on the $remove_table array, unset this field if its from a drupal table
  338. if ($remove_table[ $defn['table'] ]) {
  339. unset($view->display[$display_name]->handler->display->display_options[$section][$key]);
  340. }
  341. }
  342. }
  343. }
  344. }
  345. return $view;
  346. }
  347. /**
  348. * Retrieve the priority of the lightest priority for a given table.
  349. *
  350. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  351. * and -10 is of highest priority.
  352. *
  353. * @param $table_name
  354. * The name of the table to retrieve the setup ID for. This can be either a materialized
  355. * view or a chado table
  356. *
  357. * @return
  358. * returns the lowest priority. If the table has not been integrated, a priority of 10
  359. * is returned.
  360. *
  361. * @ingroup tripal_views_api
  362. */
  363. function tripal_get_lightest_views_integration_priority($table_name) {
  364. // D7 TODO: Check DBTNG changes work
  365. $sql = "SELECT priority FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  366. $setup = db_query($sql, array(':table' => $table_name));
  367. $setup = $setup->fetchObject();
  368. if ($setup) {
  369. return $setup->priority;
  370. }
  371. else {
  372. // default priority is 10
  373. return 10;
  374. }
  375. }
  376. /**
  377. * Retrieve the views integration setup_id with the lightest priority for a given table
  378. *
  379. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  380. * and -10 is of highest priority.
  381. *
  382. * @param $table_name
  383. * The name of the table to retrieve the setup ID for. This can be either a materialized
  384. * view or a chado table
  385. *
  386. * @return
  387. * On success, the setup_id to use for integration of this table; otherwise FALSE
  388. *
  389. * @ingroup tripal_views_api
  390. */
  391. function tripal_get_lightest_views_integration_setup($table_name) {
  392. // D7 TODO: Check DBTNG changes work
  393. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  394. $setup = db_query($sql, array(':table' => $table_name));
  395. $setup = $setup->fetchObject();
  396. if ($setup) {
  397. return $setup->setup_id;
  398. }
  399. else {
  400. return FALSE;
  401. }
  402. }
  403. /**
  404. * Retrieve the views integration setup_id with the given priority/table combination.
  405. *
  406. * @param $table_name
  407. * The name of the table to retrieve the setup ID for. This can be either a materialized
  408. * view or a chado table
  409. * @param $priority
  410. * The priority of the integration to retrieve the setup_id for
  411. *
  412. * @return
  413. * On success, the setup_id to use for integration of this table; otherwise FALSE
  414. *
  415. * @ingroup tripal_views_api
  416. */
  417. function tripal_get_views_integration_setup_id($table_name, $priority) {
  418. // D7 TODO: Check DBTNG changes work
  419. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority ORDER BY priority ASC";
  420. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  421. $setup = $setup->fetchObject();
  422. if ($setup) {
  423. return $setup->setup_id;
  424. }
  425. else {
  426. return FALSE;
  427. }
  428. }
  429. /**
  430. * Check to see if this table already has an integration record with the given priority.
  431. *
  432. * @param $table_name
  433. * The name of the table to check for integration
  434. * @param $priority (optional)
  435. * The priority of record to check for
  436. *
  437. * @return
  438. * If the table is already integrated, the setup_id of the existing integration
  439. * record is returned (If priority is not specified this will be the lightest record);
  440. * Otherwise the table is not already integrated and FALSE is returned.
  441. *
  442. * @ingroup tripal_views_api
  443. */
  444. function tripal_is_table_integrated($table_name, $priority = NULL) {
  445. if ($priority) {
  446. // D7 TODO: Check DBTNG changes work
  447. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority";
  448. $setup = db_query($sql, array(':table' => $table_name, ':priority' => $priority));
  449. $setup = $setup->fetchObject();
  450. }
  451. else {
  452. // D7 TODO: Check DBTNG changes work
  453. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name=:table ORDER BY priority ASC";
  454. $setup = db_query($sql, array(':table' => $table_name));
  455. $setup = $setup->fetchObject();
  456. }
  457. if ($setup) {
  458. return $setup->setup_id;
  459. }
  460. else {
  461. return FALSE;
  462. }
  463. }
  464. /**
  465. * Checks if you are dealing with the lightest priority setup for a given table. This is a
  466. * good way to determine whether your modules integration is being used by views.
  467. *
  468. * @param $setup_id
  469. * The ID of the setup to check (is this setup the lightest one?)
  470. * @param $table_name
  471. * The name of the table associated with this setup
  472. *
  473. * @return TRUE is this is the lightest priority; FALSE otherwise
  474. *
  475. * @ingroup tripal_views_api
  476. */
  477. function tripal_is_lightest_priority_setup($setup_id, $table_name) {
  478. $lightest_priority_setup_id = tripal_get_lightest_views_integration_setup($table_name);
  479. if ($lightest_priority_setup_id == $setup_id) {
  480. return TRUE;
  481. }
  482. else {
  483. return FALSE;
  484. }
  485. }
  486. /**
  487. * Rebuilds all the default integrations.
  488. *
  489. * This essentially clears the cache in case you mess up the integrations in your site.
  490. * This should not be used during module development since it really only rebuilds the
  491. * integrations described by all enabled modules in the site and if $delete_first is
  492. * TRUE it can delete custom integrations created by site administrators which will not
  493. * make your module popular.
  494. *
  495. * @param $delete_first
  496. * If TRUE then all integrations are first deleted.
  497. *
  498. * @ingroup tripal_views_api
  499. */
  500. function tripal_rebuild_views_integrations($delete_first = FALSE) {
  501. if ($delete_first) {
  502. tripal_views_delete_all_integrations();
  503. }
  504. tripal_views_integrate_all_chado_tables();
  505. // TODO: the function above should have a return value from which we can
  506. // determine if the message below is approprite.
  507. drupal_set_message('Successfully rebuilt default Tripal Views Integrations');
  508. }
  509. /**
  510. * Add views integration records into the tripal_views* tables.
  511. *
  512. * This is the programatic way to add your own integrations to the tripal views integrations
  513. * list. Keep in mind that the priority set in your $defn_array needs to be lighter than
  514. * any existing integrations to be used by views and that it should still be below 0
  515. * in order to allow site administrators to override it should they need to.
  516. *
  517. * @param $defn_array
  518. * An array describing the structure and fields of the table
  519. *
  520. * @return
  521. * True/False if completed successfully/not
  522. *
  523. * Example usage (in hook_install()):
  524. * @code
  525. $defn_array = array(
  526. 'table' => 'feature', //tablename or materialized view name
  527. 'name' => 'Sequence Features', // Human readable name
  528. 'type' => 'chado', //either chado or mview depending on tablename
  529. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  530. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  531. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  532. 'fields' => array(
  533. 'feature_id' => array(
  534. 'name' => 'feature_id', //field name in database
  535. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  536. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  537. 'type' => 'int', // the type of field
  538. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  539. 'field' => array(
  540. 'name' => 'chado_views_handler_numeric' //name of handler
  541. ),
  542. 'filter' => array( ... ),
  543. ...
  544. ),
  545. // Describe any joins involving this field.
  546. // Note: you can include both foreign keys (feature.type_id => cvterm.cvterm_id)
  547. // and referring tables (ie: feature.feature_id <= feature_relationship.subject_id)
  548. 'joins' => array(
  549. 'feature_relationship' => array( //table to join to.
  550. 'subject_id' => array( //field in above table (feature_relationship)
  551. 'table' => 'featureprop', //table to join to
  552. 'field' => 'feature_id', //field in above table (feature_relationship)
  553. 'handler' => 'views_join', //handler to use for joining
  554. 'relationship_handler' => 'views_handler_relationship', //handler to use when a relationship is added.
  555. 'relationship_only' => FALSE, //whether to join automatically (FALSE) or not (TRUE)
  556. ),
  557. ...
  558. ),
  559. ...
  560. ),
  561. )
  562. ),
  563. );
  564. tripal_add_views_integration($defn_array);
  565. * @endcode
  566. *
  567. * @ingroup tripal_views_api
  568. */
  569. function tripal_add_views_integration($defn_array, $setup_id = FALSE) {
  570. $no_errors = TRUE;
  571. if (empty($defn_array['table'])) {
  572. tripal_report_error('tripal_views', TRIPAL_WARNING, 'Recieved integration with no tablename: %defn', array('%defn' => print_r($defn_array,TRUE)));
  573. $no_errors = FALSE;
  574. return $no_errors;
  575. }
  576. // First insert into tripal_views
  577. $view_record = array(
  578. 'table_name' => $defn_array['table'],
  579. 'name' => $defn_array['name'],
  580. 'comment' => $defn_array['description'],
  581. 'priority' => $defn_array['priority'],
  582. 'base_table' => $defn_array['base_table'],
  583. );
  584. if ($setup_id) {
  585. $view_record['setup_id'] = $setup_id;
  586. }
  587. if ($defn_array['type'] == 'mview') {
  588. // D7 TODO: Check DBTNG changes work
  589. $mview = db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table=:table", array(':table' => $defn_array['table']));
  590. $mview = $mview->fetchObject();
  591. $view_record['mview_id'] = $mview->mview_id;
  592. if (!$mview->mview_id) {
  593. return FALSE;
  594. }
  595. }
  596. if ($view_record['name']) { // && $view_record['comment']) { # SPF: commented out 9/24/2012 .. It's not required on the form
  597. if (isset($defn_array['additional_content'])) {
  598. // D7 TODO: Check DBTNG changes work
  599. $setup = db_query(
  600. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  601. array(':table' => $view_record['table_name'], ':priority' => $view_record['priority'])
  602. );
  603. $setup = $setup->fetchObject();
  604. if (empty($setup->setup_id)) {
  605. $status = drupal_write_record('tripal_views', $view_record);
  606. }
  607. else {
  608. $view_record['setup_id'] = $setup->setup_id;
  609. $status = drupal_write_record('tripal_views', $view_record, 'setup_id');
  610. }
  611. }
  612. else {
  613. $status = drupal_write_record('tripal_views', $view_record);
  614. }
  615. }
  616. else {
  617. $status = FALSE;
  618. drupal_set_message(t('Unable to integrate "%table" table due to a missing name field.', array('%table' => $defn_array['table'])), 'error');
  619. }
  620. if ($status) {
  621. // Need to update the tripal_views record so base_table can be false
  622. // this is a fix because drupal_write_record() puts in defaults if !isset()
  623. // and a variable is considered not set if it's null!
  624. // D7 TODO: Check DBTNG changes work
  625. db_query(
  626. "UPDATE {tripal_views} SET base_table=:base WHERE table_name=:table AND priority=:priority",
  627. array(
  628. ':base' => $defn_array['base_table'],
  629. ':table' => $defn_array['table'],
  630. ':priority' => $defn_array['priority']
  631. )
  632. );
  633. // Insert Field Definitions
  634. foreach ($defn_array['fields'] as $field) {
  635. $field_record = array(
  636. 'setup_id' => $view_record['setup_id'],
  637. 'column_name' => $field['name'],
  638. 'name' => $field['title'],
  639. 'description' => $field['description'],
  640. 'type' => $field['type'],
  641. );
  642. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  643. if (isset($defn_array['additional_content'])) {
  644. // D7 TODO: Check DBTNG changes work
  645. $is_present = db_query(
  646. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  647. array(
  648. ':column' => $field_record['column_name'],
  649. ':setup' => $field_record['setup_id']
  650. )
  651. );
  652. $is_present = $is_present->fetchField();
  653. if (!$is_present) {
  654. $status = drupal_write_record('tripal_views_field', $field_record);
  655. }
  656. else {
  657. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  658. }
  659. }
  660. else {
  661. $status = drupal_write_record('tripal_views_field', $field_record);
  662. }
  663. }
  664. else {
  665. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  666. $status = FALSE;
  667. }
  668. if ($status) {
  669. // Insert Handler Definitions
  670. foreach ($field['handlers'] as $handler_type => $handler) {
  671. $handler_record = array(
  672. 'setup_id' => $view_record['setup_id'],
  673. 'column_name' => $field['name'],
  674. 'handler_type' => $handler_type,
  675. 'handler_name' => $handler['name'],
  676. 'arguments' => serialize($handler)
  677. );
  678. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  679. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  680. }
  681. else {
  682. $status = FALSE;
  683. }
  684. if (!$status) {
  685. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  686. $no_errors = FALSE;
  687. }
  688. }
  689. // Insert Joins
  690. // Note: The new defn_array structure accounts for 1+ joins to the same
  691. // table (ie: feature_relationship => feature : subject_id & object_id)
  692. // by making $field['joins'] an array of left_field keys.
  693. if (!is_array($field['joins'])) {
  694. $field['joins'] = array();
  695. }
  696. foreach ($field['joins'] as $joins) {
  697. // To keep backwards compatibility with the old defn_array which just
  698. // jumped right into the table definition allowing only a single join,
  699. // we need to check for old defn_arrays and transform them into the
  700. // new format.
  701. if (isset($joins['table'])) {
  702. $left_field = $joins['field'];
  703. $joins = array(
  704. $left_field => $joins
  705. );
  706. }
  707. // Loop on left fields
  708. foreach ($joins as $left_field => $join) {
  709. $join_record = array(
  710. 'setup_id' => $view_record['setup_id'],
  711. 'base_table' => $defn_array['table'],
  712. 'base_field' => $field['name'],
  713. 'left_table' => $join['table'],
  714. 'left_field' => $left_field,
  715. );
  716. $join_record['handler'] = (!empty($join['handler'])) ? $join['handler'] : 'views_join';
  717. $join_record['relationship_handler'] = (!empty($join['relationship_handler'])) ? $join['relationship_handler'] : 'views_handler_relationship';
  718. $join_record['relationship_only'] = (!empty($join['relationship_only'])) ? $join['relationship_only'] : 0;
  719. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $left_field) {
  720. $status = drupal_write_record('tripal_views_join', $join_record);
  721. }
  722. else {
  723. $status = FALSE;
  724. }
  725. if (!$status) {
  726. drupal_set_message(
  727. t(
  728. 'Unable to join %left_table.%left_field with %table.%field',
  729. array(
  730. '%left_table' => $join['table'],
  731. '%left_field' => $left_field,
  732. '%table' => $defn_array['table'],
  733. '%field' => $field['name']
  734. )
  735. ),
  736. 'error'
  737. );
  738. $no_errors = FALSE;
  739. }
  740. }
  741. }
  742. }
  743. else {
  744. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  745. $no_errors = FALSE;
  746. }
  747. }
  748. }
  749. else {
  750. drupal_set_message(t('Unable to set default tripal views integration'), 'error');
  751. $no_errors = FALSE;
  752. }
  753. return $no_errors;
  754. }
  755. /**
  756. * Export Views integration records.
  757. *
  758. * This is a great way to create your own integration since it returns an already defined
  759. * integration in array form that you can modify. After modifications simply set the
  760. * priority to something lighter (but still below 0) than any existing integrations
  761. * and use tripal_add_views_integration() to add it to the list of integrations.
  762. *
  763. * @param $setup_id
  764. * The unique setup id of the tripal views integration
  765. *
  766. * @return
  767. * A views integration definition array as used by tripal_add_views_integration()
  768. *
  769. * @ingroup tripal_views_api
  770. */
  771. function tripal_export_views_integration($setup_id) {
  772. // Main setup details
  773. // D7 TODO: Check DBTNG changes work
  774. $r = db_query("SELECT * FROM {tripal_views} WHERE setup_id=:setup", array(':setup' => $setup_id));
  775. $r = $r->fetchObject();
  776. $defn_array = array(
  777. 'table' => $r->table_name,
  778. 'name' => $r->name,
  779. 'type' => ($r->mview_id) ? 'mview' : 'chado',
  780. 'description' => $r->comment,
  781. 'priority' => $r->priority,
  782. 'base_table' => $r->base_table,
  783. 'fields' => array(),
  784. );
  785. // Add fields
  786. $resource = db_query("SELECT * FROM {tripal_views_field} WHERE setup_id=:setup", array(':setup' => $setup_id));
  787. foreach ($resource as $r) {
  788. $defn_array['fields'][ $r->column_name ] = array(
  789. 'name' => $r->column_name,
  790. 'title' => $r->name,
  791. 'description' => $r->description,
  792. 'type' => $r->type,
  793. 'handlers' => array(),
  794. 'joins' => array()
  795. );
  796. }
  797. // Add handlers
  798. $resource = db_query("SELECT * FROM {tripal_views_handlers} WHERE setup_id=:setup", array(':setup' => $setup_id));
  799. foreach ($resource as $r) {
  800. $defn_array['fields'][ $r->column_name ]['handlers'][ $r->handler_type ] = array(
  801. 'name' => $r->handler_name
  802. );
  803. }
  804. // Add joins
  805. $resource = db_query("SELECT * FROM {tripal_views_join} WHERE setup_id=:setup", array(':setup' => $setup_id));
  806. foreach ($resource as $r) {
  807. $defn_array['fields'][ $r->base_field ]['joins'][ $r->left_table ][ $r->left_field ] = array(
  808. 'table' => $r->left_table,
  809. 'field' => $r->left_field,
  810. 'handler' => $r->handler,
  811. );
  812. }
  813. return $defn_array;
  814. }
  815. /**
  816. * Removes a View Integration Entry when you only know the table the integration was
  817. * created for and the priority.
  818. *
  819. * This should only be used to remove integrations created by your own module (possibly
  820. * on uninstall of your module). To override existing integrations simply create your own
  821. * integration with a lighter priority using tripal_clone_views_integration() or
  822. * tripal_export_views_integration() to create a template.
  823. *
  824. * @param $identifies
  825. * An array of identifiers where the keys indicate what the identifier is. One of the
  826. * following compinations must be present:
  827. * 1) table_name & priority: the name of the table & the priority to remove a views
  828. * integration entry for
  829. * 2) setup_id: the setup_id of the entry to remove
  830. * @param $options
  831. * An array of options, currently none are supported
  832. *
  833. * @return
  834. * TRUE on Success; FALSE otherwise
  835. *
  836. * @ingroup tripal_views_api
  837. */
  838. function tripal_remove_views_integration($identifiers, $options = array()) {
  839. // Remove the views integration using the table_name/priority combo
  840. if (isset($identifiers['table_name'])) {
  841. $table_name = $identifiers['table_name'];
  842. $priority = $identifiers['priority'];
  843. $views = db_query(
  844. "SELECT * FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  845. array(
  846. ':table' => $table_name,
  847. ':priority' => $priority
  848. )
  849. );
  850. $views = $views->fetchObject();
  851. if ($views->setup_id) {
  852. $identifiers['setup_id'] = $views->setup_id;
  853. }
  854. }
  855. // Remove the views integration using the setup_id
  856. if (isset($identifiers['setup_id'])) {
  857. db_query('DELETE FROM {tripal_views} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  858. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  859. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  860. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=:setup', array(':setup' => $identifiers['setup_id']));
  861. return TRUE;
  862. }
  863. return FALSE;
  864. }
  865. /**
  866. * Update an existing Views Intregration Entry.
  867. * This essentially removes and then re-adds the integration.
  868. *
  869. * @param $setup_id
  870. * The setup ID of the views integration entry to update
  871. * @param $defn_array
  872. * An array describing the structure and fields of the table as is used in
  873. * tripal_add_views_integration().
  874. *
  875. * @ingroup tripal_views_api
  876. */
  877. function tripal_update_views_integration($setup_id, $defn_array) {
  878. tripal_remove_views_integration(array('setup_id' => $setup_id));
  879. $defn_array['additional_content'] = TRUE;
  880. tripal_add_views_integration($defn_array, $setup_id);
  881. }
  882. /**
  883. * Clone an integration. This is often a great way to create your own module-specific
  884. * integration while still benifiting from an existing (or even the lightest priority)
  885. * integration.
  886. *
  887. * @param $table_name
  888. * The table for which you'd like to clone an integration
  889. * @param $new_priority
  890. * The priority of the clone; this is the integration which will be created.
  891. * If no priority is supplied then one lighter then the $template_priority will be used.
  892. * @param $template_priority
  893. * The priority of the template to be used for the clone; this is an existing integration.
  894. * If no priority is supplied then the lightest priority will be used.
  895. *
  896. * @ingroup tripal_views_api
  897. */
  898. function tripal_clone_views_integration($table_name, $new_priority = NULL, $template_priority = NULL) {
  899. if (empty($template_priority)) {
  900. $template_setup_id = tripal_get_lightest_views_integration_setup($table_name);
  901. }
  902. else {
  903. $template_setup_id = tripal_get_views_integration_setup_id($table_name, $template_priority);
  904. }
  905. $defn_array = tripal_export_views_integration($template_setup_id);
  906. if (empty($new_priority)) {
  907. $defn_array['priority'] = $new_priority;
  908. }
  909. else {
  910. $new_priority = $defn_array['priority'] - 1;
  911. $defn_array['priority'] = $defn_array['priority'] - 1;
  912. }
  913. tripal_add_views_integration($defn_array);
  914. $setup_id = db_query(
  915. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  916. array(
  917. ':table' => $table_name,
  918. ':priority' => $new_priority
  919. )
  920. );
  921. $setup_id = $setup_id->fetchField();
  922. if (empty($setup_id)) {
  923. 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.',
  924. array('%table' => $table_name, '%field' => print_r($field_array,TRUE)));
  925. return FALSE;
  926. }
  927. else {
  928. return $setup_id;
  929. }
  930. }
  931. /**
  932. * Adds the given field to an existing or cloned integration. In the case of a cloned
  933. * integration, the lightest integration is used as the template for the clone.
  934. *
  935. * NOTE: If that field already exists in the specified integration then it will first be
  936. * deleted and the new one added.
  937. *
  938. * @param $table_name
  939. * The name of the table the integration is for
  940. * @param $priority
  941. * The priority of the integration to use; pass NULL to use the lightest integration
  942. * @param $field
  943. * An array describing the field ot add; uses the same format as the $defn_array
  944. *
  945. * @return
  946. * TRUE if the field was added successfully; FALSE otherwise
  947. *
  948. * @ingroup tripal_views_api
  949. */
  950. function tripal_add_field_to_views_integration($table_name, $priority, $field) {
  951. $no_errors = TRUE;
  952. // If no priority is supplied then add the field to the lightest integration
  953. if (empty($priority)) {
  954. $priority = tripal_get_lightest_views_integration_priority($table_name);
  955. }
  956. // First get the setup_id
  957. // D7 TODO: Check DBTNG changes work
  958. $setup_id = db_query(
  959. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  960. array(
  961. ':table' => $table_name,
  962. ':priority' => $priority
  963. )
  964. );
  965. $setup_id = $setup_id->fetchObject();
  966. // If there isn't an integration matching that table/priority combination
  967. // then clone the lightest priority integration
  968. if (empty($setup_id)) {
  969. $setup_id = tripal_clone_views_integration($table_name, $priority);
  970. }
  971. // Now delete any existing field
  972. db_query("DELETE FROM {tripal_views_field} WHERE setup_id=:setup AND column_name=:column",
  973. array(':setup' => $setup_id, 'column' => $field['name'])
  974. );
  975. db_query("DELETE FROM {tripal_views_handlers} WHERE setup_id=:setup AND column_name=:column",
  976. array(':setup' => $setup_id, 'column' => $field['name'])
  977. );
  978. db_query("DELETE FROM {tripal_views_join} WHERE setup_id=:setup AND base_table=:table AND base_field=:field",
  979. array(':setup' => $setup_id, ':table' => $table_name, ':field' => $field['name'])
  980. );
  981. // Now we need to add/update the field
  982. $field_record = array(
  983. 'setup_id' => $setup_id,
  984. 'column_name' => $field['name'],
  985. 'name' => $field['title'],
  986. 'description' => $field['description'],
  987. 'type' => $field['type'],
  988. );
  989. if ($setup_id && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  990. if ($defn_array['additional_content']) {
  991. // D7 TODO: Check DBTNG changes work
  992. $is = db_query(
  993. "SELECT true as present FROM {tripal_views_field} WHERE column_name=:column AND setup_id=:setup",
  994. array(':column' => $field_record['column_name'], ':setup' => $field_record['setup_id'])
  995. );
  996. $is = $is->fetchObject();
  997. if (!$is->present) {
  998. $status = drupal_write_record('tripal_views_field', $field_record);
  999. }
  1000. else {
  1001. $status = drupal_write_record('tripal_views_field', $field_record, array('setup_id', 'column_name'));
  1002. }
  1003. }
  1004. else {
  1005. $status = drupal_write_record('tripal_views_field', $field_record);
  1006. }
  1007. }
  1008. else {
  1009. drupal_set_message(t('Unable to integrate %name field due to missing required fields.', array('%name' => $field['name'])), 'error');
  1010. $status = FALSE;
  1011. }
  1012. if ($status) {
  1013. // Insert Handler Definitions
  1014. foreach ($field['handlers'] as $handler_type => $handler) {
  1015. $handler_record = array(
  1016. 'setup_id' => $setup_id,
  1017. 'column_name' => $field['name'],
  1018. 'handler_type' => $handler_type,
  1019. 'handler_name' => $handler['name'],
  1020. 'arguments' => serialize($handler)
  1021. );
  1022. if ($setup_id && $field['name'] && $handler_type && $handler['name'] && $handler) {
  1023. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  1024. }
  1025. else {
  1026. $status = FALSE;
  1027. }
  1028. if (!$status) {
  1029. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  1030. $no_errors = FALSE;
  1031. }
  1032. }
  1033. // Insert Joins
  1034. // Note: The new defn_array structure accounts for 1+ joins to the same
  1035. // table (ie: feature_relationship => feature : subject_id & object_id)
  1036. // by making $field['joins'] an array of left_field keys.
  1037. if (!is_array($field['joins'])) {
  1038. $field['joins'] = array();
  1039. }
  1040. foreach ($field['joins'] as $joins) {
  1041. // To keep backwards compatibility with the old defn_array which just
  1042. // jumped right into the table definition allowing only a single join,
  1043. // we need to check for old defn_arrays and transform them into the
  1044. // new format.
  1045. if (isset($joins['table'])) {
  1046. $left_field = $joins['field'];
  1047. $joins = array(
  1048. $left_field => $joins
  1049. );
  1050. }
  1051. // Loop on left fields
  1052. foreach ($joins as $left_field => $join) {
  1053. $join_record = array(
  1054. 'setup_id' => $setup_id,
  1055. 'base_table' => $defn_array['table'],
  1056. 'base_field' => $field['name'],
  1057. 'left_table' => $join['table'],
  1058. 'left_field' => $join['field'],
  1059. );
  1060. if (!empty($join['handler'])) {
  1061. $join_record['handler'] = $join['handler'];
  1062. }
  1063. else {
  1064. $join_record['handler'] = 'views_join';
  1065. }
  1066. if ($setup_id && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  1067. $status = drupal_write_record('tripal_views_join', $join_record);
  1068. }
  1069. else {
  1070. $status = FALSE;
  1071. }
  1072. if (!$status) {
  1073. drupal_set_message(
  1074. t(
  1075. 'Unable to join %left_table.%left_field with %table.%field',
  1076. array(
  1077. '%left_table' => $join['table'],
  1078. '%left_field' => $join['field'],
  1079. '%table' => $defn_array['table'],
  1080. '%field' => $field['name']
  1081. )
  1082. ),
  1083. 'error'
  1084. );
  1085. $no_errors = FALSE;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. else {
  1091. drupal_set_message(t('Unable to integrate %field_name field', array('%field_name' => $field['name'])), 'error');
  1092. $no_errors = FALSE;
  1093. }
  1094. return $no_errors;
  1095. }
  1096. /**
  1097. * Adds the given field to an existing or cloned integration. In the case of a cloned
  1098. * integration, the lightest integration is used as the template for the clone.
  1099. *
  1100. * NOTE: If that field already exists in the specified integration then it will first be
  1101. * deleted and the new one added.
  1102. *
  1103. * @param $table_name
  1104. * The name of the table the integration is for
  1105. * @param $priority
  1106. * The priority of the integration to use; pass NULL to use the lightest integration
  1107. * @param $field
  1108. * An array describing the join to add. it should contain the following keys:
  1109. * base_table, base_field, left_table, left_field, handler, relationship_handler,
  1110. * relationship_only
  1111. *
  1112. * @return
  1113. * TRUE if the field was added successfully; FALSE otherwise
  1114. *
  1115. * @ingroup tripal_views_api
  1116. */
  1117. function tripal_add_join_to_views_integration($table_name, $priority, $join) {
  1118. $no_errors = TRUE;
  1119. // If no priority is supplied then add the field to the lightest integration
  1120. if (empty($priority)) {
  1121. $priority = tripal_get_lightest_views_integration_priority($table_name);
  1122. }
  1123. // First get the setup_id
  1124. $setup_id = db_query(
  1125. "SELECT setup_id FROM {tripal_views} WHERE table_name=:table AND priority=:priority",
  1126. array(
  1127. ':table' => $table_name,
  1128. ':priority' => $priority
  1129. )
  1130. );
  1131. $setup_id = $setup_id->fetchField();
  1132. // If there isn't an integration matching that table/priority combination
  1133. // then clone the lightest priority integration
  1134. if (empty($setup_id)) {
  1135. $setup_id = tripal_clone_views_integration($table_name, $priority);
  1136. }
  1137. // Add the setup_id to the join record passed in
  1138. $join['setup_id'] = $setup_id;
  1139. drupal_write_record('tripal_views_join', $join);
  1140. }
  1141. /**
  1142. * Remove a join from an integration. This is usually done after cloning an existing
  1143. * integration using tripal_clone_views_integration().
  1144. *
  1145. * @param $setup_id
  1146. * The setup_id of the integration to delete the join from
  1147. * @param $base_table
  1148. * The name of the base table involved the join
  1149. * @param $base_field
  1150. * The field from the base table involved in the join
  1151. * @param $left_table
  1152. * The name of the other table involved in the join
  1153. * @param $left_field
  1154. * The name of the field from the other table involved in the join
  1155. *
  1156. * @ingroup tripal_views_api
  1157. */
  1158. function tripal_remove_join_from_views_integration($setup_id, $base_table, $base_field, $left_table, $left_field) {
  1159. db_query(
  1160. "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",
  1161. array(
  1162. ':setup' => $setup_id,
  1163. ':base-table' => $base_table,
  1164. ':base-field' => $base_field,
  1165. ':left-table' => $left_table,
  1166. ':left-field' => $left_field
  1167. )
  1168. );
  1169. }