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.form.inc
Validates the user input before creating an analysis node

File

tripal_analysis/includes/tripal_analysis.form.inc, line 233

Code

function tripal_analysis_validate($node, &$form) {


  // Only nodes being updated will have an nid already
  if (!is_null($node->nid)) {
    // CASE A: We are validating a form for updating an existing node

    // get the existing node    
    $values = array('analysis_id' => $node->analysis_id);
    $result = tripal_core_chado_select('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 = tripal_core_chado_select('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 = tripal_core_chado_select('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;
      }
    }
  }
  else {
    // To differentiate if we are syncing or creating a new analysis altogther, see if an
    // analysis_id already exists
    if ($node->analysis_id and $node->analysis_id != 0) {
      // CASE B: Synchronizing a node from chado to drupal
      // we don't need to do anything.
    }
    else {
      // CASE C: We are validating a form for inserting a new node
      // The unique constraint for the chado analysis table is: program, programversion, sourcename
      $values = array(
        'program' => $node->program,
        'programversion' => $node->programversion,
        'sourcename' => $node->sourcename,
      );
      $analysis = tripal_core_chado_select('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 = tripal_core_chado_select('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;
      }
    }
  }
}