tripal_core.views.inc 7.9 KB

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