tripal_core.views.inc 8.0 KB

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