tripal_core.views.inc 7.1 KB

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