tripal_example.theme.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * This file should contain all Drupal hooks for theming content. For templates
  6. * that need specific the hook_preprocess functions should be included here
  7. *
  8. */
  9. /**
  10. * implementation of hook_preprocess_HOOK()
  11. *
  12. * Used to alter or add to theme variables. The variables are passed into
  13. * templates when prossing. This function organizes the relationships
  14. * into more simple structures for parsing in the template file.
  15. *
  16. * @ingroup tripal_example
  17. */
  18. function tripal_example_preprocess_tripal_example_relationships(&$variables) {
  19. // EXPLANATION: If you have implmented a new chado node type and the record
  20. // that belongs to the node has a corresponding xxxx_relationship table
  21. // this this function can be used to provide relationships to the template
  22. // in a format that is easier to parse. This is one example where specific SQL
  23. // statements can improve performance over Tripal API calls. SQL is not
  24. // recommended inside of template files, but rather the Tripal API calls only.
  25. // Therefore, this function queries the relationships and then organizes them
  26. // into arrays that are easier and faster to parse. You should be able to
  27. // copy the content of this function
  28. // and adjust as necessary to change table names if your record has relationships.
  29. $example = $variables['node']->example;
  30. // expand the example object to include the example relationships.
  31. $options = array(
  32. 'return_array' => 1,
  33. // we don't want to fully recurse we only need information about the
  34. // relationship type and the object and subject examples (including example type)
  35. 'include_fk' => array(
  36. 'type_id' => 1,
  37. 'object_id' => array(
  38. 'type_id' => 1,
  39. ),
  40. 'subject_id' => array(
  41. 'type_id' => 1,
  42. ),
  43. ),
  44. );
  45. $example = chado_expand_var($example, 'table', 'example_relationship', $options);
  46. // get the subject relationships
  47. $srelationships = $example->example_relationship->subject_id;
  48. $orelationships = $example->example_relationship->object_id;
  49. // combine both object and subject relationshisp into a single array
  50. $relationships = array();
  51. $relationships['object'] = array();
  52. $relationships['subject'] = array();
  53. // iterate through the object relationships
  54. if ($orelationships) {
  55. foreach ($orelationships as $relationship) {
  56. $rel = new stdClass();
  57. $rel->record = $relationship;
  58. // get the relationship and child types
  59. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  60. $child_type = $relationship->subject_id->type_id->name;
  61. // get the node id of the subject
  62. $sql = "SELECT nid FROM {chado_example} WHERE example_id = :example_id";
  63. $n = db_query($sql, array(':example_id' => $relationship->subject_id->example_id))->fetchObject();
  64. if ($n) {
  65. $rel->record->nid = $n->nid;
  66. }
  67. if (!array_key_exists($rel_type, $relationships['object'])) {
  68. $relationships['object'][$rel_type] = array();
  69. }
  70. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  71. $relationships['object'][$rel_type][$child_type] = array();
  72. }
  73. $relationships['object'][$rel_type][$child_type][] = $rel;
  74. }
  75. }
  76. // now add in the subject relationships
  77. if ($srelationships) {
  78. foreach ($srelationships as $relationship) {
  79. $rel = new stdClass();
  80. $rel->record = $relationship;
  81. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  82. $parent_type = $relationship->object_id->type_id->name;
  83. // get the node id of the subject
  84. $sql = "SELECT nid FROM {chado_example} WHERE example_id = :example_id";
  85. $n = db_query($sql, array(':example_id' => $relationship->object_id->example_id))->fetchObject();
  86. if ($n) {
  87. $rel->record->nid = $n->nid;
  88. }
  89. if (!array_key_exists($rel_type, $relationships['subject'])) {
  90. $relationships['subject'][$rel_type] = array();
  91. }
  92. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  93. $relationships['subject'][$rel_type][$parent_type] = array();
  94. }
  95. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  96. }
  97. }
  98. $example->all_relationships = $relationships;
  99. }