tripal_core.views.inc 9.0 KB

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