tripal_views.api.inc 13 KB

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