function chado_add_node_form_relationships_add_button_validate

2.x tripal_core.chado_nodes.relationships.api.inc chado_add_node_form_relationships_add_button_validate($form, &$form_state)

Validate the user input for creating a new relationship. Called by the add button in chado_add_node_form_relationships.

Related topics

1 call to chado_add_node_form_relationships_add_button_validate()
chado_add_node_form_subtables_add_button_validate in tripal_core/api/tripal_core.chado_nodes.api.inc
Validate Adding Subtables entries from the node forms. Supported subtables: Properties, Relationships, Additional DBxrefs.

File

tripal_core/api/tripal_core.chado_nodes.relationships.api.inc, line 603
API to manage the chado _relationship table for various Tripal Node Types

Code

function chado_add_node_form_relationships_add_button_validate($form, &$form_state) {

  $details = unserialize($form_state['values']['relationship_table']['details']);

  // First deal with autocomplete fields.
  // Extract the base_id assuming '(###) NAME FIELD'.
  if (!empty($form_state['values']['relationship_table']['new']['subject_name'])) {
    if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['subject_name'], $matches)) {
      $form_state['values']['relationship_table']['new']['subject_id'] = $matches[1];
    }
    else {
      form_set_error('relationship_table][new][subject_name', 'You need to select the subject from the autocomplete drop-down');
    }
  }
  if (!empty($form_state['values']['relationship_table']['new']['object_name'])) {
    if (preg_match('/\((\d+)\) .*/', $form_state['values']['relationship_table']['new']['object_name'], $matches)) {
      $form_state['values']['relationship_table']['new']['object_id'] = $matches[1];
    }
    else {
      form_set_error('relationship_table][new][object_name', 'You need to select the object from the autocomplete drop-down');
    }
  }


  // NOTE: The only way to specify the current node is via checkbox. This is by
  // design since guessing if they meant the current node by name is a
  // chicken-egg problem due to the name field only being set to just the name
  // only happens once it has been determined which germplasm is the current
  // one. Furthermore, we can't use the primary key since this will not have
  // been set on insert.

  // At least one of the participants must be the current node.
  if (!($form_state['values']['relationship_table']['new']['subject_is_current'] OR $form_state['values']['relationship_table']['new']['object_is_current'])) {
    form_set_error('relationship_table][new][object_is_current', 'At least one member of the relationship must be
      the current ' . $details['nodetype'] . '. This is specified by checking the "Current ' . $details['nodetype'] . '"
      checkbox for either the subject or object.');
  }
  // Only one of the participants may be the current node.
  // We do not support circular relationships.
  if ($form_state['values']['relationship_table']['new']['subject_is_current']
   AND $form_state['values']['relationship_table']['new']['object_is_current']) {

    form_set_error('relationship_table][new][object_is_current', 'Only one member of the relationship may be
      the current ' . $details['nodetype'] . ' in order to avoid a circular relationship. If you really meant to
      add a circular relationship then check the "Current ' . $details['nodetype'] . '" for one side of the
      relationship and enter the current stock name via the autocomplete for the other side of the relationship.');
  }
  // If it was determined current via checkbox, we need to ensure the name
  // provided actually matches the current node.
  if ($form_state['values']['relationship_table']['new']['subject_is_current']
   AND !empty($form_state['values']['relationship_table']['new']['subject_name'])
     AND $form_state['values']['relationship_table']['new']['subject_name'] != $form_state['values'][$details['form_element_name']]) {

    form_set_error('relationship_table][new][subject_name', 
    'The name you supplied for the current ' . $details['nodetype'] . ' doesn\'t match the actual name
      of this ' . $details['nodetype'] . '. If you really meant the subject should be the current ' . $details['nodetype'] . ',
      simply leave the textfield empty and re-add this relationship.');
  }
  if ($form_state['values']['relationship_table']['new']['object_is_current']
   AND !empty($form_state['values']['relationship_table']['new']['object_name'])
     AND $form_state['values']['relationship_table']['new']['object_name'] != $form_state['values'][$details['form_element_name']]) {

    form_set_error('relationship_table][new][object_name', 
    'The name you supplied for the current ' . $details['nodetype'] . ' doesn\'t match the actual name
      of this ' . $details['nodetype'] . '. If you really meant the object should be the current ' . $details['nodetype'] . ',
      simply leave the textfield empty and re-add this relationship.');
  }


  // The non-current uniquename must be exist in the base table (subject).
  if (!($form_state['values']['relationship_table']['new']['subject_is_current'])) {
    $result = chado_select_record(
    $details['base_table'], 
    array($details['base_name_field']), 
    array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['subject_id'])
    );
    if (!isset($result[0])) {
      form_set_error('relationship_table][new][subject_name', 'The subject must be the unique name of an
        existing ' . $details['nodetype'] . ' unless the "Current ' . $details['nodetype'] . '" checkbox is selected');
    }
    else {
      $form_state['values']['relationship_table']['new']['subject_name'] = $result[0]->{$details['base_name_field']};
    }
  }

  // The non-current uniquename must exist in the base table (object).
  if (!($form_state['values']['relationship_table']['new']['object_is_current'])) {
    $result = chado_select_record(
    $details['base_table'], 
    array($details['base_name_field']), 
    array($details['base_foreign_key'] => $form_state['values']['relationship_table']['new']['object_id'])
    );
    if (!isset($result[0])) {
      form_set_error('relationship_table][new][object_name', 'The object must be the unique name of an
        existing ' . $details['nodetype'] . ' unless the "Current ' . $details['nodetype'] . '" checkbox is selected');
    }
    else {
      $form_state['values']['relationship_table']['new']['object_name'] = $result[0]->{$details['base_name_field']};
    }
  }

  // The type must be a valid cvterm.
  if ($form_state['values']['relationship_table']['new']['type_name']) {
    $form_state['values']['relationship_table']['new']['type_id'] = $form_state['values']['relationship_table']['new']['type_name'];
    $result = chado_select_record(
    'cvterm', 
    array('name'), 
    array('cvterm_id' => $form_state['values']['relationship_table']['new']['type_id'])
    );
    if (!isset($result[0])) {
      form_set_error('relationship_table][new][type_id', 'The select type is not a valid controlled vocabulary term.');
    }
    else {
      $form_state['values']['relationship_table']['new']['type_name'] = $result[0]->name;
    }
  }
  else {
    form_set_error('relationship_table][new][type_id', 'Please select a type of relationship');
  }
}