function tripal_analysis_validate

2.x tripal_analysis.chado_node.inc tripal_analysis_validate($node, $form, &$form_state)
3.x tripal_analysis.chado_node.inc tripal_analysis_validate($node, $form, &$form_state)
1.x tripal_analysis.form.inc tripal_analysis_validate($node, &$form)

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

1 call to tripal_analysis_validate()
chado_analysis_validate in tripal_analysis/includes/tripal_analysis.chado_node.inc
Implements hook_validate(). Validates the user input before creating an analysis node

File

tripal_analysis/includes/tripal_analysis.chado_node.inc, line 304
Implements Drupal Node hooks to create the chado_analysis node content type.

Code

function tripal_analysis_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 ($node->op != 'Save') {
    return;
  }

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

  // remove surrounding white-space on submitted values
  $node->analysisname = trim($node->analysisname);
  $node->program = trim($node->program);
  $node->programversion = trim($node->programversion);
  $node->algorithm = trim($node->algorithm);
  $node->sourcename = trim($node->sourcename);
  $node->sourceversion = trim($node->sourceversion);
  $node->sourceuri = trim($node->sourceuri);

  // Validating for an update
  if (!is_null($node->nid)) {

    // get the existing node
    $values = array('analysis_id' => $node->analysis_id);
    $result = chado_select_record('analysis', array('*'), $values);
    $analysis = $result[0];

    // if the name has changed make sure it doesn't conflict with an existing name
    if ($analysis->name != $node->analysisname) {
      $values = array('name' => $node->analysisname);
      $result = chado_select_record('analysis', array('analysis_id'), $values);
      if ($result and count($result) > 0) {
        form_set_error('analysisname', 'Cannot update the analysis with this analysis name. An analysis with this name already exists.');
        return;
      }
    }

    // if the unique constraint has changed check to make sure it doesn't conflict with an
    // existing record
    if ($analysis->program != $node->program or $analysis->programversion != $node->programversion or 
      $analysis->sourcename != $node->sourcename) {
      $values = array(
        'program' => $node->program,
        'programversion' => $node->programversion,
        'sourcename' => $node->sourcename,
      );
      $result = chado_select_record('analysis', array('analysis_id'), $values);
      if ($result and count($result) > 0) {
        if ($analysis->program != $node->program) {
          $field = 'program';
        }
        if ($analysis->programversion != $node->programversion) {
          $field = 'programversion';
        }
        if ($analysis->sourcename != $node->sourcename) {
          $field = 'sourcename';
        }
        form_set_error($field, 'Cannot update the analysis with this program,
          program version and source name. An analysis with these values already exists.');
        return;
      }
    }
  }
  // Validating for an insert
  else {
    $values = array(
      'program' => $node->program,
      'programversion' => $node->programversion,
      'sourcename' => $node->sourcename,
    );
    $analysis = chado_select_record('analysis', array('analysis_id'), $values);
    if ($analysis and count($analysis) > 0) {
      form_set_error('program', 'Cannot add the analysis with this program,
        program version and source name. An analysis with these values already exists.');
      return;
    }

    // make sure we have a unique analysis name. This is not a requirement
    // for the analysis table but we use the analysis name for the Drupal node
    // title, so it should be unique
    $values = array('name' => $node->analysisname);
    $result = chado_select_record('analysis', array('analysis_id'), $values);
    if ($result and count($result) > 0) {
      form_set_error('analysisname', 'Cannot add the analysis with this analysis name. An analysis with this name already exists.');
      return;
    }
  }
}