tripal_chado_views_integration.inc

Contains functions used to manage tripal views integrations

File

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