tripal_contact_relationships.tpl.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 contact relationships for this node, the following function call would be made:
  5. *
  6. * $contact = chado_expand_var($contact,'table','contact_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. * $contact->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 contact is the object, and the array with
  17. * the key 'subject' contains relationships where the contact is the subject
  18. */
  19. $contact = $variables['node']->contact;
  20. $all_relationships = $contact->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_contact-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 strtolower($contact->type_id->name); ?>
  29. <b><?php print $rel_type ?></b> with the
  30. following <?php print strtolower($obj_type) ?> contact(s): <?php
  31. // the $headers array is an array of fields to use as the colum headers.
  32. // additional documentation can be found here
  33. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  34. $headers = ['Name'];
  35. // the $rows array contains an array of rows where each row is an array
  36. // of values for each column of the table in that row. Additional documentation
  37. // can be found here:
  38. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  39. $rows = [];
  40. foreach ($objects as $object) {
  41. // link the contact to it's node
  42. $contact_name = $object->record->object_id->name;
  43. if (property_exists($object->record, 'nid')) {
  44. $contact_name = "<a href=\"" . url("node/" . $object->record->nid) . "\" target=\"_blank\">" . $object->record->object_id->name . "</a>";
  45. }
  46. $rows[] = [
  47. $contact_name,
  48. ];
  49. }
  50. // the $table array contains the headers and rows array as well as other
  51. // options for controlling the display of the table. Additional
  52. // documentation can be found here:
  53. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  54. $table = [
  55. 'header' => $headers,
  56. 'rows' => $rows,
  57. 'attributes' => [
  58. 'id' => 'tripal_contact-table-relationship-object',
  59. 'class' => 'tripal-data-table',
  60. ],
  61. 'sticky' => FALSE,
  62. 'caption' => '',
  63. 'colgroups' => [],
  64. 'empty' => '',
  65. ];
  66. // once we have our table array structure defined, we call Drupal's theme_table()
  67. // function to generate the table.
  68. print theme_table($table); ?>
  69. </p>
  70. <br><?php
  71. }
  72. }
  73. // second add in the object relationships.
  74. foreach ($object_rels as $rel_type => $rels) {
  75. foreach ($rels as $subject_type => $subjects) {
  76. ?>
  77. <p>The following
  78. <b><?php print $subjects[0]->record->subject_id->type_id->name ?></b>
  79. contact(s) are <?php print $rel_type ?>
  80. this <?php print $contact->type_id->name; ?>: <?php
  81. // the $headers array is an array of fields to use as the colum headers.
  82. // additional documentation can be found here
  83. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  84. $headers = ['Name'];
  85. // the $rows array contains an array of rows where each row is an array
  86. // of values for each column of the table in that row. Additional documentation
  87. // can be found here:
  88. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  89. $rows = [];
  90. foreach ($subjects as $subject) {
  91. // link the contact to it's node
  92. $contact_name = $subject->record->subject_id->name;
  93. if (property_exists($subject->record, 'nid')) {
  94. $contact_name = "<a href=\"" . url("node/" . $subject->record->nid) . "\" target=\"_blank\">" . $subject->record->subject_id->name . "</a>";
  95. }
  96. $rows[] = [
  97. $contact_name,
  98. ];
  99. }
  100. // the $table array contains the headers and rows array as well as other
  101. // options for controlling the display of the table. Additional
  102. // documentation can be found here:
  103. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  104. $table = [
  105. 'header' => $headers,
  106. 'rows' => $rows,
  107. 'attributes' => [
  108. 'id' => 'tripal_contact-table-relationship-subject',
  109. 'class' => 'tripal-data-table',
  110. ],
  111. 'sticky' => FALSE,
  112. 'caption' => '',
  113. 'colgroups' => [],
  114. 'empty' => '',
  115. ];
  116. // once we have our table array structure defined, we call Drupal's theme_table()
  117. // function to generate the table.
  118. print theme_table($table); ?>
  119. </p>
  120. <br><?php
  121. }
  122. }
  123. }