tripal_stock.theme.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions related to theme-ing including preprocess hooks
  5. */
  6. /**
  7. * Implements hook_preprocess_tripal_stock_relationships() which is the
  8. * preprocess hook for the tripal_stock_relationships template
  9. *
  10. * @ingroup tripal_legacy_stock
  11. */
  12. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  13. // we want to provide a new variable that contains the matched stocks.
  14. $stock = $variables['node']->stock;
  15. // expand the stock object to include the stock relationships.
  16. $options = [
  17. 'return_array' => 1,
  18. 'order_by' => ['rank' => 'ASC'],
  19. // we don't want to fully recurse we only need information about the
  20. // relationship type and the object and subject stocks (including stock type
  21. // and organism)
  22. 'include_fk' => [
  23. 'type_id' => 1,
  24. 'object_id' => [
  25. 'type_id' => 1,
  26. 'organism_id' => 1,
  27. ],
  28. 'subject_id' => [
  29. 'type_id' => 1,
  30. 'organism_id' => 1,
  31. ],
  32. ],
  33. ];
  34. $stock = chado_expand_var($stock, 'table', 'stock_relationship', $options);
  35. // get the subject relationships
  36. $srelationships = $stock->stock_relationship->subject_id;
  37. $orelationships = $stock->stock_relationship->object_id;
  38. // combine both object and subject relationshisp into a single array
  39. $relationships = [];
  40. $relationships['object'] = [];
  41. $relationships['subject'] = [];
  42. // iterate through the object relationships
  43. if ($orelationships) {
  44. foreach ($orelationships as $relationship) {
  45. $rel = new stdClass();
  46. $rel->record = $relationship;
  47. // get the relationship and child types
  48. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  49. $child_type = $relationship->subject_id->type_id->name;
  50. // get the node id of the subject
  51. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
  52. $n = db_query($sql, [':stock_id' => $relationship->subject_id->stock_id])->fetchObject();
  53. if ($n) {
  54. $rel->record->nid = $n->nid;
  55. }
  56. if (!array_key_exists($rel_type, $relationships['object'])) {
  57. $relationships['object'][$rel_type] = [];
  58. }
  59. if (!array_key_exists($child_type, $relationships['object'][$rel_type])) {
  60. $relationships['object'][$rel_type][$child_type] = [];
  61. }
  62. $relationships['object'][$rel_type][$child_type][] = $rel;
  63. }
  64. }
  65. // now add in the subject relationships
  66. if ($srelationships) {
  67. foreach ($srelationships as $relationship) {
  68. $rel = new stdClass();
  69. $rel->record = $relationship;
  70. $rel_type = t(preg_replace('/_/', " ", $relationship->type_id->name));
  71. $parent_type = $relationship->object_id->type_id->name;
  72. // get the node id of the subject
  73. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id = :stock_id";
  74. $n = db_query($sql, [':stock_id' => $relationship->object_id->stock_id])->fetchObject();
  75. if ($n) {
  76. $rel->record->nid = $n->nid;
  77. }
  78. if (!array_key_exists($rel_type, $relationships['subject'])) {
  79. $relationships['subject'][$rel_type] = [];
  80. }
  81. if (!array_key_exists($parent_type, $relationships['subject'][$rel_type])) {
  82. $relationships['subject'][$rel_type][$parent_type] = [];
  83. }
  84. $relationships['subject'][$rel_type][$parent_type][] = $rel;
  85. }
  86. }
  87. $stock->all_relationships = $relationships;
  88. }