tripal_chado.views.api.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Removes Chado fields from a view, if Chado is external.
  4. *
  5. * Remove any drupal fields from a chado-based default view definition if
  6. * chado is external. This ensures compatibility with an external chado
  7. * database.
  8. *
  9. * You should be calling this function in your hook_views_default_views().
  10. * This function will only remove drupal tables if chado is external; thus you
  11. * do not need to worry about checking yourself. For example, the following is
  12. * a good hook_views_default_views():
  13. *
  14. * @code
  15. * function mymodule_views_default_views() {
  16. * $views = array();
  17. *
  18. * // NOTE: <VIEW-TYPE> describes the type of view:
  19. * // - 'admin' for views used for administration of your module
  20. * // - 'search' for views used to search data
  21. * // - 'list' for views used primarily as data listings
  22. *
  23. * // <VIEW-HUMAN-READABLE-NAME>
  24. * $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  25. * $view = tripal_make_view_compatible_with_external($view);
  26. * $views[$view->name] = $view;*
  27. *
  28. * // <VIEW-HUMAN-READABLE-NAME>
  29. * $view = mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>();
  30. * $view = tripal_make_view_compatible_with_external($view);
  31. * $views[$view->name] = $view;
  32. *
  33. * return $views;
  34. * }
  35. *
  36. * function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  37. * // PASTE VIEWS EXPORT CODE HERE
  38. * return $view;
  39. * }
  40. *
  41. * function mymodule_defaultview_<VIEW-TYPE>_<VIEW-MACHINE-NAME>() {
  42. * // PASTE VIEWS EXPORT CODE HERE
  43. * return $view;
  44. * }
  45. * @endcode
  46. *
  47. * Notice that the actual views export code is in a separate function. This
  48. * makes your hook_views_default_views() more readable.
  49. *
  50. * NOTE: Currently assumes all tables not in the tripal views integration
  51. * tables are Drupal tables.
  52. *
  53. * @param $view
  54. * The default view definition object
  55. * @return
  56. * The default view with all relationships, fields, filters, sorts,
  57. * arguements for Drupal tables removed.
  58. */
  59. function tripal_make_view_compatible_with_external($view) {
  60. $remove_table = array();
  61. // First check that the base table for the view is a chado table
  62. // If it's not then don't do any filtering
  63. $schema = chado_get_schema($view->base_table);
  64. if (!$schema) {
  65. return $view;
  66. }
  67. // $setup_id = tripal_is_table_integrated($view->base_table);
  68. // if (!$setup_id) {
  69. // return $view;
  70. // }
  71. // IF chado is external then remove all config relating to drupal tables
  72. if (!chado_is_local()) {
  73. // Iterate through all displays
  74. foreach ($view->display as $display_name => $display) {
  75. $display_options = $display->handler->display->display_options;
  76. $sections = array('fields', 'filters', 'sorts', 'relationships');
  77. foreach ($sections as $section) {
  78. $display_options[$section] = (isset($display_options[$section])) ? $display_options[$section] : array();
  79. foreach ($display_options[$section] as $key => $defn) {
  80. // If the table has not already been encountered; check if it's in tripal_views
  81. if (!isset($remove_table[ $defn['table'] ])) {
  82. // If the table is view then this is a general handler; thus keep
  83. if ($defn['table'] == 'views') {
  84. $remove_table[ $defn['table'] ] = FALSE;
  85. }
  86. // If this table is integrated then it is chado; thus keep
  87. //$setup_id = tripal_is_table_integrated($defn['table']);
  88. $schema = chado_get_schema($defn['table']);
  89. //if ($setup_id) {
  90. if ($schema) {
  91. $remove_table[ $defn['table'] ] = FALSE;
  92. }
  93. else {
  94. $remove_table[ $defn['table'] ] = TRUE;
  95. }
  96. }
  97. // Based on the $remove_table array, unset this field if its from a drupal table
  98. if ($remove_table[ $defn['table'] ]) {
  99. unset($view->display[$display_name]->handler->display->display_options[$section][$key]);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. return $view;
  106. }
  107. /**
  108. * Programatically disable view.
  109. *
  110. * This should be used in a hook_menu definition as the callback to provide a link
  111. * to disable the view (first example). It can also be called directly if needed (second example).
  112. * @code
  113. * // Create a URL that when the user navigates there, a given view will be disabled.
  114. * // You will still need to provide a link to this menu item somewhere appropriate.
  115. * function mymodule_menu() {
  116. * $items = array();
  117. *
  118. * // Create one of these for each of your default views
  119. * $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/disable'] = array(
  120. * 'title' => 'Disable <VIEW-HUMAN-READABLE-NAME>',
  121. * 'page callback' => 'tripal_disable_view',
  122. * 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  123. * 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  124. * 'type' => MENU_CALLBACK,
  125. * );
  126. *
  127. * return $items;
  128. * }
  129. *
  130. * // Call this function directly to disable a view
  131. * // The example shows disabling your own default view when your module is uninstalled
  132. * function mymodule_uninstall() {
  133. *
  134. * $view_name = '<VIEW-MACHINE-NAME>';
  135. * tripal_disable_view($view_name);
  136. *
  137. * }
  138. * @endcode
  139. *
  140. * @param $view_name
  141. * The machine-name of the view to be enabled
  142. * @param $redirect_link
  143. * The path to redirect to. FALSE if no redirect needed
  144. *
  145. * @ingroup tripal_chado_views_api
  146. */
  147. function tripal_disable_view($view_name, $redirect_link = FALSE) {
  148. $status = variable_get('views_defaults', array());
  149. if (isset($status[$view_name])) {
  150. $status[$view_name] = TRUE;
  151. variable_set('views_defaults', $status);
  152. drupal_set_message("Disabled $view_name");
  153. }
  154. else {
  155. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to disable this view.",'error');
  156. }
  157. if ($redirect_link) {
  158. drupal_goto($redirect_link);
  159. }
  160. }
  161. /**
  162. * Programatically enable view
  163. *
  164. * This should be used in a hook_menu definition as the callback to provide a link
  165. * to enable the view (first example). It can also be called directly if needed (second example).
  166. * @code
  167. * // Create a URL that when the user navigates there, a given view will be enabled.
  168. * // You will still need to provide a link to this menu item somewhere appropriate (ie: an admin landing page).
  169. * function mymodule_menu() {
  170. * $items = array();
  171. *
  172. * // Create one of these for each of your default views
  173. * $items['admin/tripal/<PATH-TO-YOUR-ADMIN-SECTION>/views/<VIEW-MACHINE-NAME>/enable'] = array(
  174. * 'title' => 'Enable <VIEW-HUMAN-READABLE-NAME>',
  175. * 'page callback' => 'tripal_enable_view',
  176. * 'page arguments' => array('<VIEW-MACHINE-NAME>', '<PATH-TO-REDIRECT-TO-AFTERWARDS>'),
  177. * 'access arguments' => array('<YOUR-PERMISSION-KEY>'),
  178. * 'type' => MENU_CALLBACK,
  179. * );
  180. *
  181. * return $items;
  182. * }
  183. *
  184. * // Call this function directly to disable a view
  185. * // The example shows enabling your own default view when your module is enabled.
  186. * // This might be useful if you disable your view when your module is disabled.
  187. * function mymodule_enable() {
  188. * $view_name = '<VIEW-MACHINE-NAME>';
  189. * tripal_enable_view($view_name);
  190. * }
  191. * @endcode
  192. *
  193. * @param $view_name
  194. * The machine-name of the view to be enabled
  195. * @param $redirect_link
  196. * The path to redirect to. FALSE if no redirect needed
  197. *
  198. * @ingroup tripal_chado_views_api
  199. */
  200. function tripal_enable_view($view_name, $redirect_link = FALSE) {
  201. $status = variable_get('views_defaults', array());
  202. if (isset($status[$view_name])) {
  203. $status[$view_name] = FALSE;
  204. variable_set('views_defaults', $status);
  205. drupal_set_message("Successfully Enabled $view_name");
  206. }
  207. else {
  208. drupal_set_message("Unable to find a view by the name of '$view_name'. Unable to enable this view.",'error');
  209. }
  210. if ($redirect_link) {
  211. drupal_goto($redirect_link);
  212. }
  213. }