tripal_core.views.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // Custom Chado Handlers
  57. 'chado_views_handler_field_aggregate' => array(
  58. 'parent' => 'chado_views_handler_field',
  59. ),
  60. 'views_handler_filter_chado_select_string' => array(
  61. 'parent' => 'views_handler_filter_string',
  62. ),
  63. 'views_handler_filter_chado_select_cvterm_name' => array(
  64. 'parent' => 'views_handler_filter_string',
  65. ),
  66. // Old Handlers
  67. 'views_handler_field_node_optional' => array(
  68. 'parent' => 'views_handler_field_node',
  69. ),
  70. 'views_handler_field_cvterm_name' => array(
  71. 'parent' => 'views_handler_field',
  72. ),
  73. 'views_handler_field_chado_tf_boolean' => array(
  74. 'parent' => 'views_handler_field_boolean',
  75. ),
  76. 'views_handler_field_chado_count' => array(
  77. 'parent' => 'views_handler_field',
  78. ),
  79. 'views_handler_filter_chado_boolean' => array(
  80. 'parent' => 'views_handler_filter_boolean_operator',
  81. ),
  82. 'views_handler_field_chado_rel_by_type' => array(
  83. 'parent' => 'views_handler_field_prerender_list',
  84. ),
  85. // Wrappers for Default Views Handlers-----
  86. // Field Handlers
  87. 'chado_views_handler_field' => array(
  88. 'parent' => 'views_handler_field'
  89. ),
  90. 'chado_views_handler_field_boolean' => array(
  91. 'parent' => 'views_handler_field_boolean'
  92. ),
  93. 'chado_views_handler_field_counter' => array(
  94. 'parent' => 'views_handler_field_counter'
  95. ),
  96. 'chado_views_handler_field_custom' => array(
  97. 'parent' => 'views_handler_field_custom'
  98. ),
  99. 'chado_views_handler_field_date' => array(
  100. 'parent' => 'views_handler_field_date'
  101. ),
  102. 'chado_views_handler_field_markup' => array(
  103. 'parent' => 'views_handler_field_markup'
  104. ),
  105. 'chado_views_handler_field_math' => array(
  106. 'parent' => 'views_handler_field_math'
  107. ),
  108. 'chado_views_handler_field_numeric' => array(
  109. 'parent' => 'views_handler_field_numeric'
  110. ),
  111. // Filter Handlers
  112. 'chado_views_handler_filter_string' => array(
  113. 'parent' => 'views_handler_filter_string',
  114. ),
  115. // Sort Handlers
  116. 'chado_views_handler_sort' => array(
  117. 'parent' => 'views_handler_sort'
  118. ),
  119. 'chado_views_handler_sort_date' => array(
  120. 'parent' => 'views_handler_sort_date'
  121. ),
  122. 'chado_views_handler_sort_formula' => array(
  123. 'parent' => 'views_handler_sort_formula'
  124. ),
  125. 'chado_views_handler_sort_menu_hierarchy' => array(
  126. 'parent' => 'views_handler_sort_menu_hierarchy'
  127. ),
  128. 'chado_views_handler_sort_random' => array(
  129. 'parent' => 'views_handler_sort_random'
  130. ),
  131. ),
  132. );
  133. }
  134. /**
  135. * Implements hook_views_pre_render
  136. *
  137. * Purpose: Intercepts the view after the query has been executed
  138. * All the results are stored in $view->result
  139. * Looking up the NID here ensures the query is only executed once
  140. * for all stocks in the table.
  141. *
  142. * @todo add if !<chado/drupal same db> around NID portion
  143. *
  144. * @ingroup tripal_core
  145. */
  146. function tripal_core_views_pre_render (&$view) {
  147. //Add Node IDs in to every table that needs them
  148. // @see file: tripal_core.views.inc
  149. tripal_core_add_node_ids_to_view ($view);
  150. }
  151. /**
  152. * Adds nid fields to all pertinent view results
  153. *
  154. * Purpose: To add basetable_nid fields to all result arrays of a view
  155. * only if the basetable_nid field is added. This function will only be
  156. * called if chado/drupal are not in the same database (ie: only if
  157. * a join between the base and node table isn't possible.
  158. *
  159. * Note: Supports adding Node IDs to analysis, feature, library, organism, stock
  160. *
  161. * @param $view
  162. * the view object passed to hook_views_pre_render
  163. *
  164. * @return the views object with nids added to the result array
  165. *
  166. * @ingroup tripal_core
  167. */
  168. function tripal_core_add_node_ids_to_view (&$view) {
  169. //-----Analysis----------------------------------------------
  170. if (!empty($view->field['analysis_nid'])) {
  171. // retrieve the analysis_id for each record in the views current page
  172. $analysis_ids = array();
  173. foreach ($view->result as $row_num => $row) {
  174. if (!empty($row->analysis_id)) {
  175. //we're looking at analysis id field already in table
  176. $analysis_ids[$row_num] = $row->analysis_id;
  177. } else {
  178. //we're looking at analysis id added by field handler
  179. $analysis_ids[$row_num] = $row->analysis_analysis_id;
  180. }
  181. }
  182. $unique_analysis_ids = array_filter($analysis_ids);
  183. $unique_analysis_ids = array_unique($unique_analysis_ids);
  184. if (!empty($unique_analysis_ids)) {
  185. // Using the list of analysis_ids from the view
  186. // lookup the NIDs from drupal
  187. // and add that to the results of the view
  188. $sql = "SELECT nid, analysis_id FROM {chado_analysis} WHERE analysis_id IN (".implode(',',$unique_analysis_ids).")";
  189. $resource = db_query($sql);
  190. while ($r = db_fetch_object($resource)) {
  191. $keys = array_keys($analysis_ids, $r->analysis_id);
  192. foreach ($keys as $k) {
  193. $view->result[$k]->analysis_nid = $r->nid;
  194. }
  195. }
  196. } // if there are any analysis'
  197. } //end of case for analysis NID
  198. //-----Feature-----------------------------------------------
  199. if (!empty($view->field['feature_nid'])) {
  200. // retrieve the feature_id for each record in the views current page
  201. $feature_ids = array();
  202. foreach ($view->result as $row_num => $row) {
  203. if (!empty($row->feature_id)) {
  204. //we're looking at feature id field already in table
  205. $feature_ids[$row_num] = $row->feature_id;
  206. } else {
  207. //we're looking at feature id added by field handler
  208. $feature_ids[$row_num] = $row->feature_feature_id;
  209. }
  210. }
  211. $unique_feature_ids = array_filter($feature_ids);
  212. $unique_feature_ids = array_unique($unique_feature_ids);
  213. if (!empty($unique_feature_ids)) {
  214. // Using the list of feature_ids from the view
  215. // lookup the NIDs from drupal
  216. // and add that to the results of the view
  217. $sql = "SELECT nid, feature_id FROM {chado_feature} WHERE feature_id IN (".implode(',',$unique_feature_ids).")";
  218. $resource = db_query($sql);
  219. while ($r = db_fetch_object($resource)) {
  220. $keys = array_keys($feature_ids, $r->feature_id);
  221. foreach ($keys as $k) {
  222. $view->result[$k]->feature_nid = $r->nid;
  223. }
  224. }
  225. } // if there are any features
  226. } //end of case for feature NID
  227. //-----Library-----------------------------------------------
  228. if (!empty($view->field['library_nid'])) {
  229. // retrieve the library_id for each record in the views current page
  230. $library_ids = array();
  231. foreach ($view->result as $row_num => $row) {
  232. if (!empty($row->library_id)) {
  233. //we're looking at library id field already in table
  234. $library_ids[$row_num] = $row->library_id;
  235. } else {
  236. //we're looking at library id added by field handler
  237. $library_ids[$row_num] = $row->library_library_id;
  238. }
  239. }
  240. $unique_library_ids = array_filter($library_ids);
  241. $unique_library_ids = array_unique($unique_library_ids);
  242. if (!empty($unique_library_ids)) {
  243. // Using the list of library_ids from the view
  244. // lookup the NIDs from drupal
  245. // and add that to the results of the view
  246. $sql = "SELECT nid, library_id FROM {chado_library} WHERE library_id IN (".implode(',',$unique_library_ids).")";
  247. $resource = db_query($sql);
  248. while ($r = db_fetch_object($resource)) {
  249. $keys = array_keys($library_ids, $r->library_id);
  250. foreach ($keys as $k) {
  251. $view->result[$k]->library_nid = $r->nid;
  252. }
  253. }
  254. } // if there are libraries
  255. } //end of case for library NID
  256. //-----Organism----------------------------------------------
  257. if (!empty($view->field['organism_nid'])) {
  258. // retrieve the organism_id for each record in the views current page
  259. $organism_ids = array();
  260. foreach ($view->result as $row_num => $row) {
  261. if (!empty($row->organism_id)) {
  262. //we're looking at organism id field already in table
  263. $organism_ids[$row_num] = $row->organism_id;
  264. } else {
  265. //we're looking at organism id added by field handler
  266. $organism_ids[$row_num] = $row->organism_organism_id;
  267. }
  268. }
  269. $unique_organism_ids = array_filter($organism_ids);
  270. $unique_organism_ids = array_unique($unique_organism_ids);
  271. if (!empty($unique_organism_ids)) {
  272. // Using the list of organism_ids from the view
  273. // lookup the NIDs from drupal
  274. // and add that to the results of the view
  275. $sql = "SELECT nid, organism_id FROM {chado_organism} WHERE organism_id IN (".implode(',',$unique_organism_ids).")";
  276. $resource = db_query($sql);
  277. while ($r = db_fetch_object($resource)) {
  278. $keys = array_keys($organism_ids, $r->organism_id);
  279. foreach ($keys as $k) {
  280. $view->result[$k]->organism_nid = $r->nid;
  281. }
  282. }
  283. } // if there are organisms
  284. } //end of case for organism NID
  285. //-----Stock-------------------------------------------------
  286. if (!empty($view->field['stock_nid'])) {
  287. // retrieve the stock_id for each record in the views current page
  288. $stock_ids = array();
  289. foreach ($view->result as $row_num => $row) {
  290. if (!empty($row->stock_id)) {
  291. //we're looking at stock id field already in table
  292. $stock_ids[$row_num] = $row->stock_id;
  293. } else {
  294. //we're looking at stock id added by field handler
  295. $stock_ids[$row_num] = $row->stock_stock_id;
  296. }
  297. }
  298. $unique_stock_ids = array_filter($stock_ids);
  299. $unique_stock_ids = array_unique($unique_stock_ids);
  300. if (!empty($unique_stock_ids)) {
  301. // Using the list of stock_ids from the view
  302. // lookup the NIDs from drupal
  303. // and add that to the results of the view
  304. $sql = "SELECT nid, stock_id FROM {chado_stock} WHERE stock_id IN (".implode(',',$unique_stock_ids).")";
  305. $resource = db_query($sql);
  306. while ($r = db_fetch_object($resource)) {
  307. $keys = array_keys($stock_ids, $r->stock_id);
  308. foreach ($keys as $k) {
  309. $view->result[$k]->stock_nid = $r->nid;
  310. }
  311. }
  312. } //if there are stocks
  313. } //end of case for stock NID
  314. return $view;
  315. }