public function sbo__relationship_widget::validate
3.x sbo__relationship_widget.inc | public sbo__relationship_widget::validate($element, $form, &$form_state, $langcode, $delta) |
Overrides TripalFieldWidget::validate
See also
File
- tripal_chado/
includes/ TripalFields/ sbo__relationship/ sbo__relationship_widget.inc, line 339
Class
Code
public function validate($element, $form, &$form_state, $langcode, $delta) {
$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'];
$chado_record_id = array_key_exists('#entity', $element) ? $element['#entity']->chado_record_id : NULL;
$schema = chado_get_schema($field_table);
$fkeys = $schema['foreign keys'];
// '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;
}
}
$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'] : '';
$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] : '';
$type_id = $form_state['values'][$field_name][$langcode][$delta]['chado-' . $field_table . '__type_id'];
$subject_name = $form_state['values'][$field_name][$langcode][$delta]['subject_name'];
$object_name = $form_state['values'][$field_name][$langcode][$delta]['object_name'];
// 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) {
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
if (!key_exists($subject_id_key, $fkeys[$base_table]['columns']) ||
!key_exists($object_id_key, $fkeys[$base_table]['columns'])) {
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;
}
}
// 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];
}
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 ($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'];
}
}
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'] = '';
}
}
}