|
@@ -65,11 +65,33 @@ class sbo__relationship extends ChadoField {
|
|
|
// An array containing details about the field. The format of this array
|
|
|
// is the same as that returned by field_info_fields()
|
|
|
protected $field;
|
|
|
+
|
|
|
// An array containing details about an instance of the field. A field does
|
|
|
// not have to have an instance. But if dealing with an instance (such as
|
|
|
// when using the widgetForm, formatterSettingsForm, etc.) it should be set.
|
|
|
protected $instance;
|
|
|
|
|
|
+ // An array of columns to use as the "name" of the subject and object.
|
|
|
+ // For example, for the feature table, this will be the name,
|
|
|
+ // whereas, for the organism table this will be the genus & species.
|
|
|
+ protected $base_name_columns;
|
|
|
+
|
|
|
+ // One of 'type_id', or 'table_name'. Not all base tables have a type_id so
|
|
|
+ // this setting allows us to better handle these cases.
|
|
|
+ protected $base_type_column;
|
|
|
+
|
|
|
+ // This field depends heavily on the schema of the relationship and base
|
|
|
+ // table. The following variables cache the schema to greatly speed up
|
|
|
+ // this field.
|
|
|
+ // Note: both are ChadoSchema objects.
|
|
|
+ protected $schema;
|
|
|
+ protected $base_schema;
|
|
|
+
|
|
|
+ // The column which indicated the subject/object_id in the current
|
|
|
+ // relationship table. This allows us to support exceptions in the common
|
|
|
+ // chado naming conventions.
|
|
|
+ protected $subject_id_column;
|
|
|
+ protected $object_id_column;
|
|
|
|
|
|
/**
|
|
|
* @see TripalField::elements()
|
|
@@ -183,155 +205,238 @@ class sbo__relationship extends ChadoField {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- private function loadRelationship($relationship, &$entity, $delta) {
|
|
|
-
|
|
|
- $field_name = $this->field['field_name'];
|
|
|
- $field_table = $this->instance['settings']['chado_table'];
|
|
|
- $base_table = $this->instance['settings']['base_table'];
|
|
|
-
|
|
|
- $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
|
|
|
- $rel_type = $relationship->type_id->name;
|
|
|
- $verb = $this->get_rel_verb($rel_type);
|
|
|
+ /**
|
|
|
+ * Extends TripalField::__construct().
|
|
|
+ */
|
|
|
+ public function __construct($field, $instance) {
|
|
|
+ parent::__construct($field, $instance);
|
|
|
|
|
|
- // Get the foreign keys for the subject and object tables
|
|
|
- $subject_fkey_table = '';
|
|
|
- $object_fkey_table = '';
|
|
|
+ $reltable = $instance['settings']['chado_table'];
|
|
|
+ $base_table = $instance['settings']['base_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];
|
|
|
+ // First, initialize the schema's.
|
|
|
+ $this->schema = new ChadoSchema();
|
|
|
+ $this->schema = $this->schema->getTableSchema($reltable);
|
|
|
+ $this->base_schema = new ChadoSchema();
|
|
|
+ $this->base_schema = $this->base_schema->getTableSchema($base_table);
|
|
|
|
|
|
- // 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_fkey_table = $fktable;
|
|
|
- $subject_id_key = $fkey_lcolumn;
|
|
|
- }
|
|
|
- if (preg_match('/^object_.*id/', $fkey_lcolumn)) {
|
|
|
- $object_fkey_table = $fktable;
|
|
|
- $object_id_key = $fkey_lcolumn;
|
|
|
- }
|
|
|
+ // Determine the subject_id/object_id column names.
|
|
|
+ foreach ($this->schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
|
|
|
+ if (preg_match('/^subject_.*id/', $lcolum)) {
|
|
|
+ $this->subject_id_column = $lcolum;
|
|
|
+ }
|
|
|
+ else if (preg_match('/^object_.*id/', $lcolum)) {
|
|
|
+ $this->object_id_column = $lcolum;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Get the schemas for the subject and object table. These should
|
|
|
- // be the same as the base table but just to be safe we'll get them
|
|
|
- // separately.
|
|
|
- $subject_schema = chado_get_schema($subject_fkey_table);
|
|
|
- $subject_pkey = $subject_schema['primary key'][0];
|
|
|
- $object_schema = chado_get_schema($object_fkey_table);
|
|
|
- $object_pkey = $object_schema['primary key'][0];
|
|
|
-
|
|
|
- // Not all realtionshp tables have a name field (e.g. organism_relationship)
|
|
|
- // threfore in some cases we need to dig a bit deeper to get the entity
|
|
|
- // name and the entity type name.
|
|
|
- $subject_name = '';
|
|
|
- $subject_type = '';
|
|
|
- $object_name = '';
|
|
|
- $object_type = '';
|
|
|
-
|
|
|
- // The linked to table of a relationship linker table may not always
|
|
|
- // have a type_id or name field. So we have to be a bit more
|
|
|
- // specific about how we set some variables.
|
|
|
- switch ($relationship->tablename) {
|
|
|
+ // Determine the name and type columns.
|
|
|
+ $this->base_name_columns = [];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
+ switch ($instance['settings']['chado_table']) {
|
|
|
+
|
|
|
case 'acquisition_relationship':
|
|
|
- $subject_type = 'acquisition';
|
|
|
- $object_type = 'acquisition';
|
|
|
- break;
|
|
|
case 'analysis_relationship':
|
|
|
- $subject_type = 'analysis';
|
|
|
- $object_type = 'analysis';
|
|
|
- break;
|
|
|
case 'biomaterial_relationship':
|
|
|
- $subject_type = 'biomaterial';
|
|
|
- $object_type = 'biomaterial';
|
|
|
- break;
|
|
|
case 'cell_line_relationship':
|
|
|
- $subject_type = 'cell_line';
|
|
|
- $object_type = 'cell_line';
|
|
|
+ case 'quantification_relationship':
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
case 'element_relationship':
|
|
|
- $subject_name = $relationship->$subject_id_key->feature_id->name;
|
|
|
- $object_name = $relationship->$object_id_key->feature_id->name;
|
|
|
+ // RELATIONSHIP->subject_id_key->feature_id->name;
|
|
|
+ $this->base_name_columns = ['name'];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
case 'organism_relationship':
|
|
|
- $subject_name = $relationship->$subject_id_key->genus . ' ' . $relationship->$subject_id_key->species;
|
|
|
- $object_name = $relationship->$object_id_key->genus . ' ' . $relationship->$object_id_key->species;
|
|
|
- $subject_type = 'organism';
|
|
|
- $object_type = 'organism';
|
|
|
+ $this->base_name_columns = ['genus','species'];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
case 'project_relationship':
|
|
|
- $subject_type = 'project';
|
|
|
- $object_type = 'project';
|
|
|
+ $this->base_name_columns = ['name'];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
case 'phylonode_relationship':
|
|
|
- $subject_name = $relationship->$subject_id_key->label;
|
|
|
- $object_name = $relationship->$object_id_key->label;
|
|
|
+ $this->base_name_columns = ['label'];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
case 'pub_relationship':
|
|
|
- $subject_name = $relationship->$subject_id_key->uniquename;
|
|
|
- $object_name = $relationship->$object_id_key->uniquename;
|
|
|
+ $this->base_name_columns = ['name'];
|
|
|
+ $this->base_type_column = 'table_name';
|
|
|
break;
|
|
|
- case 'quantification_relationship':
|
|
|
- $subject_type = 'quantification';
|
|
|
- $object_type = 'quantification';
|
|
|
+ case 'contact':
|
|
|
+ $this->base_name_columns = ['name'];
|
|
|
+ $this->base_type_column = 'type_id';
|
|
|
break;
|
|
|
default:
|
|
|
- $subject_name = isset($relationship->$subject_id_key->name) ? $relationship->$subject_id_key->name : '';
|
|
|
- $subject_type = isset($relationship->$subject_id_key->type_id) ? $relationship->$subject_id_key->type_id->name : '';
|
|
|
- $object_name = isset($relationship->$object_id_key->name) ? $relationship->$object_id_key->name : '';
|
|
|
- $object_type = isset($relationship->$object_id_key->type_id) ? $relationship->$object_id_key->type_id->name : '';
|
|
|
+ // @todo update this to use the schema.
|
|
|
+ $this->base_name_columns = ['name'];
|
|
|
+ $this->base_type_column = 'type_id';
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- $entity->{$field_name}['und'][$delta]['value'] = array(
|
|
|
- 'local:relationship_subject' => array(
|
|
|
- 'rdfs:type' => $subject_type,
|
|
|
- 'schema:name' => $subject_name,
|
|
|
- ),
|
|
|
- 'local:relationship_type' => $relationship->type_id->name,
|
|
|
- 'local:relationship_object' => array(
|
|
|
- 'rdfs:type' => $object_type,
|
|
|
- 'schema:name' => $object_name,
|
|
|
- 'entity' => 'TripalEntity:' . $entity->id,
|
|
|
- )
|
|
|
- );
|
|
|
+ /**
|
|
|
+ * Retrive the subject from the current relationship.
|
|
|
+ *
|
|
|
+ * @param $relationship
|
|
|
+ * A single expanded relationship from a variable generated by chado_generate_var().
|
|
|
+ * At a minimum, if will have a subject, object and type which should be expanded to
|
|
|
+ * the appropriate type of record depending on the content type this widget
|
|
|
+ * is attached to.
|
|
|
+ * @return
|
|
|
+ * An array of information for the subject of the $relationship.
|
|
|
+ */
|
|
|
+ private function getRelationshipSubject($relationship) {
|
|
|
+ $name = [];
|
|
|
|
|
|
- // If the subject or object have a unqiuename then add that in for refernce.
|
|
|
- if (property_exists($relationship->$subject_id_key, 'uniquename')) {
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['data:0842'] = $relationship->$subject_id_key->uniquename;
|
|
|
+ foreach ($this->base_name_columns as $column) {
|
|
|
+ $name[] = $relationship->{$this->subject_id_column}->{$column};
|
|
|
}
|
|
|
- if (property_exists($relationship->$object_id_key, 'uniquename')) {
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['data:0842'] = $relationship->$object_id_key->uniquename;
|
|
|
+
|
|
|
+ // Retrieve the type.
|
|
|
+ $type = $this->instance['settings']['base_table'];
|
|
|
+ if ((!$this->base_type_column == 'table_name') AND isset($relationship->{$this->subject_id_column}->{$this->base_type_column})) {
|
|
|
+ $type_object = $relationship->{$this->subject_id_column}->{$this->base_type_column};
|
|
|
+ if (isset($type_object->name)) {
|
|
|
+ $type = $type_object->name;
|
|
|
+ }
|
|
|
+ elseif (isset($type_object->uniquename)) {
|
|
|
+ $type = $type_object->uniquename;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // If the subject or object have an organism then add that in for reference.
|
|
|
- if (property_exists($relationship->$subject_id_key, 'organism_id')) {
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['OBI:0100026'] = $relationship->$subject_id_key->organism_id->genus . ' ' . $relationship->$subject_id_key->organism_id->species;
|
|
|
+ $record = [
|
|
|
+ 'rdfs:type' => $type,
|
|
|
+ 'schema:name' => implode(' ', $name),
|
|
|
+ // @todo support the entity and determine whether this is current one or not.
|
|
|
+ //'entity' => 'TripalEntity:' . $entity->id,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // If the object has a uniquename then add that in for refernce.
|
|
|
+ if (property_exists($relationship->{$this->subject_id_column}, 'uniquename')) {
|
|
|
+ $record['data:0842'] = $relationship->{$this->subject_id_column}->uniquename;
|
|
|
}
|
|
|
- if (property_exists($relationship->$object_id_key, 'organism_id')) {
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['OBI:0100026'] = $relationship->$object_id_key->organism_id->genus . ' ' . $relationship->$object_id_key->organism_id->species;
|
|
|
+
|
|
|
+ // If the object has an organism then add that in for reference.
|
|
|
+ if (property_exists($relationship->{$this->subject_id_column}, 'organism_id')
|
|
|
+ AND is_object($relationship->{$this->subject_id_column}->organism_id)) {
|
|
|
+ $record['OBI:0100026'] = $relationship->{$this->subject_id_column}->organism_id->genus . ' ' . $relationship->{$this->subject_id_column}->organism_id->species;
|
|
|
}
|
|
|
|
|
|
- // Add in the TripalEntity ids if these base records in the relationship
|
|
|
- // are published.
|
|
|
- if (property_exists($relationship->$subject_id_key, 'entity_id')) {
|
|
|
- $entity_id = $relationship->$subject_id_key->entity_id;
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['entity'] = 'TripalEntity:' . $entity_id;
|
|
|
+ // Add in the TripalEntity ids if the object is published.
|
|
|
+ if (property_exists($relationship->{$this->subject_id_column}, 'entity_id')) {
|
|
|
+ $entity_id = $relationship->{$this->subject_id_column}->entity_id;
|
|
|
+ $record['entity'] = 'TripalEntity:' . $entity_id;
|
|
|
}
|
|
|
- if (property_exists($relationship->$object_id_key, 'entity_id')) {
|
|
|
- $entity_id = $relationship->$object_id_key->entity_id;
|
|
|
- $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['entity'] = 'TripalEntity:' . $entity_id;
|
|
|
+
|
|
|
+ return $record;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve the object from the current relationship.
|
|
|
+ *
|
|
|
+ * @param $relationship
|
|
|
+ * A single expanded relationship from a variable generated by chado_generate_var().
|
|
|
+ * At a minimum, if will have a subject, object and type which should be expanded to
|
|
|
+ * the appropriate type of record depending on the content type this widget
|
|
|
+ * is attached to.
|
|
|
+ * @return
|
|
|
+ * An array of information for the object of the $relationship.
|
|
|
+ */
|
|
|
+ private function getRelationshipObject($relationship) {
|
|
|
+ $name = [];
|
|
|
+
|
|
|
+ // Retrieve the name (may be multiple parts).
|
|
|
+ foreach ($this->base_name_columns as $column) {
|
|
|
+ $name[] = $relationship->{$this->object_id_column}->{$column};
|
|
|
}
|
|
|
|
|
|
+ // Retrieve the Type.
|
|
|
+ $type = $this->instance['settings']['base_table'];
|
|
|
+ if ((!$this->base_type_column == 'table_name') AND isset($relationship->{$this->object_id_column}->{$this->base_type_column})) {
|
|
|
+ $type_object = $relationship->{$this->object_id_column}->{$this->base_type_column};
|
|
|
+ if (isset($type_object->name)) {
|
|
|
+ $type = $type_object->name;
|
|
|
+ }
|
|
|
+ elseif (isset($type_object->uniquename)) {
|
|
|
+ $type = $type_object->uniquename;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $record = [
|
|
|
+ 'rdfs:type' => $type,
|
|
|
+ 'schema:name' => implode(' ', $name),
|
|
|
+ // @todo support the entity and determine whether this is current one or not.
|
|
|
+ //'entity' => 'TripalEntity:' . $entity->id,
|
|
|
+ ];
|
|
|
+
|
|
|
+ // If the object has a unqiuename then add that in for reference.
|
|
|
+ if (property_exists($relationship->{$this->object_id_column}, 'uniquename')) {
|
|
|
+ $record['data:0842'] = $relationship->{$this->object_id_column}->uniquename;
|
|
|
+ }
|
|
|
+
|
|
|
+ // If the object has an organism then add that in for reference.
|
|
|
+ if (property_exists($relationship->{$this->object_id_column}, 'organism_id')
|
|
|
+ AND is_object($relationship->{$this->object_id_column}->organism_id)) {
|
|
|
+ $record['OBI:0100026'] = $relationship->{$this->object_id_column}->organism_id->genus . ' ' . $relationship->{$this->object_id_column}->organism_id->species;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add in the TripalEntity ids if the object is published.
|
|
|
+ if (property_exists($relationship->{$this->object_id_column}, 'entity_id')) {
|
|
|
+ $entity_id = $relationship->{$this->object_id_column}->entity_id;
|
|
|
+ $record['entity'] = 'TripalEntity:' . $entity_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $record;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Load a specific relationship as indicated by $delta.
|
|
|
+ * This function is called by the load method below.
|
|
|
+ *
|
|
|
+ * Note: The relationship is loaded by adding it the the entitiy values.
|
|
|
+ *
|
|
|
+ * @param $relationship
|
|
|
+ * A single expanded relationship from a variable generated by chado_generate_var().
|
|
|
+ * At a minimum, if will have a subject, object and type which should be expanded to
|
|
|
+ * the appropriate type of record depending on the content type this widget
|
|
|
+ * is attached to.
|
|
|
+ * @param $entity
|
|
|
+ * The entity the widget is attached to.
|
|
|
+ * @param $delta
|
|
|
+ * An integer indicating the specific relationship to load. This is usually the rank
|
|
|
+ * from the relationship table (if there is one).
|
|
|
+ */
|
|
|
+ private function loadRelationship($relationship, &$entity, $delta) {
|
|
|
+
|
|
|
+ $field_name = $this->field['field_name'];
|
|
|
+ $field_table = $this->instance['settings']['chado_table'];
|
|
|
+ $base_table = $this->instance['settings']['base_table'];
|
|
|
+
|
|
|
+ $rel_acc = $relationship->type_id->dbxref_id->db_id->name . ':' . $relationship->type_id->dbxref_id->accession;
|
|
|
+ $rel_type = $relationship->type_id->name;
|
|
|
+ $verb = $this->get_rel_verb($rel_type);
|
|
|
+
|
|
|
+ $pkey = $this->schema['primary key'][0];
|
|
|
+ $subject_id_key = $this->subject_id_column;
|
|
|
+ $object_id_key = $this->object_id_column;
|
|
|
+ // @todo grab these separately like it was before.
|
|
|
+ $subject_pkey = $object_pkey = $this->base_schema['primary key'][0];
|
|
|
+
|
|
|
+ $entity->{$field_name}['und'][$delta]['value'] = array(
|
|
|
+ 'local:relationship_subject' => $this->getRelationshipSubject($relationship),
|
|
|
+ 'local:relationship_type' => $relationship->type_id->name,
|
|
|
+ 'local:relationship_object' => $this->getRelationshipObject($relationship),
|
|
|
+ );
|
|
|
+
|
|
|
// Add the clause to the values array. The clause is a written version
|
|
|
// of the relationships.
|
|
|
$rel_type_clean = lcfirst(preg_replace('/_/', ' ', $rel_type));
|
|
|
+ $subject_type = $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['rdfs:type'];
|
|
|
+ $subject_name = $entity->{$field_name}['und'][$delta]['value']['local:relationship_subject']['schema:name'];
|
|
|
+ $object_type = $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['rdfs:type'];
|
|
|
+ $object_name = $entity->{$field_name}['und'][$delta]['value']['local:relationship_object']['schema:name'];
|
|
|
+
|
|
|
+
|
|
|
// Remember the current entity could be either the subject or object!
|
|
|
// Example: The genetic_marker, MARKER1 , derives from the sequence_variant, VARIANT1.
|
|
|
// The above relationship will be shown both on marker and variant pages
|
|
@@ -355,14 +460,15 @@ class sbo__relationship extends ChadoField {
|
|
|
$entity->{$field_name}['und'][$delta]['type_name'] = $relationship->type_id->name;
|
|
|
$entity->{$field_name}['und'][$delta]['subject_name'] = $subject_name . ' [id: ' . $relationship->$subject_id_key->$subject_pkey . ']';
|
|
|
$entity->{$field_name}['und'][$delta]['object_name'] = $object_name . ' [id: ' . $relationship->$object_id_key->$object_pkey . ']';
|
|
|
- if (array_key_exists('value', $schema['fields'])) {
|
|
|
+ if (array_key_exists('value', $this->schema['fields'])) {
|
|
|
$entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__value'] = $relationship->value;
|
|
|
}
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
$entity->{$field_name}['und'][$delta]['chado-' . $field_table . '__rank'] = $relationship->rank;
|
|
|
}
|
|
|
}
|
|
|
- /**
|
|
|
+
|
|
|
+ /**
|
|
|
*
|
|
|
* @see TripalField::load()
|
|
|
*/
|
|
@@ -370,41 +476,26 @@ class sbo__relationship extends ChadoField {
|
|
|
$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'];
|
|
|
+ $rel_table = $field_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];
|
|
|
+ $pkey = $this->schema['primary key'][0];
|
|
|
+ // 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.
|
|
|
+ $subject_id_key = $this->subject_id_column;
|
|
|
+ $object_id_key = $this->object_id_column;
|
|
|
|
|
|
// If we don't have a chado record return before creating a stub for this field!
|
|
|
if (!$record) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // 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' => '',
|
|
@@ -422,18 +513,13 @@ class sbo__relationship extends ChadoField {
|
|
|
|
|
|
// If the table has rank and value fields then add those to the default
|
|
|
// value array.
|
|
|
- if (array_key_exists('value', $schema['fields'])) {
|
|
|
+ if (array_key_exists('value', $this->schema['fields'])) {
|
|
|
$entity->{$field_name}['und'][0]['chado-' . $field_table . '__value'] = '';
|
|
|
}
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
+ if (array_key_exists('rank', $this->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,
|
|
@@ -451,9 +537,7 @@ class sbo__relationship extends ChadoField {
|
|
|
),
|
|
|
),
|
|
|
);
|
|
|
- $rel_table = $base_table . '_relationship';
|
|
|
- $schema = chado_get_schema($rel_table);
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
$options['order_by'] = array('rank' => 'ASC');
|
|
|
}
|
|
|
$record = chado_expand_var($record, 'table', $rel_table, $options);
|
|
@@ -518,15 +602,18 @@ class sbo__relationship extends ChadoField {
|
|
|
}
|
|
|
|
|
|
// Filter by unique name of the subject or object.
|
|
|
- if ($condition['column'] == $rel_subject_identifier) {
|
|
|
- $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.object_id");
|
|
|
- $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.subject_id");
|
|
|
- $query->condition("base2.uniquename", $condition['value'], $operator);
|
|
|
- }
|
|
|
- if ($condition['column'] == $rel_object_identifier) {
|
|
|
- $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id");
|
|
|
- $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.object_id");
|
|
|
- $query->condition("base2.uniquename", $condition['value'], $operator);
|
|
|
+ // If this table has a uniquename!
|
|
|
+ if (isset($this->schema['fields']['uniquename'])) {
|
|
|
+ if ($condition['column'] == $rel_subject_identifier) {
|
|
|
+ $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.object_id");
|
|
|
+ $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.subject_id");
|
|
|
+ $query->condition("base2.uniquename", $condition['value'], $operator);
|
|
|
+ }
|
|
|
+ if ($condition['column'] == $rel_object_identifier) {
|
|
|
+ $this->queryJoinOnce($query, $chado_table, $alias, "base.$bpkey = $alias.subject_id");
|
|
|
+ $this->queryJoinOnce($query, $base_table, 'base2', "base2.$bpkey = $alias.object_id");
|
|
|
+ $query->condition("base2.uniquename", $condition['value'], $operator);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Filter by the type of the subject or object
|
|
@@ -834,151 +921,211 @@ class sbo__relationship extends ChadoField {
|
|
|
$field_column = $this->instance['settings']['chado_column'];
|
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
|
|
|
|
- $schema = chado_get_schema($field_table);
|
|
|
- $fkeys = $schema['foreign keys'];
|
|
|
+ // Grab the chado record_id for this entity.
|
|
|
+ $chado_record_id = NULL;
|
|
|
+ if ($entity AND isset($entity->chado_record_id)) {
|
|
|
+ $chado_record_id = $entity->chado_record_id;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Validate each releationship.
|
|
|
+ foreach ($items as $delta => $item) {
|
|
|
+ $item_errors = $this->validateItem($item, $chado_record_id);
|
|
|
+ if (!empty($item_errors)) {
|
|
|
+ $errors[$field_name][$delta][$langcode] = $item_errors;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Validate a Single relationship.
|
|
|
+ *
|
|
|
+ * @param $item
|
|
|
+ * A single item from the $items array passed to TripalField::validate().
|
|
|
+ * @return
|
|
|
+ * An array of errors where each has a:
|
|
|
+ * - error: this is an error code which is the name of the field.
|
|
|
+ * - message: A message to show the user describing the problem.
|
|
|
+ */
|
|
|
+ public function validateItem($item, $chado_record_id = NULL) {
|
|
|
+ $errors = array();
|
|
|
+
|
|
|
+ $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'];
|
|
|
|
|
|
// 'nd_reagent_relationship' and 'project_relationship' have different column names from
|
|
|
// subject_id/object_id. Do a pattern matching to get the column names.
|
|
|
- $subject_id_key = 'subject_id';
|
|
|
- $object_id_key = 'object_id';
|
|
|
- foreach ($schema['foreign keys'][$base_table]['columns'] AS $lcolum => $rcolum) {
|
|
|
- if (preg_match('/^subject_.*id/', $lcolum)) {
|
|
|
- $subject_id_key = $lcolum;
|
|
|
- }
|
|
|
- else if (preg_match('/^object_.*id/', $lcolum)) {
|
|
|
- $object_id_key = $lcolum;
|
|
|
- }
|
|
|
+ $subject_id_key = $this->subject_id_column;
|
|
|
+ $object_id_key = $this->object_id_column;
|
|
|
+
|
|
|
+ $subject_id = $item['chado-' . $field_table . '__' . $subject_id_key];
|
|
|
+ $object_id = $item['chado-' . $field_table . '__' . $object_id_key];
|
|
|
+ $type_id = $item['chado-' . $field_table . '__type_id'];
|
|
|
+ $type_id = isset($item['type_id']) ? $item['chado-' . $field_table . '__type_id'] : $type_id;
|
|
|
+ $type_name = isset($item['type_name']) ? $item['type_name'] : '';
|
|
|
+ $voc_id = isset($item['vocabulary']) ? $item['vocabulary'] : '';
|
|
|
+ $subject_name = isset($item['subject_name']) ? $item['subject_name'] : '';
|
|
|
+ $object_name = isset($item['object_name']) ? $item['object_name'] : '';
|
|
|
+
|
|
|
+ // If the row is empty then just continue, there's nothing to validate.
|
|
|
+ if (!$type_id and !$type_name and !$subject_name and !$object_name) {
|
|
|
+ return;
|
|
|
}
|
|
|
|
|
|
- foreach ($items as $delta => $item) {
|
|
|
- $subject_id = $item['chado-' . $field_table . '__' . $subject_id_key];
|
|
|
- $object_id = $item['chado-' . $field_table . '__' . $object_id_key];
|
|
|
- $type_id = $item['chado-' . $field_table . '__type_id'];
|
|
|
- $type_id = isset($item['type_id']) ? $item['chado-' . $field_table . '__type_id'] : $type_id;
|
|
|
- $type_name = isset($item['type_name']) ? $item['type_name'] : '';
|
|
|
- $subject_name = isset($item['subject_name']) ? $items['subject_name'] : '';
|
|
|
- $object_name = isset($item['object_name']) ? $item['object_name'] : '';
|
|
|
-
|
|
|
-
|
|
|
- // If the row is empty then just continue, there's nothing to validate.
|
|
|
- if (!$type_id and !$type_name and !$subject_name and !$object_name) {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ // Check: Make sure we have values for all of the fields.
|
|
|
+ if (!$type_name && !$type_id) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("Please provide the type of relationship."),
|
|
|
+ 'element' => 'type',
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (!$subject_name) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("Please provide the subject of the relationship."),
|
|
|
+ 'element' => 'subject',
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if (!$object_name) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("Please provide the object of the relationship."),
|
|
|
+ 'element' => 'object',
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check: Cvterm exists.
|
|
|
+ if (!$type_id AND !$type_name) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("We were unable to find the type you specified. Please check spelling and that the term already exists."),
|
|
|
+ 'element' => 'type',
|
|
|
+ );
|
|
|
+ }
|
|
|
+ elseif ($type_name AND $voc_id) {
|
|
|
+ $val = array(
|
|
|
+ 'cv_id' => $voc_id,
|
|
|
+ 'name' => $type_name
|
|
|
+ );
|
|
|
+ $cvterm = chado_generate_var('cvterm', $val);
|
|
|
|
|
|
- // Make sure we have values for all of the fields.
|
|
|
- $form_error = FALSE;
|
|
|
- if (!$type_name && !$type_id) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
+ if (!isset($cvterm->cvterm_id)) {
|
|
|
+
|
|
|
+ $errors[] = array(
|
|
|
'error' => 'sbo__relationship',
|
|
|
- 'message' => t("Please provide the type of relationship."),
|
|
|
+ 'message' => t("We were unable to find the type you specified. Please check spelling and that the term already exists."),
|
|
|
+ 'element' => 'type',
|
|
|
);
|
|
|
+
|
|
|
}
|
|
|
- if ($entity and !$subject_name) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Before submitting this form we need to make sure that our subject_id and
|
|
|
+ // object_ids are real records. There are two ways to get the record, either
|
|
|
+ // just with the text value or with an [id: \d+] string embedded. If the
|
|
|
+ // later we will pull it out.
|
|
|
+ $subject_id = '';
|
|
|
+ $fkey_rcolumn = $this->schema['foreign keys'][$base_table]['columns'][$subject_id_key];
|
|
|
+ $matches = array();
|
|
|
+ if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
|
|
|
+ $subject_id = $matches[1];
|
|
|
+ $values = array($fkey_rcolumn => $subject_id);
|
|
|
+ $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
+ if (count($subject) == 0) {
|
|
|
+ $errors[] = array(
|
|
|
'error' => 'sbo__relationship',
|
|
|
- 'message' => t("Please provide the subject of the relationship."),
|
|
|
+ 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
|
|
|
+ 'element' => 'subject',
|
|
|
);
|
|
|
}
|
|
|
- if ($entity and !$object_name) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // Otherwise we need to look it up using the name field determined in the
|
|
|
+ // constructor for the current field. There may be more then one name field
|
|
|
+ // (e.g. organism: genus + species) so we want to check both.
|
|
|
+ $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
|
|
|
+ $subject = chado_query($sql, [':keyword' => $subject_name])->fetchAll();
|
|
|
+ if (count($subject) == 0 AND $chado_record_id) {
|
|
|
+ $errors[] = array(
|
|
|
'error' => 'sbo__relationship',
|
|
|
- 'message' => t("Please provide the object of the relationship."),
|
|
|
+ 'message' => t("The subject record cannot be found. Please check spelling."),
|
|
|
+ 'element' => 'subject',
|
|
|
);
|
|
|
}
|
|
|
- if ($form_error) {
|
|
|
- continue;
|
|
|
+ elseif (count($subject) > 1) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
|
|
|
+ 'element' => 'subject',
|
|
|
+ );
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // Before submitting this form we need to make sure that our subject_id and
|
|
|
- // object_ids are real records. There are two ways to get the record, either
|
|
|
- // just with the text value or with an [id: \d+] string embedded. If the
|
|
|
- // later we will pull it out.
|
|
|
- $subject_id = '';
|
|
|
- $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
|
|
|
- $matches = array();
|
|
|
- if ($entity) {
|
|
|
- if(preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
|
|
|
- $subject_id = $matches[1];
|
|
|
- $values = array($fkey_rcolumn => $subject_id);
|
|
|
- $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
- if (count($subject) == 0) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The subject record cannot be found using the specified id (e.g. [id: xx])."),
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- $values = array('uniquename' => $subject_name);
|
|
|
- $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
- if (count($subject) == 0) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The subject record cannot be found. Please check spelling."),
|
|
|
- );
|
|
|
- }
|
|
|
- elseif (count($subject) > 1) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The subject is not unique and therefore the relationship cannot be made."),
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
+ // Now check for a matching object.
|
|
|
+ $object_id = '';
|
|
|
+ $fkey_rcolumn = $this->schema['foreign keys'][$base_table]['columns'][$object_id_key];
|
|
|
+ $matches = array();
|
|
|
+ if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
|
|
|
+ $object_id = $matches[1];
|
|
|
+ $values = array($fkey_rcolumn => $object_id);
|
|
|
+ $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
+ if (count($subject) == 0 AND $chado_record_id) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
|
|
|
+ 'element' => 'object',
|
|
|
+ );
|
|
|
}
|
|
|
-
|
|
|
- // Now check for a matching object.
|
|
|
- $object_id = '';
|
|
|
- $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
|
|
|
- $matches = array();
|
|
|
- if ($entity) {
|
|
|
- if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
|
|
|
- $object_id = $matches[1];
|
|
|
- $values = array($fkey_rcolumn => $object_id);
|
|
|
- $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
- if (count($subject) == 0) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The object record cannot be found using the specified id (e.g. [id: xx])."),
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- $values = array('uniquename' => $object_name);
|
|
|
- $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
- if (count($object) == 0) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The object record cannot be found. Please check spelling."),
|
|
|
- );;
|
|
|
- }
|
|
|
- elseif (count($object) > 1) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The object is not unique and therefore the relationship cannot be made."),
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ // Otherwise we need to look it up using the name field determined in the
|
|
|
+ // constructor for the current field. There may be more then one name field
|
|
|
+ // (e.g. organism: genus + species) so we want to check both.
|
|
|
+ $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
|
|
|
+ $object = chado_query($sql, [':keyword' => $object_name]);
|
|
|
+ if (count($object) == 0) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("The object record cannot be found. Please check spelling."),
|
|
|
+ 'element' => 'object',
|
|
|
+ );
|
|
|
+ }
|
|
|
+ elseif (count($object) > 1) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("The object is not unique and therefore the relationship cannot be made."),
|
|
|
+ 'element' => 'object',
|
|
|
+ );
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // Make sure that either our object or our subject refers to the base record.
|
|
|
- if ($entity) {
|
|
|
- $chado_record_id = $entity->chado_record_id;
|
|
|
- if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("Either the subject or the object in the relationship must refer to this record."),
|
|
|
- );
|
|
|
- }
|
|
|
+ // Make sure that either our object or our subject refers to the base record.
|
|
|
+ if ($object_id AND $subject_id) {
|
|
|
+ if ($object_id != $chado_record_id and $subject_id != $chado_record_id) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("Either the subject or the object in the relationship must refer to this record."),
|
|
|
+ 'element' => 'row',
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // Make sure that the object and subject are not both the same thing.
|
|
|
- if ($object_id == $subject_id) {
|
|
|
- $errors[$field_name][$delta]['und'][] = array(
|
|
|
- 'error' => 'sbo__relationship',
|
|
|
- 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
|
|
|
- );
|
|
|
- }
|
|
|
+ // Make sure that the object and subject are not both the same thing.
|
|
|
+ if ($object_id AND $subject_id) {
|
|
|
+ if ($object_id == $subject_id) {
|
|
|
+ $errors[] = array(
|
|
|
+ 'error' => 'sbo__relationship',
|
|
|
+ 'message' => t("The subject and the object in the relationship cannot both refer to the same record."),
|
|
|
+ 'element' => 'row',
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return $errors;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -989,4 +1136,38 @@ class sbo__relationship extends ChadoField {
|
|
|
public function queryOrder($query, $order) {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve the schema's
|
|
|
+ */
|
|
|
+ public function getRelTableSchema() {
|
|
|
+ return $this->schema;
|
|
|
+ }
|
|
|
+ public function getBaseTableSchema() {
|
|
|
+ return $this->base_schema;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve the subject/object key columns.
|
|
|
+ */
|
|
|
+ public function getSubjectIdColumn() {
|
|
|
+ return $this->subject_id_column;
|
|
|
+ }
|
|
|
+ public function getObjectIdColumn() {
|
|
|
+ return $this->object_id_column;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve the base name columns.
|
|
|
+ */
|
|
|
+ public function getBaseNameColumns() {
|
|
|
+ return $this->base_name_columns;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Retrieve the base type column.
|
|
|
+ */
|
|
|
+ public function getBaseTypeColumn() {
|
|
|
+ return $this->base_type_column;
|
|
|
+ }
|
|
|
}
|