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/includes/tripal_stock.chado_node.inc, line 330
Stock Node Functionality

Code

function chado_stock_validate(&$node, $form, &$form_state) {

  // We only want to validate when the node is saved.
  // Since this validate can be called on AJAX and Deletion of the node
  // we need to make this check to ensure queries are not executed
  // without the proper values.
  if (property_exists($node, "op") and $node->op != 'Save') {
    return;
  }

  // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  // need to validate during syncing so just skip it.
  if (!property_exists($node, 'nid') and property_exists($node, 'stock_id') and $node->stock_id != 0) {
    return;
  }

  // remove surrounding whitespace
  $node->uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
  $node->sname = property_exists($node, 'sname') ? trim($node->sname) : '';
  $node->accession = property_exists($node, 'accession') ? trim($node->accession) : '';
  $node->db_description = property_exists($node, 'db_description') ? trim($node->db_description) : '';

  $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";

  // 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 (property_exists($node, 'nid') and property_exists($node, 'stock_id')) {
    $sql = "
      SELECT *
      FROM {stock} S
        INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
      WHERE
        uniquename = :uname AND organism_id = :organism_id AND
        CVT.name = :cvtname AND NOT stock_id = :stock_id
    ";
    $result = chado_query($sql, array(':uname' => $node->uniquename,
      ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
      ':stock_id' => $node->stock_id))->fetchObject();
    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
  elseif (!empty($node->organism_id) AND !empty($node->type_id)) {
    $sql = "
      SELECT *
      FROM {stock} S
        INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
      WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
    $result = chado_query($sql, array(':uname' => $node->uniquename,
      ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
    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 {
    $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
    $new_sql = str_replace(array_keys($replace), $replace, $int_in_chado_sql);
    $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
    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 {
    $replace = array(':table' => 'organism', ':column' => 'organism_id');
    $new_sql = str_replace(array_keys($replace), $replace, $int_in_chado_sql);
    $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
    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) {
    $replace = array(':table' => 'db', ':column' => 'db_id');
    $new_sql = str_replace(array_keys($replace), $replace, $int_in_chado_sql);
    $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
    if ($num_rows->count != 1) {
      form_set_error('database', 'The database you selected is not valid. Please choose another one.');
    }
  }
}