function chado_feature_validate

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

Implementation of hook_validate().

This validation is being used for three activities: CASE A: Update a node that exists in both drupal and chado CASE B: Synchronizing a node from chado to drupal CASE C: Inserting a new node that exists in niether drupal nor chado

Related topics

File

tripal_feature/includes/tripal_feature.chado_node.inc, line 261
Implementation of hooks to create a feature content type

Code

function chado_feature_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 feature_id. We don't
  // need to validate during syncing so just skip it.
  if (!property_exists($node, 'nid') and property_exists($node, 'feature_id') and $node->feature_id != 0) {
    return;
  }

  // remove surrounding white-space on submitted values
  $node->uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
  $node->fname = property_exists($node, 'fname') ? trim($node->fname) : '';
  $node->feature_type = property_exists($node, 'feature_type') ? trim($node->feature_type) : '';
  $node->residues = property_exists($node, 'residues') ? trim($node->residues) : '';

  // Validating for an update
  if (property_exists($node, 'nid')) {

    // make sure the feature type is a real sequence ontology term
    $type = tripal_get_cvterm(array(
      'name' => $node->feature_type,
      'cv_id' => array('name' => 'sequence')
    ));
    if (!$type) {
      form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
    }

    // if this is an update, we want to make sure that a different feature for
    // the organism doesn't already have this uniquename. We don't want to give
    // two sequences the same uniquename
    if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
      $sql = "
        SELECT *
        FROM {feature} F
          INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
        WHERE
          F.uniquename     = :uname AND
          F.organism_id    = :organism_id AND
          CVT.name         = :cvtname AND
          NOT f.feature_id = :feature_id
      ";
      $args = array(':uname' => $node->uniquename, ':organism_id' => $node->organism_id,
        ':cvtname' => $node->feature_type, ':feature_id' => $node->feature_id);
      $result = chado_query($sql, $args)->fetchObject();
      if ($result) {
        form_set_error('uniquename', t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature."));
      }
    }
  }
  // Validating for an insert
  else {

    // make sure the feature type is a real sequence ontology term
    $type = tripal_get_cvterm(array(
      'name' => $node->feature_type,
      'cv_id' => array('name' => 'sequence')
    ));
    if (!$type) {
      form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
    }

    // 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
    $sql = "
      SELECT *
      FROM {feature} F
        INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
      WHERE
        F.uniquename  = :name AND
        F.organism_id = :organism_id AND
        CVT.name      = :cvtname
    ";
    $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);

    $result = chado_query($sql, $args)->fetchObject();
    if ($result) {
      form_set_error('uniquename', t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature."));
    }
  }
}