tripal_pub.theme.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_pub
  6. */
  7. function tripal_pub_preprocess_tripal_pub_relationships(&$variables) {
  8. // we want to provide a new variable that contains the matched pubs.
  9. $pub = $variables['node']->pub;
  10. // normally we would use tripal_core_expand_chado_vars to expand our
  11. // organism object and add in the relationships, however whan a large
  12. // number of relationships are present this significantly slows the
  13. // query, therefore we will manually perform the query
  14. $sql = "
  15. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  16. FROM {pub_relationship} PR
  17. INNER JOIN {pub} P ON PR.object_id = P.pub_id
  18. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  19. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  20. WHERE PR.subject_id = :subject_id
  21. ";
  22. $as_subject = chado_query($sql, array(':subject_id' => $pub->pub_id));
  23. $sql = "
  24. SELECT P.title, P.pub_id, CP.nid, CVT.name as rel_type
  25. FROM {pub_relationship} PR
  26. INNER JOIN {pub} P ON PR.subject_id = P.pub_id
  27. INNER JOIN {cvterm} CVT ON PR.type_id = CVT.cvterm_id
  28. LEFT JOIN public.chado_pub CP ON P.pub_id = CP.pub_id
  29. WHERE PR.object_id = :object_id
  30. ";
  31. $as_object = chado_query($sql, array(':object_id' => $pub->pub_id));
  32. // combine both object and subject relationshisp into a single array
  33. $relationships = array();
  34. $relationships['object'] = array();
  35. $relationships['subject'] = array();
  36. // iterate through the object relationships
  37. while ($relationship = $as_object->fetchObject()) {
  38. // get the relationship and child types
  39. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  40. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  41. if (!array_key_exists($rel_type, $relationships['object'])) {
  42. $relationships['object'][$rel_type] = array();
  43. }
  44. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  45. $relationships['object'][$rel_type][$sub_type] = array();
  46. }
  47. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  48. }
  49. // now add in the subject relationships
  50. while ($relationship = $as_subject->fetchObject()) {
  51. // get the relationship and child types
  52. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  53. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  54. if (!array_key_exists($rel_type, $relationships['subject'])) {
  55. $relationships['subject'][$rel_type] = array();
  56. }
  57. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  58. $relationships['subject'][$rel_type][$obj_type] = array();
  59. }
  60. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  61. }
  62. $pub->all_relationships = $relationships;
  63. }