tripal_core.views.inc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. include('views/handlers/views_handler_join_chado_through_linking.inc');
  3. include('views/handlers/views_handler_join_chado_aggregator.inc');
  4. /**
  5. * @defgroup views Views Integration
  6. * @{
  7. * Provide rules for formatting and composition of fields
  8. * @}
  9. */
  10. /**
  11. * @defgroup views_field_handlers Views Field Handlers
  12. * @{
  13. * Provide rules for formatting and composition of fields
  14. * @}
  15. * @ingroup views
  16. */
  17. /**
  18. * @defgroup views_filter_handlers Views Filter Handlers
  19. * @{
  20. * Provide the ability to filter based on specified data
  21. * @}
  22. * @ingroup views
  23. */
  24. /**
  25. * @defgroup views_sort_handlers Views Sort Handlers
  26. * @{
  27. * Provide methods describing how specific data should be sorted
  28. * @}
  29. * @ingroup views
  30. */
  31. /**
  32. * @defgroup views_argument_handlers Views Arguement Handlers
  33. * @{
  34. * Provide the ability to filter pased on arguments in the path of the view
  35. * @}
  36. * @ingroup views
  37. */
  38. /**
  39. * Implements hook_views_handlers()
  40. *
  41. * Purpose: Register all custom handlers with views
  42. * where a handler describes either "the type of field",
  43. * "how a field should be filtered", "how a field should be sorted"
  44. *
  45. * @return
  46. * An array of handler definitions
  47. *
  48. * @ingroup tripal_core
  49. */
  50. function tripal_core_views_handlers() {
  51. return array(
  52. 'info' => array(
  53. 'path' => drupal_get_path('module', 'tripal_core') . '/views/handlers',
  54. ),
  55. 'handlers' => array(
  56. 'views_handler_field_node_optional' => array(
  57. 'parent' => 'views_handler_field_node',
  58. ),
  59. 'views_handler_field_cvterm_name' => array(
  60. 'parent' => 'views_handler_field',
  61. ),
  62. 'views_handler_field_chado_tf_boolean' => array(
  63. 'parent' => 'views_handler_field_boolean',
  64. ),
  65. 'views_handler_field_chado_count' => array(
  66. 'parent' => 'views_handler_field',
  67. ),
  68. 'views_handler_filter_chado_select_string' => array(
  69. 'parent' => 'views_handler_filter_string',
  70. ),
  71. 'views_handler_filter_chado_select_cvterm_name' => array(
  72. 'parent' => 'views_handler_filter_string',
  73. ),
  74. 'views_handler_filter_chado_boolean' => array(
  75. 'parent' => 'views_handler_filter_boolean_operator',
  76. ),
  77. 'views_handler_field_chado_rel_by_type' => array(
  78. 'parent' => 'views_handler_field_prerender_list',
  79. ),
  80. 'views_handler_join_chado_one2many' => array(
  81. 'parent' => 'views_join'
  82. ),
  83. ),
  84. );
  85. }
  86. /**
  87. * Implements hook_views_pre_render
  88. *
  89. * Purpose: Intercepts the view after the query has been executed
  90. * All the results are stored in $view->result
  91. * Looking up the NID here ensures the query is only executed once
  92. * for all stocks in the table.
  93. *
  94. * @todo add if !<chado/drupal same db> around NID portion
  95. *
  96. * @ingroup tripal_core
  97. */
  98. function tripal_core_views_pre_render (&$view) {
  99. //Add Node IDs in to every table that needs them
  100. // @see file: tripal_core.views.inc
  101. tripal_core_add_node_ids_to_view ($view);
  102. }
  103. /**
  104. * Adds nid fields to all pertinent view results
  105. *
  106. * Purpose: To add basetable_nid fields to all result arrays of a view
  107. * only if the basetable_nid field is added. This function will only be
  108. * called if chado/drupal are not in the same database (ie: only if
  109. * a join between the base and node table isn't possible.
  110. *
  111. * Note: Supports adding Node IDs to analysis, feature, library, organism, stock
  112. *
  113. * @param $view
  114. * the view object passed to hook_views_pre_render
  115. *
  116. * @return the views object with nids added to the result array
  117. *
  118. * @ingroup tripal_core
  119. */
  120. function tripal_core_add_node_ids_to_view (&$view) {
  121. //-----Analysis----------------------------------------------
  122. if (!empty($view->field['analysis_nid'])) {
  123. // retrieve the analysis_id for each record in the views current page
  124. $analysis_ids = array();
  125. foreach ($view->result as $row_num => $row) {
  126. if (!empty($row->analysis_id)) {
  127. //we're looking at analysis id field already in table
  128. $analysis_ids[$row_num] = $row->analysis_id;
  129. } else {
  130. //we're looking at analysis id added by field handler
  131. $analysis_ids[$row_num] = $row->analysis_analysis_id;
  132. }
  133. }
  134. $unique_analysis_ids = array_filter($analysis_ids);
  135. $unique_analysis_ids = array_unique($unique_analysis_ids);
  136. if (!empty($unique_analysis_ids)) {
  137. // Using the list of analysis_ids from the view
  138. // lookup the NIDs from drupal
  139. // and add that to the results of the view
  140. $sql = "SELECT nid, analysis_id FROM {chado_analysis} WHERE analysis_id IN (".implode(',',$unique_analysis_ids).")";
  141. $resource = db_query($sql);
  142. while ($r = db_fetch_object($resource)) {
  143. $keys = array_keys($analysis_ids, $r->analysis_id);
  144. foreach ($keys as $k) {
  145. $view->result[$k]->analysis_nid = $r->nid;
  146. }
  147. }
  148. } // if there are any analysis'
  149. } //end of case for analysis NID
  150. //-----Feature-----------------------------------------------
  151. if (!empty($view->field['feature_nid'])) {
  152. // retrieve the feature_id for each record in the views current page
  153. $feature_ids = array();
  154. foreach ($view->result as $row_num => $row) {
  155. if (!empty($row->feature_id)) {
  156. //we're looking at feature id field already in table
  157. $feature_ids[$row_num] = $row->feature_id;
  158. } else {
  159. //we're looking at feature id added by field handler
  160. $feature_ids[$row_num] = $row->feature_feature_id;
  161. }
  162. }
  163. $unique_feature_ids = array_filter($feature_ids);
  164. $unique_feature_ids = array_unique($unique_feature_ids);
  165. if (!empty($unique_feature_ids)) {
  166. // Using the list of feature_ids from the view
  167. // lookup the NIDs from drupal
  168. // and add that to the results of the view
  169. $sql = "SELECT nid, feature_id FROM {chado_feature} WHERE feature_id IN (".implode(',',$unique_feature_ids).")";
  170. $resource = db_query($sql);
  171. while ($r = db_fetch_object($resource)) {
  172. $keys = array_keys($feature_ids, $r->feature_id);
  173. foreach ($keys as $k) {
  174. $view->result[$k]->feature_nid = $r->nid;
  175. }
  176. }
  177. } // if there are any features
  178. } //end of case for feature NID
  179. //-----Library-----------------------------------------------
  180. if (!empty($view->field['library_nid'])) {
  181. // retrieve the library_id for each record in the views current page
  182. $library_ids = array();
  183. foreach ($view->result as $row_num => $row) {
  184. if (!empty($row->library_id)) {
  185. //we're looking at library id field already in table
  186. $library_ids[$row_num] = $row->library_id;
  187. } else {
  188. //we're looking at library id added by field handler
  189. $library_ids[$row_num] = $row->library_library_id;
  190. }
  191. }
  192. $unique_library_ids = array_filter($library_ids);
  193. $unique_library_ids = array_unique($unique_library_ids);
  194. if (!empty($unique_library_ids)) {
  195. // Using the list of library_ids from the view
  196. // lookup the NIDs from drupal
  197. // and add that to the results of the view
  198. $sql = "SELECT nid, library_id FROM {chado_library} WHERE library_id IN (".implode(',',$unique_library_ids).")";
  199. $resource = db_query($sql);
  200. while ($r = db_fetch_object($resource)) {
  201. $keys = array_keys($library_ids, $r->library_id);
  202. foreach ($keys as $k) {
  203. $view->result[$k]->library_nid = $r->nid;
  204. }
  205. }
  206. } // if there are libraries
  207. } //end of case for library NID
  208. //-----Organism----------------------------------------------
  209. if (!empty($view->field['organism_nid'])) {
  210. // retrieve the organism_id for each record in the views current page
  211. $organism_ids = array();
  212. foreach ($view->result as $row_num => $row) {
  213. if (!empty($row->organism_id)) {
  214. //we're looking at organism id field already in table
  215. $organism_ids[$row_num] = $row->organism_id;
  216. } else {
  217. //we're looking at organism id added by field handler
  218. $organism_ids[$row_num] = $row->organism_organism_id;
  219. }
  220. }
  221. $unique_organism_ids = array_filter($organism_ids);
  222. $unique_organism_ids = array_unique($unique_organism_ids);
  223. if (!empty($unique_organism_ids)) {
  224. // Using the list of organism_ids from the view
  225. // lookup the NIDs from drupal
  226. // and add that to the results of the view
  227. $sql = "SELECT nid, organism_id FROM {chado_organism} WHERE organism_id IN (".implode(',',$unique_organism_ids).")";
  228. $resource = db_query($sql);
  229. while ($r = db_fetch_object($resource)) {
  230. $keys = array_keys($organism_ids, $r->organism_id);
  231. foreach ($keys as $k) {
  232. $view->result[$k]->organism_nid = $r->nid;
  233. }
  234. }
  235. } // if there are organisms
  236. } //end of case for organism NID
  237. //-----Stock-------------------------------------------------
  238. if (!empty($view->field['stock_nid'])) {
  239. // retrieve the stock_id for each record in the views current page
  240. $stock_ids = array();
  241. foreach ($view->result as $row_num => $row) {
  242. if (!empty($row->stock_id)) {
  243. //we're looking at stock id field already in table
  244. $stock_ids[$row_num] = $row->stock_id;
  245. } else {
  246. //we're looking at stock id added by field handler
  247. $stock_ids[$row_num] = $row->stock_stock_id;
  248. }
  249. }
  250. $unique_stock_ids = array_filter($stock_ids);
  251. $unique_stock_ids = array_unique($unique_stock_ids);
  252. if (!empty($unique_stock_ids)) {
  253. // Using the list of stock_ids from the view
  254. // lookup the NIDs from drupal
  255. // and add that to the results of the view
  256. $sql = "SELECT nid, stock_id FROM {chado_stock} WHERE stock_id IN (".implode(',',$unique_stock_ids).")";
  257. $resource = db_query($sql);
  258. while ($r = db_fetch_object($resource)) {
  259. $keys = array_keys($stock_ids, $r->stock_id);
  260. foreach ($keys as $k) {
  261. $view->result[$k]->stock_nid = $r->nid;
  262. }
  263. }
  264. } //if there are stocks
  265. } //end of case for stock NID
  266. return $view;
  267. }