tripal_feature_relationships.tpl.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* Typically in a Tripal template, the data needed is retrieved using a call to
  3. * tripal_core_expand_chado_vars function. For example, to retrieve all
  4. * of the feature relationships for this node, the following function call would be made:
  5. *
  6. * $feature = tripal_core_expand_chado_vars($feature,'table','feature_relationship');
  7. *
  8. * However, this function call can be extremely slow when there are numerous relationships.
  9. * This is because the tripal_core_expand_chado_vars function is recursive and expands
  10. * all data following the foreign key relationships tree. Therefore, to speed retrieval
  11. * of data, a special variable is provided to this template:
  12. *
  13. * $feature->all_relationships;
  14. *
  15. * This variable is an array with two sub arrays with the keys 'object' and 'subject'. The array with
  16. * key 'object' contains relationships where the feature is the object, and the array with
  17. * the key 'subject' contains relationships where the feature is the subject
  18. */
  19. $feature = $variables['node']->feature;
  20. $all_relationships = $feature->all_relationships;
  21. $object_rels = $all_relationships['object'];
  22. $subject_rels = $all_relationships['subject'];
  23. if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
  24. <div class="tripal_feature-data-block-desc tripal-data-block-desc"></div> <?php
  25. // first add in the subject relationships.
  26. foreach ($subject_rels as $rel_type => $rels){
  27. foreach ($rels as $obj_type => $objects){ ?>
  28. <p>This <?php print $feature->type_id->name;?> is <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> feature(s): <?php
  29. // the $headers array is an array of fields to use as the colum headers.
  30. // additional documentation can be found here
  31. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  32. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  33. // the $rows array contains an array of rows where each row is an array
  34. // of values for each column of the table in that row. Additional documentation
  35. // can be found here:
  36. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  37. $rows = array();
  38. foreach ($objects as $object){
  39. // link the feature to it's node
  40. $feature_name = $object->record->object_id->name;
  41. if (property_exists($object->record, 'nid')) {
  42. $feature_name = l($feature_name, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
  43. }
  44. // link the organism to it's node
  45. $organism = $object->record->object_id->organism_id;
  46. $organism_name = $organism->genus ." " . $organism->species;
  47. if (property_exists($organism, 'nid')) {
  48. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  49. }
  50. $rows[] = array(
  51. array('data' => $feature_name, 'width' => '30%'),
  52. array('data' => $object->record->object_id->uniquename, 'width' => '30%'),
  53. array('data' => $organism_name, 'width' => '30%'),
  54. array('data' => $object->record->object_id->type_id->name, 'width' => '10%'),
  55. );
  56. }
  57. // the $table array contains the headers and rows array as well as other
  58. // options for controlling the display of the table. Additional
  59. // documentation can be found here:
  60. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  61. $table = array(
  62. 'header' => $headers,
  63. 'rows' => $rows,
  64. 'attributes' => array(
  65. 'id' => 'tripal_feature-table-relationship-object',
  66. ),
  67. 'sticky' => FALSE,
  68. 'caption' => '',
  69. 'colgroups' => array(),
  70. 'empty' => '',
  71. );
  72. // once we have our table array structure defined, we call Drupal's theme_table()
  73. // function to generate the table.
  74. print theme_table($table); ?>
  75. </p>
  76. <br><?php
  77. }
  78. }
  79. // second add in the object relationships.
  80. foreach ($object_rels as $rel_type => $rels){
  81. foreach ($rels as $subject_type => $subjects){?>
  82. <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> feature(s) are <?php print $rel_type ?> this <?php print $feature->type_id->name;?>: <?php
  83. // the $headers array is an array of fields to use as the colum headers.
  84. // additional documentation can be found here
  85. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  86. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  87. // the $rows array contains an array of rows where each row is an array
  88. // of values for each column of the table in that row. Additional documentation
  89. // can be found here:
  90. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  91. $rows = array();
  92. foreach ($subjects as $subject){
  93. // link the feature to it's node
  94. $feature_name = $subject->record->subject_id->name;
  95. if (property_exists($subject->record, 'nid')) {
  96. $feature_name = l($feature_name, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  97. }
  98. // link the organism to it's node
  99. $organism = $subject->record->subject_id->organism_id;
  100. $organism_name = $organism->genus ." " . $organism->species;
  101. if (property_exists($organism, 'nid')) {
  102. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  103. }
  104. $rows[] = array(
  105. array('data' => $feature_name, 'width' => '30%'),
  106. array('data' =>$subject->record->subject_id->uniquename, 'width' => '30%'),
  107. array('data' =>$organism_name, 'width' => '30%'),
  108. array('data' =>$subject->record->subject_id->type_id->name, 'width' => '10%'),
  109. );
  110. }
  111. // the $table array contains the headers and rows array as well as other
  112. // options for controlling the display of the table. Additional
  113. // documentation can be found here:
  114. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  115. $table = array(
  116. 'header' => $headers,
  117. 'rows' => $rows,
  118. 'attributes' => array(
  119. 'id' => 'tripal_feature-table-relationship-subject',
  120. ),
  121. 'sticky' => FALSE,
  122. 'caption' => '',
  123. 'colgroups' => array(),
  124. 'empty' => '',
  125. );
  126. // once we have our table array structure defined, we call Drupal's theme_table()
  127. // function to generate the table.
  128. print theme_table($table); ?>
  129. </p>
  130. <br><?php
  131. }
  132. }
  133. }