tripal_stock_relationships.tpl.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 stock relationships for this node, the following function call would be made:
  5. *
  6. * $stock = chado_expand_var($stock,'table','stock_relationship');
  7. *
  8. * However, this function call can be extremely slow when there are numerous relationships.
  9. * This is because the chado_expand_var 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. * $stock->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 stock is the object, and the array with
  17. * the key 'subject' contains relationships where the stock is the subject
  18. */
  19. $stock = $variables['node']->stock;
  20. $all_relationships = $stock->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_stock-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 $stock->type_id->name; ?>is <?php print $rel_type ?>
  29. the following <b><?php print $obj_type ?></b> stock(s): <?php
  30. // the $headers array is an array of fields to use as the colum headers.
  31. // additional documentation can be found here
  32. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  33. $headers = ['Stock Name', 'Unique Name', 'Species', 'Type'];
  34. // the $rows array contains an array of rows where each row is an array
  35. // of values for each column of the table in that row. Additional documentation
  36. // can be found here:
  37. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  38. $rows = [];
  39. foreach ($objects as $object) {
  40. // link the stock to it's node
  41. $stock_name = $object->record->object_id->name;
  42. if (property_exists($object->record, 'nid')) {
  43. $stock_name = l($stock_name, "node/" . $object->record->nid, ['attributes' => ['target' => "_blank"]]);
  44. }
  45. // link the organism to it's node
  46. $organism = $object->record->object_id->organism_id;
  47. $organism_name = $organism->genus . " " . $organism->species;
  48. if (property_exists($organism, 'nid')) {
  49. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, ['html' => TRUE]);
  50. }
  51. $rows[] = [
  52. ['data' => $stock_name, 'width' => '30%'],
  53. [
  54. 'data' => $object->record->object_id->uniquename,
  55. 'width' => '30%',
  56. ],
  57. ['data' => $organism_name, 'width' => '30%'],
  58. [
  59. 'data' => $object->record->object_id->type_id->name,
  60. 'width' => '10%',
  61. ],
  62. ];
  63. }
  64. // the $table array contains the headers and rows array as well as other
  65. // options for controlling the display of the table. Additional
  66. // documentation can be found here:
  67. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  68. $table = [
  69. 'header' => $headers,
  70. 'rows' => $rows,
  71. 'attributes' => [
  72. 'id' => 'tripal_stock-table-relationship-object',
  73. 'class' => 'tripal-data-table',
  74. ],
  75. 'sticky' => FALSE,
  76. 'caption' => '',
  77. 'colgroups' => [],
  78. 'empty' => '',
  79. ];
  80. // once we have our table array structure defined, we call Drupal's theme_table()
  81. // function to generate the table.
  82. print theme_table($table); ?>
  83. </p>
  84. <br><?php
  85. }
  86. }
  87. // second add in the object relationships.
  88. foreach ($object_rels as $rel_type => $rels) {
  89. foreach ($rels as $subject_type => $subjects) {
  90. ?>
  91. <p>The following
  92. <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b>
  93. stock(s) are <?php print $rel_type ?>
  94. this <?php print $stock->type_id->name; ?>: <?php
  95. // the $headers array is an array of fields to use as the colum headers.
  96. // additional documentation can be found here
  97. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  98. $headers = ['Stock Name', 'Unique Name', 'Species', 'Type'];
  99. // the $rows array contains an array of rows where each row is an array
  100. // of values for each column of the table in that row. Additional documentation
  101. // can be found here:
  102. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  103. $rows = [];
  104. foreach ($subjects as $subject) {
  105. // link the stock to it's node
  106. $stock_name = $subject->record->subject_id->name;
  107. if (property_exists($subject->record, 'nid')) {
  108. $stock_name = l($stock_name, "node/" . $subject->record->nid, ['attributes' => ['target' => "_blank"]]);
  109. }
  110. // link the organism to it's node
  111. $organism = $subject->record->subject_id->organism_id;
  112. $organism_name = $organism->genus . " " . $organism->species;
  113. if (property_exists($organism, 'nid')) {
  114. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, ['html' => TRUE]);
  115. }
  116. $rows[] = [
  117. ['data' => $stock_name, 'width' => '30%'],
  118. [
  119. 'data' => $subject->record->subject_id->uniquename,
  120. 'width' => '30%',
  121. ],
  122. ['data' => $organism_name, 'width' => '30%'],
  123. [
  124. 'data' => $subject->record->subject_id->type_id->name,
  125. 'width' => '10%',
  126. ],
  127. ];
  128. }
  129. // the $table array contains the headers and rows array as well as other
  130. // options for controlling the display of the table. Additional
  131. // documentation can be found here:
  132. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  133. $table = [
  134. 'header' => $headers,
  135. 'rows' => $rows,
  136. 'attributes' => [
  137. 'id' => 'tripal_stock-table-relationship-subject',
  138. 'class' => 'tripal-data-table',
  139. ],
  140. 'sticky' => FALSE,
  141. 'caption' => '',
  142. 'colgroups' => [],
  143. 'empty' => '',
  144. ];
  145. // once we have our table array structure defined, we call Drupal's theme_table()
  146. // function to generate the table.
  147. print theme_table($table); ?>
  148. </p>
  149. <br><?php
  150. }
  151. }
  152. }