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;
9 use OpenSRF::Utils::JSON;
11 $Data::Dumper::Indent = 0;
13 my $U = 'OpenILS::Application::AppUtils';
15 sub prepare_extended_user_info {
19 $self->ctx->{user} = $self->editor->retrieve_actor_user([
20 $self->ctx->{user}->id,
24 au => [qw/card home_ou addresses ident_type billing_address/, @extra_flesh]
28 ]) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
33 # Given an event returned by a failed attempt to create a hold, do we have
34 # permission to override? XXX Should the permission check be scoped to a
35 # given org_unit context?
36 sub test_could_override {
38 my $event = $self->ctx->{"hold_failed_event"};
40 return 0 unless $event;
41 return 1 if $self->editor->allowed($event . ".override");
42 return 1 if $event->{"fail_part"} and
43 $self->editor->allowed($event->{"fail_part"} . ".override");
47 # Find out whether we care that local copies are available
48 sub local_avail_concern {
49 my ($self, $allowed, $hold_target, $hold_type, $pickup_lib) = @_;
51 my $would_block = $self->ctx->{get_org_setting}->
52 ($pickup_lib, "circ.holds.hold_has_copy_at.block");
54 $self->ctx->{get_org_setting}->
55 ($pickup_lib, "circ.holds.hold_has_copy_at.alert") and
56 not $self->cgi->param("override")
57 ) unless $would_block;
59 if ($allowed->{"success"} and ($would_block or $would_alert)) {
61 "hold_target" => $hold_target,
62 "hold_type" => $hold_type,
63 "org_unit" => $pickup_lib
65 my $local_avail = $U->simplereq(
67 "open-ils.circ.hold.has_copy_at", $self->editor->authtoken, $args
70 "copy availability information for " . Dumper($args) .
71 " is " . Dumper($local_avail)
73 if (%$local_avail) { # if hash not empty
74 $self->ctx->{hold_copy_available} = $local_avail;
75 return ($would_block, $would_alert);
83 # user : au object, fleshed
84 sub load_myopac_prefs {
86 return $self->prepare_extended_user_info || Apache2::Const::OK;
89 sub load_myopac_prefs_notify {
91 my $e = $self->editor;
93 my $user_prefs = $self->fetch_optin_prefs;
94 $user_prefs = $self->update_optin_prefs($user_prefs)
95 if $self->cgi->request_method eq 'POST';
97 $self->ctx->{opt_in_settings} = $user_prefs;
99 return Apache2::Const::OK;
102 sub fetch_optin_prefs {
104 my $e = $self->editor;
106 # fetch all of the opt-in settings the user has access to
107 # XXX: user's should in theory have options to opt-in to notices
108 # for remote locations, but that opens the door for a large
109 # set of generally un-used opt-ins.. needs discussion
110 my $opt_ins = $U->simplereq(
112 'open-ils.actor.event_def.opt_in.settings.atomic',
113 $e->authtoken, $e->requestor->home_ou);
115 # fetch user setting values for each of the opt-in settings
116 my $user_set = $U->simplereq(
118 'open-ils.actor.patron.settings.retrieve',
121 [map {$_->name} @$opt_ins]
124 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
127 sub update_optin_prefs {
129 my $user_prefs = shift;
130 my $e = $self->editor;
131 my @settings = $self->cgi->param('setting');
134 # apply now-true settings
135 for my $applied (@settings) {
136 # see if setting is already applied to this user
137 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
138 $newsets{$applied} = OpenSRF::Utils::JSON->true;
141 # remove now-false settings
142 for my $pref (grep { $_->{value} } @$user_prefs) {
143 $newsets{$pref->{cust}->name} = undef
144 unless grep { $_ eq $pref->{cust}->name } @settings;
149 'open-ils.actor.patron.settings.update',
150 $e->authtoken, $e->requestor->id, \%newsets);
152 # update the local prefs to match reality
153 for my $pref (@$user_prefs) {
154 $pref->{value} = $newsets{$pref->{cust}->name}
155 if exists $newsets{$pref->{cust}->name};
161 sub _load_user_with_prefs {
163 my $stat = $self->prepare_extended_user_info('settings');
164 return $stat if $stat; # not-OK
166 $self->ctx->{user_setting_map} = {
167 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
168 @{$self->ctx->{user}->settings}
174 sub load_myopac_prefs_settings {
177 my $stat = $self->_load_user_with_prefs;
178 return $stat if $stat;
180 return Apache2::Const::OK
181 unless $self->cgi->request_method eq 'POST';
183 # some setting values from the form don't match the
184 # required value/format for the db, so they have to be
185 # individually translated.
188 my $set_map = $self->ctx->{user_setting_map};
190 my $key = 'opac.hits_per_page';
191 my $val = $self->cgi->param($key);
192 $settings{$key}= $val unless $$set_map{$key} eq $val;
194 my $now = DateTime->now->strftime('%F');
195 for $key (qw/history.circ.retention_start history.hold.retention_start/) {
196 $val = $self->cgi->param($key);
197 if($val and $val eq 'on') {
198 # Set the start time to 'now' unless a start time already exists for the user
199 $settings{$key} = $now unless $$set_map{$key};
201 # clear the start time if one previously existed for the user
202 $settings{$key} = undef if $$set_map{$key};
206 # Send the modified settings off to be saved
209 'open-ils.actor.patron.settings.update',
210 $self->editor->authtoken, undef, \%settings);
212 # re-fetch user prefs
213 $self->ctx->{updated_user_settings} = \%settings;
214 return $self->_load_user_with_prefs || Apache2::Const::OK;
217 sub fetch_user_holds {
219 my $hold_ids = shift;
220 my $ids_only = shift;
222 my $available = shift;
226 my $e = $self->editor;
229 my $circ = OpenSRF::AppSession->create('open-ils.circ');
231 $hold_ids = $circ->request(
232 'open-ils.circ.holds.id_list.retrieve.authoritative',
238 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
242 return $hold_ids if $ids_only or @$hold_ids == 0;
245 suppress_notices => 1,
246 suppress_transits => 1,
248 suppress_patron_details => 1,
249 include_bre => $flesh ? 1 : 0
252 # ----------------------------------------------------------------
253 # Collect holds in batches of $batch_size for faster retrieval
257 my $mk_req_batch = sub {
259 my $top_idx = $batch_idx + $batch_size;
260 while($batch_idx < $top_idx) {
261 my $hold_id = $hold_ids->[$batch_idx++];
262 last unless $hold_id;
263 my $ses = OpenSRF::AppSession->create('open-ils.circ');
264 my $req = $ses->request(
265 'open-ils.circ.hold.details.retrieve',
266 $e->authtoken, $hold_id, $args);
267 push(@ses, {ses => $ses, req => $req});
273 my(@collected, @holds, @ses);
276 @ses = $mk_req_batch->() if $first;
277 last if $first and not @ses;
280 # If desired by the caller, filter any holds that are not available.
282 @collected = grep { $_->{hold}->{status} == 4 } @collected;
284 while(my $blob = pop(@collected)) {
285 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
290 for my $req_data (@ses) {
291 push(@collected, {hold => $req_data->{req}->gather(1)});
292 $req_data->{ses}->kill_me;
295 @ses = $mk_req_batch->();
296 last unless @collected or @ses;
300 # put the holds back into the original server sort order
302 for my $id (@$hold_ids) {
303 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
309 sub handle_hold_update {
312 my $hold_ids = shift;
313 my $e = $self->editor;
316 my @hold_ids = ($hold_ids) ? @$hold_ids : $self->cgi->param('hold_id'); # for non-_all actions
317 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
319 my $circ = OpenSRF::AppSession->create('open-ils.circ');
321 if($action =~ /cancel/) {
323 for my $hold_id (@hold_ids) {
324 my $resp = $circ->request(
325 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
328 } elsif ($action =~ /activate|suspend/) {
331 for my $hold_id (@hold_ids) {
332 my $vals = {id => $hold_id};
334 if($action =~ /activate/) {
335 $vals->{frozen} = 'f';
336 $vals->{thaw_date} = undef;
338 } elsif($action =~ /suspend/) {
339 $vals->{frozen} = 't';
340 # $vals->{thaw_date} = TODO;
342 push(@$vlist, $vals);
345 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
346 } elsif ($action eq 'edit') {
349 my $val = {"id" => $_};
350 $val->{"frozen"} = $self->cgi->param("frozen");
351 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
353 for my $field (qw/expire_time thaw_date/) {
354 # XXX TODO make this support other date formats, not just
356 next unless $self->cgi->param($field) =~
357 m:^(\d{2})/(\d{2})/(\d{4})$:;
358 $val->{$field} = "$3-$1-$2";
364 'open-ils.circ.hold.update.batch.atomic',
365 $e->authtoken, undef, \@vals
366 )->gather(1); # LFW XXX test for failure
367 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
371 return defined($url) ? $self->generic_redirect($url) : undef;
374 sub load_myopac_holds {
376 my $e = $self->editor;
377 my $ctx = $self->ctx;
379 my $limit = $self->cgi->param('limit') || 0;
380 my $offset = $self->cgi->param('offset') || 0;
381 my $action = $self->cgi->param('action') || '';
382 my $hold_id = $self->cgi->param('id');
383 my $available = int($self->cgi->param('available') || 0);
385 my $hold_handle_result;
386 $hold_handle_result = $self->handle_hold_update($action) if $action;
388 $ctx->{holds} = $self->fetch_user_holds($hold_id ? [$hold_id] : undef, 0, 1, $available, $limit, $offset);
390 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
393 sub load_place_hold {
395 my $ctx = $self->ctx;
396 my $gos = $ctx->{get_org_setting};
397 my $e = $self->editor;
398 my $cgi = $self->cgi;
399 $self->ctx->{page} = 'place_hold';
401 $ctx->{hold_target} = $cgi->param('hold_target');
402 $ctx->{hold_type} = $cgi->param('hold_type');
404 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # unless changed below
406 if (my $bc = $self->cgi->cookie("patron_barcode")) {
407 # passed in from staff client
408 $ctx->{patron_recipient} = $U->simplereq(
409 "open-ils.actor", "open-ils.actor.user.fleshed.retrieve_by_barcode",
410 $self->editor->authtoken, $bc
411 ) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
413 $ctx->{default_pickup_lib} = $ctx->{patron_recipient}->home_ou;
416 my $request_lib = $e->requestor->ws_ou;
418 # XXX check for failure of the retrieve_* methods called below, and
419 # possibly replace all the if,elsif with a dispatch table (meh, elegance)
422 if ($ctx->{hold_type} eq 'T') {
423 $target_field = "titleid";
424 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
425 } elsif ($ctx->{hold_type} eq 'V') {
426 $target_field = "volume_id";
427 my $vol = $e->retrieve_asset_call_number([
428 $ctx->{hold_target}, {
430 "flesh_fields" => {"acn" => ["record"]}
433 $ctx->{record} = $vol->record;
434 } elsif ($ctx->{hold_type} eq 'C') {
435 $target_field = "copy_id";
436 my $copy = $e->retrieve_asset_copy([
437 $ctx->{hold_target}, {
441 "acp" => ["call_number"]
445 $ctx->{record} = $copy->call_number->record;
446 } elsif ($ctx->{hold_type} eq 'I') {
447 $target_field = "issuanceid";
448 my $iss = $e->retrieve_serial_issuance([
449 $ctx->{hold_target}, {
452 "siss" => ["subscription"], "ssub" => ["record_entry"]
456 $ctx->{record} = $iss->subscription->record_entry;
460 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
462 if (my $pickup_lib = $cgi->param('pickup_lib')) {
463 my $requestor = $e->requestor->id;
466 if ((not $ctx->{"is_staff"}) or
467 ($cgi->param("hold_usr_is_requestor"))) {
470 my $actor = create OpenSRF::AppSession("open-ils.actor");
471 $usr = $actor->request(
472 "open-ils.actor.user.retrieve_id_by_barcode_or_username",
473 $e->authtoken, $cgi->param("hold_usr")
476 if (defined $U->event_code($usr)) {
477 $ctx->{hold_failed} = 1;
478 $ctx->{hold_failed_event} = $usr;
485 $target_field => $ctx->{"hold_target"},
486 pickup_lib => $pickup_lib,
487 hold_type => $ctx->{"hold_type"},
491 my $allowed = $U->simplereq(
493 'open-ils.circ.title_hold.is_possible',
497 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
499 my ($local_block, $local_alert) = $self->local_avail_concern(
500 $allowed, $args->{$target_field}, $args->{hold_type}, $pickup_lib
503 # Give the original CGI params back to the user in case they
504 # want to try to override something.
505 $ctx->{orig_params} = $cgi->Vars;
508 $ctx->{hold_failed} = 1;
509 $ctx->{hold_local_block} = 1;
510 } elsif ($local_alert) {
511 $ctx->{hold_failed} = 1;
512 $ctx->{hold_local_alert} = 1;
513 } elsif ($allowed->{success}) {
514 my $hold = Fieldmapper::action::hold_request->new;
516 $hold->pickup_lib($pickup_lib);
517 $hold->requestor($requestor);
519 $hold->target($ctx->{hold_target});
520 $hold->hold_type($ctx->{hold_type});
521 # frozen, expired, etc..
523 my $method = "open-ils.circ.holds.create";
524 $method .= ".override" if $cgi->param("override");
526 my $stat = $U->simplereq(
527 "open-ils.circ", $method, $e->authtoken, $hold
530 # The following did not cover all the possible return values of
531 # open-ils.circ.holds.create
532 #if($stat and $stat > 0) {
533 if ($stat and (not ref $stat) and $stat > 0) {
534 # if successful, return the user to the requesting page
535 $self->apache->log->info(
536 "Redirecting back to " . $cgi->param('redirect_to')
539 # We also clear the patron_barcode (from the staff client)
540 # cookie at this point (otherwise it haunts the staff user
541 # later). XXX todo make sure this is best; also see that
542 # template when staff mode calls xulG.opac_hold_placed()
543 return $self->generic_redirect(
546 -name => "patron_barcode",
555 $ctx->{hold_failed} = 1;
557 delete $ctx->{orig_params}{submit};
559 if (ref $stat eq 'ARRAY') {
560 $ctx->{hold_failed_event} = shift @$stat;
561 } elsif (defined $U->event_code($stat)) {
562 $ctx->{hold_failed_event} = $stat;
564 $self->apache->log->info(
565 "attempt to create hold returned $stat"
569 $ctx->{could_override} = $self->test_could_override;
571 } else { # hold *check* failed
572 $ctx->{hold_failed} = 1; # XXX process the events, etc
573 $ctx->{hold_failed_event} = $allowed->{last_event};
579 return Apache2::Const::OK;
583 sub fetch_user_circs {
585 my $flesh = shift; # flesh bib data, etc.
586 my $circ_ids = shift;
590 my $e = $self->editor;
595 @circ_ids = @$circ_ids;
599 my $circ_data = $U->simplereq(
601 'open-ils.actor.user.checked_out',
606 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
608 if($limit or $offset) {
609 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
613 return [] unless @circ_ids;
618 circ => ['target_copy'],
619 acp => ['call_number'],
625 my $circs = $e->search_action_circulation(
626 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
629 for my $circ (@$circs) {
632 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
633 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
634 undef # pre-cat copy, use the dummy title/author instead
639 # make sure the final list is in the correct order
641 for my $id (@circ_ids) {
644 (grep { $_->{circ}->id == $id } @circs)
648 return \@sorted_circs;
652 sub handle_circ_renew {
655 my $ctx = $self->ctx;
657 my @renew_ids = $self->cgi->param('circ');
659 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
661 # TODO: fire off renewal calls in batches to speed things up
663 for my $circ (@$circs) {
665 my $evt = $U->simplereq(
667 'open-ils.circ.renew',
668 $self->editor->authtoken,
670 patron_id => $self->editor->requestor->id,
671 copy_id => $circ->{circ}->target_copy,
676 # TODO return these, then insert them into the circ data
677 # blob that is shoved into the template for each circ
678 # so the template won't have to match them
679 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
686 sub load_myopac_circs {
688 my $e = $self->editor;
689 my $ctx = $self->ctx;
692 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
693 my $offset = $self->cgi->param('offset') || 0;
694 my $action = $self->cgi->param('action') || '';
696 # perform the renewal first if necessary
697 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
699 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
701 my $success_renewals = 0;
702 my $failed_renewals = 0;
703 for my $data (@{$ctx->{circs}}) {
704 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
707 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
708 $data->{renewal_response} = $evt;
709 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
710 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
714 $ctx->{success_renewals} = $success_renewals;
715 $ctx->{failed_renewals} = $failed_renewals;
717 return Apache2::Const::OK;
720 sub load_myopac_circ_history {
722 my $e = $self->editor;
723 my $ctx = $self->ctx;
724 my $limit = $self->cgi->param('limit') || 15;
725 my $offset = $self->cgi->param('offset') || 0;
727 $ctx->{circ_history_limit} = $limit;
728 $ctx->{circ_history_offset} = $offset;
730 my $circ_ids = $e->json_query({
734 transform => 'action.usr_visible_circs',
739 where => {id => $e->requestor->id},
744 $ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circ_ids]);
745 return Apache2::Const::OK;
748 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
749 sub load_myopac_hold_history {
751 my $e = $self->editor;
752 my $ctx = $self->ctx;
753 my $limit = $self->cgi->param('limit') || 15;
754 my $offset = $self->cgi->param('offset') || 0;
755 $ctx->{hold_history_limit} = $limit;
756 $ctx->{hold_history_offset} = $offset;
758 my $hold_ids = $e->json_query({
762 transform => 'action.usr_visible_holds',
767 where => {id => $e->requestor->id},
772 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$hold_ids], 0, 1, 0);
773 return Apache2::Const::OK;
776 sub load_myopac_payment_form {
780 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and return $r;
781 $r = $self->prepare_extended_user_info and return $r;
783 return Apache2::Const::OK;
786 # TODO: add other filter options as params/configs/etc.
787 sub load_myopac_payments {
789 my $limit = $self->cgi->param('limit') || 20;
790 my $offset = $self->cgi->param('offset') || 0;
791 my $e = $self->editor;
793 $self->ctx->{payment_history_limit} = $limit;
794 $self->ctx->{payment_history_offset} = $offset;
797 $args->{limit} = $limit if $limit;
798 $args->{offset} = $offset if $offset;
800 if (my $max_age = $self->ctx->{get_org_setting}->(
801 $e->requestor->home_ou, "opac.payment_history_age_limit"
803 my $min_ts = DateTime->now(
804 "time_zone" => DateTime::TimeZone->new("name" => "local"),
805 )->subtract("seconds" => interval_to_seconds($max_age))->iso8601();
807 $logger->info("XXX min_ts: $min_ts");
808 $args->{"where"} = {"payment_ts" => {">=" => $min_ts}};
811 $self->ctx->{payments} = $U->simplereq(
813 'open-ils.actor.user.payments.retrieve.atomic',
814 $e->authtoken, $e->requestor->id, $args);
816 return Apache2::Const::OK;
819 sub load_myopac_pay {
823 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and
826 # balance_owed is computed specifically from the fines we're trying
827 # to pay in this case.
828 if ($self->ctx->{fines}->{balance_owed} <= 0) {
829 $self->apache->log->info(
830 sprintf("Can't pay non-positive balance. xacts selected: (%s)",
831 join(", ", map(int, $self->cgi->param("xact"), $self->cgi->param('xact_misc'))))
833 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
836 my $cc_args = {"where_process" => 1};
838 $cc_args->{$_} = $self->cgi->param($_) for (qw/
839 number cvv2 expire_year expire_month billing_first
840 billing_last billing_address billing_city billing_state
845 "cc_args" => $cc_args,
846 "userid" => $self->ctx->{user}->id,
847 "payment_type" => "credit_card_payment",
848 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
851 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
852 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
855 $self->ctx->{"payment_response"} = $resp;
857 unless ($resp->{"textcode"}) {
858 $self->ctx->{printable_receipt} = $U->simplereq(
859 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
860 $self->editor->authtoken, $resp->{payments}
864 return Apache2::Const::OK;
867 sub load_myopac_receipt_print {
870 $self->ctx->{printable_receipt} = $U->simplereq(
871 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
872 $self->editor->authtoken, [$self->cgi->param("payment")]
875 return Apache2::Const::OK;
878 sub load_myopac_receipt_email {
881 # The following ML method doesn't actually check whether the user in
882 # question has an email address, so we do.
883 if ($self->ctx->{user}->email) {
884 $self->ctx->{email_receipt_result} = $U->simplereq(
885 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
886 $self->editor->authtoken, [$self->cgi->param("payment")]
889 $self->ctx->{email_receipt_result} =
890 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
893 return Apache2::Const::OK;
897 my ($self, $limit, $offset, $id_list) = @_;
899 # XXX TODO: check for failure after various network calls
901 # It may be unclear, but this result structure lumps circulation and
902 # reservation fines together, and keeps grocery fines separate.
903 $self->ctx->{"fines"} = {
911 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
913 # TODO: This should really be a ML call, but the existing calls
914 # return an excessive amount of data and don't offer streaming
916 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
918 my $req = $cstore->request(
919 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
921 usr => $self->editor->requestor->id,
922 balance_owed => {'!=' => 0},
923 ($id_list && @$id_list ? ("id" => $id_list) : ()),
928 mobts => [qw/grocery circulation reservation/],
929 bresv => ['target_resource_type'],
933 circ => ['target_copy'],
934 acp => ['call_number'],
937 order_by => { mobts => 'xact_start' },
942 my @total_keys = qw/total_paid total_owed balance_owed/;
943 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
945 while(my $resp = $req->recv) {
946 my $mobts = $resp->content;
947 my $circ = $mobts->circulation;
950 if($mobts->grocery) {
951 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
952 $last_billing = pop(@billings);
955 # XXX TODO confirm that the following, and the later division by 100.0
956 # to get a floating point representation once again, is sufficiently
958 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
960 my $marc_xml = undef;
961 if ($mobts->xact_type eq 'reservation' and
962 $mobts->reservation->target_resource_type->record) {
963 $marc_xml = XML::LibXML->new->parse_string(
964 $mobts->reservation->target_resource_type->record->marc
966 } elsif ($mobts->xact_type eq 'circulation' and
967 $circ->target_copy->call_number->id != -1) {
968 $marc_xml = XML::LibXML->new->parse_string(
969 $circ->target_copy->call_number->record->marc
974 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
977 last_grocery_billing => $last_billing,
978 marc_xml => $marc_xml
985 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
989 sub prepare_fines_for_payment {
990 # This assumes $self->prepare_fines has already been run
994 if ($self->ctx->{fines}) {
995 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
996 @{$self->ctx->{fines}->{circulation}},
997 @{$self->ctx->{fines}->{grocery}}
1004 sub load_myopac_main {
1006 my $limit = $self->cgi->param('limit') || 0;
1007 my $offset = $self->cgi->param('offset') || 0;
1009 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
1012 sub load_myopac_update_email {
1014 my $e = $self->editor;
1015 my $ctx = $self->ctx;
1016 my $email = $self->cgi->param('email') || '';
1018 # needed for most up-to-date email address
1019 if (my $r = $self->prepare_extended_user_info) { return $r };
1021 return Apache2::Const::OK
1022 unless $self->cgi->request_method eq 'POST';
1024 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
1025 $ctx->{invalid_email} = $email;
1026 return Apache2::Const::OK;
1029 my $stat = $U->simplereq(
1031 'open-ils.actor.user.email.update',
1032 $e->authtoken, $email);
1034 unless ($self->cgi->param("redirect_to")) {
1035 my $url = $self->apache->unparsed_uri;
1036 $url =~ s/update_email/prefs/;
1038 return $self->generic_redirect($url);
1041 return $self->generic_redirect;
1044 sub load_myopac_update_username {
1046 my $e = $self->editor;
1047 my $ctx = $self->ctx;
1048 my $username = $self->cgi->param('username') || '';
1050 return Apache2::Const::OK
1051 unless $self->cgi->request_method eq 'POST';
1053 unless($username and $username !~ /\s/) { # any other username restrictions?
1054 $ctx->{invalid_username} = $username;
1055 return Apache2::Const::OK;
1058 if($username ne $e->requestor->usrname) {
1060 my $evt = $U->simplereq(
1062 'open-ils.actor.user.username.update',
1063 $e->authtoken, $username);
1065 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
1066 $ctx->{username_exists} = $username;
1067 return Apache2::Const::OK;
1071 my $url = $self->apache->unparsed_uri;
1072 $url =~ s/update_username/prefs/;
1074 return $self->generic_redirect($url);
1077 sub load_myopac_update_password {
1079 my $e = $self->editor;
1080 my $ctx = $self->ctx;
1082 return Apache2::Const::OK
1083 unless $self->cgi->request_method eq 'POST';
1085 my $current_pw = $self->cgi->param('current_pw') || '';
1086 my $new_pw = $self->cgi->param('new_pw') || '';
1087 my $new_pw2 = $self->cgi->param('new_pw2') || '';
1089 unless($new_pw eq $new_pw2) {
1090 $ctx->{password_nomatch} = 1;
1091 return Apache2::Const::OK;
1094 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
1096 if($pw_regex and $new_pw !~ /$pw_regex/) {
1097 $ctx->{password_invalid} = 1;
1098 return Apache2::Const::OK;
1101 my $evt = $U->simplereq(
1103 'open-ils.actor.user.password.update',
1104 $e->authtoken, $new_pw, $current_pw);
1107 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
1108 $ctx->{password_incorrect} = 1;
1109 return Apache2::Const::OK;
1112 my $url = $self->apache->unparsed_uri;
1113 $url =~ s/update_password/prefs/;
1115 return $self->generic_redirect($url);
1118 sub load_myopac_bookbags {
1120 my $e = $self->editor;
1121 my $ctx = $self->ctx;
1123 $e->xact_begin; # replication...
1125 my $rv = $self->load_mylist;
1126 unless($rv eq Apache2::Const::OK) {
1132 order_by => {cbreb => 'name'},
1133 limit => $self->cgi->param('limit') || 10,
1134 offset => $self->cgi->param('offset') || 0
1137 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
1138 {owner => $self->editor->requestor->id, btype => 'bookbag'},
1139 # XXX what to do about the possibility of really large bookbags here?
1140 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
1143 if(!$ctx->{bookbags}) {
1145 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1148 # get unique record IDs
1150 foreach my $bbag (@{$ctx->{bookbags}}) {
1151 foreach my $rec_id (
1152 map { $_->target_biblio_record_entry } @{$bbag->items}
1154 $rec_ids{$rec_id} = 1;
1158 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
1161 return Apache2::Const::OK;
1165 # actions are create, delete, show, hide, rename, add_rec, delete_item
1166 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
1167 sub load_myopac_bookbag_update {
1168 my ($self, $action, $list_id) = @_;
1169 my $e = $self->editor;
1170 my $cgi = $self->cgi;
1172 $action ||= $cgi->param('action');
1173 $list_id ||= $cgi->param('list');
1175 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
1176 my @del_item = $cgi->param('del_item');
1177 my $shared = $cgi->param('shared');
1178 my $name = $cgi->param('name');
1182 if($action eq 'create') {
1183 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
1185 $list->owner($e->requestor->id);
1186 $list->btype('bookbag');
1187 $list->pub($shared ? 't' : 'f');
1188 $success = $U->simplereq('open-ils.actor',
1189 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
1193 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
1195 return Apache2::Const::HTTP_BAD_REQUEST unless
1196 $list and $list->owner == $e->requestor->id;
1199 if($action eq 'delete') {
1200 $success = $U->simplereq('open-ils.actor',
1201 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
1203 } elsif($action eq 'show') {
1204 unless($U->is_true($list->pub)) {
1206 $success = $U->simplereq('open-ils.actor',
1207 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1210 } elsif($action eq 'hide') {
1211 if($U->is_true($list->pub)) {
1213 $success = $U->simplereq('open-ils.actor',
1214 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1217 } elsif($action eq 'rename') {
1220 $success = $U->simplereq('open-ils.actor',
1221 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1224 } elsif($action eq 'add_rec') {
1225 foreach my $add_rec (@add_rec) {
1226 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
1227 $item->bucket($list_id);
1228 $item->target_biblio_record_entry($add_rec);
1229 $success = $U->simplereq('open-ils.actor',
1230 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
1231 last unless $success;
1234 } elsif($action eq 'del_item') {
1235 foreach (@del_item) {
1236 $success = $U->simplereq(
1238 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
1240 last unless $success;
1244 return $self->generic_redirect if $success;
1246 $self->ctx->{bucket_action} = $action;
1247 $self->ctx->{bucket_action_failed} = 1;
1248 return Apache2::Const::OK;