tripal_views.api.inc 12 KB

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