tripal_views.api.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 'name' => $defn_array['name'],
  81. 'comment' => $defn_array['description'],
  82. 'priority' => $defn_array['priority'],
  83. );
  84. switch($defn_array['type']) {
  85. case 'chado':
  86. $view_record['table_name'] = $defn_array['table'];
  87. break;
  88. case 'mview':
  89. $mview = db_fetch_object(db_query("SELECT mview_id FROM tripal_mviews WHERE mv_table='%s'",$defn_array['table']));
  90. $view_record['mview_id'] = $mview->mview_id;
  91. $view_record['table_name'] = $defn_array['table'];
  92. break;
  93. }
  94. $status = drupal_write_record('tripal_views',$view_record);
  95. if ($status) {
  96. // Insert Field Definitions
  97. foreach ($defn_array['fields'] as $field) {
  98. $field_record = array(
  99. 'setup_id' => $view_record['setup_id'],
  100. 'column_name' => $field['name'],
  101. 'name' => $field['title'],
  102. 'description' => $field['description'],
  103. 'type' => $field['type'],
  104. );
  105. $status = drupal_write_record('tripal_views_field',$field_record);
  106. if ($status) {
  107. // Insert Handler Definitions
  108. foreach ($field['handlers'] as $handler_type => $handler) {
  109. $handler_record = array(
  110. 'setup_id' => $view_record['setup_id'],
  111. 'column_name' => $field['name'],
  112. 'handler_type' => $handler_type,
  113. 'handler_name' => $handler['name'],
  114. 'arguments' => serialize($handler)
  115. );
  116. $status = drupal_write_record('tripal_views_handlers',$handler_record);
  117. if (!$status) {
  118. drupal_set_message('Unable to integrate '.$handler_type.' handler: '.$handler['name'], 'error');
  119. $no_errors = FALSE;
  120. }
  121. }
  122. // Insert Joins
  123. if (!is_array($field['joins'])) { $field['joins'] = array(); }
  124. foreach($field['joins'] as $join) {
  125. $join_record = array(
  126. 'setup_id' => $view_record['setup_id'],
  127. 'base_table' => $defn_array['table'],
  128. 'base_field' => $field['name'],
  129. 'left_table' => $join['table'],
  130. 'left_field' => $join['field'],
  131. );
  132. if (!empty($join['handler'])) {
  133. $join_record['handler'] = $join['handler'];
  134. } else {
  135. $join_record['handler'] = 'views_join';
  136. }
  137. $status = drupal_write_record('tripal_views_join',$join_record);
  138. if (!$status) {
  139. drupal_set_message('Unable to join '.$join['table'].'.'.$join['field'].' with '.$defn_array['table'].'.'.$field['name'], 'error');
  140. $no_errors = FALSE;
  141. }
  142. }
  143. } else {
  144. drupal_set_message('Unable to integrate field: '.$field['name'],'error');
  145. $no_errors = FALSE;
  146. }
  147. }
  148. } else {
  149. drupal_set_message('Unable to set default views integration','error');
  150. $no_errors = FALSE;
  151. }
  152. return $no_errors;
  153. }
  154. /**
  155. * Removes a View Integration Entry
  156. *
  157. * @param $table_name
  158. * The name of the table to remove a views integration entry for
  159. * @param $priority
  160. * The priority of the of views integration entry
  161. *
  162. * @return
  163. * TRUE on Success; FALSE otherwise
  164. */
  165. function tripal_views_integration_remove_entry_by_table_name ($table_name, $priority) {
  166. $views = db_fetch_object(db_query("SELECT * FROM {tripal_views} WHERE table_name='%s' AND priority=%d",$table_name,$priority));
  167. if ($views->setup_id) {
  168. tripal_views_integration_remove_entry_by_setup_id($views->setup_id);
  169. return TRUE;
  170. } else {
  171. return FALSE;
  172. }
  173. }
  174. /**
  175. * Removes a View Integration Entry
  176. *
  177. * @param $setup_id
  178. * The setup ID of the views integration entry to remove
  179. */
  180. function tripal_views_integration_remove_entry_by_setup_id ($setup_id) {
  181. db_query('DELETE FROM {tripal_views} WHERE setup_id=%d',$setup_id);
  182. db_query('DELETE FROM {tripal_views_field} WHERE setup_id=%d',$setup_id);
  183. db_query('DELETE FROM {tripal_views_handlers} WHERE setup_id=%d',$setup_id);
  184. db_query('DELETE FROM {tripal_views_join} WHERE setup_id=%d',$setup_id);
  185. }