function chado_pub_validate

2.x tripal_pub.chado_node.inc chado_pub_validate($node, $form, &$form_state)
3.x tripal_pub.chado_node.inc chado_pub_validate($node, $form, &$form_state)
1.x pub_form.inc chado_pub_validate($node, &$form)

Implements hook_validate().

Related topics

File

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

Code

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

  // get the submitted values
  $title = property_exists($node, 'pubtitle') ? trim($node->pubtitle) : '';
  $pyear = property_exists($node, 'pyear') ? trim($node->pyear) : '';
  $uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
  $is_obsolete = property_exists($node, 'is_obsolete') ? trim($node->is_obsolete) : 0;
  $type_id = property_exists($node, 'type_id') ? trim($node->type_id) : '';

  $pub = array();

  // make sure the year is four digits
  if (!preg_match('/^\d{4}$/', $pyear)) {
    form_set_error('pyear', t('The publication year should be a 4 digit year.'));
    return;
  }

  // get the type of publication
  $values = array('cvterm_id' => $type_id);
  $cvterm = chado_select_record('cvterm', array('name'), $values);
  if (count($cvterm) == 0) {
    $message = t('Invalid publication type.');
    form_set_error('type_id', $message);
    return;
  }

  // Get the media name looking at the properties
  $series_name = '';
  $properties = chado_retrieve_node_form_properties($node);
  foreach ($properties as $key => $prop_values) {
    $values = array('cvterm_id' => $key);
    $prop_type = chado_select_record('cvterm', array('name'), $values);
    if ($prop_type[0]->name == 'Conference Name' or 
      $prop_type[0]->name == 'Journal Name' or 
      $prop_type[0]->name == 'Series Name') {
      $v = array_values($prop_values);
      $series_name = $v[0];
    }
    if ($prop_type[0]->name == 'Citation') {
      $v = array_values($prop_values);
      $uniquename = $v[0];
    }
    if (count($prop_values) == 1) {
      $v = array_values($prop_values);
      $pub[$prop_type[0]->name] = $v[0];
    }
    else {
      $pub[$prop_type[0]->name] = $prop_values;
    }
  }
  // if the citation is missing then try to generate one
  if (!$uniquename) {
    $pub['Title'] = $title;
    $pub['Publication Type'][0] = $cvterm[0]->name;
    $pub['Year'] = $pyear;
    $uniquename = tripal_pub_create_citation($pub);
    if (!$uniquename) {
      form_set_error('uniquename', "Cannot automatically generate a citation '.
          'for this publication type. Please add one manually.");
    }
  }

  $skip_duplicate_check = 0;

  // if this publication is a Patent then skip the validation below.  Patents can have the title
  // name and year but be different
  if (strcmp($cvterm[0]->name, 'Patent') == 0) {
    $skip_duplicate_check = 1;
  }

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

    $pub_id = $node->pub_id;

    // check to see if a duplicate publication already exists
    if (!$skip_duplicate_check) {
      chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0], $pub_id);
    }
    chado_pub_validate_check_uniquename($uniquename, $pub_id);
  }
  // Validating for an insert
  else {
    chado_pub_validate_check_duplicate($title, $pyear, $series_name, $cvterm[0]);
    chado_pub_validate_check_uniquename($uniquename);
  }
}