tripal_chado_views_integration.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions used to manage tripal views integrations
  5. */
  6. /**
  7. * Purpose: Deletes ALL Chado Views Integrations.
  8. *
  9. * @ingroup tripal_chado_views
  10. */
  11. function tripal_chado_views_delete_all_integrations() {
  12. db_query("DELETE FROM {tripal_views}");
  13. db_query("DELETE FROM {tripal_views_field}");
  14. db_query("DELETE FROM {tripal_views_handlers}");
  15. db_query("DELETE FROM {tripal_views_join}");
  16. drupal_set_message("Successfully deleted all views integration.");
  17. }
  18. /**
  19. * Integrate all chado tables in the schema api. This integration only occurs
  20. * once and sets all Chado tables to a priority of 10
  21. *
  22. * @ingroup tripal_chado_views
  23. */
  24. function tripal_chado_views_integrate_all_chado_tables() {
  25. // The tripal_chado_views and tripal_chado module are so highly tied
  26. // together it may make sense for them to be one module. But in the
  27. // event that one is enabled and the other not we will have problems
  28. // with the load below.
  29. module_load_include('inc', 'tripal_chado', 'api/tripal_chado.mviews.api');
  30. // First integrate all of the Chado tables. Those that are base tables
  31. // get special treatment.
  32. $tables = chado_get_table_names(TRUE);
  33. // Some chado tables might have been created via the Tripal Custom Tables
  34. // or Tripal Materialized Views interfaces. We need to ensure that the
  35. // corresponding mview_id and table_id are associated with these tables.
  36. // @TODO: Add some way to show which integrations are for custom tables.
  37. //$custom_tables = chado_get_custom_table_names();
  38. $mview_tables = chado_get_mview_table_names();
  39. // Hardcode a list of base tables since there isn't really a programatic way
  40. // to determine which tables in the chado schema should be base tables.
  41. $base_tables = [
  42. 'acquisition',
  43. 'analysis',
  44. 'assay',
  45. 'biomaterial',
  46. 'contact',
  47. 'cv',
  48. 'cvterm',
  49. 'db',
  50. 'dbxref',
  51. 'environment',
  52. 'expression',
  53. 'feature',
  54. 'featuremap',
  55. 'genotype',
  56. 'library',
  57. 'nd_experiment',
  58. 'nd_geolocation',
  59. 'nd_protocol',
  60. 'nd_reagent',
  61. 'organism',
  62. 'phendesc',
  63. 'phenotype',
  64. 'phenstatement',
  65. 'phylonode',
  66. 'phylotree',
  67. 'project',
  68. 'protocol',
  69. 'pub',
  70. 'stock',
  71. 'study',
  72. 'synonym',
  73. ];
  74. // For each chado table, generate an integration array, keeping the above
  75. // details in mind, and save that integration with Tripal Views through the API.
  76. foreach ($tables as $tablename) {
  77. $priority = 10;
  78. if (!tripal_is_table_integrated($tablename, $priority)) {
  79. // Assuming that we have a default chado table, genereate an integration
  80. // array describing it's Tripal Views integration.
  81. if (in_array($tablename, $base_tables) OR (is_array($mview_tables) and in_array($tablename, $mview_tables))) {
  82. $table_integration_array = tripal_chado_views_get_integration_array_for_chado_table($tablename, TRUE, $priority);
  83. }
  84. else {
  85. $table_integration_array = tripal_chado_views_get_integration_array_for_chado_table($tablename, FALSE, $priority);
  86. }
  87. // Check to see if this table is a Materialized view and if it is,
  88. // treat it specially :).
  89. if (is_array($mview_tables) and in_array($tablename, $mview_tables)) {
  90. $table_integration_array['type'] = 'mview';
  91. }
  92. // As long as we were able to generate an integration array,
  93. // Integrate It!
  94. if ($table_integration_array) {
  95. tripal_add_views_integration($table_integration_array);
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Returns the array needed to integrate a given chado table with views
  102. *
  103. * @param $tablename
  104. * The table to generate the tripal views integration array for
  105. *
  106. * @return
  107. * The tripal views integration array which is the parameter for
  108. * tripal_add_views_integration($defn_array)
  109. *
  110. * @ingroup tripal_chado_views
  111. */
  112. function tripal_chado_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE, $priority = 9) {
  113. // Get the schema for this table (via the chado schema api)
  114. $schema = chado_get_schema($table_name);
  115. // Base definition array
  116. $defn_array = [
  117. 'table' => $table_name,
  118. 'type' => 'chado',
  119. 'name' => 'Chado ' . ucwords(str_replace('_', ' ', $table_name)),
  120. 'description' => (!empty($schema['description'])) ? $schema['description'] : ' ',
  121. 'priority' => $priority,
  122. 'base_table' => $base_table,
  123. 'fields' => [],
  124. ];
  125. // Add fields
  126. if (!isset($schema['fields'])) {
  127. tripal_report_error('tripal_chado_views', TRIPAL_NOTICE,
  128. 'There are no fields defined for %table in the Chado Schema API.', ['%table' => $table_name]);
  129. return FALSE;
  130. }
  131. foreach ($schema['fields'] as $field_name => $field_schema) {
  132. // Base field definition
  133. if (!empty($field_name)) {
  134. $defn_array['fields'][$field_name] = [
  135. 'name' => $field_name,
  136. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  137. 'type' => (isset($field_schema['type'])) ? $field_schema['type'] : 'text',
  138. 'description' => (!empty($field_schema['description'])) ? $field_schema['description'] : ucwords(str_replace('_', ' ', $field_name)),
  139. 'handlers' => [],
  140. 'joins' => [],
  141. ];
  142. // Add handlers based on type
  143. if (preg_match('/^int/', $field_schema['type'])) {
  144. $defn_array['fields'][$field_name]['handlers'] = [
  145. /** D6
  146. * 'field' => array('name' => 'chado_views_handler_field_numeric'),
  147. * 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  148. * 'sort' => array('name' => 'chado_views_handler_sort'),
  149. */
  150. 'field' => ['name' => 'views_handler_field_numeric'],
  151. 'filter' => ['name' => 'views_handler_filter_numeric'],
  152. 'sort' => ['name' => 'views_handler_sort'],
  153. 'argument' => ['name' => 'views_handler_argument_numeric'],
  154. ];
  155. }
  156. // Set the defaults for a serial type.
  157. elseif (preg_match('/^serial/', $field_schema['type'])) {
  158. $defn_array['fields'][$field_name]['handlers'] = [
  159. /** D6
  160. * 'field' => array('name' => 'chado_views_handler_field_numeric'),
  161. * 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  162. * 'sort' => array('name' => 'chado_views_handler_sort'),
  163. */
  164. 'field' => ['name' => 'views_handler_field_numeric'],
  165. 'filter' => ['name' => 'views_handler_filter_numeric'],
  166. 'sort' => ['name' => 'views_handler_sort'],
  167. 'argument' => ['name' => 'views_handler_argument_numeric'],
  168. ];
  169. $defn_array['fields'][$field_name]['type'] = 'int';
  170. }
  171. // Set the defaults for a varchar type.
  172. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  173. $defn_array['fields'][$field_name]['handlers'] = [
  174. /** D6
  175. * 'field' => array('name' => 'chado_views_handler_field'),
  176. * 'filter' => array('name' => 'chado_views_handler_filter_string'),
  177. * 'sort' => array('name' => 'chado_views_handler_sort'),
  178. */
  179. 'field' => ['name' => 'views_handler_field'],
  180. 'filter' => ['name' => 'views_handler_filter_string'],
  181. 'sort' => ['name' => 'views_handler_sort'],
  182. 'argument' => ['name' => 'views_handler_argument_string'],
  183. ];
  184. }
  185. // Set the defaults for a text type.
  186. elseif (preg_match('/^text/', $field_schema['type'])) {
  187. $defn_array['fields'][$field_name]['handlers'] = [
  188. /** D6
  189. * 'field' => array('name' => 'chado_views_handler_field'),
  190. * 'filter' => array('name' => 'chado_views_handler_filter_string'),
  191. * 'sort' => array('name' => 'chado_views_handler_sort'),
  192. */
  193. 'field' => ['name' => 'views_handler_field'],
  194. 'filter' => ['name' => 'views_handler_filter_string'],
  195. 'sort' => ['name' => 'views_handler_sort'],
  196. 'argument' => ['name' => 'views_handler_argument_string'],
  197. ];
  198. }
  199. // Set the defaults for a char type.
  200. elseif (preg_match('/^char/', $field_schema['type'])) {
  201. $defn_array['fields'][$field_name]['handlers'] = [
  202. /** D6
  203. * 'field' => array('name' => 'chado_views_handler_field'),
  204. * 'filter' => array('name' => 'chado_views_handler_filter_string'),
  205. * 'sort' => array('name' => 'chado_views_handler_sort'),
  206. */
  207. 'field' => ['name' => 'views_handler_field'],
  208. 'filter' => ['name' => 'views_handler_filter_string'],
  209. 'sort' => ['name' => 'views_handler_sort'],
  210. 'argument' => ['name' => 'views_handler_argument_string'],
  211. ];
  212. }
  213. // Set the defaults for a boolean type.
  214. elseif (preg_match('/^boolean/', $field_schema['type'])) {
  215. $defn_array['fields'][$field_name]['handlers'] = [
  216. /**
  217. * 'field' => array('name' => 'chado_views_handler_field_boolean'),
  218. * 'filter' => array('name' => 'chado_views_handler_filter_boolean_operator'),
  219. * 'sort' => array('name' => 'chado_views_handler_sort'),
  220. */
  221. 'field' => ['name' => 'views_handler_field_boolean'],
  222. 'filter' => ['name' => 'views_handler_filter_boolean_operator'],
  223. 'sort' => ['name' => 'views_handler_sort'],
  224. ];
  225. }
  226. // Set the defaults for a datatime type.
  227. elseif (preg_match('/^datetime/', $field_schema['type'])) {
  228. $defn_array['fields'][$field_name]['handlers'] = [
  229. /** D6
  230. * 'field' => array('name' => 'chado_views_handler_field_date'),
  231. * 'filter' => array('name' => 'chado_views_handler_filter_date'),
  232. * 'sort' => array('name' => 'views_handler_sort_date'),
  233. */
  234. 'field' => ['name' => 'views_handler_field_date'],
  235. 'filter' => ['name' => 'views_handler_filter_date'],
  236. 'sort' => ['name' => 'views_handler_sort_date'],
  237. 'argument' => ['name' => 'views_handler_argument_date'],
  238. ];
  239. }
  240. // Set the defaults for a float type.
  241. elseif (preg_match('/^float/', $field_schema['type'])) {
  242. $defn_array['fields'][$field_name]['handlers'] = [
  243. 'field' => ['name' => 'views_handler_field_numeric'],
  244. 'filter' => ['name' => 'views_handler_filter_numeric'],
  245. 'sort' => ['name' => 'views_handler_sort'],
  246. 'argument' => ['name' => 'views_handler_argument_numeric'],
  247. ];
  248. }
  249. // If the type is not recognize the default to a string handler.
  250. else {
  251. $defn_array['fields'][$field_name]['handlers'] = [
  252. /** D6
  253. * 'field' => array('name' => 'chado_views_handler_field'),
  254. * 'filter' => array('name' => 'chado_views_handler_filter_string'),
  255. * 'sort' => array('name' => 'chado_views_handler_sort'),
  256. */
  257. 'field' => ['name' => 'views_handler_field'],
  258. 'filter' => ['name' => 'views_handler_filter_string'],
  259. 'sort' => ['name' => 'views_handler_sort'],
  260. 'argument' => ['name' => 'views_handler_argument_string'],
  261. ];
  262. }
  263. // Specify specialty handlers
  264. if ($field_name == 'type_id' OR $field_name == 'cvterm_id') {
  265. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_cvterm';
  266. }
  267. if (preg_match('/name/', $field_name)) {
  268. $defn_array['fields'][$field_name]['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  269. }
  270. }
  271. }
  272. // Add Joins & Relationships for foreign keys to fields
  273. if (!isset($schema['foreign keys'])) {
  274. $schema['foreign keys'] = [];
  275. }
  276. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  277. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  278. // Note: Even though there can only be a single join for a foreign key
  279. // we make the joins an array keyed by left_field to ensure that both
  280. // foeign key and referring_tables (see below) can be processed the same.
  281. $defn_array['fields'][$left_field]['joins'][$foreign_key_schema['table']][$right_field] = [
  282. 'table' => $foreign_key_schema['table'],
  283. 'field' => $right_field,
  284. 'handler' => 'views_handler_join',
  285. 'relationship_handler' => 'views_handler_relationship',
  286. ];
  287. }
  288. }
  289. // Add in reverse relationships
  290. // Note: The array structure is set up with the left_field keyed array to
  291. // handle more than one join between the same set of tables on different
  292. // fields. For example, the reverse relationship between feature &
  293. // feature_relationship needs to join on both the subject_id and object_id.
  294. if (isset($schema['referring_tables'])) {
  295. foreach ($schema['referring_tables'] as $referring_table) {
  296. // D7 @todo: fix referring_tables in schema to list the keys like foreign keys does
  297. $referring_schema = chado_get_schema($referring_table);
  298. $referring_schema_fk_columns = $referring_schema['foreign keys'][$table_name]['columns'];
  299. foreach ($referring_schema_fk_columns as $left_field => $right_field) {
  300. // Also notice that it doesn't matter whether this is the first or second
  301. // reverse join on this table ($referring_table) to be added since
  302. // having $left_field as the key keeps them separate.
  303. $defn_array['fields'][$right_field]['joins'][$referring_table][$left_field] = [
  304. 'table' => $referring_table,
  305. 'field' => $left_field,
  306. 'relationship_handler' => 'views_handler_relationship',
  307. 'relationship_only' => 1,
  308. ];
  309. }
  310. }
  311. }
  312. return $defn_array;
  313. }