tripal_views.api.inc 6.7 KB

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