tripal_core.views.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // Using the list of analysis_ids from the view
  43. // lookup the NIDs from drupal
  44. // and add that to the results of the view
  45. $sql = "SELECT nid, analysis_id FROM {chado_analysis} WHERE analysis_id IN (".implode(',',$unique_analysis_ids).")";
  46. $resource = db_query($sql);
  47. while ($r = db_fetch_object($resource)) {
  48. $keys = array_keys($analysis_ids, $r->analysis_id);
  49. foreach ($keys as $k) {
  50. $view->result[$k]->analysis_nid = $r->nid;
  51. }
  52. }
  53. } //end of case for analysis NID
  54. //-----Feature-----------------------------------------------
  55. if (!empty($view->field['feature_nid'])) {
  56. // retrieve the feature_id for each record in the views current page
  57. $feature_ids = array();
  58. foreach ($view->result as $row_num => $row) {
  59. if (!empty($row->feature_id)) {
  60. //we're looking at feature id field already in table
  61. $feature_ids[$row_num] = $row->feature_id;
  62. } else {
  63. //we're looking at feature id added by field handler
  64. $feature_ids[$row_num] = $row->feature_feature_id;
  65. }
  66. }
  67. $unique_feature_ids = array_filter($feature_ids);
  68. $unique_feature_ids = array_unique($unique_feature_ids);
  69. // Using the list of feature_ids from the view
  70. // lookup the NIDs from drupal
  71. // and add that to the results of the view
  72. $sql = "SELECT nid, feature_id FROM {chado_feature} WHERE feature_id IN (".implode(',',$unique_feature_ids).")";
  73. $resource = db_query($sql);
  74. while ($r = db_fetch_object($resource)) {
  75. $keys = array_keys($feature_ids, $r->feature_id);
  76. foreach ($keys as $k) {
  77. $view->result[$k]->feature_nid = $r->nid;
  78. }
  79. }
  80. } //end of case for feature NID
  81. //-----Library-----------------------------------------------
  82. if (!empty($view->field['library_nid'])) {
  83. // retrieve the library_id for each record in the views current page
  84. $library_ids = array();
  85. foreach ($view->result as $row_num => $row) {
  86. if (!empty($row->library_id)) {
  87. //we're looking at library id field already in table
  88. $library_ids[$row_num] = $row->library_id;
  89. } else {
  90. //we're looking at library id added by field handler
  91. $library_ids[$row_num] = $row->library_library_id;
  92. }
  93. }
  94. $unique_library_ids = array_filter($library_ids);
  95. $unique_library_ids = array_unique($unique_library_ids);
  96. // Using the list of library_ids from the view
  97. // lookup the NIDs from drupal
  98. // and add that to the results of the view
  99. $sql = "SELECT nid, library_id FROM {chado_library} WHERE library_id IN (".implode(',',$unique_library_ids).")";
  100. $resource = db_query($sql);
  101. while ($r = db_fetch_object($resource)) {
  102. $keys = array_keys($library_ids, $r->library_id);
  103. foreach ($keys as $k) {
  104. $view->result[$k]->library_nid = $r->nid;
  105. }
  106. }
  107. } //end of case for library NID
  108. //-----Organism----------------------------------------------
  109. if (!empty($view->field['organism_nid'])) {
  110. // retrieve the organism_id for each record in the views current page
  111. $organism_ids = array();
  112. foreach ($view->result as $row_num => $row) {
  113. if (!empty($row->organism_id)) {
  114. //we're looking at organism id field already in table
  115. $organism_ids[$row_num] = $row->organism_id;
  116. } else {
  117. //we're looking at organism id added by field handler
  118. $organism_ids[$row_num] = $row->organism_organism_id;
  119. }
  120. }
  121. $unique_organism_ids = array_filter($organism_ids);
  122. $unique_organism_ids = array_unique($unique_organism_ids);
  123. // Using the list of organism_ids from the view
  124. // lookup the NIDs from drupal
  125. // and add that to the results of the view
  126. $sql = "SELECT nid, organism_id FROM {chado_organism} WHERE organism_id IN (".implode(',',$unique_organism_ids).")";
  127. $resource = db_query($sql);
  128. while ($r = db_fetch_object($resource)) {
  129. $keys = array_keys($organism_ids, $r->organism_id);
  130. foreach ($keys as $k) {
  131. $view->result[$k]->organism_nid = $r->nid;
  132. }
  133. }
  134. } //end of case for organism NID
  135. //-----Stock-------------------------------------------------
  136. if (!empty($view->field['stock_nid'])) {
  137. // retrieve the stock_id for each record in the views current page
  138. $stock_ids = array();
  139. foreach ($view->result as $row_num => $row) {
  140. if (!empty($row->stock_id)) {
  141. //we're looking at stock id field already in table
  142. $stock_ids[$row_num] = $row->stock_id;
  143. } else {
  144. //we're looking at stock id added by field handler
  145. $stock_ids[$row_num] = $row->stock_stock_id;
  146. }
  147. }
  148. $unique_stock_ids = array_filter($stock_ids);
  149. $unique_stock_ids = array_unique($unique_stock_ids);
  150. // Using the list of stock_ids from the view
  151. // lookup the NIDs from drupal
  152. // and add that to the results of the view
  153. $sql = "SELECT nid, stock_id FROM {chado_stock} WHERE stock_id IN (".implode(',',$unique_stock_ids).")";
  154. $resource = db_query($sql);
  155. while ($r = db_fetch_object($resource)) {
  156. $keys = array_keys($stock_ids, $r->stock_id);
  157. foreach ($keys as $k) {
  158. $view->result[$k]->stock_nid = $r->nid;
  159. }
  160. }
  161. } //end of case for stock NID
  162. return $view;
  163. }