tripal_core.views.inc 8.2 KB

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