tripal_views.api.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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'],
  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. // Need to update the tripal_views record so base_table can be false
  145. // this is a fix because drupal_write_record() puts in defaults if !isset()
  146. // and a variable is considered not set if its null!
  147. db_query(
  148. "UPDATE {tripal_views} SET base_table=%d WHERE table_name='%s' AND priority=%d",
  149. $defn_array['base_table'],
  150. $defn_array['table'],
  151. $defn_array['priority']
  152. );
  153. // Insert Field Definitions
  154. foreach ($defn_array['fields'] as $field) {
  155. $field_record = array(
  156. 'setup_id' => $view_record['setup_id'],
  157. 'column_name' => $field['name'],
  158. 'name' => $field['title'],
  159. 'description' => $field['description'],
  160. 'type' => $field['type'],
  161. );
  162. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  163. $status = drupal_write_record('tripal_views_field', $field_record);
  164. }
  165. else {
  166. drupal_set_message(t('Unable to integrate %name field due to a missing required fields.', array('%name' => $field['name'])), 'error');
  167. $status = FALSE;
  168. }
  169. if ($status) {
  170. // Insert Handler Definitions
  171. foreach ($field['handlers'] as $handler_type => $handler) {
  172. $handler_record = array(
  173. 'setup_id' => $view_record['setup_id'],
  174. 'column_name' => $field['name'],
  175. 'handler_type' => $handler_type,
  176. 'handler_name' => $handler['name'],
  177. 'arguments' => serialize($handler)
  178. );
  179. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  180. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  181. }
  182. else {
  183. $status = FALSE;
  184. }
  185. if (!$status) {
  186. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  187. $no_errors = FALSE;
  188. }
  189. }
  190. // Insert Joins
  191. if (!is_array($field['joins'])) {
  192. $field['joins'] = array();
  193. }
  194. foreach ($field['joins'] as $join) {
  195. $join_record = array(
  196. 'setup_id' => $view_record['setup_id'],
  197. 'base_table' => $defn_array['table'],
  198. 'base_field' => $field['name'],
  199. 'left_table' => $join['table'],
  200. 'left_field' => $join['field'],
  201. );
  202. if (!empty($join['handler'])) {
  203. $join_record['handler'] = $join['handler'];
  204. }
  205. else {
  206. $join_record['handler'] = 'views_join';
  207. }
  208. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  209. $status = drupal_write_record('tripal_views_join', $join_record);
  210. }
  211. else {
  212. $status = FALSE;
  213. }
  214. if (!$status) {
  215. drupal_set_message(
  216. t(
  217. 'Unable to join %left_table.%left_field with %table.%field',
  218. array(
  219. '%left_table' => $join['table'],
  220. '%left_field' => $join['field'],
  221. '%table' => $defn_array['table'],
  222. '%field' => $field['name']
  223. )
  224. ),
  225. 'error'
  226. );
  227. $no_errors = FALSE;
  228. }
  229. }
  230. }
  231. else {
  232. drupal_set_message(t('Unable to integrate field: %field_name', array('%field_name' => $field['name'])), 'error');
  233. $no_errors = FALSE;
  234. }
  235. }
  236. }
  237. else {
  238. drupal_set_message(t('Unable to set default views integration'), 'error');
  239. $no_errors = FALSE;
  240. }
  241. return $no_errors;
  242. }
  243. /**
  244. * Removes a View Integration Entry
  245. *
  246. * @param $table_name
  247. * The name of the table to remove a views integration entry for
  248. * @param $priority
  249. * The priority of the of views integration entry
  250. *
  251. * @return
  252. * TRUE on Success; FALSE otherwise
  253. */
  254. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  255. $views = db_fetch_object(db_query(
  256. "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
  257. $table_name,
  258. $priority
  259. ));
  260. if ($views->setup_id) {
  261. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  262. return TRUE;
  263. }
  264. else {
  265. return FALSE;
  266. }
  267. }
  268. /**
  269. * Removes a View Integration Entry
  270. *
  271. * @param $setup_id
  272. * The setup ID of the views integration entry to remove
  273. */
  274. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  275. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
  276. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
  277. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
  278. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
  279. }
  280. /**
  281. * Returns the array needed to integrate a given chado table with views
  282. *
  283. * @param $tablename
  284. * The table to generate the tripal views integration array for
  285. * @return
  286. * The tripal views integration array which is the parameter for
  287. * tripal_views_integration_add_entry($defn_array)
  288. */
  289. function tripal_views_get_integration_array_for_chado_table($table_name, $base_table = TRUE) {
  290. // Get the schema for this table (via the chado schema api)
  291. $schema = module_invoke_all('chado_' . $table_name . '_schema');
  292. // Base definition array
  293. $defn_array = array(
  294. 'table' => $table_name,
  295. 'type' => 'chado',
  296. 'name' => ucwords(str_replace('_', ' ', $table_name)),
  297. 'description' => ($schema['description']) ? $schema['description'] : ' ',
  298. 'priority' => 10,
  299. 'base_table' => $base_table,
  300. 'fields' => array(),
  301. );
  302. // Add fields
  303. if (!isset($schema['fields'])) {
  304. $schema['fields'] = array();
  305. drupal_set_message(t('There are no fields defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
  306. }
  307. foreach ($schema['fields'] as $field_name => $field_schema) {
  308. // Base field definition
  309. if (!empty($field_name) && !empty($field_schema['type'])) {
  310. $defn_array['fields'][$field_name] = array(
  311. 'name' => $field_name,
  312. 'title' => ucwords(str_replace('_', ' ', $field_name)),
  313. 'type' => $field_schema['type'],
  314. 'description' => ($field_schema['description']) ? $field_schema['description'] : ' ',
  315. 'handlers' => array(),
  316. 'joins' => array()
  317. );
  318. // Add handlers based on type
  319. if (preg_match('/^int/', $field_schema['type'])) {
  320. $defn_array['fields'][$field_name]['handlers'] = array(
  321. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  322. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  323. 'sort' => array('name' => 'chado_views_handler_sort'),
  324. );
  325. }
  326. elseif (preg_match('/^serial/', $field_schema['type'])) {
  327. $defn_array['fields'][$field_name]['handlers'] = array(
  328. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  329. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  330. 'sort' => array('name' => 'chado_views_handler_sort'),
  331. );
  332. $defn_array['fields'][$field_name]['type'] = 'int';
  333. }
  334. elseif (preg_match('/^varchar/', $field_schema['type'])) {
  335. $defn_array['fields'][$field_name]['handlers'] = array(
  336. 'field' => array('name' => 'chado_views_handler_field'),
  337. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  338. 'sort' => array('name' => 'chado_views_handler_sort'),
  339. );
  340. }
  341. elseif (preg_match('/^text/', $field_schema['type'])) {
  342. $defn_array['fields'][$field_name]['handlers'] = array(
  343. 'field' => array('name' => 'chado_views_handler_field'),
  344. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  345. 'sort' => array('name' => 'chado_views_handler_sort'),
  346. );
  347. }
  348. else {
  349. $defn_array['fields'][$field_name]['handlers'] = array(
  350. 'field' => array('name' => 'chado_views_handler_field'),
  351. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  352. 'sort' => array('name' => 'chado_views_handler_sort'),
  353. );
  354. }
  355. }
  356. }
  357. // Add Joins to fields
  358. if (!isset($schema['foreign keys'])) {
  359. $schema['foreign keys'] = array();
  360. drupal_set_message(t('There are no foreign keys defined for %table in the Chado Schema API.', array('%table' => $table_name)), 'warning');
  361. }
  362. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  363. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  364. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  365. 'table' => $foreign_key_schema['table'],
  366. 'field' => $right_field,
  367. 'handler' => 'views_handler_join_chado_aggregtor'
  368. );
  369. }
  370. }
  371. return $defn_array;
  372. }