tripal_stock.views.inc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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()
  13. * Purpose: Describe chado/tripal tables & fields to views
  14. *
  15. * @return: 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. function tripal_stock_views_data() {
  23. $data = array();
  24. $data = array_merge($data, retrieve_stock_views_data());
  25. return $data;
  26. }
  27. /*************************************************************************
  28. * Implements hook_views_handlers()
  29. * Purpose: Register all custom handlers with views
  30. * where a handler describes either "the type of field",
  31. * "how a field should be filtered", "how a field should be sorted"
  32. *
  33. * @return: An array of handler definitions
  34. */
  35. function tripal_stock_views_handlers() {
  36. return array(
  37. 'info' => array(
  38. 'path' => drupal_get_path('module', 'tripal_stock') . '/views/handlers',
  39. ),
  40. 'handlers' => array(
  41. 'views_handler_field_computed_stock_nid' => array(
  42. 'parent' => 'views_handler_field_numeric',
  43. ),
  44. 'views_handler_field_stockprop_by_type' => array(
  45. 'parent' => 'views_handler_field_prerender_list',
  46. ),
  47. 'views_handler_field_stockprop_all' => array(
  48. 'parent' => 'views_handler_field_prerender_list',
  49. ),
  50. 'views_handler_field_stockrel_by_type' => array(
  51. 'parent' => 'views_handler_field_prerender_list',
  52. ),
  53. 'views_handler_field_stockrel_all' => array(
  54. 'parent' => 'views_handler_field_prerender_list',
  55. ),
  56. 'views_handler_field_stock_dbxref_by_type' => array(
  57. 'parent' => 'views_handler_field_prerender_list',
  58. ),
  59. 'views_handler_field_stock_dbxref_all' => array(
  60. 'parent' => 'views_handler_field_prerender_list',
  61. ),
  62. 'views_handler_filter_stockprop_id' => array(
  63. 'parent' => 'views_handler_filter',
  64. ),
  65. 'views_handler_argument_stockprop_id' => array(
  66. 'parent' => 'views_handler_argument_string',
  67. ),
  68. ),
  69. );
  70. }
  71. /*************************************************************************
  72. * Implements hook_views_pre_render
  73. * Purpose: Intercepts the view after the query has been executed
  74. * All the results are stored in $view->result
  75. * Looking up the NID here ensures the query is only executed once
  76. * for all stocks in the table.
  77. *
  78. * @todo add if !<chado/drupal same db> around NID portion
  79. */
  80. function tripal_stock_views_pre_render (&$view) {
  81. if (preg_match('/stock/', $view->base_table)) {
  82. //-----Node IDs---------------------------------------------
  83. // @see file: tripal_core.views.inc function: tripal_core_add_node_ids_to_view (&$view);
  84. // retrieve the stock_id for each record in the views current page
  85. $stock_ids = array();
  86. foreach ($view->result as $row_num => $row) {
  87. $stock_ids[$row_num] = $row->stock_id;
  88. }
  89. if (sizeof($stock_ids)) {
  90. //-----Properties------------------------------------------
  91. $field_names = array_keys($view->field);
  92. //if any property fields are in the current view
  93. $property_field_names = preg_grep('/properties/',$field_names);
  94. if (!empty($property_field_names)) {
  95. $sql = "SELECT stockprop.*, cvterm.name as type_name FROM stockprop "
  96. ."INNER JOIN cvterm cvterm ON stockprop.type_id=cvterm.cvterm_id "
  97. ."WHERE stockprop.stock_id IN (".implode(',',$stock_ids).")";
  98. $previous_db = db_set_active('chado');
  99. $resource = db_query($sql);
  100. db_set_active($previous_db);
  101. $view->result[$key]->properties = array();
  102. while ($r = db_fetch_object($resource)) {
  103. $key = array_search($r->stock_id, $stock_ids);
  104. $view->result[$key]->properties[] = $r;
  105. }
  106. }
  107. //-----Relationships----------------------------------------
  108. //if any relationship fields are in the current view
  109. $relationship_field_names = preg_grep('/relationships/', $field_names);
  110. if (!empty($relationship_field_names)) {
  111. $sql = "SELECT stock_relationship.*, cvterm.name as type_name, "
  112. ."subject_stock.name as subject_name, object_stock.name as object_name "
  113. ."FROM stock_relationship "
  114. ."LEFT JOIN stock subject_stock ON stock_relationship.subject_id=subject_stock.stock_id "
  115. ."LEFT JOIN stock object_stock ON stock_relationship.object_id=object_stock.stock_id "
  116. ."LEFT JOIN cvterm cvterm ON stock_relationship.type_id = cvterm.cvterm_id "
  117. ."WHERE stock_relationship.subject_id IN (".implode(',',$stock_ids).") "
  118. ."OR stock_relationship.object_id IN (".implode(',',$stock_ids).") ";
  119. $previous_db = db_set_active('chado');
  120. $resource = db_query($sql);
  121. db_set_active($previous_db);
  122. while ($r = db_fetch_object($resource)) {
  123. if (in_array($r->subject_id, $stock_ids)) {
  124. $key = array_search($r->subject_id, $stock_ids);
  125. $r->stock_id = $r->subject_id;
  126. $view->result[$key]->relationships[] = clone $r;
  127. }
  128. if (in_array($r->object_id, $stock_ids)) {
  129. $key = array_search($r->object_id, $stock_ids);
  130. $r->stock_id = $r->object_id;
  131. $view->result[$key]->relationships[] = clone $r;
  132. }
  133. }
  134. }
  135. //-----DB References--------------------------------------------
  136. //if any dbxref fields are in the current view
  137. $dbxref_field_names = preg_grep('/dbxref/',$field_names);
  138. if (!empty($dbxref_field_names)) {
  139. $sql = "SELECT stock_dbxref.*, dbxref.db_id, db.name as db_name, db.urlprefix, "
  140. ."dbxref.accession, dbxref.version, dbxref.description "
  141. ."FROM stock_dbxref "
  142. ."LEFT JOIN dbxref dbxref ON stock_dbxref.dbxref_id=dbxref.dbxref_id "
  143. ."LEFT JOIN db db ON dbxref.db_id=db.db_id "
  144. ."WHERE stock_dbxref.stock_id IN (".implode(',',$stock_ids).")";
  145. $previous_db = db_set_active('chado');
  146. $resource = db_query($sql);
  147. db_set_active($previous_db);
  148. $view->result[$key]->dbxref = array();
  149. while ($r = db_fetch_object($resource)) {
  150. $key = array_search($r->stock_id, $stock_ids);
  151. $view->result[$key]->dbxref[] = $r;
  152. }
  153. }
  154. } //if there are stocks
  155. } //if we're dealing with a stock view
  156. }