tripal_core.views.inc 12 KB

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