tripal_feature_relationships.tpl.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* Typically in a Tripal template, the data needed is retrieved using a call to
  3. * chado_expand_var function. For example, to retrieve all
  4. * of the feature relationships for this node, the following function call would
  5. * be made:
  6. *
  7. * $feature = chado_expand_var($feature,'table','feature_relationship');
  8. *
  9. * However, this function call can be extremely slow when there are numerous
  10. * relationships. This is because the chado_expand_var function is recursive
  11. * and expands all data following the foreign key relationships tree.
  12. * Therefore, to speed retrieval of data, a special variable is provided to
  13. * this template:
  14. *
  15. * $feature->all_relationships;
  16. *
  17. * This variable is an array with two sub arrays with the keys 'object' and
  18. * 'subject'. The array with key 'object' contains relationships where the
  19. * feature is the object, and the array with the key 'subject' contains
  20. * relationships where the feature is the subject
  21. */
  22. $feature = $variables['node']->feature;
  23. $all_relationships = $feature->all_relationships;
  24. $object_rels = $all_relationships['object'];
  25. $subject_rels = $all_relationships['subject'];
  26. if (count($object_rels) > 0 or count($subject_rels) > 0) { ?>
  27. <div class="tripal_feature-data-block-desc tripal-data-block-desc"></div> <?php
  28. // first add in the subject relationships.
  29. foreach ($subject_rels as $rel_type => $rels){
  30. foreach ($rels as $obj_type => $objects){
  31. // Make the verb in the sentence make sense in English.
  32. switch ($rel_type) {
  33. case 'integral part of':
  34. case 'instance of':
  35. $verb = 'is an';
  36. break;
  37. case 'proper part of':
  38. case 'transformation of':
  39. case 'genome of':
  40. case 'part of':
  41. case 'position of':
  42. case 'sequence of':
  43. case 'variant of':
  44. $verb = 'is a';
  45. break;
  46. case 'derives from':
  47. case 'connects on':
  48. case 'contains':
  49. case 'finishes':
  50. case 'guides':
  51. case 'has origin':
  52. case 'has part':
  53. case 'has quality':
  54. case 'is consecutive sequence of':
  55. case 'maximally overlaps':
  56. case 'overlaps':
  57. case 'starts':
  58. $verb = '';
  59. break;
  60. default:
  61. $verb = 'is';
  62. } ?>
  63. <p>This <?php print $feature->type_id->name;?> <?php print $verb ?> <?php print $rel_type ?> the following <b><?php print $obj_type ?></b> feature(s): <?php
  64. // the $headers array is an array of fields to use as the colum headers.
  65. // additional documentation can be found here
  66. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  67. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  68. // the $rows array contains an array of rows where each row is an array
  69. // of values for each column of the table in that row. Additional documentation
  70. // can be found here:
  71. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  72. $rows = array();
  73. foreach ($objects as $object){
  74. // link the feature to it's node
  75. $feature_name = $object->record->object_id->name;
  76. if (property_exists($object->record, 'nid')) {
  77. $feature_name = l($feature_name, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
  78. }
  79. // link the organism to it's node
  80. $organism = $object->record->object_id->organism_id;
  81. $organism_name = $organism->genus ." " . $organism->species;
  82. if (property_exists($organism, 'nid')) {
  83. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  84. }
  85. $rows[] = array(
  86. array('data' => $feature_name, 'width' => '30%'),
  87. array('data' => $object->record->object_id->uniquename, 'width' => '30%'),
  88. array('data' => $organism_name, 'width' => '30%'),
  89. array('data' => $object->record->object_id->type_id->name, 'width' => '10%'),
  90. );
  91. }
  92. // the $table array contains the headers and rows array as well as other
  93. // options for controlling the display of the table. Additional
  94. // documentation can be found here:
  95. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  96. $table = array(
  97. 'header' => $headers,
  98. 'rows' => $rows,
  99. 'attributes' => array(
  100. 'id' => 'tripal_feature-table-relationship-object',
  101. 'class' => 'tripal-data-table'
  102. ),
  103. 'sticky' => FALSE,
  104. 'caption' => '',
  105. 'colgroups' => array(),
  106. 'empty' => '',
  107. );
  108. // once we have our table array structure defined, we call Drupal's theme_table()
  109. // function to generate the table.
  110. print theme_table($table); ?>
  111. </p>
  112. <br><?php
  113. }
  114. }
  115. // second add in the object relationships.
  116. foreach ($object_rels as $rel_type => $rels){
  117. foreach ($rels as $subject_type => $subjects){
  118. // Make the verb in the sentence make sense in English.
  119. switch ($rel_type) {
  120. case 'integral part of':
  121. case 'instance of':
  122. $verb = 'are an';
  123. break;
  124. case 'proper part of':
  125. case 'transformation of':
  126. case 'genome of':
  127. case 'part of':
  128. case 'position of':
  129. case 'sequence of':
  130. case 'variant of':
  131. $verb = 'are a';
  132. break;
  133. case 'derives from':
  134. case 'connects on':
  135. case 'contains':
  136. case 'finishes':
  137. case 'guides':
  138. case 'has origin':
  139. case 'has part':
  140. case 'has quality':
  141. case 'is consecutive sequence of':
  142. case 'maximally overlaps':
  143. case 'overlaps':
  144. case 'starts':
  145. $verb = '';
  146. break;
  147. default:
  148. $verb = 'are';
  149. } ?>
  150. <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> feature(s) <?php print $verb ?> <?php print $rel_type ?> this <?php print $feature->type_id->name;?>: <?php
  151. // the $headers array is an array of fields to use as the colum headers.
  152. // additional documentation can be found here
  153. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  154. $headers = array('Feature Name' ,'Unique Name', 'Species', 'Type');
  155. // the $rows array contains an array of rows where each row is an array
  156. // of values for each column of the table in that row. Additional documentation
  157. // can be found here:
  158. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  159. $rows = array();
  160. foreach ($subjects as $subject){
  161. // link the feature to it's node
  162. $feature_name = $subject->record->subject_id->name;
  163. if (property_exists($subject->record, 'nid')) {
  164. $feature_name = l($feature_name, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  165. }
  166. // link the organism to it's node
  167. $organism = $subject->record->subject_id->organism_id;
  168. $organism_name = $organism->genus ." " . $organism->species;
  169. if (property_exists($organism, 'nid')) {
  170. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  171. }
  172. $rows[] = array(
  173. array('data' => $feature_name, 'width' => '30%'),
  174. array('data' =>$subject->record->subject_id->uniquename, 'width' => '30%'),
  175. array('data' =>$organism_name, 'width' => '30%'),
  176. array('data' =>$subject->record->subject_id->type_id->name, 'width' => '10%'),
  177. );
  178. }
  179. // the $table array contains the headers and rows array as well as other
  180. // options for controlling the display of the table. Additional
  181. // documentation can be found here:
  182. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  183. $table = array(
  184. 'header' => $headers,
  185. 'rows' => $rows,
  186. 'attributes' => array(
  187. 'id' => 'tripal_feature-table-relationship-subject',
  188. 'class' => 'tripal-data-table'
  189. ),
  190. 'sticky' => FALSE,
  191. 'caption' => '',
  192. 'colgroups' => array(),
  193. 'empty' => '',
  194. );
  195. // once we have our table array structure defined, we call Drupal's theme_table()
  196. // function to generate the table.
  197. print theme_table($table); ?>
  198. </p>
  199. <br><?php
  200. }
  201. }
  202. }