You are here

function patron_validate in Library 6

Same name and namespace in other branches
  1. 5.2 patron/patron.module \patron_validate()

Implementation of hook_validate().

File

patron/patron.module, line 315

Code

function patron_validate(&$node) {

  //Validate email
  if (!empty($node->email)) {
    if (!valid_email_address($node->email)) {
      form_set_error('email', t('Invalid email address.'));
    }
    else {
      $result = db_result(db_query_range("SELECT nid from {library_patrons} where email= '%s' and nid <> '%d'", $node->email, $node->nid, 0, 1));
      if ($result) {
        form_set_error('email', t("Duplicate email address. " . l('View existing entry with this email address.', 'node/' . $result)));
      }
    }
  }

  //Validate identifier
  $dup_result = db_result(db_query_range("SELECT nid FROM {node} WHERE title= '%s' AND nid <> %d AND type='patron'", $node->title, $node->nid, 0, 1));
  if ($dup_result) {
    form_set_error('title', t("A Patron with that identifier already exists. " . l('View existing entry.', 'node/' . $dup_result)));
  }

  //Validate that barcode is unique
  if (variable_get('patron_use_barcodes', PATRON_NO_BARCODES) == PATRON_BARCODES) {
    if (!empty($node->barcode)) {
      $node->barcode = check_plain($node->barcode);
      $not_unique = db_result(db_query("SELECT COUNT(*) FROM {library_patrons} WHERE barcode = '%s' AND barcode <> '' AND nid <> %d", $node->barcode, $node->nid));
      if ($result) {
        form_set_error('barcode', t('The barcode %barcode already exists. Please enter a different barcode.', array(
          '%barcode' => $node->barcode,
        )));
      }
    }
  }
}