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