tripal_chado_views.api.inc 39 KB

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