1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 use OpenSRF::Utils::JSON;
9 my $U = 'OpenILS::Application::AppUtils';
13 # user : au object, fleshed
14 sub load_myopac_prefs {
17 $self->ctx->{user} = $self->editor->retrieve_actor_user([
18 $self->ctx->{user}->id,
22 au => [qw/card home_ou addresses ident_type/]
28 return Apache2::Const::OK;
31 sub load_myopac_prefs_notify {
33 my $e = $self->editor;
35 my $user_prefs = $self->fetch_optin_prefs;
36 $user_prefs = $self->update_optin_prefs($user_prefs)
37 if $self->cgi->request_method eq 'POST';
39 $self->ctx->{opt_in_settings} = $user_prefs;
41 return Apache2::Const::OK;
44 sub fetch_optin_prefs {
46 my $e = $self->editor;
48 # fetch all of the opt-in settings the user has access to
49 # XXX: user's should in theory have options to opt-in to notices
50 # for remote locations, but that opens the door for a large
51 # set of generally un-used opt-ins.. needs discussion
52 my $opt_ins = $U->simplereq(
54 'open-ils.actor.event_def.opt_in.settings.atomic',
55 $e->authtoken, $e->requestor->home_ou);
57 # fetch user setting values for each of the opt-in settings
58 my $user_set = $U->simplereq(
60 'open-ils.actor.patron.settings.retrieve',
63 [map {$_->name} @$opt_ins]
66 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
69 sub update_optin_prefs {
71 my $user_prefs = shift;
72 my $e = $self->editor;
73 my @settings = $self->cgi->param('setting');
76 # apply now-true settings
77 for my $applied (@settings) {
78 # see if setting is already applied to this user
79 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
80 $newsets{$applied} = OpenSRF::Utils::JSON->true;
83 # remove now-false settings
84 for my $pref (grep { $_->{value} } @$user_prefs) {
85 $newsets{$pref->{cust}->name} = undef
86 unless grep { $_ eq $pref->{cust}->name } @settings;
91 'open-ils.actor.patron.settings.update',
92 $e->authtoken, $e->requestor->id, \%newsets);
94 # update the local prefs to match reality
95 for my $pref (@$user_prefs) {
96 $pref->{value} = $newsets{$pref->{cust}->name}
97 if exists $newsets{$pref->{cust}->name};
103 sub load_myopac_prefs_settings {
106 $self->ctx->{user} = $self->editor->retrieve_actor_user([
107 $self->ctx->{user}->id,
111 au => [qw/card home_ou addresses ident_type/]
117 return Apache2::Const::OK;
121 sub fetch_user_holds {
123 my $hold_ids = shift;
124 my $ids_only = shift;
126 my $available = shift;
130 my $e = $self->editor;
132 my $circ = OpenSRF::AppSession->create('open-ils.circ');
136 $hold_ids = $circ->request(
137 'open-ils.circ.holds.id_list.retrieve.authoritative',
142 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
146 return $hold_ids if $ids_only or @$hold_ids == 0;
149 suppress_notices => 1,
150 suppress_transits => 1,
152 suppress_patron_details => 1,
153 include_bre => $flesh ? 1 : 0
156 # ----------------------------------------------------------------
157 # Collect holds in batches of $batch_size for faster retrieval
161 my $mk_req_batch = sub {
163 my $top_idx = $batch_idx + $batch_size;
164 while($batch_idx < $top_idx) {
165 my $hold_id = $hold_ids->[$batch_idx++];
166 last unless $hold_id;
167 my $ses = OpenSRF::AppSession->create('open-ils.circ');
168 my $req = $ses->request(
169 'open-ils.circ.hold.details.retrieve',
170 $e->authtoken, $hold_id, $args);
171 push(@ses, {ses => $ses, req => $req});
177 my(@collected, @holds, @ses);
180 @ses = $mk_req_batch->() if $first;
181 last if $first and not @ses;
184 # If desired by the caller, filter any holds that are not available.
186 @collected = grep { $_->{hold}->{status} == 4 } @collected;
188 while(my $blob = pop(@collected)) {
189 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
194 for my $req_data (@ses) {
195 push(@collected, {hold => $req_data->{req}->gather(1)});
196 $req_data->{ses}->kill_me;
199 @ses = $mk_req_batch->();
200 last unless @collected or @ses;
204 # put the holds back into the original server sort order
206 for my $id (@$hold_ids) {
207 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
213 sub handle_hold_update {
216 my $e = $self->editor;
219 my @hold_ids = $self->cgi->param('hold_id'); # for non-_all actions
220 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
222 my $circ = OpenSRF::AppSession->create('open-ils.circ');
224 if($action =~ /cancel/) {
226 for my $hold_id (@hold_ids) {
227 my $resp = $circ->request(
228 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
231 } elsif ($action =~ /activate|suspend/) {
234 for my $hold_id (@hold_ids) {
235 my $vals = {id => $hold_id};
237 if($action =~ /activate/) {
238 $vals->{frozen} = 'f';
239 $vals->{thaw_date} = undef;
241 } elsif($action =~ /suspend/) {
242 $vals->{frozen} = 't';
243 # $vals->{thaw_date} = TODO;
245 push(@$vlist, $vals);
248 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
249 } elsif ($action eq 'edit') {
252 my $val = {"id" => $_};
253 $val->{"frozen"} = $self->cgi->param("frozen");
254 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
256 for my $field (qw/expire_time thaw_date/) {
257 # XXX TODO make this support other date formats, not just
259 next unless $self->cgi->param($field) =~
260 m:^(\d{2})/(\d{2})/(\d{4})$:;
261 $val->{$field} = "$3-$1-$2";
267 'open-ils.circ.hold.update.batch.atomic',
268 $e->authtoken, undef, \@vals
269 )->gather(1); # LFW XXX test for failure
270 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
274 return defined($url) ? $self->generic_redirect($url) : undef;
277 sub load_myopac_holds {
279 my $e = $self->editor;
280 my $ctx = $self->ctx;
283 my $limit = $self->cgi->param('limit') || 0;
284 my $offset = $self->cgi->param('offset') || 0;
285 my $action = $self->cgi->param('action') || '';
286 my $available = int($self->cgi->param('available') || 0);
288 my $hold_handle_result;
289 $hold_handle_result = $self->handle_hold_update($action) if $action;
291 $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
293 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
296 sub load_place_hold {
298 my $ctx = $self->ctx;
299 my $e = $self->editor;
300 my $cgi = $self->cgi;
301 $self->ctx->{page} = 'place_hold';
303 $ctx->{hold_target} = $cgi->param('hold_target');
304 $ctx->{hold_type} = $cgi->param('hold_type');
305 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
307 if($ctx->{hold_type} eq 'T') {
308 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
312 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
314 if(my $pickup_lib = $cgi->param('pickup_lib')) {
317 patronid => $e->requestor->id,
318 titleid => $ctx->{hold_target}, # XXX
319 pickup_lib => $pickup_lib,
323 my $allowed = $U->simplereq(
325 'open-ils.circ.title_hold.is_possible',
329 if($allowed->{success} == 1) {
330 my $hold = Fieldmapper::action::hold_request->new;
332 $hold->pickup_lib($pickup_lib);
333 $hold->requestor($e->requestor->id);
334 $hold->usr($e->requestor->id); # XXX staff
335 $hold->target($ctx->{hold_target});
336 $hold->hold_type($ctx->{hold_type});
337 # frozen, expired, etc..
339 my $stat = $U->simplereq(
341 'open-ils.circ.holds.create',
345 if($stat and $stat > 0) {
346 # if successful, return the user to the requesting page
347 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
348 return $self->generic_redirect;
351 $ctx->{hold_failed} = 1;
353 } else { # hold *check* failed
354 $ctx->{hold_failed} = 1; # XXX process the events, etc
355 $ctx->{hold_failed_event} = $allowed->{last_event};
359 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
362 return Apache2::Const::OK;
366 sub fetch_user_circs {
368 my $flesh = shift; # flesh bib data, etc.
369 my $circ_ids = shift;
373 my $e = $self->editor;
378 @circ_ids = @$circ_ids;
382 my $circ_data = $U->simplereq(
384 'open-ils.actor.user.checked_out',
389 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
391 if($limit or $offset) {
392 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
396 return [] unless @circ_ids;
398 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
403 circ => ['target_copy'],
404 acp => ['call_number'],
410 my $circs = $e->search_action_circulation(
411 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
414 for my $circ (@$circs) {
417 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
418 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
419 undef # pre-cat copy, use the dummy title/author instead
424 # make sure the final list is in the correct order
426 for my $id (@circ_ids) {
429 (grep { $_->{circ}->id == $id } @circs)
433 return \@sorted_circs;
437 sub handle_circ_renew {
440 my $ctx = $self->ctx;
442 my @renew_ids = $self->cgi->param('circ');
444 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
446 # TODO: fire off renewal calls in batches to speed things up
448 for my $circ (@$circs) {
450 my $evt = $U->simplereq(
452 'open-ils.circ.renew',
453 $self->editor->authtoken,
455 patron_id => $self->editor->requestor->id,
456 copy_id => $circ->{circ}->target_copy,
461 # TODO return these, then insert them into the circ data
462 # blob that is shoved into the template for each circ
463 # so the template won't have to match them
464 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
471 sub load_myopac_circs {
473 my $e = $self->editor;
474 my $ctx = $self->ctx;
477 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
478 my $offset = $self->cgi->param('offset') || 0;
479 my $action = $self->cgi->param('action') || '';
481 # perform the renewal first if necessary
482 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
484 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
486 my $success_renewals = 0;
487 my $failed_renewals = 0;
488 for my $data (@{$ctx->{circs}}) {
489 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
492 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
493 $data->{renewal_response} = $evt;
494 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
495 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
499 $ctx->{success_renewals} = $success_renewals;
500 $ctx->{failed_renewals} = $failed_renewals;
502 return Apache2::Const::OK;
505 sub load_myopac_circ_history {
507 my $e = $self->editor;
508 my $ctx = $self->ctx;
509 my $limit = $self->cgi->param('limit') || 15;
510 my $offset = $self->cgi->param('offset') || 0;
512 $ctx->{circ_history_limit} = $limit;
513 $ctx->{circ_history_offset} = $offset;
515 my $circs = $e->json_query({
516 from => ['action.usr_visible_circs', $e->requestor->id],
517 #limit => $limit || 25,
518 #offset => $offset || 0,
521 # XXX: order-by in the json_query above appears to do nothing, so in-query
522 # paging is not reallly an option. do the sorting/paging here
524 # sort newest to oldest
525 $circs = [ sort { $b->{xact_start} cmp $a->{xact_start} } @$circs ];
526 my @ids = map { $_->{id} } @$circs;
528 # find the selected page and trim cruft
529 @ids = @ids[$offset..($offset + $limit - 1)] if $limit;
530 @ids = grep { defined $_ } @ids;
532 $ctx->{circs} = $self->fetch_user_circs(1, \@ids);
533 #$ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circs], $limit, $offset);
535 return Apache2::Const::OK;
538 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
539 sub load_myopac_hold_history {
541 my $e = $self->editor;
542 my $ctx = $self->ctx;
543 my $limit = $self->cgi->param('limit') || 15;
544 my $offset = $self->cgi->param('offset') || 0;
545 $ctx->{hold_history_limit} = $limit;
546 $ctx->{hold_history_offset} = $offset;
549 my $holds = $e->json_query({
550 from => ['action.usr_visible_holds', $e->requestor->id],
551 limit => $limit || 25,
552 offset => $offset || 0
555 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$holds], 0, 1, 0, $limit, $offset);
557 return Apache2::Const::OK;
560 # TODO: add other filter options as params/configs/etc.
561 sub load_myopac_payments {
563 my $limit = $self->cgi->param('limit') || 20;
564 my $offset = $self->cgi->param('offset') || 0;
565 my $e = $self->editor;
567 $self->ctx->{payment_history_limit} = $limit;
568 $self->ctx->{payment_history_offset} = $offset;
571 $args->{limit} = $limit if $limit;
572 $args->{offset} = $offset if $offset;
574 $self->ctx->{payments} = $U->simplereq(
576 'open-ils.actor.user.payments.retrieve.atomic',
577 $e->authtoken, $e->requestor->id, $args);
579 return Apache2::Const::OK;
584 sub load_myopac_main {
586 my $limit = $self->cgi->param('limit') || 0;
587 my $offset = $self->cgi->param('offset') || 0;
588 my $e = $self->editor;
589 my $ctx = $self->ctx;
600 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
602 # TODO: This should really be a ML call, but the existing calls
603 # return an excessive amount of data and don't offer streaming
605 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
607 my $req = $cstore->request(
608 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
610 usr => $e->requestor->id,
611 balance_owed => {'!=' => 0}
616 mobts => [qw/grocery circulation reservation/],
617 bresv => ['target_resource_type'],
621 circ => ['target_copy'],
622 acp => ['call_number'],
625 order_by => { mobts => 'xact_start' },
630 while(my $resp = $req->recv) {
631 my $mobts = $resp->content;
632 my $circ = $mobts->circulation;
635 if($mobts->grocery) {
636 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
637 $last_billing = pop(@billings);
640 # XXX TODO switch to some money-safe non-fp library for math
641 $ctx->{"fines"}->{$_} += $mobts->$_ for (
642 qw/total_paid total_owed balance_owed/
645 my $marc_xml = undef;
646 if ($mobts->xact_type eq 'reservation' and
647 $mobts->reservation->target_resource_type->record) {
648 $marc_xml = XML::LibXML->new->parse_string(
649 $mobts->reservation->target_resource_type->record->marc
651 } elsif ($mobts->xact_type eq 'circulation' and
652 $circ->target_copy->call_number->id != -1) {
653 $marc_xml = XML::LibXML->new->parse_string(
654 $circ->target_copy->call_number->record->marc
659 @{$ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
662 last_grocery_billing => $last_billing,
663 marc_xml => $marc_xml
668 return Apache2::Const::OK;
671 sub load_myopac_update_email {
673 my $e = $self->editor;
674 my $ctx = $self->ctx;
675 my $email = $self->cgi->param('email') || '';
677 return Apache2::Const::OK
678 unless $self->cgi->request_method eq 'POST';
680 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
681 $ctx->{invalid_email} = $email;
682 return Apache2::Const::OK;
685 my $stat = $U->simplereq(
687 'open-ils.actor.user.email.update',
688 $e->authtoken, $email);
690 my $url = $self->apache->unparsed_uri;
691 $url =~ s/update_email/prefs/;
693 return $self->generic_redirect($url);
696 sub load_myopac_update_username {
698 my $e = $self->editor;
699 my $ctx = $self->ctx;
700 my $username = $self->cgi->param('username') || '';
702 return Apache2::Const::OK
703 unless $self->cgi->request_method eq 'POST';
705 unless($username and $username !~ /\s/) { # any other username restrictions?
706 $ctx->{invalid_username} = $username;
707 return Apache2::Const::OK;
710 if($username ne $e->requestor->usrname) {
712 my $evt = $U->simplereq(
714 'open-ils.actor.user.username.update',
715 $e->authtoken, $username);
717 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
718 $ctx->{username_exists} = $username;
719 return Apache2::Const::OK;
723 my $url = $self->apache->unparsed_uri;
724 $url =~ s/update_username/prefs/;
726 return $self->generic_redirect($url);
729 sub load_myopac_update_password {
731 my $e = $self->editor;
732 my $ctx = $self->ctx;
734 return Apache2::Const::OK
735 unless $self->cgi->request_method eq 'POST';
737 my $current_pw = $self->cgi->param('current_pw') || '';
738 my $new_pw = $self->cgi->param('new_pw') || '';
739 my $new_pw2 = $self->cgi->param('new_pw2') || '';
741 unless($new_pw eq $new_pw2) {
742 $ctx->{password_nomatch} = 1;
743 return Apache2::Const::OK;
746 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
748 if($pw_regex and $new_pw !~ /$pw_regex/) {
749 $ctx->{password_invalid} = 1;
750 return Apache2::Const::OK;
753 my $evt = $U->simplereq(
755 'open-ils.actor.user.password.update',
756 $e->authtoken, $new_pw, $current_pw);
759 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
760 $ctx->{password_incorrect} = 1;
761 return Apache2::Const::OK;
764 my $url = $self->apache->unparsed_uri;
765 $url =~ s/update_password/prefs/;
767 return $self->generic_redirect($url);
770 sub load_myopac_bookbags {
772 my $e = $self->editor;
773 my $ctx = $self->ctx;
775 $e->xact_begin; # replication...
777 my $rv = $self->load_mylist;
778 unless($rv eq Apache2::Const::OK) {
784 order_by => {cbreb => 'name'},
785 limit => $self->cgi->param('limit') || 10,
786 offset => $self->cgi->param('offset') || 0
789 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
790 {owner => $self->editor->requestor->id, btype => 'bookbag'},
791 # XXX what to do about the possibility of really large bookbags here?
792 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
795 if(!$ctx->{bookbags}) {
797 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
800 # get unique record IDs
802 foreach my $bbag (@{$ctx->{bookbags}}) {
804 map { $_->target_biblio_record_entry } @{$bbag->items}
806 $rec_ids{$rec_id} = 1;
810 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
813 return Apache2::Const::OK;
817 # actions are create, delete, show, hide, rename, add_rec, delete_item
818 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
819 sub load_myopac_bookbag_update {
820 my ($self, $action, $list_id) = @_;
821 my $e = $self->editor;
822 my $cgi = $self->cgi;
824 $action ||= $cgi->param('action');
825 $list_id ||= $cgi->param('list');
827 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
828 my @del_item = $cgi->param('del_item');
829 my $shared = $cgi->param('shared');
830 my $name = $cgi->param('name');
834 if($action eq 'create') {
835 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
837 $list->owner($e->requestor->id);
838 $list->btype('bookbag');
839 $list->pub($shared ? 't' : 'f');
840 $success = $U->simplereq('open-ils.actor',
841 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
845 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
847 return Apache2::Const::HTTP_BAD_REQUEST unless
848 $list and $list->owner == $e->requestor->id;
851 if($action eq 'delete') {
852 $success = $U->simplereq('open-ils.actor',
853 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
855 } elsif($action eq 'show') {
856 unless($U->is_true($list->pub)) {
858 $success = $U->simplereq('open-ils.actor',
859 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
862 } elsif($action eq 'hide') {
863 if($U->is_true($list->pub)) {
865 $success = $U->simplereq('open-ils.actor',
866 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
869 } elsif($action eq 'rename') {
872 $success = $U->simplereq('open-ils.actor',
873 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
876 } elsif($action eq 'add_rec') {
877 foreach my $add_rec (@add_rec) {
878 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
879 $item->bucket($list_id);
880 $item->target_biblio_record_entry($add_rec);
881 $success = $U->simplereq('open-ils.actor',
882 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
883 last unless $success;
886 } elsif($action eq 'del_item') {
887 foreach (@del_item) {
888 $success = $U->simplereq(
890 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
892 last unless $success;
896 return $self->generic_redirect if $success;
898 $self->ctx->{bucket_action} = $action;
899 $self->ctx->{bucket_action_failed} = 1;
900 return Apache2::Const::OK;