tripal_views.api.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * @file
  4. * API functions for Tripal Views Integration
  5. */
  6. /**
  7. * Retrieve the views integration setup with the lightest priority for a given table
  8. *
  9. * NOTE: Uses lightest priority (drupal-style) where the range is from -10 to 10
  10. * and -10 is of highest priority.
  11. *
  12. * @param $table_name
  13. * The name of the table to retrieve the setup ID for. This can be either a materialized
  14. * view or a chado table
  15. *
  16. * @return
  17. * On success, the setup_id to use for integration of this table; otherwise FALSE
  18. */
  19. function tripal_views_get_lightest_priority_setup($table_name) {
  20. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
  21. $setup = db_fetch_object(db_query($sql, $table_name));
  22. if ($setup) {
  23. return $setup->setup_id;
  24. }
  25. else {
  26. return FALSE;
  27. }
  28. }
  29. /**
  30. * Check to see if this table already has an integration record with the given priority
  31. *
  32. * @param $table_name
  33. * The name of the table to check for integration
  34. * @param $priority (optional)
  35. * The priority of record to check for
  36. *
  37. * @return
  38. * If the table is already integrated, the setup_id of the existing integration
  39. * record is returned (If priority is not specified this will be the lightest record);
  40. * Otherwise the table is not already integrated and FALSE is returned.
  41. */
  42. function tripal_views_is_integrated($table_name, $priority = NULL) {
  43. if ($priority) {
  44. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' AND priority=%d";
  45. $setup = db_fetch_object(db_query($sql, $table_name, $priority));
  46. } else {
  47. $sql = "SELECT setup_id FROM {tripal_views} WHERE table_name='%s' ORDER BY priority ASC";
  48. $setup = db_fetch_object(db_query($sql, $table_name));
  49. }
  50. if ($setup) {
  51. return $setup->setup_id;
  52. } else {
  53. return FALSE;
  54. }
  55. }
  56. /**
  57. * Checks if you are dealing with the lightest priority setup for a given table
  58. *
  59. * @param $setup_id
  60. * The ID of the setup to check (is this setup the lightest one?)
  61. * @param $table_name
  62. * The name of the table associated with this setup
  63. *
  64. * @return TRUE is this is the lightest priority; FALSE otherwise
  65. */
  66. function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
  67. $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  68. if ($lightest_priority_setup_id == $setup_id) {
  69. return TRUE;
  70. }
  71. else {
  72. return FALSE;
  73. }
  74. }
  75. /**
  76. * Add views integration records into the tripal_views* tables
  77. *
  78. * @param $defn_array
  79. * An array describing the structure and fields of the table
  80. *
  81. * @return
  82. * True/False if completed successfully/not
  83. *
  84. * Example usage (in hook_install()):
  85. * @code
  86. $defn_array = array(
  87. 'table' => 'feature', //tablename or materialized view name
  88. 'name' => 'Sequence Features', // Human readable name
  89. 'type' => 'chado', //either chado or mview depending on tablename
  90. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  91. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  92. 'base_table' => TRUE //either TRUE or FALSE depending on whether the current table should show up in the add view list
  93. 'fields' => array(
  94. 'feature_id' => array(
  95. 'name' => 'feature_id', //field name in database
  96. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  97. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  98. 'type' => 'int', // the type of field
  99. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  100. 'field' => array(
  101. 'name' => 'chado_views_handler_numeric' //name of handler
  102. ),
  103. 'filter' => array( ... ),
  104. ...
  105. ),
  106. 'join' => array( //describe a table that joins to this one via this field
  107. 'table' => 'featureprop', //table to join to
  108. 'field' => 'feature_id', //field in above table (featureprop)
  109. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  110. ),
  111. )
  112. ),
  113. );
  114. tripal_views_integration_add_entry($defn_array);
  115. * @endcode
  116. */
  117. function tripal_views_integration_add_entry($defn_array) {
  118. $no_errors = TRUE;
  119. // First insert into tripal_views
  120. $view_record = array(
  121. 'table_name' => $defn_array['table'],
  122. 'name' => $defn_array['name'],
  123. 'comment' => $defn_array['description'],
  124. 'priority' => $defn_array['priority'],
  125. 'base_table' => ($defn_array['base_table']) ? 1 : 0,
  126. );
  127. if ($defn_array['type'] == 'mview') {
  128. $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
  129. $view_record['mview_id'] = $mview->mview_id;
  130. if (!$mview->mview_id) {
  131. return FALSE;
  132. }
  133. }
  134. if ($view_record['name'] && $view_record['comment']) {
  135. $status = drupal_write_record('tripal_views',$view_record);
  136. } else {
  137. $status = FALSE;
  138. drupal_set_message('Unable to integrate '.$defn_array['table'].' table due to a missing name or comment field.', 'error');
  139. }
  140. if ($status) {
  141. // Insert Field Definitions
  142. foreach ($defn_array['fields'] as $field) {
  143. $field_record = array(
  144. 'setup_id' => $view_record['setup_id'],
  145. 'column_name' => $field['name'],
  146. 'name' => $field['title'],
  147. 'description' => $field['description'],
  148. 'type' => $field['type'],
  149. );
  150. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  151. $status = drupal_write_record('tripal_views_field',$field_record);
  152. } else {
  153. drupal_set_message('Unable to integrate '.$field['name'].' field due to a missing required fields.', 'error');
  154. $status = FALSE;
  155. }
  156. if ($status) {
  157. // Insert Handler Definitions
  158. foreach ($field['handlers'] as $handler_type => $handler) {
  159. $handler_record = array(
  160. 'setup_id' => $view_record['setup_id'],
  161. 'column_name' => $field['name'],
  162. 'handler_type' => $handler_type,
  163. 'handler_name' => $handler['name'],
  164. 'arguments' => serialize($handler)
  165. );
  166. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  167. $status = drupal_write_record('tripal_views_handlers',$handler_record);
  168. } else {
  169. $status = FALSE;
  170. }
  171. if (!$status) {
  172. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  173. $no_errors = FALSE;
  174. }
  175. }
  176. // Insert Joins
  177. if (!is_array($field['joins'])) {
  178. $field['joins'] = array();
  179. }
  180. foreach ($field['joins'] as $join) {
  181. $join_record = array(
  182. 'setup_id' => $view_record['setup_id'],
  183. 'base_table' => $defn_array['table'],
  184. 'base_field' => $field['name'],
  185. 'left_table' => $join['table'],
  186. 'left_field' => $join['field'],
  187. );
  188. if (!empty($join['handler'])) {
  189. $join_record['handler'] = $join['handler'];
  190. }
  191. else {
  192. $join_record['handler'] = 'views_join';
  193. }
  194. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  195. $status = drupal_write_record('tripal_views_join',$join_record);
  196. } else {
  197. $status = FALSE;
  198. }
  199. if (!$status) {
  200. drupal_set_message(
  201. t(
  202. 'Unable to join %left_table.%left_field with %table.%field',
  203. array(
  204. '%left_table' => $join['table'],
  205. '%left_field' => $join['field'],
  206. '%table' => $defn_array['table'],
  207. '%field' => $field['name']
  208. )
  209. ),
  210. 'error'
  211. );
  212. $no_errors = FALSE;
  213. }
  214. }
  215. }
  216. else {
  217. drupal_set_message(t('Unable to integrate field: %field_name', array('%field_name' => $field['name'])), 'error');
  218. $no_errors = FALSE;
  219. }
  220. }
  221. }
  222. else {
  223. drupal_set_message(t('Unable to set default views integration'), 'error');
  224. $no_errors = FALSE;
  225. }
  226. return $no_errors;
  227. }
  228. /**
  229. * Removes a View Integration Entry
  230. *
  231. * @param $table_name
  232. * The name of the table to remove a views integration entry for
  233. * @param $priority
  234. * The priority of the of views integration entry
  235. *
  236. * @return
  237. * TRUE on Success; FALSE otherwise
  238. */
  239. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  240. $views = db_fetch_object(db_query(
  241. "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
  242. $table_name,
  243. $priority
  244. ));
  245. if ($views->setup_id) {
  246. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  247. return TRUE;
  248. }
  249. else {
  250. return FALSE;
  251. }
  252. }
  253. /**
  254. * Removes a View Integration Entry
  255. *
  256. * @param $setup_id
  257. * The setup ID of the views integration entry to remove
  258. */
  259. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  260. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
  261. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
  262. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
  263. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
  264. }
  265. /**
  266. * Returns the array needed to integrate a given chado table with views
  267. *
  268. * @param $tablename
  269. * The table to generate the tripal views integration array for
  270. * @return
  271. * The tripal views integration array which is the parameter for
  272. * tripal_views_integration_add_entry($defn_array)
  273. */
  274. function tripal_views_get_integration_array_for_chado_table ($table_name, $base_table = TRUE) {
  275. // Get the schema for this table (via the chado schema api)
  276. $schema = module_invoke_all('chado_'.$table_name.'_schema');
  277. // Base definition array
  278. $defn_array = array(
  279. 'table' => $table_name,
  280. 'type' => 'chado',
  281. 'name' => ucwords(str_replace('_',' ',$table_name)),
  282. 'description' => ($schema['description']) ? $schema['description'] : ' ',
  283. 'priority' => 10,
  284. 'base_table' => $base_table,
  285. 'fields' => array(),
  286. );
  287. // Add fields
  288. if (!isset($schema['fields'])) {
  289. $schema['fields'] = array();
  290. drupal_set_message('There are no fields defined for '.$table_name.' in the Chado Schema API.','warning');
  291. }
  292. foreach ($schema['fields'] as $field_name => $field_schema) {
  293. // Base field definition
  294. if (!empty($field_name) && !empty($field_schema['type'])) {
  295. $defn_array['fields'][$field_name] = array(
  296. 'name' => $field_name,
  297. 'title' => ucwords(str_replace('_',' ',$field_name)),
  298. 'type' => $field_schema['type'],
  299. 'description' => ($field_schema['description']) ? $field_schema['description'] : ' ',
  300. 'handlers' => array(),
  301. 'joins' => array()
  302. );
  303. // Add handlers based on type
  304. if (preg_match('/^int/',$field_schema['type'])) {
  305. $defn_array['fields'][$field_name]['handlers'] = array(
  306. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  307. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  308. 'sort' => array('name' => 'chado_views_handler_sort'),
  309. );
  310. } elseif (preg_match('/^serial/',$field_schema['type'])) {
  311. $defn_array['fields'][$field_name]['handlers'] = array(
  312. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  313. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  314. 'sort' => array('name' => 'chado_views_handler_sort'),
  315. );
  316. $defn_array['fields'][$field_name]['type'] = 'int';
  317. } elseif (preg_match('/^varchar/',$field_schema['type'])) {
  318. $defn_array['fields'][$field_name]['handlers'] = array(
  319. 'field' => array('name' => 'chado_views_handler_field'),
  320. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  321. 'sort' => array('name' => 'chado_views_handler_sort'),
  322. );
  323. } elseif (preg_match('/^text/',$field_schema['type'])) {
  324. $defn_array['fields'][$field_name]['handlers'] = array(
  325. 'field' => array('name' => 'chado_views_handler_field'),
  326. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  327. 'sort' => array('name' => 'chado_views_handler_sort'),
  328. );
  329. } else {
  330. $defn_array['fields'][$field_name]['handlers'] = array(
  331. 'field' => array('name' => 'chado_views_handler_field'),
  332. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  333. 'sort' => array('name' => 'chado_views_handler_sort'),
  334. );
  335. }
  336. }
  337. }
  338. // Add Joins to fields
  339. if (!isset($schema['foreign keys'])) {
  340. $schema['foreign keys'] = array();
  341. drupal_set_message('There are no foreign keys defined for '.$table_name.' in the Chado Schema API.','warning');
  342. }
  343. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  344. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  345. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  346. 'table' => $foreign_key_schema['table'],
  347. 'field' => $right_field,
  348. 'handler' => 'views_handler_join_chado_aggregtor'
  349. );
  350. }
  351. }
  352. return $defn_array;
  353. }