|
@@ -7,6 +7,59 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
// The list of field types for which this formatter is appropriate.
|
|
// The list of field types for which this formatter is appropriate.
|
|
public static $field_types = array('sbo__relationship');
|
|
public static $field_types = array('sbo__relationship');
|
|
|
|
|
|
|
|
+ // --------------------------------------------------------------------------
|
|
|
|
+ // PROTECTED CLASS MEMBERS -- DO NOT OVERRIDE
|
|
|
|
+ // --------------------------------------------------------------------------
|
|
|
|
+
|
|
|
|
+ // 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;
|
|
|
|
+
|
|
|
|
+ // 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;
|
|
|
|
+
|
|
|
|
+ // The field instance for this widget. This allows us to use some of the
|
|
|
|
+ // field methods and info in the widget.
|
|
|
|
+ protected $field_instance;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Extends TripalField::__construct().
|
|
|
|
+ */
|
|
|
|
+ public function __construct($field, $instance) {
|
|
|
|
+ parent::__construct($field, $instance);
|
|
|
|
+
|
|
|
|
+ module_load_include('inc', 'tripal_chado', 'includes/TripalFields/sbo__relationship/sbo__relationship');
|
|
|
|
+ $this->field_instance = new sbo__relationship($field, $instance);
|
|
|
|
+
|
|
|
|
+ // Retrieve the schema's.
|
|
|
|
+ $this->schema = $this->field_instance->getRelTableSchema();
|
|
|
|
+ $this->base_schema = $this->field_instance->getBaseTableSchema();
|
|
|
|
+
|
|
|
|
+ // Retrieve the subject/object column names.
|
|
|
|
+ $this->subject_id_column = $this->field_instance->getSubjectIdColumn();
|
|
|
|
+ $this->object_id_column = $this->field_instance->getObjectIdColumn();
|
|
|
|
+
|
|
|
|
+ // Retrieve the columns to use for name/type.
|
|
|
|
+ $this->base_name_columns = $this->field_instance->getBaseNameColumns();
|
|
|
|
+ $this->base_type_column = $this->field_instance->getBaseTypeColumn();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
* @see TripalFieldWidget::form()
|
|
* @see TripalFieldWidget::form()
|
|
@@ -14,120 +67,121 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
|
|
public function form(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
|
|
parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
|
|
parent::form($widget, $form, $form_state, $langcode, $items, $delta, $element);
|
|
|
|
|
|
- // TODO: make this widget deal better with the various relationship
|
|
|
|
- // tables. See the load function as it does a better job of this.
|
|
|
|
- return;
|
|
|
|
-
|
|
|
|
// Get the field settings.
|
|
// Get the field settings.
|
|
$field_name = $this->field['field_name'];
|
|
$field_name = $this->field['field_name'];
|
|
$field_type = $this->field['type'];
|
|
$field_type = $this->field['type'];
|
|
$field_table = $this->instance['settings']['chado_table'];
|
|
$field_table = $this->instance['settings']['chado_table'];
|
|
$field_column = $this->instance['settings']['chado_column'];
|
|
$field_column = $this->instance['settings']['chado_column'];
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
|
|
+ $widget['#table_name'] = $field_table;
|
|
|
|
|
|
- // Get the FK column that links to the base table.
|
|
|
|
- $base_table = $this->instance['settings']['base_table'];
|
|
|
|
- $schema = chado_get_schema($field_table);
|
|
|
|
- $pkey = $schema['primary key'][0];
|
|
|
|
- $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
|
|
|
|
- $fkey = $fkeys[0];
|
|
|
|
-
|
|
|
|
- // Get the instance settings. There are three options for how this widget
|
|
|
|
- // will be displayed. Those are controlled in the instance settings
|
|
|
|
- // of the field.
|
|
|
|
- // Option 1: relationship types are limited to a specific vocabulary.
|
|
|
|
- // Option 2: relationship types are limited to a subset of one vocabulary.
|
|
|
|
- // Option 3: relationship types are limited to a predefined set.
|
|
|
|
- $instance = $this->instance;
|
|
|
|
- $settings = '';
|
|
|
|
- $option1_vocabs = '';
|
|
|
|
- $option2_parent = '';
|
|
|
|
- $option2_vocab = '';
|
|
|
|
- $option3_rtypes = '';
|
|
|
|
- if (array_key_exists('relationships', $instance)) {
|
|
|
|
- $settings = $instance['settings']['relationships'];
|
|
|
|
- $option1_vocabs = $settings['option1_vocabs'];
|
|
|
|
- $option2_vocab = $settings['option2_vocab'];
|
|
|
|
- $option2_parent = $settings['option2_parent'];
|
|
|
|
- $option3_rtypes = $settings['relationship_types'];
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // For testing if there are selected vocabs for option1 we'll copy the
|
|
|
|
- // contents in a special variable for later.
|
|
|
|
- $option1_test = $option1_vocabs;
|
|
|
|
|
|
+ // Get the primary key of the relationship table
|
|
|
|
+ $pkey = $this->schema['primary key'][0];
|
|
|
|
|
|
- // Get the field defaults.
|
|
|
|
|
|
+ // 'nd_reagent_relationship' and 'project_relationship' have different column names from
|
|
|
|
+ // subject_id/object_id. Retrieve those determined in the constructor.
|
|
|
|
+ $subject_id_key = $this->subject_id_column;
|
|
|
|
+ $object_id_key = $this->object_id_column;
|
|
|
|
+ // And save them in the widget for use in testing/debugging.
|
|
|
|
+ $widget['#subject_id_key'] = $subject_id_key;
|
|
|
|
+ $widget['#object_id_key'] = $object_id_key;
|
|
|
|
+
|
|
|
|
+ // Default Values:
|
|
|
|
+ //----------------
|
|
$record_id = '';
|
|
$record_id = '';
|
|
$subject_id = '';
|
|
$subject_id = '';
|
|
$object_id = '';
|
|
$object_id = '';
|
|
$type_id = '';
|
|
$type_id = '';
|
|
$value = '';
|
|
$value = '';
|
|
$rank = '';
|
|
$rank = '';
|
|
- $subject_uniquename = '';
|
|
|
|
- $object_uniquename = '';
|
|
|
|
|
|
+ $subject_label = '';
|
|
|
|
+ $object_label = '';
|
|
$type = '';
|
|
$type = '';
|
|
|
|
|
|
- // '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;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// If the field already has a value then it will come through the $items
|
|
// If the field already has a value then it will come through the $items
|
|
// array. This happens when editing an existing record.
|
|
// array. This happens when editing an existing record.
|
|
if (count($items) > 0 and array_key_exists($delta, $items)) {
|
|
if (count($items) > 0 and array_key_exists($delta, $items)) {
|
|
- // Check for element values that correspond to fields in the Chado table.
|
|
|
|
- $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
|
|
|
|
- $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
|
|
|
|
- $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
|
|
|
|
|
|
+
|
|
|
|
+ // Sometimes empty/initialized items are getting through.
|
|
|
|
+ // To determine if it this one of them, the type_id must always be there.
|
|
$type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
|
|
$type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
|
|
- // Not all Chado tables have a value and rank. So we'll only get
|
|
|
|
- // those if applicable.
|
|
|
|
- if (array_key_exists('value', $schema['fields'])) {
|
|
|
|
- $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
|
|
|
|
- }
|
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
|
- $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
|
|
|
|
|
|
+ if (!empty($type_id)) {
|
|
|
|
+
|
|
|
|
+ // Check for element values that correspond to fields in the Chado table.
|
|
|
|
+ $record_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $pkey, $record_id);
|
|
|
|
+ $subject_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $subject_id_key, $subject_id);
|
|
|
|
+ $object_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__' . $object_id_key, $object_id);
|
|
|
|
+ $type_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__type_id', $type_id);
|
|
|
|
+
|
|
|
|
+ // Not all Chado tables have a value and rank. So we'll only get
|
|
|
|
+ // those if applicable.
|
|
|
|
+ if (array_key_exists('value', $this->schema['fields'])) {
|
|
|
|
+ $value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__value', $value);
|
|
|
|
+ }
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
|
+ $rank = tripal_get_field_item_keyval($items, $delta, 'chado-' . $field_table . '__rank', $rank);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Get element values added to help support insert/updates.
|
|
|
|
+ $object_label = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_label);
|
|
|
|
+ $subject_label = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_label);
|
|
|
|
+ $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
|
|
|
|
+
|
|
}
|
|
}
|
|
- // Get element values added to help support insert/updates.
|
|
|
|
- $object_uniquename = tripal_get_field_item_keyval($items, $delta, 'object_name', $object_uniquename);
|
|
|
|
- $subject_uniquename = tripal_get_field_item_keyval($items, $delta, 'subject_name', $subject_uniquename);
|
|
|
|
- $type = tripal_get_field_item_keyval($items, $delta, 'type_name', $type);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
// Check $form_state['values'] to see if an AJAX call set the values.
|
|
// Check $form_state['values'] to see if an AJAX call set the values.
|
|
if (array_key_exists('values', $form_state) and
|
|
if (array_key_exists('values', $form_state) and
|
|
- array_key_exists($field_name, $form_state['values'])) {
|
|
|
|
|
|
+ array_key_exists($field_name, $form_state['values'])) {
|
|
$record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
|
|
$record_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $pkey];
|
|
$subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
|
|
$subject_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $subject_id_key];
|
|
$object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
|
|
$object_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $object_id_key];
|
|
$type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
|
|
$type_id = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__type_id'];
|
|
- if (array_key_exists('value', $schema['fields'])) {
|
|
|
|
|
|
+ if (array_key_exists('value', $this->schema['fields'])) {
|
|
$value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
|
|
$value = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__value'];
|
|
}
|
|
}
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
$rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
|
|
$rank = $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__rank'];
|
|
}
|
|
}
|
|
- $object_uniquename = $form_state['values'][$field_name]['und'][$delta]['object_name'];
|
|
|
|
- $subject_uniquename = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
|
|
|
|
|
|
+ $object_label = $form_state['values'][$field_name]['und'][$delta]['object_name'];
|
|
|
|
+ $subject_label = $form_state['values'][$field_name]['und'][$delta]['subject_name'];
|
|
$type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
|
|
$type = $form_state['values'][$field_name]['und'][$delta]['type_name'];
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Getting default values for the relationship type element.
|
|
|
|
+ $default_voc = '';
|
|
|
|
+ if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
|
|
|
|
+ $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
|
|
|
|
+ }
|
|
|
|
+ $default_term = '';
|
|
|
|
+ if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
|
|
|
|
+ $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $default_type_id = $type_id;
|
|
|
|
+ if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
|
|
|
|
+ $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Check if we have autocomplete available for this base table
|
|
|
|
+ $autocomplete_path = "admin/tripal/storage/chado/auto_name/$base_table";
|
|
|
|
+ $has_autocomplete = db_query('SELECT 1 FROM menu_router WHERE path=:path',
|
|
|
|
+ array(':path' => $autocomplete_path.'/%'))->fetchField();
|
|
|
|
+
|
|
|
|
+ // Save some values for later...
|
|
$widget['#table_name'] = $field_table;
|
|
$widget['#table_name'] = $field_table;
|
|
|
|
|
|
- $widget['#fkeys'] = $schema['foreign keys'];
|
|
|
|
|
|
+ $widget['#fkeys'] = $this->schema['foreign keys'];
|
|
$widget['#base_table'] = $base_table;
|
|
$widget['#base_table'] = $base_table;
|
|
$widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
|
|
$widget['#chado_record_id'] = isset($form['#entity']) ? $form['#entity']->chado_record_id : '';
|
|
//$widget['#element_validate'] = array('sbo__relationship_validate');
|
|
//$widget['#element_validate'] = array('sbo__relationship_validate');
|
|
$widget['#prefix'] = "<span id='$field_table-$delta'>";
|
|
$widget['#prefix'] = "<span id='$field_table-$delta'>";
|
|
$widget['#suffix'] = "</span>";
|
|
$widget['#suffix'] = "</span>";
|
|
|
|
|
|
|
|
+ // Save the values needed by the Chado Storage API.
|
|
|
|
+ //-------------------------------------------------
|
|
$widget['value'] = array(
|
|
$widget['value'] = array(
|
|
'#type' => 'value',
|
|
'#type' => 'value',
|
|
'#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
|
|
'#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
|
|
@@ -148,131 +202,44 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
'#type' => 'value',
|
|
'#type' => 'value',
|
|
'#default_value' => $object_id,
|
|
'#default_value' => $object_id,
|
|
);
|
|
);
|
|
- if (array_key_exists('value', $schema['fields'])) {
|
|
|
|
|
|
+ if (array_key_exists('value', $this->schema['fields'])) {
|
|
$widget['chado-' . $field_table . '__value'] = array(
|
|
$widget['chado-' . $field_table . '__value'] = array(
|
|
'#type' => 'value',
|
|
'#type' => 'value',
|
|
'#default_value' => $value,
|
|
'#default_value' => $value,
|
|
);
|
|
);
|
|
}
|
|
}
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
$widget['chado-' . $field_table . '__rank'] = array(
|
|
$widget['chado-' . $field_table . '__rank'] = array(
|
|
'#type' => 'value',
|
|
'#type' => 'value',
|
|
'#default_value' => $rank,
|
|
'#default_value' => $rank,
|
|
);
|
|
);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Subject:
|
|
|
|
+ //----------
|
|
$widget['subject_name'] = array(
|
|
$widget['subject_name'] = array(
|
|
'#type' => 'textfield',
|
|
'#type' => 'textfield',
|
|
'#title' => t('Subject'),
|
|
'#title' => t('Subject'),
|
|
- '#default_value' => $subject_uniquename,
|
|
|
|
|
|
+ '#default_value' => $subject_label,
|
|
'#required' => $element['#required'],
|
|
'#required' => $element['#required'],
|
|
- '#maxlength' => array_key_exists($subject_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$subject_id_key]) ? $schema['fields'][$subject_id_key]['length'] : 255,
|
|
|
|
|
|
+ '#maxlength' => array_key_exists($subject_id_key, $this->schema['fields']) && array_key_exists('length', $this->schema['fields'][$subject_id_key]) ? $this->schema['fields'][$subject_id_key]['length'] : 255,
|
|
'#size' => 35,
|
|
'#size' => 35,
|
|
- '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
|
|
|
|
);
|
|
);
|
|
-
|
|
|
|
- // Getting default values for the relationship type element.
|
|
|
|
- $default_voc = '';
|
|
|
|
- if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'])) {
|
|
|
|
- $default_voc = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['vocabulary'];
|
|
|
|
- }
|
|
|
|
- $default_term = '';
|
|
|
|
- if (isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'])) {
|
|
|
|
- $default_term = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_name'];
|
|
|
|
|
|
+ // Add autocomplete if we have one for this base table.
|
|
|
|
+ if ($has_autocomplete) {
|
|
|
|
+ $widget['subject_name']['#autocomplete_path'] = $autocomplete_path;
|
|
}
|
|
}
|
|
|
|
|
|
- $default_type_id = $type_id;
|
|
|
|
- if (!$type_id && isset($form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'])) {
|
|
|
|
- $default_type_id = $form_state['field'][$field_name]['und']['instance']['default_value'][0]['type_id'];
|
|
|
|
- }
|
|
|
|
- // Option 3: Custom list of Relationship Types
|
|
|
|
- $rtype_options = array();
|
|
|
|
- $rtype_options[] = 'Select a Type';
|
|
|
|
- if ($option3_rtypes) {
|
|
|
|
- $rtypes = explode(PHP_EOL, $option3_rtypes);
|
|
|
|
- foreach($rtypes AS $rtype) {
|
|
|
|
- // Ignore empty lines
|
|
|
|
- if (trim($rtype) == '') {
|
|
|
|
- continue;
|
|
|
|
- }
|
|
|
|
- $term = chado_get_cvterm(array('name' => trim($rtype)));
|
|
|
|
- // Try to get term with vocabulary specified
|
|
|
|
- if (!$term) {
|
|
|
|
- $tmp = explode('|', trim($rtype), 2);
|
|
|
|
- $cv = chado_get_cv(array('name' => trim($tmp[0])));
|
|
|
|
- $rtype = trim($tmp[1]);
|
|
|
|
- $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
|
|
|
|
- }
|
|
|
|
- $rtype_options[$term->cvterm_id] = $term->name;
|
|
|
|
- }
|
|
|
|
- $widget['type_id'] = array(
|
|
|
|
- '#type' => 'select',
|
|
|
|
- '#title' => t('Relationship Type'),
|
|
|
|
- '#options' => $rtype_options,
|
|
|
|
- '#default_value' => $default_type_id,
|
|
|
|
- );
|
|
|
|
- if ($type_id && !key_exists($type_id, $rtype_options)) {
|
|
|
|
- form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // Option 2: Child terms of a selected cvterm
|
|
|
|
- else if ($option2_vocab) {
|
|
|
|
- $values = array(
|
|
|
|
- 'cv_id' => $option2_vocab,
|
|
|
|
- 'name' => $option2_parent
|
|
|
|
- );
|
|
|
|
- $parent_term = chado_get_cvterm($values);
|
|
|
|
|
|
+ // Type:
|
|
|
|
+ //-------
|
|
|
|
+ $rtype_options = $this->get_rtype_select_options();
|
|
|
|
+ if ($rtype_options) {
|
|
|
|
|
|
- // If the term wasn't found then see if it's a synonym.
|
|
|
|
- if(!$parent_term) {
|
|
|
|
- $values = array(
|
|
|
|
- 'synonym' => array(
|
|
|
|
- 'name' => trim($option2_parent),
|
|
|
|
- )
|
|
|
|
- );
|
|
|
|
- $synonym = chado_get_cvterm($values);
|
|
|
|
- if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
|
|
|
|
- $parent_term = $synonym;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // Get the child terms of the parent term found above.
|
|
|
|
- $sql = "
|
|
|
|
- SELECT subject_id,
|
|
|
|
- (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
|
|
|
|
- FROM {cvtermpath}
|
|
|
|
- WHERE
|
|
|
|
- object_id = :parent_cvterm_id AND
|
|
|
|
- cv_id = :parent_cv_id
|
|
|
|
- ORDER BY name
|
|
|
|
- ";
|
|
|
|
- $args = array(
|
|
|
|
- ':parent_cvterm_id' => $parent_term->cvterm_id,
|
|
|
|
- ':parent_cv_id' => $parent_term->cv_id->cv_id
|
|
|
|
- );
|
|
|
|
- $results = chado_query($sql, $args);
|
|
|
|
- while($child = $results->fetchObject()) {
|
|
|
|
- $rtype_options[$child->subject_id] = $child->name;
|
|
|
|
- }
|
|
|
|
- $widget['type_id'] = array(
|
|
|
|
- '#type' => 'select',
|
|
|
|
- '#title' => t('Relationship Type'),
|
|
|
|
- '#options' => $rtype_options,
|
|
|
|
- '#default_value' => $default_type_id,
|
|
|
|
- );
|
|
|
|
- if ($type_id && !key_exists($type_id, $rtype_options)) {
|
|
|
|
- form_set_error($this->field['field_name'] . '[' . $langcode . '][' . $delta . '][type_id]', 'Illegal option detected for Relationship Type. Please contact site administrator to fix the problem');
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // Option 1: All terms of selected vocabularies
|
|
|
|
- else if ($option1_test && array_pop($option1_test)) {
|
|
|
|
- $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
|
|
|
|
- $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
|
|
|
|
- while ($obj = $results->fetchObject()) {
|
|
|
|
- $rtype_options[$obj->cvterm_id] = $obj->name;
|
|
|
|
- }
|
|
|
|
$widget['type_id'] = array(
|
|
$widget['type_id'] = array(
|
|
'#type' => 'select',
|
|
'#type' => 'select',
|
|
'#title' => t('Relationship Type'),
|
|
'#title' => t('Relationship Type'),
|
|
'#options' => $rtype_options,
|
|
'#options' => $rtype_options,
|
|
|
|
+ '#empty_option' => 'Select a Type',
|
|
'#default_value' => $default_type_id,
|
|
'#default_value' => $default_type_id,
|
|
);
|
|
);
|
|
if ($type_id && !key_exists($type_id, $rtype_options)) {
|
|
if ($type_id && !key_exists($type_id, $rtype_options)) {
|
|
@@ -280,11 +247,14 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Default option:
|
|
// Default option:
|
|
|
|
+ // If we were determine an type_id option set...
|
|
|
|
+ // then we will need to provide a cv + type autocomplete.
|
|
else {
|
|
else {
|
|
// Set up available cvterms for selection
|
|
// Set up available cvterms for selection
|
|
- $vocs = array(0 => 'Select a vocabulary');
|
|
|
|
|
|
+ $vocs = array();
|
|
$vocs = chado_get_cv_select_options();
|
|
$vocs = chado_get_cv_select_options();
|
|
- $cv_id = isset($form_state['values'][$field_name]['und'][0]['vocabulary']) ? $form_state['values'][$field_name]['und'][0]['vocabulary'] : 0;
|
|
|
|
|
|
+ unset($vocs[0]);
|
|
|
|
+ $cv_id = isset($form_state['values'][$field_name][$langcode][$delta]['vocabulary']) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : 0;
|
|
// Try getting the cv_id from cvterm for existing records
|
|
// Try getting the cv_id from cvterm for existing records
|
|
if (!$cv_id && $type_id) {
|
|
if (!$cv_id && $type_id) {
|
|
$cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
|
|
$cvterm = chado_get_cvterm(array('cvterm_id' => $type_id));
|
|
@@ -302,6 +272,7 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
'#options' => $vocs,
|
|
'#options' => $vocs,
|
|
'#required' => $element['#required'],
|
|
'#required' => $element['#required'],
|
|
'#default_value' => $cv_id,
|
|
'#default_value' => $cv_id,
|
|
|
|
+ '#empty_option' => 'Select a Vocabulary',
|
|
'#ajax' => array(
|
|
'#ajax' => array(
|
|
'callback' => "sbo__relationship_widget_form_ajax_callback",
|
|
'callback' => "sbo__relationship_widget_form_ajax_callback",
|
|
'wrapper' => "$field_table-$delta",
|
|
'wrapper' => "$field_table-$delta",
|
|
@@ -309,27 +280,33 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
'method' => 'replace'
|
|
'method' => 'replace'
|
|
),
|
|
),
|
|
);
|
|
);
|
|
|
|
+ $widget['type_name'] = array(
|
|
|
|
+ '#type' => 'textfield',
|
|
|
|
+ '#title' => t('Relationship Type'),
|
|
|
|
+ '#size' => 15,
|
|
|
|
+ '#default_value' => $default_term,
|
|
|
|
+ '#disabled' => TRUE,
|
|
|
|
+ '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
|
|
|
|
+ );
|
|
if ($cv_id) {
|
|
if ($cv_id) {
|
|
- $options = array();
|
|
|
|
- $widget['type_name'] = array(
|
|
|
|
- '#type' => 'textfield',
|
|
|
|
- '#title' => t('Relationship Type'),
|
|
|
|
- '#size' => 15,
|
|
|
|
- '#default_value' => $default_term,
|
|
|
|
- '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/cvterm/$cv_id"
|
|
|
|
- );
|
|
|
|
|
|
+ $widget['type_name']['#disabled'] = FALSE;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Object:
|
|
|
|
+ //--------
|
|
$widget['object_name'] = array(
|
|
$widget['object_name'] = array(
|
|
'#type' => 'textfield',
|
|
'#type' => 'textfield',
|
|
'#title' => t('Object'),
|
|
'#title' => t('Object'),
|
|
- '#default_value' => $object_uniquename,
|
|
|
|
|
|
+ '#default_value' => $object_label,
|
|
'#required' => $element['#required'],
|
|
'#required' => $element['#required'],
|
|
- '#maxlength' => array_key_exists($object_id_key, $schema['fields']) && array_key_exists('length', $schema['fields'][$object_id_key]) ? $schema['fields'][$object_id_key]['length'] : 255,
|
|
|
|
|
|
+ '#maxlength' => array_key_exists($object_id_key, $this->schema['fields']) && array_key_exists('length', $this->schema['fields'][$object_id_key]) ? $this->schema['fields'][$object_id_key]['length'] : 255,
|
|
'#size' => 35,
|
|
'#size' => 35,
|
|
- '#autocomplete_path' => "admin/tripal/storage/chado/auto_name/$base_table",
|
|
|
|
);
|
|
);
|
|
|
|
+ // Add autocomplete if we have one for this base table.
|
|
|
|
+ if ($has_autocomplete) {
|
|
|
|
+ $widget['object_name']['#autocomplete_path'] = $autocomplete_path;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -345,97 +322,158 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
$chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
|
|
$chado_record_id = array_key_exists('#entity', $element)? $element['#entity']->chado_record_id : NULL;
|
|
|
|
|
|
- $schema = chado_get_schema($field_table);
|
|
|
|
- $fkeys = $schema['foreign keys'];
|
|
|
|
|
|
+ $fkeys = $this->schema['foreign keys'];
|
|
|
|
|
|
// 'nd_reagent_relationship' and 'project_relationship' have different column names from
|
|
// '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/object_id. Retrieve the column names determined in the form.
|
|
|
|
+ $subject_id_key = $this->subject_id_column;
|
|
|
|
+ $object_id_key = $this->object_id_column;
|
|
|
|
|
|
|
|
+ // Retrieve the values from the form for the current $delta.
|
|
$voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
|
|
$voc_id = array_key_exists('vocabulary', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['vocabulary'] : '';
|
|
$type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
|
|
$type_name = array_key_exists('type_name', $form_state['values'][$field_name][$langcode][$delta]) ? $form_state['values'][$field_name][$langcode][$delta]['type_name'] : '';
|
|
$subject_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] : '';
|
|
$subject_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] : '';
|
|
$object_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]: '';
|
|
$object_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key]: '';
|
|
$type_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] : '';
|
|
$type_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] : '';
|
|
|
|
|
|
- $subject_name = isset($form_state['values'][$field_name][$langcode][$delta]['subject_name']) ? $form_state['values'][$field_name][$langcode][$delta]['subject_name']: '';
|
|
|
|
|
|
+ $subject_name = isset($form_state['values'][$field_name][$langcode][$delta]['subject_name']) ? $form_state['values'][$field_name][$langcode][$delta]['subject_name'] : '';
|
|
$object_name = isset($form_state['values'][$field_name][$langcode][$delta]['object_name']) ? $form_state['values'][$field_name][$langcode][$delta]['object_name'] : '';
|
|
$object_name = isset($form_state['values'][$field_name][$langcode][$delta]['object_name']) ? $form_state['values'][$field_name][$langcode][$delta]['object_name'] : '';
|
|
|
|
|
|
|
|
+ // Validation:
|
|
|
|
+ //------------
|
|
// If the row is empty then skip this one, there's nothing to validate.
|
|
// If the row is empty then skip this one, there's nothing to validate.
|
|
- if (!($type_id or !$type_name) and !$subject_name and !$object_name) {
|
|
|
|
|
|
+ if (!($type_id || $type_name) && !$subject_name && !$object_name) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- else if ($type_name && $voc_id) {
|
|
|
|
- $val = array(
|
|
|
|
- 'cv_id' => $voc_id,
|
|
|
|
- 'name' => $type_name
|
|
|
|
- );
|
|
|
|
- $cvterm = chado_generate_var('cvterm', $val);
|
|
|
|
- $type_id = $cvterm->cvterm_id;
|
|
|
|
- }
|
|
|
|
|
|
|
|
// Do not proceed if subject ID or object ID does not exist
|
|
// Do not proceed if subject ID or object ID does not exist
|
|
if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
|
|
if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
|
|
- !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
|
|
|
|
|
|
+ !key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- // Get the subject ID.
|
|
|
|
- $subject_id = '';
|
|
|
|
- $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
|
|
|
|
- $matches = array();
|
|
|
|
- if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
|
|
|
|
- $subject_id = $matches[1];
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- $values = array('uniquename' => $subject_name);
|
|
|
|
- $subject = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
|
- if(count($subject) > 0) {
|
|
|
|
- $subject_id = $subject[0]->$fkey_rcolumn;
|
|
|
|
|
|
+
|
|
|
|
+ // Validation is occuring in the field::validate() but we need to know if it finds errors.
|
|
|
|
+ // As such, I'm calling it here to check.
|
|
|
|
+ // Also, field::validate() doesn't seem to always show it's errors
|
|
|
|
+ // OR stop form submission? so we need to ensure that happens here.
|
|
|
|
+ // sbo__relationship::validate($entity_type, $entity, $langcode, $items, &$errors)
|
|
|
|
+ $errors = $this->field_instance->validateItem($form_state['values'][$field_name][$langcode][$delta], $element['#chado_record_id']);
|
|
|
|
+ if ($errors) {
|
|
|
|
+ foreach ($errors as $error) {
|
|
|
|
+ switch ($error['element']) {
|
|
|
|
+ case 'subject':
|
|
|
|
+ form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][subject_name', $error['message']);
|
|
|
|
+ break;
|
|
|
|
+ case 'type':
|
|
|
|
+ form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
|
|
|
|
+ break;
|
|
|
|
+ case 'object':
|
|
|
|
+ form_set_error('sbo__relationship]['.$langcode.']['.$delta.'][object_name', $error['message']);
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ form_set_error('sbo__relationship]['.$langcode.']['.$delta, $error['message']);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- // Get the object ID.
|
|
|
|
- $object_id = '';
|
|
|
|
- $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
|
|
|
|
- $matches = array();
|
|
|
|
- if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
|
|
|
|
- $object_id = $matches[1];
|
|
|
|
|
|
+ // Ensure data is prepared for the storage backend:
|
|
|
|
+ //-------------------------------------------------
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
- $values = array('uniquename' => $object_name);
|
|
|
|
- $object = chado_select_record($base_table, array($fkey_rcolumn), $values);
|
|
|
|
- if (count($object) > 0) {
|
|
|
|
- $object_id = $object[0]->$fkey_rcolumn;
|
|
|
|
|
|
+
|
|
|
|
+ if ($type_name && $voc_id) {
|
|
|
|
+ $val = array(
|
|
|
|
+ 'cv_id' => $voc_id,
|
|
|
|
+ 'name' => $type_name
|
|
|
|
+ );
|
|
|
|
+ $cvterm = chado_generate_var('cvterm', $val);
|
|
|
|
+
|
|
|
|
+ if (isset($cvterm->cvterm_id)) {
|
|
|
|
+ $type_id = $cvterm->cvterm_id;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
|
|
- if ($subject_id && $object_id && $type_id) {
|
|
|
|
- // Set the IDs according to the values that were determined above.
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
|
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
|
|
|
|
|
|
+ // Get the subject ID.
|
|
|
|
+ $subject_id = '';
|
|
|
|
+ $fkey_rcolumn = $fkeys[$base_table]['columns'][$subject_id_key];
|
|
|
|
+ $matches = array();
|
|
|
|
+ // First check if it's in the textfield due to use of the autocomplete.
|
|
|
|
+ if (preg_match('/\[id: (\d+)\]/', $subject_name, $matches)) {
|
|
|
|
+ $subject_id = $matches[1];
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else {
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
|
|
|
|
- if (array_key_exists('rank', $schema['fields'])) {
|
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
|
|
|
|
|
|
+ // 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.
|
|
|
|
+ else {
|
|
|
|
+ $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) {
|
|
|
|
+ $subject_id = $subject[0]->$fkey_rcolumn;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Get the object ID.
|
|
|
|
+ $object_id = '';
|
|
|
|
+ $fkey_rcolumn = $fkeys[$base_table]['columns'][$object_id_key];
|
|
|
|
+ $matches = array();
|
|
|
|
+ // First check if it's in the textfield due to use of the autocomplete.
|
|
|
|
+ if (preg_match('/\[id: (\d+)\]/', $object_name, $matches)) {
|
|
|
|
+ $object_id = $matches[1];
|
|
|
|
+ }
|
|
|
|
+ // 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.
|
|
|
|
+ else {
|
|
|
|
+ $sql = 'SELECT ' . $fkey_rcolumn . ' FROM {' . $base_table . '} WHERE ' . implode('||', $this->base_name_columns) . '=:keyword';
|
|
|
|
+ $object = chado_query($sql, [':keyword' => $object_name])->fetchAll();
|
|
|
|
+ if (count($object) > 0) {
|
|
|
|
+ $object_id = $object[0]->$fkey_rcolumn;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // If we have all three values required for a relationship...
|
|
|
|
+ // Then set them as the chado field storage expects them to be set.
|
|
|
|
+ if ($subject_id && $object_id && $type_id) {
|
|
|
|
+ // Set the IDs according to the values that were determined above.
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Otherwise, maybe we are creating the entity...
|
|
|
|
+ // The storage API sohuld handle this case and automagically add the key in once
|
|
|
|
+ // the chado record is created... so all we need to do is set the other columns.
|
|
|
|
+ elseif ($subject_name && $object_id && $type_id) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = $object_id;
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ elseif ($subject_id && $object_name && $type_id) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['value'] = 'value must be set but is not used';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = $subject_id;
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = $type_id;
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = $form_state['values'][$field_name][$langcode][$delta]['_weight'];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Otherwise, we don't have a vallue to insert so leave them blank.
|
|
|
|
+ else {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $subject_id_key] = '';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__' . $object_id_key] = '';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'] = '';
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__value'] = '';
|
|
|
|
+ if (array_key_exists('rank', $this->schema['fields'])) {
|
|
|
|
+ $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__rank'] = '';
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ return $errors;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -445,25 +483,138 @@ class sbo__relationship_widget extends ChadoFieldWidget {
|
|
$layout = "
|
|
$layout = "
|
|
<div class=\"chado-linker--relationship-widget\">
|
|
<div class=\"chado-linker--relationship-widget\">
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
- drupal_render($element['subject_name']) . "
|
|
|
|
|
|
+ drupal_render($element['subject_name']) . "
|
|
</div>
|
|
</div>
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
- drupal_render($element['vocabulary']) . "
|
|
|
|
|
|
+ drupal_render($element['vocabulary']) . "
|
|
</div>
|
|
</div>
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
- drupal_render($element['type_name']) . "
|
|
|
|
|
|
+ drupal_render($element['type_name']) . "
|
|
</div>
|
|
</div>
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
<div class=\"chado-linker--relationship-widget-item\">" .
|
|
- drupal_render($element['type_id']) . "
|
|
|
|
|
|
+ drupal_render($element['type_id']) . "
|
|
</div>
|
|
</div>
|
|
<div>" .
|
|
<div>" .
|
|
- drupal_render($element['object_name']) . "
|
|
|
|
|
|
+ drupal_render($element['object_name']) . "
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
";
|
|
";
|
|
return $layout;
|
|
return $layout;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Retrieve options for the type drop-down for the relationship widget.
|
|
|
|
+ */
|
|
|
|
+ public function get_rtype_select_options() {
|
|
|
|
+
|
|
|
|
+ // This is slated for Release 2 of this widget.
|
|
|
|
+ // It still needs extensive functional and automated testing.
|
|
|
|
+ // Thus for now we are falling back on the Default option:
|
|
|
|
+ // Form will provide a type autocomplete + vocab select.
|
|
|
|
+ // @todo test this.
|
|
|
|
+ return FALSE;
|
|
|
|
+
|
|
|
|
+ // Get the instance settings. There are three options for how this widget
|
|
|
|
+ // will be displayed. Those are controlled in the instance settings
|
|
|
|
+ // of the field.
|
|
|
|
+ // Option 1: relationship types are limited to a specific vocabulary.
|
|
|
|
+ // Option 2: relationship types are limited to a subset of one vocabulary.
|
|
|
|
+ // Option 3: relationship types are limited to a predefined set.
|
|
|
|
+ $instance = $this->instance;
|
|
|
|
+ $settings = '';
|
|
|
|
+ $option1_vocabs = '';
|
|
|
|
+ $option2_parent = '';
|
|
|
|
+ $option2_vocab = '';
|
|
|
|
+ $option3_rtypes = '';
|
|
|
|
+ if (array_key_exists('relationships', $instance)) {
|
|
|
|
+ $settings = $instance['settings']['relationships'];
|
|
|
|
+ $option1_vocabs = $settings['option1_vocabs'];
|
|
|
|
+ $option2_vocab = $settings['option2_vocab'];
|
|
|
|
+ $option2_parent = $settings['option2_parent'];
|
|
|
|
+ $option3_rtypes = $settings['relationship_types'];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // For testing if there are selected vocabs for option1 we'll copy the
|
|
|
|
+ // contents in a special variable for later.
|
|
|
|
+ $option1_test = $option1_vocabs;
|
|
|
|
+
|
|
|
|
+ // Option 3: Custom list of Relationship Types
|
|
|
|
+ $rtype_options = array();
|
|
|
|
+ if ($option3_rtypes) {
|
|
|
|
+ $rtypes = explode(PHP_EOL, $option3_rtypes);
|
|
|
|
+ foreach($rtypes AS $rtype) {
|
|
|
|
+ // Ignore empty lines
|
|
|
|
+ if (trim($rtype) == '') {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ $term = chado_get_cvterm(array('name' => trim($rtype)));
|
|
|
|
+ // Try to get term with vocabulary specified
|
|
|
|
+ if (!$term) {
|
|
|
|
+ $tmp = explode('|', trim($rtype), 2);
|
|
|
|
+ $cv = chado_get_cv(array('name' => trim($tmp[0])));
|
|
|
|
+ $rtype = trim($tmp[1]);
|
|
|
|
+ $term = chado_get_cvterm(array('name' => $rtype, 'cv_id' => $cv->cv_id));
|
|
|
|
+ }
|
|
|
|
+ $rtype_options[$term->cvterm_id] = $term->name;
|
|
|
|
+ }
|
|
|
|
+ return $rtype_options;
|
|
|
|
+ }
|
|
|
|
+ // Option 2: Child terms of a selected cvterm
|
|
|
|
+ else if ($option2_vocab) {
|
|
|
|
+ $values = array(
|
|
|
|
+ 'cv_id' => $option2_vocab,
|
|
|
|
+ 'name' => $option2_parent
|
|
|
|
+ );
|
|
|
|
+ $parent_term = chado_get_cvterm($values);
|
|
|
|
+
|
|
|
|
+ // If the term wasn't found then see if it's a synonym.
|
|
|
|
+ if(!$parent_term) {
|
|
|
|
+ $values = array(
|
|
|
|
+ 'synonym' => array(
|
|
|
|
+ 'name' => trim($option2_parent),
|
|
|
|
+ )
|
|
|
|
+ );
|
|
|
|
+ $synonym = chado_get_cvterm($values);
|
|
|
|
+ if ($synonym && $synonym->cv_id->cv_id == $option2_vocab) {
|
|
|
|
+ $parent_term = $synonym;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Get the child terms of the parent term found above.
|
|
|
|
+ $sql = "
|
|
|
|
+ SELECT subject_id,
|
|
|
|
+ (SELECT name from {cvterm} where cvterm_id = subject_id) AS name
|
|
|
|
+ FROM {cvtermpath}
|
|
|
|
+ WHERE
|
|
|
|
+ object_id = :parent_cvterm_id AND
|
|
|
|
+ cv_id = :parent_cv_id
|
|
|
|
+ ORDER BY name
|
|
|
|
+ ";
|
|
|
|
+ $args = array(
|
|
|
|
+ ':parent_cvterm_id' => $parent_term->cvterm_id,
|
|
|
|
+ ':parent_cv_id' => $parent_term->cv_id->cv_id
|
|
|
|
+ );
|
|
|
|
+ $results = chado_query($sql, $args);
|
|
|
|
+ while($child = $results->fetchObject()) {
|
|
|
|
+ $rtype_options[$child->subject_id] = $child->name;
|
|
|
|
+ }
|
|
|
|
+ return $rtype_options;
|
|
|
|
+ }
|
|
|
|
+ // Option 1: All terms of selected vocabularies
|
|
|
|
+ else if ($option1_test && array_pop($option1_test)) {
|
|
|
|
+ $sql = "SELECT cvterm_id, name FROM {cvterm} WHERE cv_id IN (:cv_id) ORDER BY name";
|
|
|
|
+ $results = chado_query($sql, array(':cv_id' => $option1_vocabs));
|
|
|
|
+ while ($obj = $results->fetchObject()) {
|
|
|
|
+ $rtype_options[$obj->cvterm_id] = $obj->name;
|
|
|
|
+ }
|
|
|
|
+ return $rtype_options;
|
|
|
|
+ }
|
|
|
|
+ // Default option:
|
|
|
|
+ // Let the form deal with this by providing a type autocomplete?
|
|
|
|
+ else {
|
|
|
|
+ return FALSE;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
function theme_sbo__relationship_instance_settings ($variables) {
|
|
function theme_sbo__relationship_instance_settings ($variables) {
|