| 1 |
<?php
|
| 2 |
// $Id:
|
| 3 |
|
| 4 |
function library_transaction_form(&$form_state, $aid, $item = NULL) {
|
| 5 |
$patron_id = arg(4);
|
| 6 |
|
| 7 |
if ($patron_id) {
|
| 8 |
$patron = node_load($patron_id);
|
| 9 |
}
|
| 10 |
else if (!user_access('view patron content')) {
|
| 11 |
global $user;
|
| 12 |
$user_patron = patron_load_by_uid($user->uid);
|
| 13 |
if (is_object($user_patron)) {
|
| 14 |
$patron = $user_patron;
|
| 15 |
}
|
| 16 |
else {
|
| 17 |
drupal_set_message('You do not have sufficient permissions.', 'error');
|
| 18 |
drupal_goto('/');
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
if ($aid) {
|
| 23 |
$action = library_get_action($aid);
|
| 24 |
if ($action->name) {
|
| 25 |
drupal_set_title($action->name);
|
| 26 |
}
|
| 27 |
else {
|
| 28 |
drupal_set_message('Invalid action.', 'error');
|
| 29 |
drupal_goto('');
|
| 30 |
}
|
| 31 |
}
|
| 32 |
else {
|
| 33 |
drupal_set_message('No action was selected.', 'error');
|
| 34 |
drupal_goto('');
|
| 35 |
}
|
| 36 |
|
| 37 |
$form = array();
|
| 38 |
|
| 39 |
$form['action_aid'] = array(
|
| 40 |
'#type' => 'value',
|
| 41 |
'#value' => $action->aid,
|
| 42 |
);
|
| 43 |
$form['action_name'] = array(
|
| 44 |
'#type' => 'value',
|
| 45 |
'#value' => $action->name,
|
| 46 |
);
|
| 47 |
$form['action_status_change'] = array(
|
| 48 |
'#type' => 'value',
|
| 49 |
'#value' => $action->status_change,
|
| 50 |
);
|
| 51 |
|
| 52 |
if ($item) {
|
| 53 |
$form['item_id'] = array(
|
| 54 |
'#type' => 'value',
|
| 55 |
'#value' => $item->id,
|
| 56 |
);
|
| 57 |
$form['item_id_set'] = array(
|
| 58 |
'#type' => 'value',
|
| 59 |
'#value' => $item->id,
|
| 60 |
);
|
| 61 |
$form['item_display'] = array(
|
| 62 |
'#type' => 'item',
|
| 63 |
'#title' => t('Item'),
|
| 64 |
'#value' => $item->title,
|
| 65 |
);
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$form['item_id'] = library_autocomplete_input($item);
|
| 69 |
}
|
| 70 |
|
| 71 |
if ($patron) {
|
| 72 |
$form['patron_id'] = array(
|
| 73 |
'#type' => 'value',
|
| 74 |
'#value' => $patron->nid,
|
| 75 |
);
|
| 76 |
$form['patron_id_set'] = array(
|
| 77 |
'#type' => 'value',
|
| 78 |
'#value' => $patron->nid,
|
| 79 |
);
|
| 80 |
$form['patron_display'] = array(
|
| 81 |
'#type' => 'item',
|
| 82 |
'#value' => $patron->name_first .' '. $patron->name_last,
|
| 83 |
'#title' => t('Patron'),
|
| 84 |
);
|
| 85 |
if (user_access('view patron content') && $action->status_change == LIBRARY_ACTION_NO_CHANGE) {
|
| 86 |
$link = 'library-items/transaction/'. $action->aid;
|
| 87 |
if ($item) {
|
| 88 |
$link .= '/'. $item->id;
|
| 89 |
}
|
| 90 |
$form['patron_change_link'] = array(
|
| 91 |
'#type' => 'item',
|
| 92 |
'#value' => 'Change Patron',
|
| 93 |
'#prefix' => '<a href="'. url($link) .'">',
|
| 94 |
'#suffix' => '</a>',
|
| 95 |
);
|
| 96 |
}
|
| 97 |
}
|
| 98 |
else {
|
| 99 |
$form['patron_id'] = array(
|
| 100 |
'#type' => 'textfield',
|
| 101 |
'#title' => t('Patron'),
|
| 102 |
'#default_value' => ($patron ? $patron->title .' [nid:'. $patron->nid .']' : ''),
|
| 103 |
'#autocomplete_path' => 'patrons/autocomplete',
|
| 104 |
'#required' => TRUE
|
| 105 |
);
|
| 106 |
}
|
| 107 |
|
| 108 |
$form['notes'] = array(
|
| 109 |
'#type' => 'textarea',
|
| 110 |
'#title' => t('Message'),
|
| 111 |
'#required' => FALSE,
|
| 112 |
'#maxlength' => 250,
|
| 113 |
'#default_value' => '',
|
| 114 |
'#description' => t('If you are reserving an item, make sure to include the date and time you would like it to be ready.'),
|
| 115 |
);
|
| 116 |
$form['submit'] = array(
|
| 117 |
'#type' => 'submit',
|
| 118 |
'#value' => t($action->name),
|
| 119 |
);
|
| 120 |
$form['#validate'][] = 'library_transaction_form_validate';
|
| 121 |
$form['#submit'][] = 'library_transaction_form_submit';
|
| 122 |
|
| 123 |
return $form;
|
| 124 |
}
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Handles transaction form submissions.
|
| 128 |
*
|
| 129 |
* @see library_transaction_form()
|
| 130 |
*/
|
| 131 |
function library_transaction_form_validate($form, &$form_state) {
|
| 132 |
$barcodes = variable_get('library_item_barcodes', LIBRARY_NO_BARCODES) == LIBRARY_BARCODES;
|
| 133 |
|
| 134 |
//Validate patron information
|
| 135 |
if ($form_state['values']['patron_id']) {
|
| 136 |
if ($form_state['values']['patron_id_set'] && is_numeric($form_state['values']['patron_id'])) {
|
| 137 |
$nid = $form_state['values']['patron_id'];
|
| 138 |
$n_patron = node_load($nid);
|
| 139 |
if (empty($n_patron)) {
|
| 140 |
form_set_error('patron_id', t('Invalid Patron ID.'));
|
| 141 |
}
|
| 142 |
}
|
| 143 |
else {
|
| 144 |
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $form_state['values']['patron_id'], $matches);
|
| 145 |
if (!empty($matches)) {
|
| 146 |
// explicit nid
|
| 147 |
list($temp, $title, $nid) = $matches;
|
| 148 |
if (!empty($title) && ($n_patron = node_load($nid))) {
|
| 149 |
if ($title != $n_patron->title) {
|
| 150 |
form_set_error('patron_id', t('Library Patron: Name mismatch. Please check your selection.'));
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|
| 155 |
if (!empty($n_patron)) {
|
| 156 |
$form_state['values']['patron_email'] = $n_patron->email;
|
| 157 |
$form_state['values']['patron_uid'] = $n_patron->patron_uid;
|
| 158 |
$form_state['values']['patron_id'] = $n_patron->nid;
|
| 159 |
$form_state['values']['patron_name'] = $n_patron->title;
|
| 160 |
$form_state['values']['patron_name_last'] = $n_patron->name_last;
|
| 161 |
$form_state['values']['patron_name_first'] = $n_patron->name_first;
|
| 162 |
}
|
| 163 |
else {
|
| 164 |
form_set_error('patron_id', t('You must specify a library patron.'));
|
| 165 |
}
|
| 166 |
}
|
| 167 |
else {
|
| 168 |
form_set_error('patron_id', t('You must specify a library patron.'));
|
| 169 |
}
|
| 170 |
|
| 171 |
if ($form_state['values']['item_id']) {
|
| 172 |
if ($form_state['values']['item_id_set'] && is_numeric($form_state['values']['item_id'])) {
|
| 173 |
$item = library_load($form_state['values']['item_id']);
|
| 174 |
}
|
| 175 |
else if ($barcodes) {
|
| 176 |
$item = library_get_item_by_barcode(check_plain($form_state['values']['item_id']));
|
| 177 |
}
|
| 178 |
else {
|
| 179 |
preg_match('/^(?:\s*|(.*))?\[\s*id\s*:\s*(\d+)\s*\]$/', $form_state['values']['item_id'], $matches);
|
| 180 |
if (!empty($matches)) {
|
| 181 |
list($temp, $title, $id) = $matches;
|
| 182 |
$item = library_load($id);
|
| 183 |
if ((isset($item) && !empty($title) && (strcasecmp(rtrim(check_plain($title)), rtrim(check_plain($item->title))) != 0)) || empty($item)) {
|
| 184 |
form_set_error('item_id', t('Library Item : Title mismatch. Please check your selection.'));
|
| 185 |
}
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
form_set_error('item_id', t('You must specify a valid library item.'));
|
| 189 |
}
|
| 190 |
}
|
| 191 |
if (!empty($item)) {
|
| 192 |
$form_state['values']['item_id'] = $item->id;
|
| 193 |
$form_state['values']['item_name'] = $item->title;
|
| 194 |
$form_state['values']['nid'] = $item->nid;
|
| 195 |
$form_state['values']['barcode'] = $item->barcode;
|
| 196 |
$form_state['values']['node_type'] = $item->type;
|
| 197 |
}
|
| 198 |
if ($form_state['values']['action_aid']) {
|
| 199 |
if ($item->in_circulation == LIBRARY_REFERENCE_ONLY && $action->status_change <> LIBRARY_ACTION_NO_CHANGE) {
|
| 200 |
form_set_error('action_aid', t('This item is not in circulation.'));
|
| 201 |
}
|
| 202 |
elseif ($item->library_status == LIBRARY_ITEM_AVAILABLE && $action->status_change == LIBRARY_ITEM_TYPE_AVAILABLE) {
|
| 203 |
form_set_error('action_aid', t('This item is already available.'));
|
| 204 |
}
|
| 205 |
elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $action->status_change == LIBRARY_ITEM_TYPE_UNAVAILABLE) {
|
| 206 |
form_set_error('action_aid', t('This item is not currently available.'));
|
| 207 |
}
|
| 208 |
elseif ($item->library_status == LIBRARY_ITEM_UNAVAILABLE && $n_patron->nid <> $item->last_patron_id) {
|
| 209 |
form_set_error('patron_id', t('This item was made unavailable by a different patron. Only that patron can perform actions on it.'));
|
| 210 |
}
|
| 211 |
}
|
| 212 |
}
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
| 216 |
* Handles transaction form submissions.
|
| 217 |
*
|
| 218 |
* @see library_transaction_form()
|
| 219 |
*/
|
| 220 |
function library_transaction_form_submit($form, &$form_state) {
|
| 221 |
$now = time();
|
| 222 |
$duedate = NULL;
|
| 223 |
$clean = library_clean_action_name($form_state['values']['action_name']);
|
| 224 |
if ($form_state['values']['action_status_change'] == LIBRARY_ACTION_TYPE_UNAVAILABLE) {
|
| 225 |
$type = $form_state['values']['node_type'];
|
| 226 |
$period = variable_get('library_period_for_'. $type .'_'. $clean, 0);
|
| 227 |
if ($period > 0) {
|
| 228 |
$duedate = library_get_due_date($now, $clean, $type);
|
| 229 |
}
|
| 230 |
$new_status = LIBRARY_ITEM_UNAVAILABLE;
|
| 231 |
}
|
| 232 |
elseif ($form_state['values']['action_status_change'] == LIBRARY_ACTION_TYPE_AVAILABLE) {
|
| 233 |
$new_status = LIBRARY_ITEM_AVAILABLE;
|
| 234 |
}
|
| 235 |
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);
|
| 236 |
if ($form_state['values']['action_status_change'] > LIBRARY_ACTION_NO_CHANGE) {
|
| 237 |
db_query("UPDATE {library} set library_status = %d WHERE id = %d", $new_status, $form_state['values']['item_id']);
|
| 238 |
}
|
| 239 |
|
| 240 |
drupal_set_message(t('The '. $form_state['values']['action_name'] .' was successful.'));
|
| 241 |
if (module_exists('trigger')) {
|
| 242 |
$transaction_id = db_last_insert_id('library_transactions', 'tid');
|
| 243 |
$context = array();
|
| 244 |
$object = (object) $context;
|
| 245 |
$context['item'] = array(
|
| 246 |
'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']
|
| 247 |
);
|
| 248 |
$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);
|
| 249 |
$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']);
|
| 250 |
$actions = _library_get_hook_aids('library', 'after_'. $clean);
|
| 251 |
if (!empty($actions)) {
|
| 252 |
actions_do($actions, $object, $context);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
$form_state['redirect'] = 'node/'. $form_state['values']['nid'];
|
| 256 |
return;
|
| 257 |
}
|
| 258 |
|
| 259 |
function library_transaction_view($tid) {
|
| 260 |
$transaction = library_get_transaction_by_tid($tid);
|
| 261 |
if (isset($transaction)) {
|
| 262 |
$list[] = t("Date: @action_date", array('@action_date' => format_date($transaction->created, 'custom', 'M j, Y')));
|
| 263 |
$list[] = t("Item: ". l($transaction->item_name, 'node/'. $transaction->nid));
|
| 264 |
$list[] = t("Action: @action", array('@action' => $transaction->action_name));
|
| 265 |
$list[] = t("Patron: ". l($transaction->name_first .' '. $transaction->name_last, 'node/'. $transaction->patron_id));
|
| 266 |
|
| 267 |
if (!empty($transaction->duedate)) {
|
| 268 |
$list[] = t('Due Date: @due_date', array('@due_date' => format_date($transaction->duedate, 'custom', 'M j, Y')));
|
| 269 |
}
|
| 270 |
if (!empty($transaction->notes)) {
|
| 271 |
$list[] = $transaction->notes;
|
| 272 |
}
|
| 273 |
return theme('item_list', $list);
|
| 274 |
}
|
| 275 |
else {
|
| 276 |
drupal_set_message(t('No transaction found.'), array());
|
| 277 |
}
|
| 278 |
}
|
| 279 |
|
| 280 |
/**
|
| 281 |
* Menu callback; show the transaction history of a single node.
|
| 282 |
*/
|
| 283 |
function library_history($node) {
|
| 284 |
$duedate_enabled = library_duedates_enabled($node->type);
|
| 285 |
global $user;
|
| 286 |
|
| 287 |
if ($node->type == 'patron') {
|
| 288 |
$is_patron = TRUE;
|
| 289 |
}
|
| 290 |
else {
|
| 291 |
$is_patron = FALSE;
|
| 292 |
}
|
| 293 |
|
| 294 |
if (library_item_in_library($node) || $is_patron) {
|
| 295 |
$header = array(t('Date'), t('Transaction'), t('Notes'));
|
| 296 |
if ($duedate_enabled) {
|
| 297 |
$header[] = t('Due Date');
|
| 298 |
}
|
| 299 |
$header[] = t('Actions');
|
| 300 |
$rows = array();
|
| 301 |
|
| 302 |
$transactions = library_get_transactions_by_node($node);
|
| 303 |
|
| 304 |
if (!empty($transactions[0])) {
|
| 305 |
foreach ($transactions as $value) {
|
| 306 |
$last_item = '';
|
| 307 |
foreach ($value as $transaction) {
|
| 308 |
$detail_link = '';
|
| 309 |
$action_link = '';
|
| 310 |
$cur_item = $transaction->item_id;
|
| 311 |
$created = format_date($transaction->created, 'small');
|
| 312 |
if ($duedate_enabled && $cur_item <> $last_item && !empty($transaction->duedate)) {
|
| 313 |
$duedate = format_date($transaction->duedate, 'small');
|
| 314 |
}
|
| 315 |
elseif ($duedate_enabled) {
|
| 316 |
$duedate = '';
|
| 317 |
}
|
| 318 |
if (user_access('administer transactions')) {
|
| 319 |
$detail_link = l('View Details', 'library-items/transaction/view/'. $transaction->tid);
|
| 320 |
}
|
| 321 |
if ($cur_item <> $last_item) {
|
| 322 |
$item = array('id' => $transaction->item_id, 'library_status' => $transaction->library_status, 'last_patron_id' => $transaction->patron_id, 'in_circulation' => $transaction->in_circulation);
|
| 323 |
$links = library_get_action_links($item);
|
| 324 |
if (!empty($links)) {
|
| 325 |
$action_link = implode(" | ", $links);
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
| 329 |
$temp_array = array($created, $transaction->action_name, $transaction->notes);
|
| 330 |
if (isset($duedate)) {
|
| 331 |
$temp_array[] = $duedate;
|
| 332 |
}
|
| 333 |
if (!empty($detail_link) && !empty($action_link)) {
|
| 334 |
$temp_array[] = $detail_link .' | '. $action_link;
|
| 335 |
}
|
| 336 |
else if (!empty($action_link)) {
|
| 337 |
$temp_array[] = $action_link;
|
| 338 |
}
|
| 339 |
else if (!empty($detail_link)) {
|
| 340 |
$temp_array[] = $detail_link;
|
| 341 |
}
|
| 342 |
else {
|
| 343 |
$temp_array[] = '';
|
| 344 |
}
|
| 345 |
$rows[] = $temp_array;
|
| 346 |
$last_item = $cur_item;
|
| 347 |
}
|
| 348 |
}
|
| 349 |
return theme('table', $header, $rows);
|
| 350 |
}
|
| 351 |
else {
|
| 352 |
if ($is_patron) {
|
| 353 |
return "<p>This patron has not performed any actions.</p>";
|
| 354 |
}
|
| 355 |
else {
|
| 356 |
return "<p>No actions have been performed on this item.</p>";
|
| 357 |
}
|
| 358 |
}
|
| 359 |
}
|
| 360 |
else {
|
| 361 |
return "<p>This item type is not part of the library.</p>";
|
| 362 |
}
|
| 363 |
}
|
| 364 |
|
| 365 |
function library_display_items() {
|
| 366 |
$var = library_get_table_header();
|
| 367 |
$header = $var['header'];
|
| 368 |
|
| 369 |
$nodes = library_get_items_group_by_node();
|
| 370 |
$rows = array();
|
| 371 |
foreach ($nodes as $node) {
|
| 372 |
$node_rows = library_get_table_row($node, $var);
|
| 373 |
foreach ($node_rows as $row) {
|
| 374 |
$rows[] = $row;
|
| 375 |
}
|
| 376 |
}
|
| 377 |
$output = theme('table', $header, $rows, array('class' => 'library-list'));
|
| 378 |
$output .= theme('pager', NULL, LIBRARY_RESULTS_PER_PAGE);
|
| 379 |
return $output;
|
| 380 |
}
|
| 381 |
|
| 382 |
function library_overdue_items() {
|
| 383 |
if (library_duedates_enabled()) {
|
| 384 |
$records = library_get_overdue_items();
|
| 385 |
if (empty($records)) {
|
| 386 |
return "<p>No overdue items at this time.</p>";
|
| 387 |
}
|
| 388 |
else {
|
| 389 |
$header = array(t('Title'), t('Patron'), t('Due Date'), t('Actions'));
|
| 390 |
$rows = array();
|
| 391 |
foreach ($records as $patron_id => $record) {
|
| 392 |
$patron_name = $record['patron']['patron_name'];
|
| 393 |
$patron_email = $record['patron']['patron_email'];
|
| 394 |
foreach ($record['items'] as $id => $values) {
|
| 395 |
$item = array('library_status' => LIBRARY_ITEM_UNAVAILABLE, 'id' => $id, 'last_patron_id' => $patron_id, 'in_circulation' => $values['in_circulation']);
|
| 396 |
$links = implode(" | ", library_get_action_links($item));
|
| 397 |
|
| 398 |
$rows[] = array($values['item_name'], $patron_name, format_date($values['due_date'], 'small'), l('Item Details', 'node/'. $values['nid']) .' | '. $links);
|
| 399 |
$item = array();
|
| 400 |
}
|
| 401 |
}
|
| 402 |
$output = theme('table', $header, $rows);
|
| 403 |
$output .= theme('pager', NULL, LIBRARY_RESULTS_PER_PAGE, 0);
|
| 404 |
return $output;
|
| 405 |
}
|
| 406 |
}
|
| 407 |
else {
|
| 408 |
return "<p>Library due dates are not enabled.</p>";
|
| 409 |
}
|
| 410 |
}
|