views_handler_field_chado_relationship_all.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Field handler for terms.
  5. */
  6. class views_handler_field_chado_relationship_all extends views_handler_field_prerender_list {
  7. function init(&$view, $options) {
  8. parent::init($view, $options);
  9. }
  10. function query() {
  11. // not sure why this is needed since nothing is added to the query...
  12. $this->add_additional_fields();
  13. }
  14. function pre_render($values) {
  15. $this->aliases['relationships'] = 'relationships';
  16. $this->aliases['primary_id'] = $this->view->base_table . '_id';
  17. $this->field_alias = $this->aliases['primary_id'];
  18. $relationship_tablename = $this->view->base_table . '_relationship';
  19. $this->aliases['relationship_primary_id'] = $relationship_tablename . '_id';
  20. $table_desc = module_invoke_all('chado_' . $relationship_tablename . '_schema');
  21. foreach ($table_desc['foreign keys'][$this->view->base_table]['columns'] as $k => $v) {
  22. if (preg_match('/subject/', $k)) {
  23. $subject_id = $k;
  24. }
  25. if (preg_match('/object/', $k)) {
  26. $object_id = $k;
  27. }
  28. }
  29. $this->aliases['subject_id'] = $subject_id;
  30. $this->aliases['object_id'] = $object_id;
  31. //Add values for all if possible
  32. if (!$this->view->result[0]->relationships) {
  33. //get base table primary keys
  34. $primary_ids = array();
  35. foreach ($this->view->result as $row_num => $row) {
  36. $primary_ids[$row_num] = $row->{$this->aliases['primary_id']};
  37. }
  38. //generate results from db
  39. $sql = "SELECT " . $relationship_tablename . ".*, cvterm.name as type_name, "
  40. ."subject_parent.name as subject_name, object_parent.name as object_name "
  41. ."FROM " . $relationship_tablename . " "
  42. ."LEFT JOIN " . $this->view->base_table . " subject_parent ON "
  43. . $relationship_tablename . "." . $subject_id . "=subject_parent." . $this->aliases['primary_id'] . " "
  44. ."LEFT JOIN " . $this->view->base_table . " object_parent ON "
  45. . $relationship_tablename . "." . $object_id . "=object_parent." . $this->aliases['primary_id'] . " "
  46. ."LEFT JOIN cvterm cvterm ON "
  47. . $relationship_tablename . ".type_id = cvterm.cvterm_id "
  48. ."WHERE " . $relationship_tablename . "." . $subject_id . " IN (" . implode(',', $primary_ids) . ") "
  49. ."OR " . $relationship_tablename . "." . $object_id . " IN (" . implode(',', $primary_ids) . ") ";
  50. $previous_db = tripal_db_set_active('chado');
  51. $resource = db_query($sql);
  52. tripal_db_set_active($previous_db);
  53. //add results to views results
  54. while ($r = db_fetch_object($resource)) {
  55. if (in_array($r->{$subject_id}, $primary_ids)) {
  56. $key = array_search($r->{$subject_id}, $primary_ids);
  57. $r->{$this->aliases['primary_id']} = $r->{$subject_id};
  58. $this->view->result[$key]->relationships[] = clone $r;
  59. }
  60. if (in_array($r->{$object_id}, $primary_ids)) {
  61. $key = array_search($r->{$object_id}, $primary_ids);
  62. $r->{$this->aliases['primary_id']} = $r->{$object_id};
  63. $this->view->result[$key]->relationships[] = clone $r;
  64. }
  65. } //end of while
  66. }// end of if add values
  67. //for each stock in this view page, add the values
  68. foreach ($values as $result) {
  69. if (!empty($result->{$this->aliases['relationships']})) {
  70. // all relationships including the current stock
  71. $relationships = $result->{$this->aliases['relationships']};
  72. foreach ($relationships as $relationship) {
  73. // Add relationship to the list of items to be rendered
  74. // Note: $this->aliases['primary_id'] = base table primary key
  75. $elements = array(
  76. $this->aliases['primary_id'] => $relationship->{$this->aliases['primary_id']},
  77. $this->aliases['relationship_primary_id'] => $relationship->{$this->aliases['relationship_primary_id']},
  78. 'subject_id' => $relationship->{$this->aliases['subject_id']},
  79. 'subject_name' => $relationship->subject_name,
  80. 'object_id' => $relationship->{$this->aliases['object_id']},
  81. 'object_name' => $relationship->object_name,
  82. 'type_id' => $relationship->type_id,
  83. 'type_name' => $relationship->type_name,
  84. );
  85. $this->items[$relationship->{$this->aliases['primary_id']}][$relationship->{$this->aliases['relationship_primary_id']}] = $elements;
  86. }
  87. }
  88. }
  89. }
  90. function render_item($count, $item) {
  91. return $item['subject_name'] . ' ' . $item['type_name'] . ' ' . $item['object_name'];
  92. }
  93. }