tripal_views.api.inc 13 KB

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