tripal_chado_views_integration.inc 13 KB

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