You are here

function library_transaction_form_submit in Library 7

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

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_submit'
library_transaction_form in ./library.pages.inc
The library transaction form.

File

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

Code

function library_transaction_form_submit($form, &$form_state) {
  $now = REQUEST_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_insert('library_transactions')
    ->fields(array(
    'nid' => $form_state['values']['nid'],
    'item_id' => $form_state['values']['item_id'],
    'uid' => $form_state['values']['patron_uid'],
    'action_aid' => $form_state['values']['action_aid'],
    'duedate' => $duedate,
    'notes' => check_plain($form_state['values']['notes']),
    'created' => $now,
  ))
    ->execute();
  if ($form_state['values']['action_status_change'] > LIBRARY_ACTION_NO_CHANGE) {
    db_update('library')
      ->fields(array(
      'library_status' => $new_status,
    ))
      ->condition('id', $form_state['values']['item_id'])
      ->execute();
  }
  drupal_set_message(t('The @action was successful.', array(
    '@action' => $form_state['values']['action_name'],
  )));
  if (module_exists('trigger')) {
    $object = NULL;
    $context = array();
    $context['group'] = 'library';
    $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' => $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(
      'name' => $form_state['values']['patron_name'],
      'mail' => $form_state['values']['patron_email'],
      'uid' => $form_state['values']['patron_uid'],
    );
    $aids = trigger_get_assigned_actions('library_after_' . $clean);
    if (!empty($aids)) {
      $context['hook'] = 'library_after_' . $clean;
      actions_do(array_keys($aids), $object, $context);
    }
  }
  $form_state['redirect'] = 'node/' . $form_state['values']['nid'];
}