tripal_pub.theme.inc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_legacy_pub
  6. */
  7. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  8. $pub = $variables['node']->pub;
  9. // expand the pub object to include the pub relationships.
  10. $options = array(
  11. 'return_array' => 1,
  12. // we don't want to fully recurse we only need information about the
  13. // relationship type and the object and subject pubs (including pub type)
  14. 'include_fk' => array(
  15. 'type_id' => 1,
  16. 'object_id' => array(
  17. 'type_id' => 1,
  18. ),
  19. 'subject_id' => array(
  20. 'type_id' => 1,
  21. ),
  22. ),
  23. );
  24. $pub = chado_expand_var($pub, 'table', 'pub_relationship', $options);
  25. // get the subject relationships
  26. $srelationships = $pub->pub_relationship->subject_id;
  27. $orelationships = $pub->pub_relationship->object_id;
  28. // combine both object and subject relationshisp into a single array
  29. $relationships = array();
  30. $relationships['object'] = array();
  31. $relationships['subject'] = array();
  32. // iterate through the object relationships
  33. if ($orelationships) {
  34. foreach ($orelationships as $relationship) {
  35. $rel = new stdClass();
  36. $rel->record = $relationship;
  37. // get the relationship and child types
  38. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  39. $child_type = $relationship->subject_id->type_id->name;
  40. // get the node id of the subject
  41. $sql = "SELECT nid FROM {chado_pub} WHERE pub_id = :pub_id";
  42. $n = db_query($sql, array(':pub_id' => $relationship->subject_id->pub_id))->fetchObject();
  43. if ($n) {
  44. $rel->record->nid = $n->nid;
  45. }
  46. if (!array_key_exists($rel_type, $relationships['object'])) {
  47. $relationships['object'][$rel_type] = array();
  48. }
  49. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  50. $relationships['object'][$rel_type][$child_type] = array();
  51. }
  52. $relationships['object'][$rel_type][$child_type][] = $rel;
  53. }
  54. }
  55. // now add in the subject relationships
  56. if ($srelationships) {
  57. foreach ($srelationships as $relationship) {
  58. $rel = new stdClass();
  59. $rel->record = $relationship;
  60. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  61. $parent_type = $relationship->object_id->type_id->name;
  62. // get the node id of the subject
  63. $sql = "SELECT nid FROM {chado_pub} WHERE pub_id = :pub_id";
  64. $n = db_query($sql, array(':pub_id' => $relationship->object_id->pub_id))->fetchObject();
  65. if ($n) {
  66. $rel->record->nid = $n->nid;
  67. }
  68. if (!array_key_exists($rel_type, $relationships['subject'])) {
  69. $relationships['subject'][$rel_type] = array();
  70. }
  71. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  72. $relationships['subject'][$rel_type][$parent_type] = array();
  73. }
  74. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  75. }
  76. }
  77. $pub->all_relationships = $relationships;
  78. }