tripal_views.api.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. 'fields' => array(
  87. 'feature_id' => array(
  88. 'name' => 'feature_id', //field name in database
  89. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  90. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  91. 'type' => 'int', // the type of field
  92. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  93. 'field' => array(
  94. 'name' => 'chado_views_handler_numeric' //name of handler
  95. ),
  96. 'filter' => array( ... ),
  97. ...
  98. ),
  99. 'join' => array( //describe a table that joins to this one via this field
  100. 'table' => 'featureprop', //table to join to
  101. 'field' => 'feature_id', //field in above table (featureprop)
  102. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  103. ),
  104. )
  105. ),
  106. );
  107. tripal_views_integration_add_entry($defn_array);
  108. * @endcode
  109. */
  110. function tripal_views_integration_add_entry($defn_array) {
  111. $no_errors = TRUE;
  112. // First insert into tripal_views
  113. $view_record = array(
  114. 'table_name' => $defn_array['table'],
  115. 'name' => $defn_array['name'],
  116. 'comment' => $defn_array['description'],
  117. 'priority' => $defn_array['priority'],
  118. );
  119. if ($defn_array['type'] == 'mview') {
  120. $mview = db_fetch_object(db_query("SELECT mview_id FROM tripal_mviews WHERE mv_table='%s'",$defn_array['table']));
  121. $view_record['mview_id'] = $mview->mview_id;
  122. if (!$mview->mview_id) {
  123. return FALSE;
  124. }
  125. }
  126. if ($view_record['name'] && $view_record['comment']) {
  127. $status = drupal_write_record('tripal_views',$view_record);
  128. } else {
  129. $status = FALSE;
  130. drupal_set_message('Unable to integrate '.$defn_array['table'].' table due to a missing name or comment field.', 'error');
  131. }
  132. if ($status) {
  133. // Insert Field Definitions
  134. foreach ($defn_array['fields'] as $field) {
  135. $field_record = array(
  136. 'setup_id' => $view_record['setup_id'],
  137. 'column_name' => $field['name'],
  138. 'name' => $field['title'],
  139. 'description' => $field['description'],
  140. 'type' => $field['type'],
  141. );
  142. if ($view_record['setup_id'] && $field['name'] && $field['title'] && $field['description'] && $field['type']) {
  143. $status = drupal_write_record('tripal_views_field',$field_record);
  144. } else {
  145. drupal_set_message('Unable to integrate '.$field['name'].' field due to a missing required fields.', 'error');
  146. $status = FALSE;
  147. }
  148. if ($status) {
  149. // Insert Handler Definitions
  150. foreach ($field['handlers'] as $handler_type => $handler) {
  151. $handler_record = array(
  152. 'setup_id' => $view_record['setup_id'],
  153. 'column_name' => $field['name'],
  154. 'handler_type' => $handler_type,
  155. 'handler_name' => $handler['name'],
  156. 'arguments' => serialize($handler)
  157. );
  158. if ($view_record['setup_id'] && $field['name'] && $handler_type && $handler['name'] && $handler) {
  159. $status = drupal_write_record('tripal_views_handlers',$handler_record);
  160. } else {
  161. $status = FALSE;
  162. }
  163. if (!$status) {
  164. drupal_set_message('Unable to integrate '.$handler_type.' handler: '.$handler['name'], 'error');
  165. $no_errors = FALSE;
  166. }
  167. }
  168. // Insert Joins
  169. if (!is_array($field['joins'])) { $field['joins'] = array(); }
  170. foreach($field['joins'] as $join) {
  171. $join_record = array(
  172. 'setup_id' => $view_record['setup_id'],
  173. 'base_table' => $defn_array['table'],
  174. 'base_field' => $field['name'],
  175. 'left_table' => $join['table'],
  176. 'left_field' => $join['field'],
  177. );
  178. if (!empty($join['handler'])) {
  179. $join_record['handler'] = $join['handler'];
  180. } else {
  181. $join_record['handler'] = 'views_join';
  182. }
  183. if ($view_record['setup_id'] && $defn_array['table'] && $field['name'] && $join['table'] && $join['field']) {
  184. $status = drupal_write_record('tripal_views_join',$join_record);
  185. } else {
  186. $status = FALSE;
  187. }
  188. if (!$status) {
  189. drupal_set_message('Unable to join '.$join['table'].'.'.$join['field'].' with '.$defn_array['table'].'.'.$field['name'], 'error');
  190. $no_errors = FALSE;
  191. }
  192. }
  193. } else {
  194. drupal_set_message('Unable to integrate field: '.$field['name'],'error');
  195. $no_errors = FALSE;
  196. }
  197. }
  198. } else {
  199. drupal_set_message('Unable to set default views integration','error');
  200. $no_errors = FALSE;
  201. }
  202. return $no_errors;
  203. }
  204. /**
  205. * Removes a View Integration Entry
  206. *
  207. * @param $table_name
  208. * The name of the table to remove a views integration entry for
  209. * @param $priority
  210. * The priority of the of views integration entry
  211. *
  212. * @return
  213. * TRUE on Success; FALSE otherwise
  214. */
  215. function tripal_views_integration_remove_entry_by_table_name ($table_name, $priority) {
  216. $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",$table_name,$priority));
  217. if ($views->setup_id) {
  218. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  219. return TRUE;
  220. } else {
  221. return FALSE;
  222. }
  223. }
  224. /**
  225. * Removes a View Integration Entry
  226. *
  227. * @param $setup_id
  228. * The setup ID of the views integration entry to remove
  229. */
  230. function tripal_views_integration_remove_entry_by_setup_id ($setup_id) {
  231. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$setup_id);
  232. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d',$setup_id);
  233. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$setup_id);
  234. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$setup_id);
  235. }
  236. /**
  237. * Returns the array needed to integrate a given chado table with views
  238. *
  239. * @param $tablename
  240. * The table to generate the tripal views integration array for
  241. * @return
  242. * The tripal views integration array which is the parameter for
  243. * tripal_views_integration_add_entry($defn_array)
  244. */
  245. function tripal_views_get_integration_array_for_chado_table ($table_name) {
  246. // Get the schema for this table (via the chado schema api)
  247. $schema = module_invoke_all('chado_'.$table_name.'_schema');
  248. // Base definition array
  249. $defn_array = array(
  250. 'table' => $table_name,
  251. 'type' => 'chado',
  252. 'name' => ucwords(str_replace('_',' ',$table_name)),
  253. 'description' => ($schema['description']) ? $schema['description'] : ' ',
  254. 'priority' => 10,
  255. 'fields' => array(),
  256. );
  257. // Add fields
  258. foreach ($schema['fields'] as $field_name => $field_schema) {
  259. // Base field definition
  260. if (!empty($field_name) && !empty($field_schema['type'])) {
  261. $defn_array['fields'][$field_name] = array(
  262. 'name' => $field_name,
  263. 'title' => ucwords(str_replace('_',' ',$field_name)),
  264. 'type' => $field_schema['type'],
  265. 'description' => ($field_schema['description']) ? $field_schema['description'] : ' ',
  266. 'handlers' => array(),
  267. 'joins' => array()
  268. );
  269. // Add handlers based on type
  270. if (preg_match('/^int/',$field_schema['type'])) {
  271. $defn_array['fields'][$field_name]['handlers'] = array(
  272. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  273. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  274. 'sort' => array('name' => 'chado_views_handler_sort'),
  275. );
  276. } elseif (preg_match('/^serial/',$field_schema['type'])) {
  277. $defn_array['fields'][$field_name]['handlers'] = array(
  278. 'field' => array('name' => 'chado_views_handler_field_numeric'),
  279. 'filter' => array('name' => 'chado_views_handler_filter_numeric'),
  280. 'sort' => array('name' => 'chado_views_handler_sort'),
  281. );
  282. $defn_array['fields'][$field_name]['type'] = 'int';
  283. } elseif (preg_match('/^varchar/',$field_schema['type'])) {
  284. $defn_array['fields'][$field_name]['handlers'] = array(
  285. 'field' => array('name' => 'chado_views_handler_field'),
  286. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  287. 'sort' => array('name' => 'chado_views_handler_sort'),
  288. );
  289. } elseif (preg_match('/^text/',$field_schema['type'])) {
  290. $defn_array['fields'][$field_name]['handlers'] = array(
  291. 'field' => array('name' => 'chado_views_handler_field'),
  292. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  293. 'sort' => array('name' => 'chado_views_handler_sort'),
  294. );
  295. } else {
  296. $defn_array['fields'][$field_name]['handlers'] = array(
  297. 'field' => array('name' => 'chado_views_handler_field'),
  298. 'filter' => array('name' => 'chado_views_handler_filter_string'),
  299. 'sort' => array('name' => 'chado_views_handler_sort'),
  300. );
  301. }
  302. }
  303. }
  304. // Add Joins to fields
  305. foreach ($schema['foreign keys'] as $foreign_key_schema) {
  306. foreach ($foreign_key_schema['columns'] as $left_field => $right_field) {
  307. $defn_array['fields'][$left_field]['joins'][ $foreign_key_schema['table'] ] = array(
  308. 'table' => $foreign_key_schema['table'],
  309. 'field' => $right_field,
  310. 'handler' => 'views_handler_join_chado_aggregtor'
  311. );
  312. }
  313. }
  314. return $defn_array;
  315. }