public function sio__references::load

3.x sio__references.inc public sio__references::load($entity)

Overrides TripalField::load

See also

TripalField::load()

File

tripal_chado/includes/TripalFields/sio__references/sio__references.inc, line 78

Class

sio__references

Code

public function load($entity) {

  $field_name = $this->field['field_name'];
  $field_type = $this->field['type'];
  $field_table = $this->instance['settings']['chado_table'];
  $field_column = $this->instance['settings']['chado_column'];
  $base_table = $this->instance['settings']['base_table'];

  // Set some defaults for the empty record.
  $chado_record = $entity->chado_record;
  $entity->{$field_name}['und'][0] = array(
    'value' => '',
  );

  // Iterate through all of the _pub tables and look for any that have
  // linked to this record. If so then add them.
  $chado_tables = chado_get_table_names(TRUE);
  $delta = 0;
  foreach ($chado_tables as $chado_table) {
    $matches = array();
    if (preg_match('/^(.+?)_pub$/', $chado_table, $matches)) {
      $reference_table = $matches[1];
      // Find the base table this links to and get the fk columns that map it.
      $schema = chado_get_schema($chado_table);
      $fkeys = $schema['foreign keys'];
      foreach ($fkeys as $linked_table => $fk_details) {
        if ($linked_table == $reference_table) {
          $fkleft = array_keys($fk_details['columns'])[0];
          $fkright = $fk_details['columns'][$fkleft];
        }
      }
      // Iterate through all of the records in the linker table that
      // match the given pub ID.
      $records = chado_generate_var($chado_table, array('pub_id' => $chado_record->pub_id), array('return_array' => TRUE));
      foreach ($records as $record) {
        // We want to add a 'type' and 'name' element to the values (at a
        // minimum) for each of the records.  Unfortunately, every base table
        // is different and their may not be an easy to identify name,
        // so... we'll do the best we can.
        $entity->{$field_name}['und'][$delta]['value'] = array();

        // First get the type of record.
        if (property_exists($record->$fkleft, 'type_id') and $record->$fkleft->type_id) {
          $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $record->$fkleft->type_id->name;
        }
        else {
          // If there's not a type_id column then see if the table is mapped
          // to a type.
          $mapping = db_select('chado_cvterm_mapping', 'CVM')
            ->fields('CVM')
            ->condition('chado_table', $reference_table)
            ->execute()
            ->fetchObject();
          if ($mapping) {
            $cvterm = chado_get_cvterm(array('cvterm_id' => $mapping->cvterm_id));
            $entity->{$field_name}['und'][$delta]['value']['rdfs:type'] = $cvterm->name;
          }
        }

        // Add in the name and uniquename (identifier) if those fields exist.
        if (property_exists($record->$fkleft, 'name')) {
          $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $record->$fkleft->name;
        }
        if (property_exists($record->$fkleft, 'uniquename')) {
          $entity->{$field_name}['und'][$delta]['value']['data:0842'] = $record->$fkleft->name;
        }

        // If this records is also a published entity then include that.
        if (property_exists($record->$fkleft, 'entity_id')) {
          $entity->{$field_name}['und'][$delta]['value']['entity'] = 'TripalEntity:' . $record->$fkleft->entity_id;
        }

        // If this is the organism table then we will create the name
        // specially.
        if (property_exists($record->$fkleft, 'genus')) {
          $name = '<i>' . $record->$fkleft->genus . ' ' . $record->$fkleft->species . '</i>';
          if (property_exists($record->$fkleft, 'infraspecific_name')) {
            if ($record->$fkleft->type_id) {
              $name .= ' ' . $record->$fkleft->type_id->name;
            }
            $name .= ' ' . $record->$fkleft->infraspecific_name;
          }
          $entity->{$field_name}['und'][$delta]['value']['schema:name'] = $name;
        }
        $delta++;
      }
    }
  }
}