tripal_project.relationships.tpl.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 project relationships for this node, the following function call would be made:
  5. *
  6. * $project = tripal_core_expand_chado_vars($project,'table','project_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. * $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 id="tripal_project-relationships-box" class="tripal_project-info-box tripal-info-box">
  25. <div class="tripal_project-info-box-title tripal-info-box-title">Relationships</div>
  26. <div class="tripal_project-info-box-desc tripal-info-box-desc">This project is related to the following other projects:</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 = array('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 = array();
  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, array('attributes' => array('target' => "_blank")));
  43. }
  44. $rows[] = array(
  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, array('attributes' => array('target' => "_blank")));
  56. }
  57. $rows[] = array(
  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 = array(
  67. 'header' => $headers,
  68. 'rows' => $rows,
  69. 'attributes' => array(
  70. 'id' => 'tripal_project-table-relationship-subject',
  71. ),
  72. 'sticky' => FALSE,
  73. 'caption' => '',
  74. 'colgroups' => array(),
  75. 'empty' => '',
  76. );
  77. // once we have our table array structure defined, we call Drupal's theme_table()
  78. // function to generate the table.
  79. print theme_table($table); ?>
  80. </div> <?php
  81. }