You are here

function library_transaction_form_validate in Library 7

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. 6 library.pages.inc \library_transaction_form_validate()

Handles transaction form submissions.

Parameters

array $form: A regular form array (unused).

array $form_state: A regular form_state array, modifiable.

See also

library_transaction_form()

1 string reference to 'library_transaction_form_validate'
library_transaction_form in ./library.pages.inc
The library transaction form.

File

./library.pages.inc, line 162
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;
  $autocomplete_disabled = variable_get('library_disable_patron_autocomplete', 0) == 1;

  // Validate patron information.
  if ($form_state['values']['patron_uid']) {
    if (isset($form_state['values']['patron_uid_set']) && is_numeric($form_state['values']['patron_uid'])) {
      $uid = $form_state['values']['patron_uid'];
      $patron = user_load($uid);
      if (empty($patron)) {
        form_set_error('patron_uid', t('Invalid Patron User ID.'));
      }
    }
    elseif ($autocomplete_disabled && !empty($form_state['values']['patron_uid'])) {
      $patron = user_load_by_name($form_state['values']['patron_uid']);
      if (empty($patron)) {
        form_set_error('patron_uid', t('Library Patron: Could not find a corresponding Drupal user.'));
      }
    }
    else {
      preg_match('/^(?:\\s*|(.*) )?\\[\\s*uid\\s*:\\s*(\\d+)\\s*\\]$/', $form_state['values']['patron_uid'], $matches);
      if (!empty($matches)) {

        // The explicit uid is matched.
        list($temp, $name, $uid) = $matches;
        $patron = user_load($uid);
        if (empty($patron)) {
          form_set_error('patron_uid', t('Library Patron: Could not find a corresponding Drupal user.'));
        }
      }
    }
    if (!empty($patron)) {
      $form_state['values']['patron_email'] = $patron->mail;
      $form_state['values']['patron_uid'] = $patron->uid;
      $form_state['values']['patron_name'] = $patron->name;
    }
    else {
      form_set_error('patron_uid', t('You must specify a library patron.'));
    }
  }
  else {
    form_set_error('patron_uid', 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 (is_numeric($form_state['values']['action_aid'])) {
      $action = library_get_action($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_ACTION_TYPE_AVAILABLE) {
        form_set_error('action_aid', t('This item is already available.'));
      }
      elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $action->status_change == LIBRARY_ACTION_TYPE_UNAVAILABLE) {
        form_set_error('action_aid', t('This item is not currently available.'));
      }
      elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $patron->uid != $item->last_patron_uid) {
        form_set_error('patron_uid', t('This item was made unavailable by a different patron.  That patron must release the item first.'));
      }
    }
  }
}