tripal_contact.theme.inc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_legacy_contact
  6. */
  7. function tripal_contact_preprocess_tripal_contact_relationships(&$variables) {
  8. $contact = $variables['node']->contact;
  9. // expand the contact object to include the contact relationships.
  10. $options = [
  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 contacts (including contact type)
  14. 'include_fk' => [
  15. 'type_id' => 1,
  16. 'object_id' => [
  17. 'type_id' => 1,
  18. ],
  19. 'subject_id' => [
  20. 'type_id' => 1,
  21. ],
  22. ],
  23. ];
  24. $contact = chado_expand_var($contact, 'table', 'contact_relationship', $options);
  25. // get the subject relationships
  26. $srelationships = $contact->contact_relationship->subject_id;
  27. $orelationships = $contact->contact_relationship->object_id;
  28. // combine both object and subject relationshisp into a single array
  29. $relationships = [];
  30. $relationships['object'] = [];
  31. $relationships['subject'] = [];
  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_contact} WHERE contact_id = :contact_id";
  42. $n = db_query($sql, [':contact_id' => $relationship->subject_id->contact_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] = [];
  48. }
  49. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  50. $relationships['object'][$rel_type][$child_type] = [];
  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_contact} WHERE contact_id = :contact_id";
  64. $n = db_query($sql, [':contact_id' => $relationship->object_id->contact_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] = [];
  70. }
  71. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  72. $relationships['subject'][$rel_type][$parent_type] = [];
  73. }
  74. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  75. }
  76. }
  77. $contact->all_relationships = $relationships;
  78. }