tripal_views.api.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. * Checks if you are dealing with the lightest priority setup for a given table
  31. */
  32. function tripal_views_is_lightest_priority_setup($setup_id, $table_name) {
  33. $lightest_priority_setup_id = tripal_views_get_lightest_priority_setup($table_name);
  34. if ($lightest_priority_setup_id == $setup_id) {
  35. return TRUE;
  36. }
  37. else {
  38. return FALSE;
  39. }
  40. }
  41. /**
  42. * Add views integration records into the tripal_views* tables
  43. *
  44. * @param $defn_array
  45. * An array describing the structure and fields of the table
  46. *
  47. * @return
  48. * True/False if completed successfully/not
  49. *
  50. * Example usage (in hook_install()):
  51. * @code
  52. $defn_array = array(
  53. 'table' => 'feature', //tablename or materialized view name
  54. 'name' => 'Sequence Features', // Human readable name
  55. 'type' => 'chado', //either chado or mview depending on tablename
  56. 'description' => 'Create a listing of features.', //description seen when creating a view of this type
  57. 'priority' => 10, //For Base tripal modules: 10; custom modules: 9 to 0;
  58. 'fields' => array(
  59. 'feature_id' => array(
  60. 'name' => 'feature_id', //field name in database
  61. 'title' => 'Feature ID', //human-readable name -seen in Views UI
  62. 'description' => 'This is the unique identifier for features', //help/description seen in Views UI
  63. 'type' => 'int', // the type of field
  64. 'handlers' => array( //possible keys are field, filter, sort, argument, relationship
  65. 'field' => array(
  66. 'name' => 'chado_views_handler_numeric' //name of handler
  67. ),
  68. 'filter' => array( ... ),
  69. ...
  70. ),
  71. 'join' => array( //describe a table that joins to this one via this field
  72. 'table' => 'featureprop', //table to join to
  73. 'field' => 'feature_id', //field in above table (featureprop)
  74. 'handler' => 'views_handler_join_chado_aggregator', //handler to use
  75. ),
  76. )
  77. ),
  78. );
  79. tripal_views_integration_add_entry($defn_array);
  80. * @endcode
  81. */
  82. function tripal_views_integration_add_entry($defn_array) {
  83. $no_errors = TRUE;
  84. // First insert into tripal_views
  85. $view_record = array(
  86. 'table_name' => $defn_array['table'],
  87. 'name' => $defn_array['name'],
  88. 'comment' => $defn_array['description'],
  89. 'priority' => $defn_array['priority'],
  90. );
  91. if ($defn_array['type'] == 'mview') {
  92. $mview = db_fetch_object(db_query("SELECT mview_id FROM {tripal_mviews} WHERE mv_table='%s'", $defn_array['table']));
  93. $view_record['mview_id'] = $mview->mview_id;
  94. if (!$mview->mview_id) {
  95. return FALSE;
  96. }
  97. }
  98. $status = drupal_write_record('tripal_views', $view_record);
  99. if ($status) {
  100. // Insert Field Definitions
  101. foreach ($defn_array['fields'] as $field) {
  102. $field_record = array(
  103. 'setup_id' => $view_record['setup_id'],
  104. 'column_name' => $field['name'],
  105. 'name' => $field['title'],
  106. 'description' => $field['description'],
  107. 'type' => $field['type'],
  108. );
  109. $status = drupal_write_record('tripal_views_field', $field_record);
  110. if ($status) {
  111. // Insert Handler Definitions
  112. foreach ($field['handlers'] as $handler_type => $handler) {
  113. $handler_record = array(
  114. 'setup_id' => $view_record['setup_id'],
  115. 'column_name' => $field['name'],
  116. 'handler_type' => $handler_type,
  117. 'handler_name' => $handler['name'],
  118. 'arguments' => serialize($handler)
  119. );
  120. $status = drupal_write_record('tripal_views_handlers', $handler_record);
  121. if (!$status) {
  122. drupal_set_message(t('Unable to integrate %handler_type handler: %handler_name', array('%handler_type' => $handler_type, '%handler_name' => $handler['name'])), 'error');
  123. $no_errors = FALSE;
  124. }
  125. }
  126. // Insert Joins
  127. if (!is_array($field['joins'])) {
  128. $field['joins'] = array();
  129. }
  130. foreach ($field['joins'] as $join) {
  131. $join_record = array(
  132. 'setup_id' => $view_record['setup_id'],
  133. 'base_table' => $defn_array['table'],
  134. 'base_field' => $field['name'],
  135. 'left_table' => $join['table'],
  136. 'left_field' => $join['field'],
  137. );
  138. if (!empty($join['handler'])) {
  139. $join_record['handler'] = $join['handler'];
  140. }
  141. else {
  142. $join_record['handler'] = 'views_join';
  143. }
  144. $status = drupal_write_record('tripal_views_join', $join_record);
  145. if (!$status) {
  146. drupal_set_message(
  147. t(
  148. 'Unable to join %left_table.%left_field with %table.%field',
  149. array(
  150. '%left_table' => $join['table'],
  151. '%left_field' => $join['field'],
  152. '%table' => $defn_array['table'],
  153. '%field' => $field['name']
  154. )
  155. ),
  156. 'error'
  157. );
  158. $no_errors = FALSE;
  159. }
  160. }
  161. }
  162. else {
  163. drupal_set_message(t('Unable to integrate field: %field_name', array('%field_name' => $field['name'])), 'error');
  164. $no_errors = FALSE;
  165. }
  166. }
  167. }
  168. else {
  169. drupal_set_message(t('Unable to set default views integration'), 'error');
  170. $no_errors = FALSE;
  171. }
  172. return $no_errors;
  173. }
  174. /**
  175. * Removes a View Integration Entry
  176. *
  177. * @param $table_name
  178. * The name of the table to remove a views integration entry for
  179. * @param $priority
  180. * The priority of the of views integration entry
  181. *
  182. * @return
  183. * TRUE on Success; FALSE otherwise
  184. */
  185. function tripal_views_integration_remove_entry_by_table_name($table_name, $priority) {
  186. $views = db_fetch_object(db_query(
  187. "SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",
  188. $table_name,
  189. $priority
  190. ));
  191. if ($views->setup_id) {
  192. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  193. return TRUE;
  194. }
  195. else {
  196. return FALSE;
  197. }
  198. }
  199. /**
  200. * Removes a View Integration Entry
  201. *
  202. * @param $setup_id
  203. * The setup ID of the views integration entry to remove
  204. */
  205. function tripal_views_integration_remove_entry_by_setup_id($setup_id) {
  206. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d', $setup_id);
  207. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d', $setup_id);
  208. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d', $setup_id);
  209. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d', $setup_id);
  210. }