tripal_project_relationships.tpl.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 project relationships for this node, the following function call would be made:
  5. *
  6. * $project = chado_expand_var($project,'table','project_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. * $project->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 project is the object, and the array with
  17. * the key 'subject' contains relationships where the project is the subject
  18. */
  19. $project = $variables['node']->project;
  20. $all_relationships = $project->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_project-data-block-desc tripal-data-block-desc">This
  25. project is related to the following other projects:
  26. </div> <?php
  27. // the $headers array is an array of fields to use as the colum headers.
  28. // additional documentation can be found here
  29. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  30. $headers = ['Relationship'];
  31. // the $rows array contains an array of rows where each row is an array
  32. // of values for each column of the table in that row. Additional documentation
  33. // can be found here:
  34. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  35. $rows = [];
  36. // first add in the subject relationships.
  37. foreach ($subject_rels as $rel_type => $objects) {
  38. foreach ($objects as $object) {
  39. // link the project to it's node
  40. $object_name = $object->record->object_project_id->name;
  41. if (property_exists($object->record, 'nid')) {
  42. $object_name = l($object_name, "node/" . $object->record->nid, ['attributes' => ['target' => "_blank"]]);
  43. }
  44. $rows[] = [
  45. "$project->name is a \"$rel_type\" of $object_name",
  46. ];
  47. }
  48. }
  49. // second add in the object relationships.
  50. foreach ($object_rels as $rel_type => $subjects) {
  51. foreach ($subjects as $subject) {
  52. // link the project to it's node
  53. $subject_name = $subject->record->subject_project_id->name;
  54. if (property_exists($subject->record, 'nid')) {
  55. $subject_name = l($subject_name, "node/" . $subject->record->nid, ['attributes' => ['target' => "_blank"]]);
  56. }
  57. $rows[] = [
  58. "$subject_name is a \"$rel_type\" of $project->name",
  59. ];
  60. }
  61. }
  62. // the $table array contains the headers and rows array as well as other
  63. // options for controlling the display of the table. Additional
  64. // documentation can be found here:
  65. // https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_table/7
  66. $table = [
  67. 'header' => $headers,
  68. 'rows' => $rows,
  69. 'attributes' => [
  70. 'id' => 'tripal_project-table-relationship-subject',
  71. 'class' => 'tripal-data-table',
  72. ],
  73. 'sticky' => FALSE,
  74. 'caption' => '',
  75. 'colgroups' => [],
  76. 'empty' => '',
  77. ];
  78. // once we have our table array structure defined, we call Drupal's theme_table()
  79. // function to generate the table.
  80. print theme_table($table);
  81. }