You are here

function library_transaction_form_validate in Library 6

Same name and namespace in other branches
  1. 5.2 library.pages.inc \library_transaction_form_validate()
  2. 6.2 library.pages.inc \library_transaction_form_validate()
  3. 7 library.pages.inc \library_transaction_form_validate()

Handles transaction form submissions.

See also

library_transaction_form()

1 string reference to 'library_transaction_form_validate'
library_transaction_form in ./library.pages.inc
@file Functions for generating page displays related to the library module

File

./library.pages.inc, line 135
Functions for generating page displays related to the library module

Code

function library_transaction_form_validate($form, &$form_state) {
  $barcodes = variable_get('library_item_barcodes', LIBRARY_NO_BARCODES) == LIBRARY_BARCODES;

  //Validate patron information
  if ($form_state['values']['patron_id']) {
    if ($form_state['values']['patron_id_set'] && is_numeric($form_state['values']['patron_id'])) {
      $nid = $form_state['values']['patron_id'];
      $n_patron = node_load($nid);
      if (empty($n_patron)) {
        form_set_error('patron_id', t('Invalid Patron ID.'));
      }
    }
    else {
      preg_match('/^(?:\\s*|(.*) )?\\[\\s*nid\\s*:\\s*(\\d+)\\s*\\]$/', $form_state['values']['patron_id'], $matches);
      if (!empty($matches)) {

        // explicit nid
        list($temp, $title, $nid) = $matches;
        if (!empty($title) && ($n_patron = node_load($nid))) {
          if ($title != $n_patron->title) {
            form_set_error('patron_id', t('Library Patron: Name mismatch. Please check your selection.'));
          }
        }
      }
    }
    if (!empty($n_patron)) {
      $form_state['values']['patron_email'] = $n_patron->email;
      $form_state['values']['patron_uid'] = $n_patron->patron_uid;
      $form_state['values']['patron_id'] = $n_patron->nid;
      $form_state['values']['patron_name'] = $n_patron->title;
      $form_state['values']['patron_name_last'] = $n_patron->name_last;
      $form_state['values']['patron_name_first'] = $n_patron->name_first;
    }
    else {
      form_set_error('patron_id', t('You must specify a library patron.'));
    }
  }
  else {
    form_set_error('patron_id', t('You must specify a library patron.'));
  }
  if ($form_state['values']['item_id']) {
    if ($form_state['values']['item_id_set'] && is_numeric($form_state['values']['item_id'])) {
      $item = library_load($form_state['values']['item_id']);
    }
    elseif ($barcodes) {
      $item = library_get_item_by_barcode(check_plain($form_state['values']['item_id']));
    }
    else {
      preg_match('/^(?:\\s*|(.*))?\\[\\s*id\\s*:\\s*(\\d+)\\s*\\]$/', $form_state['values']['item_id'], $matches);
      if (!empty($matches)) {
        list($temp, $title, $id) = $matches;
        $item = library_load($id);
        if (isset($item) && !empty($title) && strcasecmp(rtrim(check_plain($title)), rtrim(check_plain($item->title))) != 0 || empty($item)) {
          form_set_error('item_id', t('Library Item : Title mismatch. Please check your selection.'));
        }
      }
      else {
        form_set_error('item_id', t('You must specify a valid library item.'));
      }
    }
    if (!empty($item)) {
      $form_state['values']['item_id'] = $item->id;
      $form_state['values']['item_name'] = $item->title;
      $form_state['values']['nid'] = $item->nid;
      $form_state['values']['barcode'] = $item->barcode;
      $form_state['values']['node_type'] = $item->type;
    }
    if ($form_state['values']['action_aid']) {
      if ($item->in_circulation == LIBRARY_REFERENCE_ONLY && $action->status_change != LIBRARY_ACTION_NO_CHANGE) {
        form_set_error('action_aid', t('This item is not in circulation.'));
      }
      elseif ($item->library_status == LIBRARY_ITEM_AVAILABLE && $action->status_change == LIBRARY_ITEM_TYPE_AVAILABLE) {
        form_set_error('action_aid', t('This item is already available.'));
      }
      elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $action->status_change == LIBRARY_ITEM_TYPE_UNAVAILABLE) {
        form_set_error('action_aid', t('This item is not currently available.'));
      }
      elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $n_patron->nid != $item->last_patron_id) {
        form_set_error('patron_id', t('This item was made unavailable by a different patron.  Only that patron can perform actions on it.'));
      }
    }
  }
}