tripal_core.views.inc 7.7 KB

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