TripalEntity.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * A class the controller will use for instantiating the TripalEntity entity.
  4. */
  5. class TripalEntity extends Entity {
  6. public function __construct($values = array(), $entity_type) {
  7. parent::__construct($values, $entity_type);
  8. // If we have an ID then this entity has been saved and it will
  9. // also therefore have a chado_entity record. We want to
  10. // load this record so it is always part of the entity object.
  11. if (property_exists($this, 'id') and $this->id) {
  12. // For Tripal created entities add in the chado_entity details.
  13. $dbs = tripal_entities_get_db_names_for_published_vocabularies();
  14. if (in_array($this->type, $dbs)) {
  15. $details = db_select('chado_entity', 'ce')
  16. ->fields('ce')
  17. ->condition('entity_id', $this->id)
  18. ->execute()
  19. ->fetchObject();
  20. // Add the chado entity details to the entity in case it's needed
  21. // downstream (e.g. in field widget construction).
  22. $this->chado_entity_id = $details->chado_entity_id;
  23. $this->chado_table = $details->data_table;
  24. $this->chado_field = $details->field;
  25. // Add in the record.
  26. $schema = chado_get_schema($this->chado_table);
  27. $pkey = $schema['primary key'][0];
  28. $this->chado_record_id = $details->record_id;
  29. $this->chado_record = chado_generate_var($this->chado_table, array($pkey =>+ $details->record_id));
  30. }
  31. }
  32. // If we do not have an ID then we need to do a few queries to get
  33. // information about the chado table this entity maps to.
  34. else {
  35. // Use the cvterm_id to look up the base table for this term. We find
  36. // the base table by looking in the term_usage table for the mapping.
  37. $sel_values = array(
  38. 'term_id' => array(
  39. 'cvterm_id' => $this->cvterm_id,
  40. ),
  41. );
  42. $term_usage = chado_generate_var('tripal_term_usage', $sel_values, array('return_array' => 1));
  43. // For each table that uses this term, insert the field recursively
  44. foreach ($term_usage as $usage) {
  45. $this->chado_table = $usage->data_table;
  46. $this->chado_field = $usage->field;
  47. }
  48. }
  49. }
  50. protected function defaultLabel() {
  51. return $this->title;
  52. }
  53. protected function defaultUri() {
  54. return array('path' => 'data/' . $this->id);
  55. }
  56. }