tripal_pub_relationships.tpl.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 pub relationships for this node, the following function call would be made:
  5. *
  6. * $pub = tripal_core_expand_chado_vars($pub,'table','pub_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. * $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 ?> the following <b><?php print $obj_type ?></b> pub(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('Publication');
  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 pub to it's node
  40. $title = $object->record->object_id->title;
  41. if (property_exists($object->record, 'nid')) {
  42. $title = l($title, "node/" . $object->record->nid, array('attributes' => array('target' => "_blank")));
  43. }
  44. // get the citation
  45. $values = array(
  46. 'pub_id' => $object->record->object_id->pub_id,
  47. 'type_id' => array(
  48. 'name' => 'Citation',
  49. ),
  50. );
  51. $citation = tripal_core_generate_chado_var('pubprop', $values);
  52. $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value');
  53. $rows[] = array(
  54. $title . '<br>' . htmlspecialchars($citation->value),
  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_pub-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> pub(s) are <?php print $rel_type ?> this <?php print $pub->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('Publication');
  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 pub to it's node
  94. $title = $subject->record->subject_id->title;
  95. if (property_exists($subject->record, 'nid')) {
  96. $title = l($title, "node/" . $subject->record->nid, array('attributes' => array('target' => "_blank")));
  97. }
  98. // get the citation
  99. $values = array(
  100. 'pub_id' => $subject->record->subject_id->pub_id,
  101. 'type_id' => array(
  102. 'name' => 'Citation',
  103. ),
  104. );
  105. $citation = tripal_core_generate_chado_var('pubprop', $values);
  106. $citation = tripal_core_expand_chado_vars($citation, 'field', 'pubprop.value');
  107. $rows[] = array(
  108. $title . '<br>' . htmlspecialchars($citation->value),
  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_pub-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. }