tripal_core.views.inc 7.8 KB

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