forked from aadl/sopac
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sopac_user.php
2424 lines (2195 loc) · 89.4 KB
/
sopac_user.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* SOPAC is The Social OPAC: a Drupal module that serves as a wholly integrated web OPAC for the Drupal CMS
* This file contains the Drupal include functions for all the SOPAC admin pieces and configuration options
* This file is called via hook_user
*
* @package SOPAC
* @version 2.1
* @author John Blyberg
*/
/**
* This is a sub-function of the hook_user "view" operation.
*/
function sopac_user_view($op, &$edit, &$account, $category = NULL) {
$locum = sopac_get_locum();
// SOPAC uses the first 7 characters of the MD5 hash instead of caching the user's password
// like it used to do. It's more secure this way, IMHO.
$account->locum_pass = substr($account->pass, 0, 7);
// Patron information table (top of the page)
$patron_details_table = sopac_user_info_table($account, $locum);
if (variable_get('sopac_summary_enable', 1)) {
$result['patroninfo']['#title'] = t('Account Summary');
$result['patroninfo']['#weight'] = 0;
$result['patroninfo']['#type'] = 'sopac_patron_profile';
$result['patroninfo']['details']['#value'] = $patron_details_table;
}
// Patron checkouts (middle of the page)
if ($account->valid_card && $account->bcode_verify) {
$max_disp = intval($account->profile_numco);
$co_table = sopac_user_chkout_table($account, $locum);
if ($co_table) {
$result['patronco']['#title'] = t('Checked-out Items');
$result['patronco']['#weight'] = 2;
$result['patronco']['#type'] = 'user_profile_category';
$result['patronco']['details']['#value'] = $co_table;
}
}
// Patron holds (bottom of the page)
if ($account->valid_card && $account->bcode_verify) {
$max_disp = intval($account->profile_numreq);
$holds_table = drupal_get_form('sopac_user_holds_form', $account, $max_disp);
if ($holds_table) {
$result['patronholds']['#title'] = t('Requested Items');
$result['patronholds']['#weight'] = 3;
$result['patronholds']['#type'] = 'user_profile_category';
$result['patronholds']['details']['#value'] = $holds_table;
}
}
// Commit the page content
$account->content = array_merge($account->content, $result);
// The Summary is not really needed.
if (variable_get('sopac_history_hide', 1)) {
unset($account->content['summary']);
}
unset($account->content['Preferences']);
}
/**
* Returns a Drupal themed table of patron information for the "My Account" page.
*
* @param object $account Drupal user object for account being viewed
* @param object $locum Instansiated Locum object
* @return string Drupal themed table
*/
function sopac_user_info_table(&$account, &$locum) {
$rows = array();
// create home branch link if appropriate
if ($account->profile_pref_home_branch) {
$home_branch_link = l($account->profile_pref_home_branch, 'user/' . $account->uid . '/edit/Preferences');
}
elseif (variable_get('sopac_home_selector_options', FALSE)) {
$home_branch_link = l(t('Click to select your home branch'), 'user/' . $account->uid . '/edit/Preferences');
}
else {
$home_branch_link = NULL;
}
if ($account->profile_pref_cardnum) {
$cardnum = $account->profile_pref_cardnum;
$cardnum_link = l($cardnum, 'user/' . $account->uid . '/edit/Preferences');
$userinfo = $locum->get_patron_info($cardnum);
$bcode_verify = sopac_bcode_isverified($account);
if ($bcode_verify) {
$account->bcode_verify = TRUE;
}
else {
$account->bcode_verify = FALSE;
}
if ($userinfo['pnum']) {
$account->valid_card = TRUE;
}
else {
$account->valid_card = FALSE;
}
// Construct the user details table based on what is configured in the admin interface
if ($account->valid_card && $bcode_verify) {
if (variable_get('sopac_pname_enable', 1)) {
$rows[] = array(array('data' => t('Patron Name'), 'class' => 'attr_name'), $userinfo['name']);
}
if (variable_get('sopac_lcard_enable', 1)) {
$rows[] = array(array('data' => t('Library Card Number'), 'class' => 'attr_name'), $cardnum_link);
}
// Add row for home branch if appropriate
if ($home_branch_link) {
$rows[] = array(array('data' => t('Home Branch'), 'class' => 'attr_name'), $home_branch_link);
}
// Checkout history, if it's turned on
if (variable_get('sopac_checkout_history_enable', 0)) {
$cohist_enabled = $user->profile_pref_cohist ? 'Enabled' : 'Disabled';
$last_import = db_result(db_query("SELECT DATESUB(NOW() - last_hist_check) FROM {sopac_last_hist_check} WHERE uid = '" . $user->uid . "'"));
if ($cohist_enabled = 'Enabled') {
// TODO Check ILS, enable it if it's not (w/ cache check)
// Grab + update newest checkouts
}
else {
// TODO Check ILS, disable it is it's not (w/ cache check)
}
// Reset cache age
db_query("UPDATE {sopac_last_hist_check} SET last_hist_check = NOW()");
$rows[] = array(array('data' => t('Checkout History'), 'class' => 'attr_name'), l($cohist_enabled, 'user/'. $account->uid .'checkout/history'));
}
if (variable_get('sopac_numco_enable', 1)) {
$rows[] = array(array('data' => t('Items Checked Out'), 'class' => 'attr_name'), $userinfo['checkouts']);
}
if (variable_get('sopac_fines_display', 1) && variable_get('sopac_fines_enable', 1)) {
if (variable_get('sopac_fines_warning_amount', 0) > 0 && $userinfo['balance'] > variable_get('sopac_fines_warning_amount', 0)) {
drupal_set_message('WARNING: Your fine balance of $' .
number_format($userinfo['balance'], 2, '.', '') .
' exceeds the limit. You will not be able to request or renew items.');
}
$balance = '$' . number_format($userinfo['balance'], 2, '.', '');
if ($userinfo['balance'] > 0) {
$balance = l($balance, 'user/' . $account->uid . '/fines');
}
$rows[] = array(array('data' => t('Fine Balance'), 'class' => 'attr_name'), $balance);
}
if (variable_get('sopac_cardexp_enable', 1)) {
$rows[] = array(array('data' => t('Card Expiration Date'), 'class' => 'attr_name'), date('m-d-Y', $userinfo['expires']));
}
if (variable_get('sopac_tel_enable', 1)) {
$rows[] = array(array('data' => t('Telephone'), 'class' => 'attr_name'), $userinfo['tel1']);
}
}
else {
$rows[] = array(array('data' => t('Library Card Number'), 'class' => 'attr_name'), $cardnum_link);
}
}
else {
$cardnum_link = l(t('Click to add your library card'), 'user/' . $account->uid . '/edit/Preferences');
$rows[] = array(array('data' => t('Library Card Number'), 'class' => 'attr_name'), $cardnum_link);
// add row for home branch if appropriate
if ($home_branch_link) {
$rows[] = array(array('data' => t('Home Branch'), 'class' => 'attr_name'), $home_branch_link);
}
}
if ($account->mail && variable_get('sopac_email_enable', 1)) {
$email_link = l(t($account->mail), 'user/' . $account->uid . '/edit');
$rows[] = array(array('data' => t('Email'), 'class' => 'attr_name'), $email_link);
}
// Begin creating the user information display content
$user_info_disp = theme('table', NULL, $rows, array('id' => 'patroninfo-summary', 'cellspacing' => '0'));
if ($account->valid_card && !$bcode_verify) {
$user_info_disp .= '<div class="error">' . variable_get('sopac_uv_cardnum', t('The card number you have provided has not yet been verified by you. In order to make sure that you are the rightful owner of this library card number, we need to ask you some simple questions.')) . '</div>' . drupal_get_form('sopac_bcode_verify_form', $account->uid, $cardnum);
}
elseif ($cardnum && !$account->valid_card) {
$user_info_disp .= '<div class="error">' . variable_get('sopac_invalid_cardnum', t('It appears that the library card number stored on our website is invalid. If you have received a new card, or feel that this is an error, please click on the card number above to change it to your most recent library card. If you need further help, please contact us.')) . '</div>';
}
return $user_info_disp;
}
/**
* Returns a Drupal-themed table of checked-out items as well as the renewal form functionality.
*
* @param object $account Drupal user object for account being viewed
* @param object $locum Instansiated Locum object
* @return string Drupal themed table
*/
function sopac_user_chkout_table(&$account, &$locum, $max_disp = NULL) {
// Process any renew requests that have been submitted
if ($_POST['sub_type'] == 'Renew Selected') {
if (count($_POST['inum'])) {
foreach ($_POST['inum'] as $inum => $varname) {
$items[$inum] = $varname;
}
$renew_status = $locum->renew_items($account->profile_pref_cardnum, $account->locum_pass, $items);
}
}
elseif ($_POST['sub_type'] == 'Renew All') {
$renew_status = $locum->renew_items($account->profile_pref_cardnum, $account->locum_pass, 'all');
}
// Create the check-outs table
$rows = array();
if ($account->profile_pref_cardnum) {
$locum_pass = substr($account->pass, 0, 7);
$cardnum = $account->profile_pref_cardnum;
$checkouts = $locum->get_patron_checkouts($cardnum, $locum_pass);
$total = count($checkouts);
if (!$total) {
return t('No items checked out.');
}
$locum_cfg = $locum->locum_config;
$header = array('', t('Title'), t('Format'), t('Author'), t('Renews'), t('Due Date'));
$now = time();
$ill_bnums = array(1358991, 1356138, 1358990, 1358993, 1358992); // Make config option
foreach ($checkouts as $co) {
if ($max_disp && $total > $max_disp && ++$checkout_num > $max_disp) {
break;
}
if ($renew_status[$co['inum']]['error']) {
$duedate = '<span style="color: red;">' . $renew_status[$co['inum']]['error'] . '</span>';
}
else {
if ($now > $co['duedate']) {
$duedate = '<span style="color: red;">' . date('m-d-Y', $co['duedate']) . '</span>';
}
else {
$duedate = date('m-d-Y', $co['duedate']);
}
}
$today = strtotime(date('Y-m-d'));
$moddue = strtotime(date('Y-m-d', $co['duedate']));
$days = ($moddue - $today) / 86400;
$dayspan = '';
if ($days == 0) {
$dayspan = ' <span style="color: red; font-weight:bold;">(Today)</span>';
}
if ($days < 7 && $days > 0) {
$dayspan = ' <span style="color: red;">(this '.date('l',$co[duedate]).')</span>';
}
// Use Author formatting from the catalog
include_once('sopac_catalog.php');
$new_author_str = sopac_author_format($co['bib']['author'], $co['bib']['addl_author']);
// Magazine display should show scraped title
if ($co['bib']['mat_code'] == 's' && $co['scraped_title']) {
$co['title'] = $co['scraped_title'];
}
// Hover text for the bib
$hover = $co['title'] . "\n" .
$new_author_str . "\n" .
$co['bib']['callnum'] . "\n" .
$co['bib']['pub_info'] . "\n" .
$co['bib']['descr'];
$checkbox = '';
// CUSTOM ILL DISPLAY
if (in_array($co['bnum'], $ill_bnums)) {
// Display call number as the title
$title = $co['callnum'];
$author = l('Interlibrary Loan', 'node/37575'); // ILL FAQ page
}
else {
if ($co['bib']['active']) { // Not suppressed
$title = l($co['title'], 'catalog/record/' . $co['bnum'], array('attributes' => array('title' => $hover)));
}
else {
$title = $co['title'];
}
$author = l($new_author_str, variable_get('sopac_url_prefix', 'cat/seek') . '/search/author/' . urlencode($new_author_str));
if ($co['avail']['holds'] == 0 &&
strpos($co['callnum'], 'Zoom Lends') === FALSE &&
$co['bib']['mat_code'] != 's' &&
$co['bib']['mat_code'] != 'p' &&
!($co['ill'] == 1 && $co['numrenews'] > 0)) {
$checkbox = '<input type="checkbox" name="inum[' . $co['inum'] . ']" value="' . $co['varname'] . '">';
}
}
$rows[] = array(
$checkbox,
$title,
$locum_cfg['formats'][$co['bib']['mat_code']],
$author,
$co['numrenews'],
$duedate . $dayspan,
);
}
$submit_buttons = '<input type="submit" name="sub_type" value="' . t('Renew Selected') . '"> <input type="submit" name="sub_type" value="' . t('Renew All') . '">';
if ($max_disp && $total > $max_disp) {
$current_pref = l("Showing $max_disp of $total Checkouts", 'user/' . $account->uid . '/edit/Preferences', array('attributes' => array('title' => "Change this setting")));
$rows[] = array('data' => array(array('data' => $current_pref . " [ " . l("See All Checkouts", 'user/' . $account->uid . '/checkouts') . " ]",
'colspan' => 6,
'style' => "text-align: right")));
}
$rows[] = array('data' => array(array('data' => $submit_buttons, 'colspan' => count($rows[0]))), 'class' => 'profile_button' );
}
else {
return FALSE;
}
// Wrap it together inside a form
$content = '<form method="post">' . theme('table', $header, $rows, array('id' => 'patroninfo', 'cellspacing' => '0')) . '</form>';
return $content;
}
/**
* Use form API to creat holds table
*
* @return string
*/
function sopac_user_holds_form($form_state, $account = NULL, $max_disp = NULL) {
if (!$account) {
global $user;
$account = user_load($user->uid);
}
$form = array();
$cardnum = $account->profile_pref_cardnum;
$ils_pass = $account->locum_pass;
$locum = sopac_get_locum();
$locum_cfg = $locum->locum_config;
$holds = $locum->get_patron_holds($cardnum, $ils_pass);
$total = count($holds);
if (!$total) {
$form['empty'] = array(
'#type' => 'markup',
'#value' => t('No items requested.'),
);
return $form;
}
include_once('sopac_catalog.php');
$suspend_holds = variable_get('sopac_suspend_holds', FALSE);
if ($suspend_holds) {
return _sopac_user_holds_form_multirow($holds);
}
$form = array(
'#theme' => 'form_theme_bridge',
'#bridge_to_theme' => 'sopac_user_holds_list',
'#cardnum' => $cardnum,
'#ils_pass' => $ils_pass,
);
$sopac_prefix = variable_get('sopac_url_prefix', 'cat/seek') . '/record/';
$freezes_enabled = variable_get('sopac_hold_freezes_enable', 1);
$ill_bnums = array(1358991, 1356138, 1358990, 1358993, 1358992); // Make config option
$form['holds'] = array(
'#tree' => TRUE,
'#iterable' => TRUE,
);
foreach ($holds as $hold) {
if ($max_disp && $total > $max_disp && ++$hold_num > $max_disp) {
break;
}
$bnum = $hold['bnum'];
$varname = $hold['varname'];
$new_author_str = sopac_author_format($hold['bib']['author'], $hold['bib']['addl_author']);
// CUSTOM ILL DISPLAY
if (in_array($bnum, $ill_bnums)) {
if (preg_match('/canceli([\d]{7})/', $varname, $matches)) {
// If item is ready, grab the item record info
$item_url = 'http://' . $locum_cfg['ils_config']['ils_server'] . '/xrecord=i' . $matches[1];
$xrecord = simplexml_load_file($item_url);
foreach ($xrecord->VARFLD as $varfld) {
if ((string)$varfld->HEADER->TAG == "CALL #") {
$title = trim((string)$varfld->MARCSUBFLD->SUBFIELDDATA);
break;
}
}
}
else {
// Item isn't in yet, use the bib call number at the title
$title = $hold['bib']['callnum'];
}
$author = l('Interlibrary Loan', 'node/37575'); // ILL FAQ page
$hold['bib']['mat_code'] = ''; // leave material type blank
}
else {
// Magazine display should show scraped title
if ($hold['bib']['mat_code'] == 's' && $hold['scraped_title']) {
$hold['title'] = $hold['scraped_title'];
}
if ($hold['bib']['active']) { // Not suppressed
// Hover text for the bib
$hover = $hold['title'] . "\n" .
$new_author_str . "\n" .
$hold['bib']['callnum'] . "\n" .
$hold['bib']['pub_info'] . "\n" .
$hold['bib']['descr'];
$title = l($hold['title'], $sopac_prefix . $bnum, array('attributes' => array('title' => $hover)));
}
else {
$title = $hold['title'];
}
$author = l($new_author_str, variable_get('sopac_url_prefix', 'cat/seek') . '/search/author/' . urlencode($new_author_str));
}
// Replace 'In Transit' with 'Requested' for ILL items
if (stripos($hold['status'], 'in transit') !== FALSE &&
($hold['ill'] || in_array($bnum, $ill_bnums))) {
$hold['status'] = 'Requested';
}
$ready = (strpos($hold['status'], 'Ready') !== FALSE || strpos($hold['status'], 'MEL RECEIVED') !== FALSE);
if (module_exists('sopac_lockers')) {
if (sopac_lockers_available($hold)) {
$hold['status'] .= '*';
$locker_message = "<br />*We're testing out a new service. You can " .
l("request a locker for outdoor or after hours pickup of this item", "user/' . $account->uid . '/locker") . ".<br />";
}
}
$hold_to_theme = array();
$hold_to_theme['bnum'] = array(
'#type' => 'value',
'#value' => $bnum,
);
$hold_to_theme['title'] = array(
'#type' => 'value',
'#value' => $hold['title'],
);
$hold_to_theme['cancel'] = array(
'#type' => 'checkbox',
'#default_value' => FALSE,
);
$hold_to_theme['title_link'] = array(
'#type' => 'markup',
'#value' => $title,
);
$hold_to_theme['format'] = array(
'#type' => 'markup',
'#value' => $locum_cfg['formats'][$hold['bib']['mat_code']],
);
$hold_to_theme['author'] = array(
'#type' => 'markup',
'#value' => $author,
);
$hold_to_theme['ready'] = array(
'#type' => 'markup',
'#value' => $ready ? 'request-ready' : 'request-waiting',
);
$hold_to_theme['status'] = array(
'#type' => 'markup',
'#value' => $hold['status']
);
$hold_to_theme['pickup'] = array(
'#type' => 'markup',
'#value' => $hold['pickuploc'],
);
if ($freezes_enabled) {
if ($hold['can_freeze']) {
$hold_to_theme['freeze'] = array(
'#type' => 'checkbox',
'#default_value' => $hold['is_frozen'],
);
}
else {
$hold_to_theme['freeze'] = array(
'#type' => 'markup',
'#value' => ' '
);
}
}
$form['holds'][$varname] = $hold_to_theme;
}
if ($max_disp && $total > $max_disp) {
$current_pref = l("Showing $max_disp of $total requests", 'user/' . $account->uid . '/edit/Preferences', array('attributes' => array('title' => "Change this setting")));
$form['see_all'] = array(
'#value' => $current_pref . " [ " . l("See All Requests", 'user/' . $account->uid . '/requests') . " ]",
);
}
if ($locker_message) {
$form['lockers'] = array('#value' => $locker_message);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => $freezes_enabled ? t('Update Requests') : t('Cancel Selected Requests'),
);
$form['towish'] = array(
'#type' => 'submit',
'#name' => 'towish',
'#value' => t('Cancel and Move to Wishlist'),
);
return $form;
}
/**
* Validate request to change holds.
*
* @param array $form
* @param array $form_state
*/
function sopac_user_holds_form_validate(&$form, &$form_state) {
// Set defaults to avoid errors when debugging.
$pickup_changes = $suspend_from_changes = $suspend_to_changes = NULL;
$update_holds = FALSE;
$cancel_requested = TRUE;
$cancellations = array();
$freeze_changes = array();
$pickup_changes = array();
$suspend_from_changes = array();
$suspend_to_changes = array();
// Get holds.
$cardnum = $form['#cardnum'];
$password = $form['#ils_pass'];
$locum = sopac_get_locum();
$holds = $locum->get_patron_holds($cardnum, $password);
// Should be how it comes back from locum
$holds_by_varname = array();
foreach ($holds as $hold) {
$holds_by_varname[$hold['varname']] = $hold;
}
$submitted_holds = $form_state['values']['holds'];
$change_pickup = variable_get('sopac_changeable_pickup_location', FALSE);
$suspend_holds = variable_get('sopac_suspend_holds', FALSE);
if ($suspend_holds) {
// Set up time object for use in validating suspension dates
$locum = sopac_get_locum();
$sClosedByTimezone = $locum->locum_config['harvest_config']['timezone'];
$date_object = new DateTime(now, new DateTimeZone($sClosedByTimezone));
}
foreach ($submitted_holds as $varname => $hold_data) {
if ($hold_data['cancel']) {
$cancellations[$varname] = $cancel_requested;
$update_holds = TRUE;
continue;
}
$freeze_requested = $hold_data['freeze'];
if ($freeze_requested != $holds_by_varname[$varname]['is_frozen']) {
$freeze_changes[$varname] = $freeze_requested;
$update_holds = TRUE;
}
if ($change_pickup) {
$pickup_location = $hold_data['pickup'];
if ($pickup_location != $holds_by_varname[$varname]['pickuploc']['selected']) {
$pickup_changes[$varname] = $pickup_location;
$update_holds = TRUE;
}
}
if ($suspend_holds) {
$suspend_from = $hold_data['suspend_from'];
// Catch unchanged default.
if ($suspend_from == 'mm/dd/yyyy') {
$suspend_from = '';
}
// Make sure it's a date (allow 2-digit years, but ask for 4).
elseif (!preg_match('/^([1-9]|1[012])\/([1-9]|[12][0-9]|3[01])\/(20[1-9][0-9]|[1-9][0-9])$/', $suspend_from)) {
form_set_error("holds[$varname][suspend_from", t('Please enter suspend dates in the form 4/15/1980 (mm/dd/yyyy).'));
}
elseif ($suspend_from != $holds_by_varname[$varname]['start_suspend']) {
$suspend_from_changes[$varname] = $suspend_from;
$update_holds = TRUE;
}
$suspend_to = $hold_data['suspend_to'];
// Catch unchanged default.
if ($suspend_to == 'mm/dd/yyyy') {
$suspend_to = '';
}
// Make sure it's a date (allow 2-digit years, but ask for 4).
elseif (!preg_match('/^([1-9]|1[012])\/([1-9]|[12][0-9]|3[01])\/(20[1-9][0-9]|[1-9][0-9])$/', $suspend_to)) {
form_set_error("holds[$varname][suspend_to", t('Please enter suspend dates in the form 4/15/1980 (mm/dd/yyyy).'));
}
elseif ($suspend_to != $holds_by_varname[$varname]['end_suspend']) {
$suspend_to_changes[$varname] = $suspend_to;
$update_holds = TRUE;
}
if ($suspend_to && !$suspend_from) {
form_set_error("holds][$varname][suspend_to", t('You cannot set a suspend to date without a corresponding suspend from date.'));
}
elseif ($suspend_to && $suspend_from) {
$date_parts = explode('/', $suspend_from);
$date_object->setDate($date_parts[2], $date_parts[0], $date_parts[1]);
$from_date = $date_object->format('Ymd');
$date_parts = explode('/', $suspend_to);
$date_object->setDate($date_parts[2], $date_parts[0], $date_parts[1]);
$to_date = $date_object->format('Ymd');
if ($to_date < $from_date) {
form_set_error("holds[$varname][suspend_to", t('A suspend to date cannot be before the corresponding suspend from date.'));
}
}
}
}
$errors = form_get_errors();
if (is_array($errors)) {
// Skip rest of this structure.
}
elseif (!$update_holds) {
form_set_error('', 'Your request to ' . $form['submit']['#value'] . ' did not include any changes.');
}
// Store data for use by submit function.
else {
$form_state['sopac_user_holds'] = array(
'cancellations' => $cancellations,
'freeze_changes' => $freeze_changes,
'pickup_changes' => $pickup_changes,
'suspend_from_changes' => $suspend_from_changes,
'suspend_to_changes' => $suspend_to_changes,
);
}
}
/**
* Pass locum validated request to update holds.
*
* @param array $form
* @param array $form_state
*/
function sopac_user_holds_form_submit(&$form, &$form_state) {
$cardnum = $form['#cardnum'];
$password = $form['#ils_pass'];
$cancellations = $form_state['sopac_user_holds']['cancellations'];
$freeze_changes = $form_state['sopac_user_holds']['freeze_changes'];
$pickup_changes = $form_state['sopac_user_holds']['pickup_changes'];
$suspend_changes = array(
'from' => $form_state['sopac_user_holds']['suspend_from_changes'],
'to' => $form_state['sopac_user_holds']['suspend_to_changes'],
);
$locum = sopac_get_locum();
$locum->update_holds($cardnum, $password, $cancellations, $freeze_changes, $pickup_changes, $suspend_changes);
// Check if cancelled holds should be added to wishlist
if ($form_state['clicked_button']['#name'] == 'towish') {
// Grab the bnums of the cancelled items
$wish_titles = array();
foreach ($cancellations as $cancel_id => $item) {
sopac_list_add($form_state['values']['holds'][$cancel_id]['bnum'], 'wish');
$wish_titles[] = $form_state['values']['holds'][$cancel_id]['title'];
}
drupal_set_message('Added ' . implode(', ', $wish_titles) . ' to your wishlist');
}
}
/**
* Fork to allow support for changing hold pickup location, and suspend dates. Uses
* different tpl since extra options require different layout.
*
* @param array $holds
* @return array
*/
function _sopac_user_holds_form_multirow($holds) {
// <CraftySpace+> TODO: do we need to check for multi-branch, else no pickup location?
$form = array(
'#theme' => 'form_theme_bridge',
'#layout_theme' => 'sopac_user_holds_list_multirow',
);
$sopac_prefix = variable_get('sopac_url_prefix', 'cat/seek') . '/record/';
$form['holds'] = array(
'#tree' => TRUE,
'#iterable' => TRUE,
);
foreach ($holds as $hold) {
$bnum = $hold['bnum'];
$form['holds'][$bnum] = array(
'cancel' => array(
'#type' => 'checkbox',
'#default_value' => FALSE,
),
'title_link' => array(
'#type' => 'markup',
'#value' => l(t($hold['title']), $sopac_prefix . $bnum),
),
'status' => array(
'#type' => 'markup',
'#value' => $hold['status']
),
'pickup' => array(
'#type' => 'select',
'#options' => sopac_get_branch_options(),
'#default_value' => $hold['pickuploc']['selected'],
),
'freeze' => array(
'#type' => 'radios',
'#default_value' => $hold['is_frozen'],
'#options' => array(0 => t('Active'), 1 => t('Inactive')),
),
'suspend_from' => array(
'#type' => 'textfield',
'#title' => 'From',
'#default_value' => $hold['start_suspend'] ? $hold['start_suspend'] : 'mm/dd/yyyy',
'#attributes' => array('maxlength' => '10', 'size' => '15'),
),
'suspend_to' => array(
'#type' => 'textfield',
'#title' => 'To',
'#default_value' => $hold['end_suspend'] ? $hold['end_suspend'] : 'mm/dd/yyyy',
'#attributes' => array('maxlength' => '10', 'size' => '15'),
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#name' => 'op',
'#value' => t('Update Requests'),
);
return $form;
}
/**
* A dedicated check-outs page to list all checkouts.
*/
function sopac_checkouts_page() {
global $user;
$account = user_load($user->uid);
$cardnum = $account->profile_pref_cardnum;
$locum = sopac_get_locum();
$userinfo = $locum->get_patron_info($cardnum);
$bcode_verify = sopac_bcode_isverified($account);
if ($bcode_verify) {
$account->bcode_verify = TRUE;
}
else {
$account->bcode_verify = FALSE;
}
if ($userinfo['pnum']) {
$account->valid_card = TRUE;
}
else {
$account->valid_card = FALSE;
}
//profile_load_profile(&$user);
if ($account->valid_card && $bcode_verify) {
$content = sopac_user_chkout_table(&$user, &$locum);
}
elseif ($account->valid_card && !$bcode_verify) {
$content = '<div class="error">' . variable_get('sopac_uv_cardnum', t('The card number you have provided has not yet been verified by you. In order to make sure that you are the rightful owner of this library card number, we need to ask you some simple questions.')) . '</div>' . drupal_get_form('sopac_bcode_verify_form', $account->uid, $cardnum);
}
elseif ($cardnum && !$account->valid_card) {
$content = '<div class="error">' . variable_get('sopac_invalid_cardnum', t('It appears that the library card number stored on our website is invalid. If you have received a new card, or feel that this is an error, please click on the card number above to change it to your most recent library card. If you need further help, please contact us.')) . '</div>';
}
elseif (!$user->uid) {
$content = '<div class="error">' . t('You must be ') . l(t('logged in'), 'user') . t(' to view this page.') . '</div>';
}
elseif (!$cardnum) {
$content = '<div class="error">' . t('You must register a valid ') . l(t('library card number'), 'user/' . $user->uid . '/edit/Preferences') . t(' to view this page.') . '</div>';
}
return $content;
}
/**
* A dedicated checkout history page.
*/
function sopac_checkout_history_page() {
global $user;
//profile_load_profile(&$user);
if ($user->profile_pref_cardnum) {
// Get the time since the last update
$last_import = db_result(db_query("SELECT DATESUB(NOW() - last_hist_check) FROM {sopac_last_hist_check} WHERE uid = '" . $user->uid . "'"));
// Check profile to see if CO hist is enabled
$user_co_hist_enabled = $user->profile_pref_cohist;
if (!$user_co_hist_enabled) {
// CO hist is not enabled, would you like to enable it?
return $content;
}
// CO hist is enabled, would you like to disable it?
// Set up our data sets
$url_prefix = variable_get('sopac_url_prefix', 'cat/seek');
$insurge = sopac_get_insurge();
$locum = sopac_get_locum();
$locum_pass = substr($user->pass, 0, 7);
$cardnum = $user->profile_pref_cardnum;
$last_checkout_result = $insurge->get_checkout_history($user->uid, 1);
$last_checkout[(string) $last_checkout_result['bnum']] = $last_checkout_result['codate']; // Like this?
// If we haven't imported data recently, do it now.
if ($last_import >= variable_get('sopac_checkout_history_cache_time', 60)) {
$checkouts = $locum->get_patron_checkout_history($cardnum, $locum_pass, $last_checkout);
// Check: if profile->co hist is enabled , verify that it's on in the ILS
// check: "" disables "" off
if (!is_array($checkouts)) {
if ($checkouts == 'out') {
$content = '<div>'. t('This feature is currently turned off.') . '</div>';
$toggle = l(t('Opt In'), 'user/' . $user->uid . '/checkouts/history/opt/in');
}
if ($checkouts == 'in') {
$content = '<div>There are no items in your checkout history.</div>';
$toggle = l(t('Opt Out'), 'user/' . $user->uid . '/checkouts/history/opt/out');
}
}
else {
foreach ($checkouts as $checkout) {
$bib_item = $locum->get_bib_item($checkout['bnum']);
if ($bib_item['bnum']) {
$insurge->add_checkout_history($user->uid, $checkout['bnum'], $bib_item['title'], $bib_item['author'] . ' ' . $bib_item['addl_author']);
}
}
}
// Reset cache age
db_query("UPDATE {sopac_last_hist_check} SET last_hist_check = NOW()");
}
// Set up pagination
// Grab checkout history from Insurge
$checkout_history = $insurge->get_checkout_history($user->uid);
if (count ($checkout_history)) {
// Set up the table
$header = array('', t('Title'), t('Author'), t('Check-Out Date'));
$rows = array();
foreach ($checkout_history as $hist_item) {
$item = $locum->get_bib_item($hist_item['bnum']);
$new_author_str = sopac_author_format($item['author'], $item['addl_author']);
$rows[] = array(
l(ucwords($item['title']), $url_prefix . '/record/' . $item['bnum']),
l($new_author_str, $url_prefix . '/search/author/' . urlencode($new_author_str)),
$hist_item['codate'], // Figure out the best way to format this
);
$content = theme('table', $header, $rows, array('id' => 'patroninfo', 'cellspacing' => '0'));
}
}
else {
// nothing in users co hist
$content = t('You do not have anything in your checkout history yet.');
}
}
else {
$content = '<div class="cohist_nocard">' . t('Please register your library card to take advantage of this feature.') . '</div>';
}
return $content;
}
/**
* Handle toggling checkout history on or off.
*/
function sopac_checkout_history_toggle($action) {
global $user;
if ($action != 'in' && $action != 'out') { drupal_goto('user/' . $user->uid . '/checkouts/history'); }
$adjective = $action == 'in' ? t('on') : t('off');
//profile_load_profile(&$user);
if ($user->profile_pref_cardnum) {
if (!$_GET['confirm']) {
$confirm_link = l(t('confirm'), $_GET['q'], array('query' => 'confirm=true'));
$content = "<div>Please $confirm_link that you wish to turn $adjective your checkout history.";
if ($action == 'out') {
$content .= ' ' . t('Please note: this will delete your entire checkout history.');
}
}
else {
$locum = sopac_get_locum();
$locum_pass = substr($user->pass, 0, 7);
$cardnum = $user->profile_pref_cardnum;
$success = $locum->set_patron_checkout_history($cardnum, $locum_pass, $action);
if ($success === TRUE) {
$content = "<div>Your checkout history has been turned $adjective.</div>";
}
else {
$content = "<div>An error occurred. Your checkout history has not been turned $adjective. Please try again.</div>";
}
}
}
else {
$content = '<div>' . t('Please register your library card to take advantage of this feature.') . '</div>';
}
return $content;
}
/**
* A dedicated holds page to list all holds.
*/
function sopac_holds_page() {
global $user;
$account = user_load($user->uid);
$cardnum = $account->profile_pref_cardnum;
$locum = sopac_get_locum();
$userinfo = $locum->get_patron_info($cardnum);
$bcode_verify = sopac_bcode_isverified($account);
if ($bcode_verify) {
$account->bcode_verify = TRUE;
}
else {
$account->bcode_verify = FALSE;
}
if ($userinfo['pnum']) {
$account->valid_card = TRUE;
}
else {
$account->valid_card = FALSE;
}
if ($account->valid_card && $bcode_verify) {
$content = drupal_get_form('sopac_user_holds_form', $account);
}
elseif ($account->valid_card && !$bcode_verify) {
$content = '<div class="error">' . variable_get('sopac_uv_cardnum', t('The card number you have provided has not yet been verified by you. In order to make sure that you are the rightful owner of this library card number, we need to ask you some simple questions.')) . '</div>' . drupal_get_form('sopac_bcode_verify_form', $account->uid, $cardnum);
}
elseif ($cardnum && !$account->valid_card) {
$content = '<div class="error">' . variable_get('sopac_invalid_cardnum', t('It appears that the library card number stored on our website is invalid. If you have received a new card, or feel that this is an error, please click on the card number above to change it to your most recent library card. If you need further help, please contact us.')) . '</div>';
}
elseif (!$user->uid) {
$content = '<div class="error">' . t('You must be ') . l(t('logged in'), 'user') . t(' to view this page.') . '</div>';
}
elseif (!$cardnum) {
$content = '<div class="error">' . t('You must register a valid ') . l(t('library card number'), 'user/' . $user->uid . '/edit/Preferences') . t(' to view this page.') . '</div>';
}
return $content;
}
/**
* A dedicated page for managing fines and payments.
*/
function sopac_fines_page() {
global $user;
$locum = sopac_get_locum();
//profile_load_profile(&$user);
if ($user->profile_pref_cardnum && sopac_bcode_isverified(&$user)) {
$locum_pass = substr($user->pass, 0, 7);
$cardnum = $user->profile_pref_cardnum;
$fines = $locum->get_patron_fines($cardnum, $locum_pass);
if (!count($fines)) {
$notice = t('You do not have any fines, currently.');
}
else {
$header = array('', t('Amount'), t('Description'));
$fine_total = (float) 0;
foreach ($fines as $fine) {
$col1 = variable_get('sopac_payments_enable', 1) ? '<input type="checkbox" name="varname[]" value="' . $fine['varname'] . '">' : '';
$rows[] = array(
$col1,
'$' . number_format($fine['amount'], 2),
$fine['desc'],
);
$hidden_vars .= '<input type="hidden" name="fine_summary[' . $fine['varname'] . '][amount]" value="' . addslashes($fine['amount']) . '">';
$hidden_vars .= '<input type="hidden" name="fine_summary[' . $fine['varname'] . '][desc]" value="' . addslashes($fine['desc']) . '">';
$fine_total = $fine_total + $fine['amount'];
}
$rows[] = array('<strong>Total:</strong>', '$' . number_format($fine_total, 2), '');
$submit_button = '<input type="submit" value="' . t('Pay Selected Charges') . '">';
if (variable_get('sopac_payments_enable', 1)) {
$rows[] = array( 'data' => array(array('data' => $submit_button, 'colspan' => 3)), 'class' => 'profile_button' );
}
$fine_table = '<form method="post" action="' . url('user/' . $user->uid . '/fines/pay') . '">' . theme('table', $header, $rows, array('id' => 'patroninfo', 'cellspacing' => '0')) . $hidden_vars . '</form>';
$notice = t('Your current fine balance is $') . number_format($fine_total, 2) . '.';
}
}
else {
$notice = t('You do not yet have a library card validated with our system. You can add and validate a card using your ') . l(t('account page'), 'user') . '.';
}
$result_page = theme('sopac_fines', $notice, $fine_table, &$user);
return '<p>'. t($result_page) .'</p>';
}
/**
* A dedicated page for viewing payment information.
*/
function sopac_finespaid_page() {
global $user;
$limit = 20; // TODO Make this configurable
if (count($_POST['payment_id'])) {
foreach ($_POST['payment_id'] as $pid) {
db_query('DELETE FROM {sopac_fines_paid} WHERE payment_id = ' . $pid . ' AND uid = ' . $user->uid);
}
}
if (db_result(db_query('SELECT COUNT(*) FROM {sopac_fines_paid} WHERE uid = ' . $user->uid))) {
$header = array('', 'Payment Date', 'Payment Description', 'Amount');
$dbq = pager_query('SELECT payment_id, UNIX_TIMESTAMP(trans_date) as trans_date, fine_desc, amount FROM {sopac_fines_paid} WHERE uid = ' . $user->uid . ' ORDER BY trans_date DESC', $limit);