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 {
37 my ($self, $event) = @_;
39 return 0 unless $event;
40 return 1 if $self->editor->allowed($event->{textcode} . ".override");
41 return 1 if $event->{"fail_part"} and
42 $self->editor->allowed($event->{"fail_part"} . ".override");
46 # Find out whether we care that local copies are available
47 sub local_avail_concern {
48 my ($self, $hold_target, $hold_type, $pickup_lib) = @_;
50 my $would_block = $self->ctx->{get_org_setting}->
51 ($pickup_lib, "circ.holds.hold_has_copy_at.block");
53 $self->ctx->{get_org_setting}->
54 ($pickup_lib, "circ.holds.hold_has_copy_at.alert") and
55 not $self->cgi->param("override")
56 ) unless $would_block;
58 if ($would_block or $would_alert) {
60 "hold_target" => $hold_target,
61 "hold_type" => $hold_type,
62 "org_unit" => $pickup_lib
64 my $local_avail = $U->simplereq(
66 "open-ils.circ.hold.has_copy_at", $self->editor->authtoken, $args
69 "copy availability information for " . Dumper($args) .
70 " is " . Dumper($local_avail)
72 if (%$local_avail) { # if hash not empty
73 $self->ctx->{hold_copy_available} = $local_avail;
74 return ($would_block, $would_alert);
82 # user : au object, fleshed
83 sub load_myopac_prefs {
85 return $self->prepare_extended_user_info || Apache2::Const::OK;
88 sub load_myopac_prefs_notify {
90 my $e = $self->editor;
92 my $user_prefs = $self->fetch_optin_prefs;
93 $user_prefs = $self->update_optin_prefs($user_prefs)
94 if $self->cgi->request_method eq 'POST';
96 $self->ctx->{opt_in_settings} = $user_prefs;
98 return Apache2::Const::OK;
101 sub fetch_optin_prefs {
103 my $e = $self->editor;
105 # fetch all of the opt-in settings the user has access to
106 # XXX: user's should in theory have options to opt-in to notices
107 # for remote locations, but that opens the door for a large
108 # set of generally un-used opt-ins.. needs discussion
109 my $opt_ins = $U->simplereq(
111 'open-ils.actor.event_def.opt_in.settings.atomic',
112 $e->authtoken, $e->requestor->home_ou);
114 # fetch user setting values for each of the opt-in settings
115 my $user_set = $U->simplereq(
117 'open-ils.actor.patron.settings.retrieve',
120 [map {$_->name} @$opt_ins]
123 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
126 sub update_optin_prefs {
128 my $user_prefs = shift;
129 my $e = $self->editor;
130 my @settings = $self->cgi->param('setting');
133 # apply now-true settings
134 for my $applied (@settings) {
135 # see if setting is already applied to this user
136 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
137 $newsets{$applied} = OpenSRF::Utils::JSON->true;
140 # remove now-false settings
141 for my $pref (grep { $_->{value} } @$user_prefs) {
142 $newsets{$pref->{cust}->name} = undef
143 unless grep { $_ eq $pref->{cust}->name } @settings;
148 'open-ils.actor.patron.settings.update',
149 $e->authtoken, $e->requestor->id, \%newsets);
151 # update the local prefs to match reality
152 for my $pref (@$user_prefs) {
153 $pref->{value} = $newsets{$pref->{cust}->name}
154 if exists $newsets{$pref->{cust}->name};
160 sub _load_user_with_prefs {
162 my $stat = $self->prepare_extended_user_info('settings');
163 return $stat if $stat; # not-OK
165 $self->ctx->{user_setting_map} = {
166 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
167 @{$self->ctx->{user}->settings}
173 sub load_myopac_prefs_settings {
176 my $stat = $self->_load_user_with_prefs;
177 return $stat if $stat;
179 return Apache2::Const::OK
180 unless $self->cgi->request_method eq 'POST';
182 # some setting values from the form don't match the
183 # required value/format for the db, so they have to be
184 # individually translated.
187 my $set_map = $self->ctx->{user_setting_map};
189 my $key = 'opac.hits_per_page';
190 my $val = $self->cgi->param($key);
191 $settings{$key}= $val unless $$set_map{$key} eq $val;
193 my $now = DateTime->now->strftime('%F');
194 for $key (qw/history.circ.retention_start history.hold.retention_start/) {
195 $val = $self->cgi->param($key);
196 if($val and $val eq 'on') {
197 # Set the start time to 'now' unless a start time already exists for the user
198 $settings{$key} = $now unless $$set_map{$key};
200 # clear the start time if one previously existed for the user
201 $settings{$key} = undef if $$set_map{$key};
205 # Send the modified settings off to be saved
208 'open-ils.actor.patron.settings.update',
209 $self->editor->authtoken, undef, \%settings);
211 # re-fetch user prefs
212 $self->ctx->{updated_user_settings} = \%settings;
213 return $self->_load_user_with_prefs || Apache2::Const::OK;
216 sub fetch_user_holds {
218 my $hold_ids = shift;
219 my $ids_only = shift;
221 my $available = shift;
225 my $e = $self->editor;
228 my $circ = OpenSRF::AppSession->create('open-ils.circ');
230 $hold_ids = $circ->request(
231 'open-ils.circ.holds.id_list.retrieve.authoritative',
237 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
241 return $hold_ids if $ids_only or @$hold_ids == 0;
244 suppress_notices => 1,
245 suppress_transits => 1,
247 suppress_patron_details => 1,
248 include_bre => $flesh ? 1 : 0
251 # ----------------------------------------------------------------
252 # Collect holds in batches of $batch_size for faster retrieval
256 my $mk_req_batch = sub {
258 my $top_idx = $batch_idx + $batch_size;
259 while($batch_idx < $top_idx) {
260 my $hold_id = $hold_ids->[$batch_idx++];
261 last unless $hold_id;
262 my $ses = OpenSRF::AppSession->create('open-ils.circ');
263 my $req = $ses->request(
264 'open-ils.circ.hold.details.retrieve',
265 $e->authtoken, $hold_id, $args);
266 push(@ses, {ses => $ses, req => $req});
272 my(@collected, @holds, @ses);
275 @ses = $mk_req_batch->() if $first;
276 last if $first and not @ses;
279 # If desired by the caller, filter any holds that are not available.
281 @collected = grep { $_->{hold}->{status} == 4 } @collected;
283 while(my $blob = pop(@collected)) {
284 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
289 for my $req_data (@ses) {
290 push(@collected, {hold => $req_data->{req}->gather(1)});
291 $req_data->{ses}->kill_me;
294 @ses = $mk_req_batch->();
295 last unless @collected or @ses;
299 # put the holds back into the original server sort order
301 for my $id (@$hold_ids) {
302 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
308 sub handle_hold_update {
311 my $hold_ids = shift;
312 my $e = $self->editor;
315 my @hold_ids = ($hold_ids) ? @$hold_ids : $self->cgi->param('hold_id'); # for non-_all actions
316 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
318 my $circ = OpenSRF::AppSession->create('open-ils.circ');
320 if($action =~ /cancel/) {
322 for my $hold_id (@hold_ids) {
323 my $resp = $circ->request(
324 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
327 } elsif ($action =~ /activate|suspend/) {
330 for my $hold_id (@hold_ids) {
331 my $vals = {id => $hold_id};
333 if($action =~ /activate/) {
334 $vals->{frozen} = 'f';
335 $vals->{thaw_date} = undef;
337 } elsif($action =~ /suspend/) {
338 $vals->{frozen} = 't';
339 # $vals->{thaw_date} = TODO;
341 push(@$vlist, $vals);
344 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
345 } elsif ($action eq 'edit') {
348 my $val = {"id" => $_};
349 $val->{"frozen"} = $self->cgi->param("frozen");
350 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
352 for my $field (qw/expire_time thaw_date/) {
353 # XXX TODO make this support other date formats, not just
355 next unless $self->cgi->param($field) =~
356 m:^(\d{2})/(\d{2})/(\d{4})$:;
357 $val->{$field} = "$3-$1-$2";
363 'open-ils.circ.hold.update.batch.atomic',
364 $e->authtoken, undef, \@vals
365 )->gather(1); # LFW XXX test for failure
366 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
370 return defined($url) ? $self->generic_redirect($url) : undef;
373 sub load_myopac_holds {
375 my $e = $self->editor;
376 my $ctx = $self->ctx;
378 my $limit = $self->cgi->param('limit') || 0;
379 my $offset = $self->cgi->param('offset') || 0;
380 my $action = $self->cgi->param('action') || '';
381 my $hold_id = $self->cgi->param('id');
382 my $available = int($self->cgi->param('available') || 0);
384 my $hold_handle_result;
385 $hold_handle_result = $self->handle_hold_update($action) if $action;
387 $ctx->{holds} = $self->fetch_user_holds($hold_id ? [$hold_id] : undef, 0, 1, $available, $limit, $offset);
389 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
392 sub load_place_hold {
394 my $ctx = $self->ctx;
395 my $gos = $ctx->{get_org_setting};
396 my $e = $self->editor;
397 my $cgi = $self->cgi;
399 $self->ctx->{page} = 'place_hold';
400 my @targets = $cgi->param('hold_target');
401 $ctx->{hold_type} = $cgi->param('hold_type');
402 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # unless changed below
404 return $self->post_hold_redirect unless @targets;
406 $logger->info("Looking at hold targets: @targets");
408 # if the staff client provides a patron barcode, fetch the patron
409 if (my $bc = $self->cgi->cookie("patron_barcode")) {
410 $ctx->{patron_recipient} = $U->simplereq(
411 "open-ils.actor", "open-ils.actor.user.fleshed.retrieve_by_barcode",
412 $self->editor->authtoken, $bc
413 ) or return Apache2::Const::HTTP_BAD_REQUEST;
415 $ctx->{default_pickup_lib} = $ctx->{patron_recipient}->home_ou;
418 my $request_lib = $e->requestor->ws_ou;
420 $ctx->{hold_data} = \@hold_data;
422 my $type_dispatch = {
424 my $recs = $e->batch_retrieve_biblio_record_entry(\@targets, {substream => 1});
425 for my $id (@targets) { # force back into the correct order
426 my ($rec) = grep {$_->id eq $id} @$recs;
427 push(@hold_data, {target => $rec, record => $rec});
431 my $vols = $e->batch_retrieve_asset_call_number([
434 "flesh_fields" => {"acn" => ["record"]}
436 ], {substream => 1});
438 for my $id (@targets) {
439 my ($vol) = grep {$_->id eq $id} @$vols;
440 push(@hold_data, {target => $vol, record => $vol->record});
444 my $copies = $e->batch_retrieve_asset_copy([
449 "acp" => ["call_number"]
452 ], {substream => 1});
454 for my $id (@targets) {
455 my ($copy) = grep {$_->id eq $id} @$copies;
456 push(@hold_data, {target => $copy, record => $copy->call_number->record});
460 my $isses = $e->batch_retrieve_serial_issuance([
464 "siss" => ["subscription"], "ssub" => ["record_entry"]
467 ], {substream => 1});
469 for my $id (@targets) {
470 my ($iss) = grep {$_->id eq $id} @$isses;
471 push(@hold_data, {target => $iss, record => $iss->subscription->record_entry});
476 }->{$ctx->{hold_type}}->();
478 # caller sent bad target IDs or the wrong hold type
479 return Apache2::Const::HTTP_BAD_REQUEST unless @hold_data;
481 # generate the MARC xml for each record
482 $_->{marc_xml} = XML::LibXML->new->parse_string($_->{record}->marc) for @hold_data;
484 my $pickup_lib = $cgi->param('pickup_lib');
485 # no pickup lib means no holds placement
486 return Apache2::Const::OK unless $pickup_lib;
488 $ctx->{hold_attempt_made} = 1;
490 # Give the original CGI params back to the user in case they
491 # want to try to override something.
492 $ctx->{orig_params} = $cgi->Vars;
493 delete $ctx->{orig_params}{submit};
494 delete $ctx->{orig_params}{hold_target};
496 my $usr = $e->requestor->id;
498 if ($ctx->{is_staff} and !$cgi->param("hold_usr_is_requestor")) {
499 # find the real hold target
501 $usr = $U->simplereq(
503 "open-ils.actor.user.retrieve_id_by_barcode_or_username",
504 $e->authtoken, $cgi->param("hold_usr"));
506 if (defined $U->event_code($usr)) {
507 $ctx->{hold_failed} = 1;
508 $ctx->{hold_failed_event} = $usr;
512 # First see if we should warn/block for any holds that
513 # might have locally available items.
514 for my $hdata (@hold_data) {
515 my ($local_block, $local_alert) = $self->local_avail_concern(
516 $hdata->{target}->id, $ctx->{hold_type}, $pickup_lib);
519 $hdata->{hold_failed} = 1;
520 $hdata->{hold_local_block} = 1;
521 } elsif ($local_alert) {
522 $hdata->{hold_failed} = 1;
523 $hdata->{hold_local_alert} = 1;
528 my $method = 'open-ils.circ.holds.test_and_create.batch';
529 $method .= '.override' if $cgi->param('override');
531 my @create_targets = map {$_->{target}->id} (grep { !$_->{hold_failed} } @hold_data);
533 if(@create_targets) {
535 my $bses = OpenSRF::AppSession->create('open-ils.circ');
536 my $breq = $bses->request(
540 pickup_lib => $pickup_lib,
541 hold_type => $ctx->{hold_type}
546 while (my $resp = $breq->recv) {
548 $resp = $resp->content;
549 $logger->info('batch hold placement result: ' . OpenSRF::Utils::JSON->perl2JSON($resp));
551 if ($U->event_code($resp)) {
552 $ctx->{general_hold_error} = $resp;
556 my ($hdata) = grep {$_->{target}->id eq $resp->{target}} @hold_data;
557 my $result = $resp->{result};
559 if ($U->event_code($result)) {
560 # e.g. permission denied
561 $hdata->{hold_failed} = 1;
562 $hdata->{hold_failed_event} = $result;
566 if(not ref $result and $result > 0) {
567 # successul hold returns the hold ID
569 $hdata->{hold_success} = $result;
572 # hold-specific failure event
573 $hdata->{hold_failed} = 1;
574 $hdata->{hold_failed_event} = $result->{last_event};
575 $hdata->{could_override} = $self->test_could_override($hdata->{hold_failed_event});
583 # stay on the current page and display the results
584 return Apache2::Const::OK if
585 (grep {$_->{hold_failed}} @hold_data) or $ctx->{general_hold_error};
587 # if successful, do some cleanup and return the
588 # user to the requesting page.
590 return $self->post_hold_redirect;
593 sub post_hold_redirect {
596 # We also clear the patron_barcode (from the staff client)
597 # cookie at this point (otherwise it haunts the staff user
598 # later). XXX todo make sure this is best; also see that
599 # template when staff mode calls xulG.opac_hold_placed()
600 return $self->generic_redirect(
603 -name => "patron_barcode",
613 sub fetch_user_circs {
615 my $flesh = shift; # flesh bib data, etc.
616 my $circ_ids = shift;
620 my $e = $self->editor;
625 @circ_ids = @$circ_ids;
629 my $circ_data = $U->simplereq(
631 'open-ils.actor.user.checked_out',
636 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
638 if($limit or $offset) {
639 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
643 return [] unless @circ_ids;
648 circ => ['target_copy'],
649 acp => ['call_number'],
655 my $circs = $e->search_action_circulation(
656 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
659 for my $circ (@$circs) {
662 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
663 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
664 undef # pre-cat copy, use the dummy title/author instead
669 # make sure the final list is in the correct order
671 for my $id (@circ_ids) {
674 (grep { $_->{circ}->id == $id } @circs)
678 return \@sorted_circs;
682 sub handle_circ_renew {
685 my $ctx = $self->ctx;
687 my @renew_ids = $self->cgi->param('circ');
689 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
691 # TODO: fire off renewal calls in batches to speed things up
693 for my $circ (@$circs) {
695 my $evt = $U->simplereq(
697 'open-ils.circ.renew',
698 $self->editor->authtoken,
700 patron_id => $self->editor->requestor->id,
701 copy_id => $circ->{circ}->target_copy,
706 # TODO return these, then insert them into the circ data
707 # blob that is shoved into the template for each circ
708 # so the template won't have to match them
709 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
716 sub load_myopac_circs {
718 my $e = $self->editor;
719 my $ctx = $self->ctx;
722 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
723 my $offset = $self->cgi->param('offset') || 0;
724 my $action = $self->cgi->param('action') || '';
726 # perform the renewal first if necessary
727 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
729 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
731 my $success_renewals = 0;
732 my $failed_renewals = 0;
733 for my $data (@{$ctx->{circs}}) {
734 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
737 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
738 $data->{renewal_response} = $evt;
739 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
740 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
744 $ctx->{success_renewals} = $success_renewals;
745 $ctx->{failed_renewals} = $failed_renewals;
747 return Apache2::Const::OK;
750 sub load_myopac_circ_history {
752 my $e = $self->editor;
753 my $ctx = $self->ctx;
754 my $limit = $self->cgi->param('limit') || 15;
755 my $offset = $self->cgi->param('offset') || 0;
757 $ctx->{circ_history_limit} = $limit;
758 $ctx->{circ_history_offset} = $offset;
760 my $circ_ids = $e->json_query({
764 transform => 'action.usr_visible_circs',
769 where => {id => $e->requestor->id},
774 $ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circ_ids]);
775 return Apache2::Const::OK;
778 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
779 sub load_myopac_hold_history {
781 my $e = $self->editor;
782 my $ctx = $self->ctx;
783 my $limit = $self->cgi->param('limit') || 15;
784 my $offset = $self->cgi->param('offset') || 0;
785 $ctx->{hold_history_limit} = $limit;
786 $ctx->{hold_history_offset} = $offset;
788 my $hold_ids = $e->json_query({
792 transform => 'action.usr_visible_holds',
797 where => {id => $e->requestor->id},
802 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$hold_ids], 0, 1, 0);
803 return Apache2::Const::OK;
806 sub load_myopac_payment_form {
810 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and return $r;
811 $r = $self->prepare_extended_user_info and return $r;
813 return Apache2::Const::OK;
816 # TODO: add other filter options as params/configs/etc.
817 sub load_myopac_payments {
819 my $limit = $self->cgi->param('limit') || 20;
820 my $offset = $self->cgi->param('offset') || 0;
821 my $e = $self->editor;
823 $self->ctx->{payment_history_limit} = $limit;
824 $self->ctx->{payment_history_offset} = $offset;
827 $args->{limit} = $limit if $limit;
828 $args->{offset} = $offset if $offset;
830 if (my $max_age = $self->ctx->{get_org_setting}->(
831 $e->requestor->home_ou, "opac.payment_history_age_limit"
833 my $min_ts = DateTime->now(
834 "time_zone" => DateTime::TimeZone->new("name" => "local"),
835 )->subtract("seconds" => interval_to_seconds($max_age))->iso8601();
837 $logger->info("XXX min_ts: $min_ts");
838 $args->{"where"} = {"payment_ts" => {">=" => $min_ts}};
841 $self->ctx->{payments} = $U->simplereq(
843 'open-ils.actor.user.payments.retrieve.atomic',
844 $e->authtoken, $e->requestor->id, $args);
846 return Apache2::Const::OK;
849 sub load_myopac_pay {
853 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and
856 # balance_owed is computed specifically from the fines we're trying
857 # to pay in this case.
858 if ($self->ctx->{fines}->{balance_owed} <= 0) {
859 $self->apache->log->info(
860 sprintf("Can't pay non-positive balance. xacts selected: (%s)",
861 join(", ", map(int, $self->cgi->param("xact"), $self->cgi->param('xact_misc'))))
863 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
866 my $cc_args = {"where_process" => 1};
868 $cc_args->{$_} = $self->cgi->param($_) for (qw/
869 number cvv2 expire_year expire_month billing_first
870 billing_last billing_address billing_city billing_state
875 "cc_args" => $cc_args,
876 "userid" => $self->ctx->{user}->id,
877 "payment_type" => "credit_card_payment",
878 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
881 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
882 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
885 $self->ctx->{"payment_response"} = $resp;
887 unless ($resp->{"textcode"}) {
888 $self->ctx->{printable_receipt} = $U->simplereq(
889 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
890 $self->editor->authtoken, $resp->{payments}
894 return Apache2::Const::OK;
897 sub load_myopac_receipt_print {
900 $self->ctx->{printable_receipt} = $U->simplereq(
901 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
902 $self->editor->authtoken, [$self->cgi->param("payment")]
905 return Apache2::Const::OK;
908 sub load_myopac_receipt_email {
911 # The following ML method doesn't actually check whether the user in
912 # question has an email address, so we do.
913 if ($self->ctx->{user}->email) {
914 $self->ctx->{email_receipt_result} = $U->simplereq(
915 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
916 $self->editor->authtoken, [$self->cgi->param("payment")]
919 $self->ctx->{email_receipt_result} =
920 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
923 return Apache2::Const::OK;
927 my ($self, $limit, $offset, $id_list) = @_;
929 # XXX TODO: check for failure after various network calls
931 # It may be unclear, but this result structure lumps circulation and
932 # reservation fines together, and keeps grocery fines separate.
933 $self->ctx->{"fines"} = {
941 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
943 # TODO: This should really be a ML call, but the existing calls
944 # return an excessive amount of data and don't offer streaming
946 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
948 my $req = $cstore->request(
949 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
951 usr => $self->editor->requestor->id,
952 balance_owed => {'!=' => 0},
953 ($id_list && @$id_list ? ("id" => $id_list) : ()),
958 mobts => [qw/grocery circulation reservation/],
959 bresv => ['target_resource_type'],
963 circ => ['target_copy'],
964 acp => ['call_number'],
967 order_by => { mobts => 'xact_start' },
972 my @total_keys = qw/total_paid total_owed balance_owed/;
973 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
975 while(my $resp = $req->recv) {
976 my $mobts = $resp->content;
977 my $circ = $mobts->circulation;
980 if($mobts->grocery) {
981 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
982 $last_billing = pop(@billings);
985 # XXX TODO confirm that the following, and the later division by 100.0
986 # to get a floating point representation once again, is sufficiently
988 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
990 my $marc_xml = undef;
991 if ($mobts->xact_type eq 'reservation' and
992 $mobts->reservation->target_resource_type->record) {
993 $marc_xml = XML::LibXML->new->parse_string(
994 $mobts->reservation->target_resource_type->record->marc
996 } elsif ($mobts->xact_type eq 'circulation' and
997 $circ->target_copy->call_number->id != -1) {
998 $marc_xml = XML::LibXML->new->parse_string(
999 $circ->target_copy->call_number->record->marc
1004 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
1007 last_grocery_billing => $last_billing,
1008 marc_xml => $marc_xml
1015 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
1019 sub prepare_fines_for_payment {
1020 # This assumes $self->prepare_fines has already been run
1024 if ($self->ctx->{fines}) {
1025 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
1026 @{$self->ctx->{fines}->{circulation}},
1027 @{$self->ctx->{fines}->{grocery}}
1034 sub load_myopac_main {
1036 my $limit = $self->cgi->param('limit') || 0;
1037 my $offset = $self->cgi->param('offset') || 0;
1039 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
1042 sub load_myopac_update_email {
1044 my $e = $self->editor;
1045 my $ctx = $self->ctx;
1046 my $email = $self->cgi->param('email') || '';
1048 # needed for most up-to-date email address
1049 if (my $r = $self->prepare_extended_user_info) { return $r };
1051 return Apache2::Const::OK
1052 unless $self->cgi->request_method eq 'POST';
1054 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
1055 $ctx->{invalid_email} = $email;
1056 return Apache2::Const::OK;
1059 my $stat = $U->simplereq(
1061 'open-ils.actor.user.email.update',
1062 $e->authtoken, $email);
1064 unless ($self->cgi->param("redirect_to")) {
1065 my $url = $self->apache->unparsed_uri;
1066 $url =~ s/update_email/prefs/;
1068 return $self->generic_redirect($url);
1071 return $self->generic_redirect;
1074 sub load_myopac_update_username {
1076 my $e = $self->editor;
1077 my $ctx = $self->ctx;
1078 my $username = $self->cgi->param('username') || '';
1080 return Apache2::Const::OK
1081 unless $self->cgi->request_method eq 'POST';
1083 unless($username and $username !~ /\s/) { # any other username restrictions?
1084 $ctx->{invalid_username} = $username;
1085 return Apache2::Const::OK;
1088 if($username ne $e->requestor->usrname) {
1090 my $evt = $U->simplereq(
1092 'open-ils.actor.user.username.update',
1093 $e->authtoken, $username);
1095 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
1096 $ctx->{username_exists} = $username;
1097 return Apache2::Const::OK;
1101 my $url = $self->apache->unparsed_uri;
1102 $url =~ s/update_username/prefs/;
1104 return $self->generic_redirect($url);
1107 sub load_myopac_update_password {
1109 my $e = $self->editor;
1110 my $ctx = $self->ctx;
1112 return Apache2::Const::OK
1113 unless $self->cgi->request_method eq 'POST';
1115 my $current_pw = $self->cgi->param('current_pw') || '';
1116 my $new_pw = $self->cgi->param('new_pw') || '';
1117 my $new_pw2 = $self->cgi->param('new_pw2') || '';
1119 unless($new_pw eq $new_pw2) {
1120 $ctx->{password_nomatch} = 1;
1121 return Apache2::Const::OK;
1124 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
1126 if($pw_regex and $new_pw !~ /$pw_regex/) {
1127 $ctx->{password_invalid} = 1;
1128 return Apache2::Const::OK;
1131 my $evt = $U->simplereq(
1133 'open-ils.actor.user.password.update',
1134 $e->authtoken, $new_pw, $current_pw);
1137 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
1138 $ctx->{password_incorrect} = 1;
1139 return Apache2::Const::OK;
1142 my $url = $self->apache->unparsed_uri;
1143 $url =~ s/update_password/prefs/;
1145 return $self->generic_redirect($url);
1148 sub load_myopac_bookbags {
1150 my $e = $self->editor;
1151 my $ctx = $self->ctx;
1153 $e->xact_begin; # replication...
1155 my $rv = $self->load_mylist;
1156 unless($rv eq Apache2::Const::OK) {
1162 order_by => {cbreb => 'name'},
1163 limit => $self->cgi->param('limit') || 10,
1164 offset => $self->cgi->param('offset') || 0
1167 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket(
1169 {owner => $self->editor->requestor->id, btype => 'bookbag'},
1170 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
1175 if(!$ctx->{bookbags}) {
1177 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1180 # get unique record IDs
1182 foreach my $bbag (@{$ctx->{bookbags}}) {
1183 foreach my $rec_id (
1184 map { $_->target_biblio_record_entry } @{$bbag->items}
1186 $rec_ids{$rec_id} = 1;
1190 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
1193 return Apache2::Const::OK;
1197 # actions are create, delete, show, hide, rename, add_rec, delete_item, place_hold
1198 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
1199 sub load_myopac_bookbag_update {
1200 my ($self, $action, $list_id, @hold_recs) = @_;
1201 my $e = $self->editor;
1202 my $cgi = $self->cgi;
1204 $action ||= $cgi->param('action');
1205 $list_id ||= $cgi->param('list');
1207 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
1208 my @selected_item = $cgi->param('selected_item');
1209 my $shared = $cgi->param('shared');
1210 my $name = $cgi->param('name');
1214 if($action eq 'create') {
1215 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
1217 $list->owner($e->requestor->id);
1218 $list->btype('bookbag');
1219 $list->pub($shared ? 't' : 'f');
1220 $success = $U->simplereq('open-ils.actor',
1221 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
1223 } elsif($action eq 'place_hold') {
1225 # @hold_recs comes from anon lists redirect; selected_itesm comes from existing buckets
1226 unless (@hold_recs) {
1227 if (@selected_item) {
1228 my $items = $e->search_container_biblio_record_entry_bucket_item({id => \@selected_item});
1229 @hold_recs = map { $_->target_biblio_record_entry } @$items;
1233 return Apache2::Const::OK unless @hold_recs;
1234 $logger->info("placing holds from list page on: @hold_recs");
1236 my $url = $self->ctx->{opac_root} . '/place_hold?hold_type=T';
1237 $url .= ';hold_target=' . $_ for @hold_recs;
1238 return $self->generic_redirect($url);
1242 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
1244 return Apache2::Const::HTTP_BAD_REQUEST unless
1245 $list and $list->owner == $e->requestor->id;
1248 if($action eq 'delete') {
1249 $success = $U->simplereq('open-ils.actor',
1250 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
1252 } elsif($action eq 'show') {
1253 unless($U->is_true($list->pub)) {
1255 $success = $U->simplereq('open-ils.actor',
1256 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1259 } elsif($action eq 'hide') {
1260 if($U->is_true($list->pub)) {
1262 $success = $U->simplereq('open-ils.actor',
1263 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1266 } elsif($action eq 'rename') {
1269 $success = $U->simplereq('open-ils.actor',
1270 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1273 } elsif($action eq 'add_rec') {
1274 foreach my $add_rec (@add_rec) {
1275 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
1276 $item->bucket($list_id);
1277 $item->target_biblio_record_entry($add_rec);
1278 $success = $U->simplereq('open-ils.actor',
1279 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
1280 last unless $success;
1283 } elsif($action eq 'del_item') {
1284 foreach (@selected_item) {
1285 $success = $U->simplereq(
1287 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
1289 last unless $success;
1293 return $self->generic_redirect if $success;
1295 $self->ctx->{bucket_action} = $action;
1296 $self->ctx->{bucket_action_failed} = 1;
1297 return Apache2::Const::OK;