tripal_project_relationships.tpl.php 3.6 KB

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