public function sbo__relationship::load

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

Overrides TripalField::load

See also

TripalField::load()

File

tripal_chado/includes/TripalFields/sbo__relationship/sbo__relationship.inc, line 361

Class

sbo__relationship

Code

public function load($entity) {
  $settings = $this->field['settings'];

  $record = $entity->chado_record;
  $bundle = tripal_load_bundle_entity(array('name' => $entity->bundle));

  $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'];

  // Get the PKey for this table
  $schema = chado_get_schema($field_table);
  $pkey = $schema['primary key'][0];
  $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];

  // Not all tables have the columns named 'subject_id' and 'object_id'.
  // some have variations on that name and we need to determine what they are.
  $fkeys = $schema['foreign keys'];
  $subject_id_key = 'subject_id';
  $object_id_key = 'object_id';
  foreach ($fkeys as $fktable => $details) {
    foreach ($details['columns'] as $fkey_lcolumn => $fkey_rcolumn) {
      if (preg_match('/^subject_.*id/', $fkey_lcolumn)) {
        $subject_id_key = $fkey_lcolumn;
      }
      if (preg_match('/^object_.*id/', $fkey_lcolumn)) {
        $object_id_key = $fkey_lcolumn;
      }
    }
  }

  // Set some defaults for the empty record.
  $entity->{$field_name}['und'][0] = array(
    'value' => '',
    'chado-' . $field_table . '__' . $pkey => '',
    'chado-' . $field_table . '__' . $subject_id_key => '',
    'chado-' . $field_table . '__' . $object_id_key => '',
    'chado-' . $field_table . '__type_id' => '',
    // These elements don't need to follow the naming scheme above
    // becasue we don't need the chado_field_storage to try and
    // save these values.
    'object_name' => '',
    'subject_name' => '',
    'type_name' => '',
  );

  // If the table has rank and value fields then add those to the default
  // value array.
  if (array_key_exists('value', $schema['fields'])) {
    $entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
  }
  if (array_key_exists('rank', $schema['fields'])) {
    $entity->{$field_name}['und'][0]['chado-' . $field_table . '__rank'] = '';
  }

  // If we have no record then just return.
  if (!$record) {
    return;
  }

  // Expand the object to include the relationships.
  $options = array(
    'return_array' => 1,
    // we don't want to fully recurse we only need information about the
    // relationship type and the object and subject
    'include_fk' => array(
      'type_id' => 1,
      $object_id_key => array(
        'type_id' => 1,
      ),
      $subject_id_key => array(
        'type_id' => 1,
      ),
    ),
  );
  $rel_table = $base_table . '_relationship';
  $schema = chado_get_schema($rel_table);
  if (array_key_exists('rank', $schema['fields'])) {
    $options['order_by'] = array('rank' => 'ASC');
  }
  $record = chado_expand_var($record, 'table', $rel_table, $options);
  if (!$record->$rel_table) {
    return;
  }

  // Load the subject relationships
  $i = 0;
  if (isset($record->$rel_table->$subject_id_key)) {
    $srelationships = $record->$rel_table->$subject_id_key;
    foreach ($srelationships as $relationship) {
      $this->loadRelationship($relationship, $entity, $i);
      $i++;
    }
  }

  // Load the object relationships
  if (isset($record->$rel_table->$object_id_key)) {
    $orelationships = $record->$rel_table->$object_id_key;
    foreach ($orelationships as $relationship) {
      $this->loadRelationship($relationship, $entity, $i);
      $i++;
    }
  }
}