You are here

function library_transaction_form_submit in Library 6

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

Handles transaction form submissions.

See also

library_transaction_form()

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

File

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

Code

function library_transaction_form_submit($form, &$form_state) {
  $now = time();
  $duedate = NULL;
  $clean = library_clean_action_name($form_state['values']['action_name']);
  if ($form_state['values']['action_status_change'] == LIBRARY_ACTION_TYPE_UNAVAILABLE) {
    $type = $form_state['values']['node_type'];
    $period = variable_get('library_period_for_' . $type . '_' . $clean, 0);
    if ($period > 0) {
      $duedate = library_get_due_date($now, $clean, $type);
    }
    $new_status = LIBRARY_ITEM_UNAVAILABLE;
  }
  elseif ($form_state['values']['action_status_change'] == LIBRARY_ACTION_TYPE_AVAILABLE) {
    $new_status = LIBRARY_ITEM_AVAILABLE;
  }
  db_query("INSERT {library_transactions} (nid, item_id, patron_id, action_aid, duedate, notes, created) VALUES (%d, %d, %d, %d, %d, '%s', %d)", $form_state['values']['nid'], $form_state['values']['item_id'], $form_state['values']['patron_id'], $form_state['values']['action_aid'], $duedate, check_plain($form_state['values']['notes']), $now);
  if ($form_state['values']['action_status_change'] > LIBRARY_ACTION_NO_CHANGE) {
    db_query("UPDATE {library} set library_status = %d WHERE id = %d", $new_status, $form_state['values']['item_id']);
  }
  drupal_set_message(t('The ' . $form_state['values']['action_name'] . ' was successful.'));
  if (module_exists('trigger')) {
    $transaction_id = db_last_insert_id('library_transactions', 'tid');
    $context = array();
    $object = (object) $context;
    $context['item'] = array(
      'id' => $form_state['values']['item_id'],
      'nid' => $form_state['values']['nid'],
      'title' => $form_state['values']['item_name'],
      'barcode' => $form_state['values']['barcode'],
      'node_type' => $form_state['values']['node_type'],
    );
    $context['transaction'] = array(
      'id' => $transaction_id,
      'action_name' => $form_state['values']['action_name'],
      'aid' => $form_state['values']['action_aid'],
      'notes' => check_plain($form_state['values']['notes']),
      'duedate' => $duedate,
      'created' => $now,
    );
    $context['patron'] = array(
      'nid' => $form_state['values']['patron_id'],
      'name' => $form_state['values']['patron_name_first'] . ' ' . $form_state['values']['patron_name_last'],
      'email' => $form_state['values']['patron_email'],
      'uid' => $form_state['values']['patron_uid'],
    );
    $actions = _library_get_hook_aids('library', 'after_' . $clean);
    if (!empty($actions)) {
      actions_do($actions, $object, $context);
    }
  }
  $form_state['redirect'] = 'node/' . $form_state['values']['nid'];
  return;
}