tripal_core.views.inc 8.8 KB

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