tripal_stock_relationships.tpl.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 ?> the following <b><?php print $obj_type ?></b> stock(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('Stock 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 stock to it's node
  40. $stock_name = $object->record->object_id->name;
  41. if (property_exists($object->record, 'nid')) {
  42. $stock_name = l($stock_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' => $stock_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_stock-table-relationship-object',
  66. 'class' => 'tripal-data-table'
  67. ),
  68. 'sticky' => FALSE,
  69. 'caption' => '',
  70. 'colgroups' => array(),
  71. 'empty' => '',
  72. );
  73. // once we have our table array structure defined, we call Drupal's theme_table()
  74. // function to generate the table.
  75. print theme_table($table); ?>
  76. </p>
  77. <br><?php
  78. }
  79. }
  80. // second add in the object relationships.
  81. foreach ($object_rels as $rel_type => $rels){
  82. foreach ($rels as $subject_type => $subjects){?>
  83. <p>The following <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b> stock(s) are <?php print $rel_type ?> this <?php print $stock->type_id->name;?>: <?php
  84. // the $headers array is an array of fields to use as the colum headers.
  85. // additional documentation can be found here
  86. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  87. $headers = array('Stock Name' ,'Unique Name', 'Species', 'Type');
  88. // the $rows array contains an array of rows where each row is an array
  89. // of values for each column of the table in that row. Additional documentation
  90. // can be found here:
  91. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  92. $rows = array();
  93. foreach ($subjects as $subject){
  94. // link the stock to it's node
  95. $stock_name = $subject->record->subject_id->name;
  96. if (property_exists($subject->record, 'nid')) {
  97. $stock_name = l($stock_name, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  98. }
  99. // link the organism to it's node
  100. $organism = $subject->record->subject_id->organism_id;
  101. $organism_name = $organism->genus ." " . $organism->species;
  102. if (property_exists($organism, 'nid')) {
  103. $organism_name = l("<i>" . $organism->genus . " " . $organism->species . "</i>", "node/" . $organism->nid, array('html' => TRUE));
  104. }
  105. $rows[] = array(
  106. array('data' => $stock_name, 'width' => '30%'),
  107. array('data' => $subject->record->subject_id->uniquename, 'width' => '30%'),
  108. array('data' => $organism_name, 'width' => '30%'),
  109. array('data' =>$subject->record->subject_id->type_id->name, 'width' => '10%'),
  110. );
  111. }
  112. // the $table array contains the headers and rows array as well as other
  113. // options for controlling the display of the table. Additional
  114. // documentation can be found here:
  115. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  116. $table = array(
  117. 'header' => $headers,
  118. 'rows' => $rows,
  119. 'attributes' => array(
  120. 'id' => 'tripal_stock-table-relationship-subject',
  121. 'class' => 'tripal-data-table'
  122. ),
  123. 'sticky' => FALSE,
  124. 'caption' => '',
  125. 'colgroups' => array(),
  126. 'empty' => '',
  127. );
  128. // once we have our table array structure defined, we call Drupal's theme_table()
  129. // function to generate the table.
  130. print theme_table($table); ?>
  131. </p>
  132. <br><?php
  133. }
  134. }
  135. }