tripal_pub_relationships.tpl.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 pub relationships for this node, the following function call would be made:
  5. *
  6. * $pub = chado_expand_var($pub,'table','pub_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. * $pub->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 pub is the object, and the array with
  17. * the key 'subject' contains relationships where the pub is the subject
  18. */
  19. $pub = $variables['node']->pub;
  20. $all_relationships = $pub->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_pub-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 $pub->type_id->name; ?> is <?php print $rel_type ?>
  29. the following <b><?php print $obj_type ?></b> pub(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 = ['Publication'];
  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 pub to it's node
  41. $title = $object->record->object_id->title;
  42. if (property_exists($object->record, 'nid')) {
  43. $title = l($title, "node/" . $object->record->nid, ['attributes' => ['target' => "_blank"]]);
  44. }
  45. // get the citation
  46. $values = [
  47. 'pub_id' => $object->record->object_id->pub_id,
  48. 'type_id' => [
  49. 'name' => 'Citation',
  50. ],
  51. ];
  52. $citation = chado_generate_var('pubprop', $values);
  53. $citation = chado_expand_var($citation, 'field', 'pubprop.value');
  54. $rows[] = [
  55. $title . '<br>' . htmlspecialchars($citation->value),
  56. ];
  57. }
  58. // the $table array contains the headers and rows array as well as other
  59. // options for controlling the display of the table. Additional
  60. // documentation can be found here:
  61. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  62. $table = [
  63. 'header' => $headers,
  64. 'rows' => $rows,
  65. 'attributes' => [
  66. 'id' => 'tripal_pub-table-relationship-object',
  67. 'class' => 'tripal-data-table',
  68. ],
  69. 'sticky' => FALSE,
  70. 'caption' => '',
  71. 'colgroups' => [],
  72. 'empty' => '',
  73. ];
  74. // once we have our table array structure defined, we call Drupal's theme_table()
  75. // function to generate the table.
  76. print theme_table($table); ?>
  77. </p>
  78. <br><?php
  79. }
  80. }
  81. // second add in the object relationships.
  82. foreach ($object_rels as $rel_type => $rels) {
  83. foreach ($rels as $subject_type => $subjects) {
  84. ?>
  85. <p>The following
  86. <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b>
  87. pub(s) are <?php print $rel_type ?>
  88. this <?php print $pub->type_id->name; ?>: <?php
  89. // the $headers array is an array of fields to use as the colum headers.
  90. // additional documentation can be found here
  91. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  92. $headers = ['Publication'];
  93. // the $rows array contains an array of rows where each row is an array
  94. // of values for each column of the table in that row. Additional documentation
  95. // can be found here:
  96. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  97. $rows = [];
  98. foreach ($subjects as $subject) {
  99. // link the pub to it's node
  100. $title = $subject->record->subject_id->title;
  101. if (property_exists($subject->record, 'nid')) {
  102. $title = l($title, "node/" . $subject->record->nid, ['attributes' => ['target' => "_blank"]]);
  103. }
  104. // get the citation
  105. $values = [
  106. 'pub_id' => $subject->record->subject_id->pub_id,
  107. 'type_id' => [
  108. 'name' => 'Citation',
  109. ],
  110. ];
  111. $citation = chado_generate_var('pubprop', $values);
  112. $citation = chado_expand_var($citation, 'field', 'pubprop.value');
  113. $rows[] = [
  114. $title . '<br>' . htmlspecialchars($citation->value),
  115. ];
  116. }
  117. // the $table array contains the headers and rows array as well as other
  118. // options for controlling the display of the table. Additional
  119. // documentation can be found here:
  120. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  121. $table = [
  122. 'header' => $headers,
  123. 'rows' => $rows,
  124. 'attributes' => [
  125. 'id' => 'tripal_pub-table-relationship-subject',
  126. 'class' => 'tripal-data-table',
  127. ],
  128. 'sticky' => FALSE,
  129. 'caption' => '',
  130. 'colgroups' => [],
  131. 'empty' => '',
  132. ];
  133. // once we have our table array structure defined, we call Drupal's theme_table()
  134. // function to generate the table.
  135. print theme_table($table); ?>
  136. </p>
  137. <br><?php
  138. }
  139. }
  140. }