function chado_stock_validate

2.x tripal_stock.chado_node.inc chado_stock_validate(&$node, $form, &$form_state)
3.x tripal_stock.chado_node.inc chado_stock_validate(&$node, $form, &$form_state)
1.x tripal_stock.module chado_stock_validate($node, &$form)

Implements hook_validate(): Validate the input from the chado_stock node form

Parameters

$node: The current node including fields with the form element names and submitted values

$form: A description of the form to be rendered by drupal_get_form()

Related topics

File

tripal_stock/tripal_stock.module, line 565
Implements Tripal Stock Module hooks

Code

function chado_stock_validate($node, &$form) {

  $int_in_chado_sql = "SELECT count(*) as count FROM {%s} WHERE %s=%d";
  $string_in_chado_sql = "SELECT count(*) as count FROM {%s} WHERE %s='%s'";

  // if this is an update, we want to make sure that a different stock for
  // the organism doesn't already have this uniquename. We don't want to give
  // two sequences the same uniquename
  if ($node->stock_id) {
    $sql = "SELECT *
            FROM {stock} S
              INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
            WHERE uniquename = '%s'
             AND organism_id = %d AND CVT.name = '%s' AND NOT stock_id = %d";
    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->stock_type, $node->stock_id));
    if ($result) {
      form_set_error('uniquename', t("Stock update cannot proceed. The stock name '$node->uniquename' is not unique for this organism. Please provide a unique name for this stock."));
    }
  }

  // if this is an insert then we just need to make sure this name doesn't
  // already exist for this organism if it does then we need to throw an error
  else {
    $sql = "SELECT *
            FROM {Stock} S
              INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
            WHERE uniquename = '%s'
             AND organism_id = %d AND CVT.name = '%s'";
    $result = db_fetch_object(chado_query($sql, $node->uniquename, $node->organism_id, $node->stock_type));
    if ($result) {
      form_set_error('uniquename', t("Stock insert cannot proceed. The stock name '$node->uniquename' already exists for this organism. Please provide a unique name for this stock."));
    }
  }


  // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  if ($node->type_id == 0) {
    form_set_error('type_id', 'Please select a type of stock.');
  }
  else {
    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
    if ($num_rows->count != 1) {
      form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)");
    }
  }

  // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  if ($node->organism_id == 0) {
    form_set_error('organism_id', 'Please select a source organism for this stock');
  }
  else {
    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
    if ($num_rows->count != 1) {
      form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)");
    }
  }

  // Check if Accession also database
  if ($node->accession != '') {
    if ($node->database == 0) {
      // there is an accession but no database selected
      form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
    }
  }
  else {
    if ($node->database > 0) {
      // there is a database selected but no accession
      form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
    }
  }

  // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  if ($node->database > 0) {
    $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'db', 'db_id', $node->database));
    if ($num_rows->count != 1) {
      form_set_error('database', 'The database you selected is not valid. Please choose another one.');
    }
  }
}