tripal_stock.views.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions for views integration of
  5. * chado/tripal stock tables. Supplementary functions can be found in
  6. * ./views/
  7. *
  8. * Documentation on views integration can be found at
  9. * http://views2.logrus.com/doc/html/index.html.
  10. */
  11. /*************************************************************************
  12. * Implements hook_views_data(): Describe chado/tripal tables & fields to views
  13. *
  14. * @return array
  15. * A data array which follows the structure outlined in the
  16. * views2 documentation for this hook. Essentially, it's an array of table
  17. * definitions keyed by chado/tripal table name. Each table definition
  18. * includes basic details about the table, fields in that table and
  19. * relationships between that table and others (joins)
  20. */
  21. require_once('views/stock.views.inc');
  22. require_once('views/chado_stock.views.inc');
  23. require_once('views/misc_tables.views.inc');
  24. function tripal_stock_views_data() {
  25. $data = array();
  26. $data = array_merge($data, retrieve_stock_views_data());
  27. $data = array_merge($data, retrieve_chado_stock_views_data());
  28. $data = array_merge($data, retrieve_stock_misc_tables_views_data());
  29. return $data;
  30. }
  31. /*************************************************************************
  32. * Implements hook_views_handlers()
  33. *
  34. * Purpose: Register all custom handlers with views
  35. * where a handler describes either "the type of field",
  36. * "how a field should be filtered", "how a field should be sorted"
  37. *
  38. * @return array
  39. * An array of handler definitions
  40. */
  41. function tripal_stock_views_handlers() {
  42. return array(
  43. 'info' => array(
  44. 'path' => drupal_get_path('module', 'tripal_stock') . '/views/handlers',
  45. ),
  46. 'handlers' => array(
  47. 'views_handler_field_computed_stock_nid' => array(
  48. 'parent' => 'views_handler_field_numeric',
  49. ),
  50. 'views_handler_field_stockprop_by_type' => array(
  51. 'parent' => 'views_handler_field_prerender_list',
  52. ),
  53. 'views_handler_field_stockprop_all' => array(
  54. 'parent' => 'views_handler_field_prerender_list',
  55. ),
  56. 'views_handler_field_stockrel_by_type' => array(
  57. 'parent' => 'views_handler_field_prerender_list',
  58. ),
  59. 'views_handler_field_stockrel_all' => array(
  60. 'parent' => 'views_handler_field_prerender_list',
  61. ),
  62. 'views_handler_field_stock_dbxref_by_type' => array(
  63. 'parent' => 'views_handler_field_prerender_list',
  64. ),
  65. 'views_handler_field_stock_dbxref_all' => array(
  66. 'parent' => 'views_handler_field_prerender_list',
  67. ),
  68. 'views_handler_filter_stockprop_id' => array(
  69. 'parent' => 'views_handler_filter',
  70. ),
  71. 'views_handler_filter_stock_dbxref_id' => array(
  72. 'parent' => 'views_handler_filter',
  73. ),
  74. 'views_handler_argument_stockprop_id' => array(
  75. 'parent' => 'views_handler_argument_string',
  76. ),
  77. ),
  78. );
  79. }
  80. /*************************************************************************
  81. * Implements hook_views_pre_render
  82. *
  83. * Purpose: Intercepts the view after the query has been executed
  84. * All the results are stored in $view->result
  85. * Looking up the NID here ensures the query is only executed once
  86. * for all stocks in the table.
  87. *
  88. * @todo add if !<chado/drupal same db> around NID portion
  89. */
  90. function tripal_stock_views_pre_render (&$view) {
  91. if (preg_match('/stock/', $view->base_table)) {
  92. //-----Node IDs---------------------------------------------
  93. // @see file: tripal_core.views.inc function: tripal_core_add_node_ids_to_view (&$view);
  94. // retrieve the stock_id for each record in the views current page
  95. $stock_ids = array();
  96. foreach ($view->result as $row_num => $row) {
  97. $stock_ids[$row_num] = $row->stock_id;
  98. }
  99. if (sizeof($stock_ids)) {
  100. //-----Properties------------------------------------------
  101. $field_names = array_keys($view->field);
  102. //if any property fields are in the current view
  103. $property_field_names = preg_grep('/properties/',$field_names);
  104. if (!empty($property_field_names)) {
  105. $sql = "SELECT stockprop.*, cvterm.name as type_name FROM stockprop "
  106. ."INNER JOIN cvterm cvterm ON stockprop.type_id=cvterm.cvterm_id "
  107. ."WHERE stockprop.stock_id IN (".implode(',',$stock_ids).")";
  108. $previous_db = tripal_db_set_active('chado');
  109. $resource = db_query($sql);
  110. tripal_db_set_active($previous_db);
  111. $view->result[$key]->properties = array();
  112. while ($r = db_fetch_object($resource)) {
  113. $key = array_search($r->stock_id, $stock_ids);
  114. $view->result[$key]->properties[] = $r;
  115. }
  116. }
  117. //-----Relationships----------------------------------------
  118. //if any relationship fields are in the current view
  119. $relationship_field_names = preg_grep('/relationships/', $field_names);
  120. if (!empty($relationship_field_names)) {
  121. $sql = "SELECT stock_relationship.*, cvterm.name as type_name, "
  122. ."subject_stock.name as subject_name, object_stock.name as object_name "
  123. ."FROM stock_relationship "
  124. ."LEFT JOIN stock subject_stock ON stock_relationship.subject_id=subject_stock.stock_id "
  125. ."LEFT JOIN stock object_stock ON stock_relationship.object_id=object_stock.stock_id "
  126. ."LEFT JOIN cvterm cvterm ON stock_relationship.type_id = cvterm.cvterm_id "
  127. ."WHERE stock_relationship.subject_id IN (".implode(',',$stock_ids).") "
  128. ."OR stock_relationship.object_id IN (".implode(',',$stock_ids).") ";
  129. $previous_db = tripal_db_set_active('chado');
  130. $resource = db_query($sql);
  131. tripal_db_set_active($previous_db);
  132. while ($r = db_fetch_object($resource)) {
  133. if (in_array($r->subject_id, $stock_ids)) {
  134. $key = array_search($r->subject_id, $stock_ids);
  135. $r->stock_id = $r->subject_id;
  136. $view->result[$key]->relationships[] = clone $r;
  137. }
  138. if (in_array($r->object_id, $stock_ids)) {
  139. $key = array_search($r->object_id, $stock_ids);
  140. $r->stock_id = $r->object_id;
  141. $view->result[$key]->relationships[] = clone $r;
  142. }
  143. }
  144. }
  145. //-----DB References--------------------------------------------
  146. //if any dbxref fields are in the current view
  147. $dbxref_field_names = preg_grep('/dbxref/',$field_names);
  148. if (!empty($dbxref_field_names)) {
  149. $sql = "SELECT stock_dbxref.*, dbxref.db_id, db.name as db_name, db.urlprefix, "
  150. ."dbxref.accession, dbxref.version, dbxref.description "
  151. ."FROM stock_dbxref "
  152. ."LEFT JOIN dbxref dbxref ON stock_dbxref.dbxref_id=dbxref.dbxref_id "
  153. ."LEFT JOIN db db ON dbxref.db_id=db.db_id "
  154. ."WHERE stock_dbxref.stock_id IN (".implode(',',$stock_ids).")";
  155. $previous_db = tripal_db_set_active('chado');
  156. $resource = db_query($sql);
  157. tripal_db_set_active($previous_db);
  158. $view->result[$key]->dbxref = array();
  159. while ($r = db_fetch_object($resource)) {
  160. $key = array_search($r->stock_id, $stock_ids);
  161. $view->result[$key]->dbxref[] = $r;
  162. }
  163. }
  164. } //if there are stocks
  165. } //if we're dealing with a stock view
  166. }