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 {
18 my $e = $self->editor;
20 # are we already in a transaction?
21 my $local_xact = !$e->{xact_id};
22 $e->xact_begin if $local_xact;
24 $self->ctx->{user} = $self->editor->retrieve_actor_user([
25 $self->ctx->{user}->id,
29 au => [qw/card home_ou addresses ident_type billing_address/, @extra_flesh]
35 $e->rollback if $local_xact;
37 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR
38 unless $self->ctx->{user};
43 # Given an event returned by a failed attempt to create a hold, do we have
44 # permission to override? XXX Should the permission check be scoped to a
45 # given org_unit context?
46 sub test_could_override {
47 my ($self, $event) = @_;
49 return 0 unless $event;
50 return 1 if $self->editor->allowed($event->{textcode} . ".override");
51 return 1 if $event->{"fail_part"} and
52 $self->editor->allowed($event->{"fail_part"} . ".override");
56 # Find out whether we care that local copies are available
57 sub local_avail_concern {
58 my ($self, $hold_target, $hold_type, $pickup_lib) = @_;
60 my $would_block = $self->ctx->{get_org_setting}->
61 ($pickup_lib, "circ.holds.hold_has_copy_at.block");
63 $self->ctx->{get_org_setting}->
64 ($pickup_lib, "circ.holds.hold_has_copy_at.alert") and
65 not $self->cgi->param("override")
66 ) unless $would_block;
68 if ($would_block or $would_alert) {
70 "hold_target" => $hold_target,
71 "hold_type" => $hold_type,
72 "org_unit" => $pickup_lib
74 my $local_avail = $U->simplereq(
76 "open-ils.circ.hold.has_copy_at", $self->editor->authtoken, $args
79 "copy availability information for " . Dumper($args) .
80 " is " . Dumper($local_avail)
82 if (%$local_avail) { # if hash not empty
83 $self->ctx->{hold_copy_available} = $local_avail;
84 return ($would_block, $would_alert);
92 # user : au object, fleshed
93 sub load_myopac_prefs {
96 my $e = $self->editor;
97 my $pending_addr = $cgi->param('pending_addr');
98 my $replace_addr = $cgi->param('replace_addr');
99 my $delete_pending = $cgi->param('delete_pending');
101 $self->prepare_extended_user_info;
102 my $user = $self->ctx->{user};
104 my $lock_usernames = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.lock_usernames');
105 if($lock_usernames == 1) {
106 # Policy says no username changes
107 $self->ctx->{username_change_disallowed} = 1;
109 my $username_unlimit = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.unlimit_usernames');
110 if($username_unlimit != 1) {
111 my $regex_check = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.barcode_regex');
113 # Default is "starts with a number"
114 $regex_check = '^\d+';
116 # You already have a username?
117 if($regex_check and $self->ctx->{user}->usrname !~ /$regex_check/) {
118 $self->ctx->{username_change_disallowed} = 1;
123 return Apache2::Const::OK unless
124 $pending_addr or $replace_addr or $delete_pending;
126 my @form_fields = qw/address_type street1 street2 city county state country post_code/;
129 if( $pending_addr ) { # update an existing pending address
131 ($paddr) = grep { $_->id == $pending_addr } @{$user->addresses};
132 return Apache2::Const::HTTP_BAD_REQUEST unless $paddr;
133 $paddr->$_( $cgi->param($_) ) for @form_fields;
135 } elsif( $replace_addr ) { # create a new pending address for 'replace_addr'
137 $paddr = Fieldmapper::actor::user_address->new;
139 $paddr->usr($user->id);
140 $paddr->pending('t');
141 $paddr->replaces($replace_addr);
142 $paddr->$_( $cgi->param($_) ) for @form_fields;
144 } elsif( $delete_pending ) {
145 $paddr = $e->retrieve_actor_user_address($delete_pending);
146 return Apache2::Const::HTTP_BAD_REQUEST unless
147 $paddr and $paddr->usr == $user->id and $U->is_true($paddr->pending);
148 $paddr->isdeleted(1);
151 my $resp = $U->simplereq(
153 'open-ils.actor.user.address.pending.cud',
154 $e->authtoken, $paddr);
156 if( $U->event_code($resp) ) {
157 $logger->error("Error updating pending address: $resp");
158 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
161 # in light of these changes, re-fetch latest data
163 $self->prepare_extended_user_info;
166 return Apache2::Const::OK;
169 sub load_myopac_prefs_notify {
171 my $e = $self->editor;
173 my $user_prefs = $self->fetch_optin_prefs;
174 $user_prefs = $self->update_optin_prefs($user_prefs)
175 if $self->cgi->request_method eq 'POST';
177 $self->ctx->{opt_in_settings} = $user_prefs;
180 my $set_map = $self->ctx->{user_setting_map};
184 opac.default_sms_notify
186 my $val = $self->cgi->param($key);
187 $settings{$key}= $val unless $$set_map{$key} eq $val;
190 my $key = 'opac.default_sms_carrier';
191 my $val = $self->cgi->param('sms_carrier');
192 $settings{$key}= $val unless $$set_map{$key} eq $val;
194 $key = 'opac.hold_notify';
195 my @notify_methods = ();
196 if ($self->cgi->param($key . ".email") eq 'on') {
197 push @notify_methods, "email";
199 if ($self->cgi->param($key . ".phone") eq 'on') {
200 push @notify_methods, "phone";
202 if ($self->cgi->param($key . ".sms") eq 'on') {
203 push @notify_methods, "sms";
205 $val = join("|",@notify_methods);
206 $settings{$key}= $val unless $$set_map{$key} eq $val;
208 # Send the modified settings off to be saved
211 'open-ils.actor.patron.settings.update',
212 $self->editor->authtoken, undef, \%settings);
214 # re-fetch user prefs
215 $self->ctx->{updated_user_settings} = \%settings;
216 return $self->_load_user_with_prefs || Apache2::Const::OK;
219 sub fetch_optin_prefs {
221 my $e = $self->editor;
223 # fetch all of the opt-in settings the user has access to
224 # XXX: user's should in theory have options to opt-in to notices
225 # for remote locations, but that opens the door for a large
226 # set of generally un-used opt-ins.. needs discussion
227 my $opt_ins = $U->simplereq(
229 'open-ils.actor.event_def.opt_in.settings.atomic',
230 $e->authtoken, $e->requestor->home_ou);
232 # some opt-ins are staff-only
233 $opt_ins = [ grep { $U->is_true($_->opac_visible) } @$opt_ins ];
235 # fetch user setting values for each of the opt-in settings
236 my $user_set = $U->simplereq(
238 'open-ils.actor.patron.settings.retrieve',
241 [map {$_->name} @$opt_ins]
244 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
247 sub update_optin_prefs {
249 my $user_prefs = shift;
250 my $e = $self->editor;
251 my @settings = $self->cgi->param('setting');
254 # apply now-true settings
255 for my $applied (@settings) {
256 # see if setting is already applied to this user
257 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
258 $newsets{$applied} = OpenSRF::Utils::JSON->true;
261 # remove now-false settings
262 for my $pref (grep { $_->{value} } @$user_prefs) {
263 $newsets{$pref->{cust}->name} = undef
264 unless grep { $_ eq $pref->{cust}->name } @settings;
269 'open-ils.actor.patron.settings.update',
270 $e->authtoken, $e->requestor->id, \%newsets);
272 # update the local prefs to match reality
273 for my $pref (@$user_prefs) {
274 $pref->{value} = $newsets{$pref->{cust}->name}
275 if exists $newsets{$pref->{cust}->name};
281 sub _load_user_with_prefs {
283 my $stat = $self->prepare_extended_user_info('settings');
284 return $stat if $stat; # not-OK
286 $self->ctx->{user_setting_map} = {
287 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
288 @{$self->ctx->{user}->settings}
294 sub _get_bookbag_sort_params {
295 my ($self, $param_name) = @_;
297 # The interface that feeds this cgi parameter will provide a single
298 # argument for a QP sort filter, and potentially a modifier after a period.
299 # In practice this means the "sort" parameter will be something like
300 # "titlesort" or "authorsort.descending".
301 my $sorter = $self->cgi->param($param_name) || "";
304 $sorter =~ s/^(.*?)\.(.*)/$1/;
305 $modifier = $2 || undef;
308 return ($sorter, $modifier);
311 sub _prepare_bookbag_container_query {
312 my ($self, $container_id, $sorter, $modifier) = @_;
315 "container(bre,bookbag,%d,%s)%s%s",
316 $container_id, $self->editor->authtoken,
317 ($sorter ? " sort($sorter)" : ""),
318 ($modifier ? "#$modifier" : "")
322 sub _prepare_anonlist_sorting_query {
323 my ($self, $list, $sorter, $modifier) = @_;
326 "record_list(%s)%s%s",
328 ($sorter ? " sort($sorter)" : ""),
329 ($modifier ? "#$modifier" : "")
334 sub load_myopac_prefs_settings {
339 opac.default_search_location
342 my $stat = $self->_load_user_with_prefs;
343 return $stat if $stat;
345 return Apache2::Const::OK
346 unless $self->cgi->request_method eq 'POST';
348 # some setting values from the form don't match the
349 # required value/format for the db, so they have to be
350 # individually translated.
353 my $set_map = $self->ctx->{user_setting_map};
355 foreach my $key (@user_prefs) {
356 my $val = $self->cgi->param($key);
357 $settings{$key}= $val unless $$set_map{$key} eq $val;
360 my $now = DateTime->now->strftime('%F');
361 foreach my $key (qw/history.circ.retention_start history.hold.retention_start/) {
362 my $val = $self->cgi->param($key);
363 if($val and $val eq 'on') {
364 # Set the start time to 'now' unless a start time already exists for the user
365 $settings{$key} = $now unless $$set_map{$key};
367 # clear the start time if one previously existed for the user
368 $settings{$key} = undef if $$set_map{$key};
372 # Send the modified settings off to be saved
375 'open-ils.actor.patron.settings.update',
376 $self->editor->authtoken, undef, \%settings);
378 # re-fetch user prefs
379 $self->ctx->{updated_user_settings} = \%settings;
380 return $self->_load_user_with_prefs || Apache2::Const::OK;
383 sub fetch_user_holds {
385 my $hold_ids = shift;
386 my $ids_only = shift;
388 my $available = shift;
392 my $e = $self->editor;
395 my $circ = OpenSRF::AppSession->create('open-ils.circ');
397 $hold_ids = $circ->request(
398 'open-ils.circ.holds.id_list.retrieve.authoritative',
404 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
408 return $hold_ids if $ids_only or @$hold_ids == 0;
411 suppress_notices => 1,
412 suppress_transits => 1,
414 suppress_patron_details => 1
417 # ----------------------------------------------------------------
418 # Collect holds in batches of $batch_size for faster retrieval
422 my $mk_req_batch = sub {
424 my $top_idx = $batch_idx + $batch_size;
425 while($batch_idx < $top_idx) {
426 my $hold_id = $hold_ids->[$batch_idx++];
427 last unless $hold_id;
428 my $ses = OpenSRF::AppSession->create('open-ils.circ');
429 my $req = $ses->request(
430 'open-ils.circ.hold.details.retrieve',
431 $e->authtoken, $hold_id, $args);
432 push(@ses, {ses => $ses, req => $req});
438 my(@collected, @holds, @ses);
441 @ses = $mk_req_batch->() if $first;
442 last if $first and not @ses;
445 # If desired by the caller, filter any holds that are not available.
447 @collected = grep { $_->{hold}->{status} == 4 } @collected;
449 while(my $blob = pop(@collected)) {
450 my (undef, @data) = $self->get_records_and_facets(
451 [$blob->{hold}->{bre_id}], undef, {flesh => '{mra}'}
453 $blob->{marc_xml} = $data[0]->{marc_xml};
458 for my $req_data (@ses) {
459 push(@collected, {hold => $req_data->{req}->gather(1)});
460 $req_data->{ses}->kill_me;
463 @ses = $mk_req_batch->();
464 last unless @collected or @ses;
468 # put the holds back into the original server sort order
470 for my $id (@$hold_ids) {
471 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
477 sub handle_hold_update {
480 my $hold_ids = shift;
481 my $e = $self->editor;
484 my @hold_ids = ($hold_ids) ? @$hold_ids : $self->cgi->param('hold_id'); # for non-_all actions
485 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
487 my $circ = OpenSRF::AppSession->create('open-ils.circ');
489 if($action =~ /cancel/) {
491 for my $hold_id (@hold_ids) {
492 my $resp = $circ->request(
493 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
496 } elsif ($action =~ /activate|suspend/) {
499 for my $hold_id (@hold_ids) {
500 my $vals = {id => $hold_id};
502 if($action =~ /activate/) {
503 $vals->{frozen} = 'f';
504 $vals->{thaw_date} = undef;
506 } elsif($action =~ /suspend/) {
507 $vals->{frozen} = 't';
508 # $vals->{thaw_date} = TODO;
510 push(@$vlist, $vals);
513 my $resp = $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
514 $self->ctx->{hold_suspend_post_capture} = 1 if
515 grep {$U->event_equals($_, 'HOLD_SUSPEND_AFTER_CAPTURE')} @$resp;
517 } elsif ($action eq 'edit') {
520 my $val = {"id" => $_};
521 $val->{"frozen"} = $self->cgi->param("frozen");
522 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
524 for my $field (qw/expire_time thaw_date/) {
525 # XXX TODO make this support other date formats, not just
527 next unless $self->cgi->param($field) =~
528 m:^(\d{2})/(\d{2})/(\d{4})$:;
529 $val->{$field} = "$3-$1-$2";
535 'open-ils.circ.hold.update.batch.atomic',
536 $e->authtoken, undef, \@vals
537 )->gather(1); # LFW XXX test for failure
538 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
542 return defined($url) ? $self->generic_redirect($url) : undef;
545 sub load_myopac_holds {
547 my $e = $self->editor;
548 my $ctx = $self->ctx;
550 my $limit = $self->cgi->param('limit') || 0;
551 my $offset = $self->cgi->param('offset') || 0;
552 my $action = $self->cgi->param('action') || '';
553 my $hold_id = $self->cgi->param('id');
554 my $available = int($self->cgi->param('available') || 0);
556 my $hold_handle_result;
557 $hold_handle_result = $self->handle_hold_update($action) if $action;
559 $ctx->{holds} = $self->fetch_user_holds($hold_id ? [$hold_id] : undef, 0, 1, $available, $limit, $offset);
561 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
564 sub load_place_hold {
566 my $ctx = $self->ctx;
567 my $gos = $ctx->{get_org_setting};
568 my $e = $self->editor;
569 my $cgi = $self->cgi;
571 $self->ctx->{page} = 'place_hold';
572 my @targets = $cgi->param('hold_target');
573 my @parts = $cgi->param('part');
575 $ctx->{hold_type} = $cgi->param('hold_type');
576 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # unless changed below
577 $ctx->{email_notify} = $cgi->param('email_notify');
578 if ($cgi->param('phone_notify_checkbox')) {
579 $ctx->{phone_notify} = $cgi->param('phone_notify');
581 if ($cgi->param('sms_notify_checkbox')) {
582 $ctx->{sms_notify} = $cgi->param('sms_notify');
583 $ctx->{sms_carrier} = $cgi->param('sms_carrier');
586 return $self->generic_redirect unless @targets;
588 $logger->info("Looking at hold_type: " . $ctx->{hold_type} . " and targets: @targets");
590 # if the staff client provides a patron barcode, fetch the patron
591 if (my $bc = $self->cgi->cookie("patron_barcode")) {
592 $ctx->{patron_recipient} = $U->simplereq(
593 "open-ils.actor", "open-ils.actor.user.fleshed.retrieve_by_barcode",
594 $self->editor->authtoken, $bc
595 ) or return Apache2::Const::HTTP_BAD_REQUEST;
597 $ctx->{default_pickup_lib} = $ctx->{patron_recipient}->home_ou;
599 $ctx->{staff_recipient} = $self->editor->retrieve_actor_user([
607 ]) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
609 my $user_setting_map = {
610 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
612 $ctx->{patron_recipient}
613 ? $ctx->{patron_recipient}->settings
614 : $ctx->{staff_recipient}->settings
617 $ctx->{user_setting_map} = $user_setting_map;
619 my $default_notify = $$user_setting_map{'opac.hold_notify'} || '';
620 if ($default_notify =~ /email/) {
621 $ctx->{default_email_notify} = 'checked';
623 $ctx->{default_email_notify} = '';
625 if ($default_notify =~ /phone/) {
626 $ctx->{default_phone_notify} = 'checked';
628 $ctx->{default_phone_notify} = '';
630 if ($default_notify =~ /sms/) {
631 $ctx->{default_sms_notify} = 'checked';
633 $ctx->{default_sms_notify} = '';
636 my $request_lib = $e->requestor->ws_ou;
638 $ctx->{hold_data} = \@hold_data;
642 if ($ctx->{email_notify}) { $hdata->{email_notify} = $ctx->{email_notify}; }
643 if ($ctx->{phone_notify}) { $hdata->{phone_notify} = $ctx->{phone_notify}; }
644 if ($ctx->{sms_notify}) { $hdata->{sms_notify} = $ctx->{sms_notify}; }
645 if ($ctx->{sms_carrier}) { $hdata->{sms_carrier} = $ctx->{sms_carrier}; }
649 my $type_dispatch = {
651 my $recs = $e->batch_retrieve_biblio_record_entry(\@targets, {substream => 1});
653 for my $id (@targets) { # force back into the correct order
654 my ($rec) = grep {$_->id eq $id} @$recs;
656 # NOTE: if tpac ever supports locked-down pickup libs,
657 # we'll need to pass a pickup_lib param along with the
658 # record to filter the set of monographic parts.
659 my $parts = $U->simplereq(
661 'open-ils.search.biblio.record_hold_parts',
665 # T holds on records that have parts are OK, but if the record has
666 # no non-part copies, the hold will ultimately fail. When that
667 # happens, require the user to select a part.
668 my $part_required = 0;
670 my $np_copies = $e->json_query({
671 select => { acp => [{column => 'id', transform => 'count', alias => 'count'}]},
672 from => {acp => {acn => {}, acpm => {type => 'left'}}},
674 '+acp' => {deleted => 'f'},
675 '+acn' => {deleted => 'f', record => $rec->id},
676 '+acpm' => {id => undef}
679 $part_required = 1 if $np_copies->[0]->{count} == 0;
682 push(@hold_data, data_filler({
686 part_required => $part_required
691 my $vols = $e->batch_retrieve_asset_call_number([
694 "flesh_fields" => {"acn" => ["record"]}
696 ], {substream => 1});
698 for my $id (@targets) {
699 my ($vol) = grep {$_->id eq $id} @$vols;
700 push(@hold_data, data_filler({target => $vol, record => $vol->record}));
704 my $copies = $e->batch_retrieve_asset_copy([
709 "acp" => ["call_number"]
712 ], {substream => 1});
714 for my $id (@targets) {
715 my ($copy) = grep {$_->id eq $id} @$copies;
716 push(@hold_data, data_filler({target => $copy, record => $copy->call_number->record}));
720 my $isses = $e->batch_retrieve_serial_issuance([
724 "siss" => ["subscription"], "ssub" => ["record_entry"]
727 ], {substream => 1});
729 for my $id (@targets) {
730 my ($iss) = grep {$_->id eq $id} @$isses;
731 push(@hold_data, data_filler({target => $iss, record => $iss->subscription->record_entry}));
736 }->{$ctx->{hold_type}}->();
738 # caller sent bad target IDs or the wrong hold type
739 return Apache2::Const::HTTP_BAD_REQUEST unless @hold_data;
741 # generate the MARC xml for each record
742 $_->{marc_xml} = XML::LibXML->new->parse_string($_->{record}->marc) for @hold_data;
744 my $pickup_lib = $cgi->param('pickup_lib');
745 # no pickup lib means no holds placement
746 return Apache2::Const::OK unless $pickup_lib;
748 $ctx->{hold_attempt_made} = 1;
750 # Give the original CGI params back to the user in case they
751 # want to try to override something.
752 $ctx->{orig_params} = $cgi->Vars;
753 delete $ctx->{orig_params}{submit};
754 delete $ctx->{orig_params}{hold_target};
755 delete $ctx->{orig_params}{part};
757 my $usr = $e->requestor->id;
759 if ($ctx->{is_staff} and !$cgi->param("hold_usr_is_requestor")) {
760 # find the real hold target
762 $usr = $U->simplereq(
764 "open-ils.actor.user.retrieve_id_by_barcode_or_username",
765 $e->authtoken, $cgi->param("hold_usr"));
767 if (defined $U->event_code($usr)) {
768 $ctx->{hold_failed} = 1;
769 $ctx->{hold_failed_event} = $usr;
773 # target_id is the true target_id for holds placement.
774 # needed for attempt_hold_placement()
775 # With the exception of P-type holds, target_id == target->id.
776 $_->{target_id} = $_->{target}->id for @hold_data;
778 if ($ctx->{hold_type} eq 'T') {
780 # Much like quantum wave-particles, P-type holds pop into
781 # and out of existence at the user's whim. For our purposes,
782 # we treat such holds as T(itle) holds with a selected_part
783 # designation. When the time comes to pass the hold information
784 # off for holds possibility testing and placement, make it look
785 # like a real P-type hold.
786 my (@p_holds, @t_holds);
788 for my $idx (0..$#parts) {
789 my $hdata = $hold_data[$idx];
790 if (my $part = $parts[$idx]) {
791 $hdata->{target_id} = $part;
792 $hdata->{selected_part} = $part;
793 push(@p_holds, $hdata);
795 push(@t_holds, $hdata);
799 $self->attempt_hold_placement($usr, $pickup_lib, 'P', @p_holds) if @p_holds;
800 $self->attempt_hold_placement($usr, $pickup_lib, 'T', @t_holds) if @t_holds;
803 $self->attempt_hold_placement($usr, $pickup_lib, $ctx->{hold_type}, @hold_data);
806 # NOTE: we are leaving the staff-placed patron barcode cookie
807 # in place. Otherwise, it's not possible to place more than
808 # one hold for the patron within a staff/patron session. This
809 # does leave the barcode to linger longer than is ideal, but
810 # normal staff work flow will cause the cookie to be replaced
811 # with each new patron anyway.
812 # TODO: See about getting the staff client to clear the cookie
814 # return to the place_hold page so the results of the hold
815 # placement attempt can be reported to the user
816 return Apache2::Const::OK;
819 sub attempt_hold_placement {
820 my ($self, $usr, $pickup_lib, $hold_type, @hold_data) = @_;
821 my $cgi = $self->cgi;
822 my $ctx = $self->ctx;
823 my $e = $self->editor;
825 # First see if we should warn/block for any holds that
826 # might have locally available items.
827 for my $hdata (@hold_data) {
828 my ($local_block, $local_alert) = $self->local_avail_concern(
829 $hdata->{target_id}, $hold_type, $pickup_lib);
832 $hdata->{hold_failed} = 1;
833 $hdata->{hold_local_block} = 1;
834 } elsif ($local_alert) {
835 $hdata->{hold_failed} = 1;
836 $hdata->{hold_local_alert} = 1;
840 my $method = 'open-ils.circ.holds.test_and_create.batch';
841 $method .= '.override' if $cgi->param('override');
843 my @create_targets = map {$_->{target_id}} (grep { !$_->{hold_failed} } @hold_data);
845 if(@create_targets) {
847 my $bses = OpenSRF::AppSession->create('open-ils.circ');
848 my $breq = $bses->request(
851 data_filler({ patronid => $usr,
852 pickup_lib => $pickup_lib,
853 hold_type => $hold_type
858 while (my $resp = $breq->recv) {
860 $resp = $resp->content;
861 $logger->info('batch hold placement result: ' . OpenSRF::Utils::JSON->perl2JSON($resp));
863 if ($U->event_code($resp)) {
864 $ctx->{general_hold_error} = $resp;
868 my ($hdata) = grep {$_->{target_id} eq $resp->{target}} @hold_data;
869 my $result = $resp->{result};
871 if ($U->event_code($result)) {
872 # e.g. permission denied
873 $hdata->{hold_failed} = 1;
874 $hdata->{hold_failed_event} = $result;
878 if(not ref $result and $result > 0) {
879 # successul hold returns the hold ID
881 $hdata->{hold_success} = $result;
884 # hold-specific failure event
885 $hdata->{hold_failed} = 1;
887 if (ref $result eq 'HASH') {
888 $hdata->{hold_failed_event} = $result->{last_event};
889 } elsif (ref $result eq 'ARRAY') {
890 $hdata->{hold_failed_event} = pop @$result;
893 $hdata->{could_override} = $self->test_could_override($hdata->{hold_failed_event});
902 sub fetch_user_circs {
904 my $flesh = shift; # flesh bib data, etc.
905 my $circ_ids = shift;
909 my $e = $self->editor;
914 @circ_ids = @$circ_ids;
919 select => {circ => ['id']},
923 usr => $e->requestor->id,
924 checkin_time => undef,
926 {stop_fines => undef},
927 {stop_fines => {'not in' => ['LOST','CLAIMSRETURNED','LONGOVERDUE']}}
931 order_by => {circ => ['due_date']}
934 $query->{limit} = $limit if $limit;
935 $query->{offset} = $offset if $offset;
937 my $ids = $e->json_query($query);
938 @circ_ids = map {$_->{id}} @$ids;
941 return [] unless @circ_ids;
946 circ => ['target_copy'],
947 acp => ['call_number'],
953 my $circs = $e->search_action_circulation(
954 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
957 for my $circ (@$circs) {
960 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
961 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
962 undef # pre-cat copy, use the dummy title/author instead
967 # make sure the final list is in the correct order
969 for my $id (@circ_ids) {
972 (grep { $_->{circ}->id == $id } @circs)
976 return \@sorted_circs;
980 sub handle_circ_renew {
983 my $ctx = $self->ctx;
985 my @renew_ids = $self->cgi->param('circ');
987 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
989 # TODO: fire off renewal calls in batches to speed things up
991 for my $circ (@$circs) {
993 my $evt = $U->simplereq(
995 'open-ils.circ.renew',
996 $self->editor->authtoken,
998 patron_id => $self->editor->requestor->id,
999 copy_id => $circ->{circ}->target_copy,
1004 # TODO return these, then insert them into the circ data
1005 # blob that is shoved into the template for each circ
1006 # so the template won't have to match them
1007 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
1014 sub load_myopac_circs {
1016 my $e = $self->editor;
1017 my $ctx = $self->ctx;
1020 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
1021 my $offset = $self->cgi->param('offset') || 0;
1022 my $action = $self->cgi->param('action') || '';
1024 # perform the renewal first if necessary
1025 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
1027 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
1029 my $success_renewals = 0;
1030 my $failed_renewals = 0;
1031 for my $data (@{$ctx->{circs}}) {
1032 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
1035 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
1036 $data->{renewal_response} = $evt;
1037 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
1038 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
1042 $ctx->{success_renewals} = $success_renewals;
1043 $ctx->{failed_renewals} = $failed_renewals;
1045 return Apache2::Const::OK;
1048 sub load_myopac_circ_history {
1050 my $e = $self->editor;
1051 my $ctx = $self->ctx;
1052 my $limit = $self->cgi->param('limit') || 15;
1053 my $offset = $self->cgi->param('offset') || 0;
1055 $ctx->{circ_history_limit} = $limit;
1056 $ctx->{circ_history_offset} = $offset;
1058 my $circ_ids = $e->json_query({
1062 transform => 'action.usr_visible_circs',
1063 result_field => 'id'
1067 where => {id => $e->requestor->id},
1072 $ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circ_ids]);
1073 return Apache2::Const::OK;
1076 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
1077 sub load_myopac_hold_history {
1079 my $e = $self->editor;
1080 my $ctx = $self->ctx;
1081 my $limit = $self->cgi->param('limit') || 15;
1082 my $offset = $self->cgi->param('offset') || 0;
1083 $ctx->{hold_history_limit} = $limit;
1084 $ctx->{hold_history_offset} = $offset;
1086 my $hold_ids = $e->json_query({
1090 transform => 'action.usr_visible_holds',
1091 result_field => 'id'
1095 where => {id => $e->requestor->id},
1100 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$hold_ids], 0, 1, 0);
1101 return Apache2::Const::OK;
1104 sub load_myopac_payment_form {
1108 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and return $r;
1109 $r = $self->prepare_extended_user_info and return $r;
1111 return Apache2::Const::OK;
1114 # TODO: add other filter options as params/configs/etc.
1115 sub load_myopac_payments {
1117 my $limit = $self->cgi->param('limit') || 20;
1118 my $offset = $self->cgi->param('offset') || 0;
1119 my $e = $self->editor;
1121 $self->ctx->{payment_history_limit} = $limit;
1122 $self->ctx->{payment_history_offset} = $offset;
1125 $args->{limit} = $limit if $limit;
1126 $args->{offset} = $offset if $offset;
1128 if (my $max_age = $self->ctx->{get_org_setting}->(
1129 $e->requestor->home_ou, "opac.payment_history_age_limit"
1131 my $min_ts = DateTime->now(
1132 "time_zone" => DateTime::TimeZone->new("name" => "local"),
1133 )->subtract("seconds" => interval_to_seconds($max_age))->iso8601();
1135 $logger->info("XXX min_ts: $min_ts");
1136 $args->{"where"} = {"payment_ts" => {">=" => $min_ts}};
1139 $self->ctx->{payments} = $U->simplereq(
1141 'open-ils.actor.user.payments.retrieve.atomic',
1142 $e->authtoken, $e->requestor->id, $args);
1144 return Apache2::Const::OK;
1147 sub load_myopac_pay {
1151 my @payment_xacts = ($self->cgi->param('xact'), $self->cgi->param('xact_misc'));
1152 $logger->info("tpac paying fines for xacts @payment_xacts");
1154 $r = $self->prepare_fines(undef, undef, \@payment_xacts) and return $r;
1156 # balance_owed is computed specifically from the fines we're trying
1157 # to pay in this case.
1158 if ($self->ctx->{fines}->{balance_owed} <= 0) {
1159 $self->apache->log->info(
1160 sprintf("Can't pay non-positive balance. xacts selected: (%s)",
1161 join(", ", map(int, $self->cgi->param("xact"), $self->cgi->param('xact_misc'))))
1163 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1166 my $cc_args = {"where_process" => 1};
1168 $cc_args->{$_} = $self->cgi->param($_) for (qw/
1169 number cvv2 expire_year expire_month billing_first
1170 billing_last billing_address billing_city billing_state
1175 "cc_args" => $cc_args,
1176 "userid" => $self->ctx->{user}->id,
1177 "payment_type" => "credit_card_payment",
1178 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
1181 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
1182 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
1185 $self->ctx->{"payment_response"} = $resp;
1187 unless ($resp->{"textcode"}) {
1188 $self->ctx->{printable_receipt} = $U->simplereq(
1189 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
1190 $self->editor->authtoken, $resp->{payments}
1194 return Apache2::Const::OK;
1197 sub load_myopac_receipt_print {
1200 $self->ctx->{printable_receipt} = $U->simplereq(
1201 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
1202 $self->editor->authtoken, [$self->cgi->param("payment")]
1205 return Apache2::Const::OK;
1208 sub load_myopac_receipt_email {
1211 # The following ML method doesn't actually check whether the user in
1212 # question has an email address, so we do.
1213 if ($self->ctx->{user}->email) {
1214 $self->ctx->{email_receipt_result} = $U->simplereq(
1215 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
1216 $self->editor->authtoken, [$self->cgi->param("payment")]
1219 $self->ctx->{email_receipt_result} =
1220 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
1223 return Apache2::Const::OK;
1227 my ($self, $limit, $offset, $id_list) = @_;
1229 # XXX TODO: check for failure after various network calls
1231 # It may be unclear, but this result structure lumps circulation and
1232 # reservation fines together, and keeps grocery fines separate.
1233 $self->ctx->{"fines"} = {
1234 "circulation" => [],
1241 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
1243 # TODO: This should really be a ML call, but the existing calls
1244 # return an excessive amount of data and don't offer streaming
1246 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
1248 my $req = $cstore->request(
1249 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
1251 usr => $self->editor->requestor->id,
1252 balance_owed => {'!=' => 0},
1253 ($id_list && @$id_list ? ("id" => $id_list) : ()),
1258 mobts => [qw/grocery circulation reservation/],
1259 bresv => ['target_resource_type'],
1263 circ => ['target_copy'],
1264 acp => ['call_number'],
1267 order_by => { mobts => 'xact_start' },
1272 my @total_keys = qw/total_paid total_owed balance_owed/;
1273 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
1275 while(my $resp = $req->recv) {
1276 my $mobts = $resp->content;
1277 my $circ = $mobts->circulation;
1280 if($mobts->grocery) {
1281 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
1282 $last_billing = pop(@billings);
1285 # XXX TODO confirm that the following, and the later division by 100.0
1286 # to get a floating point representation once again, is sufficiently
1287 # "money-safe" math.
1288 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
1290 my $marc_xml = undef;
1291 if ($mobts->xact_type eq 'reservation' and
1292 $mobts->reservation->target_resource_type->record) {
1293 $marc_xml = XML::LibXML->new->parse_string(
1294 $mobts->reservation->target_resource_type->record->marc
1296 } elsif ($mobts->xact_type eq 'circulation' and
1297 $circ->target_copy->call_number->id != -1) {
1298 $marc_xml = XML::LibXML->new->parse_string(
1299 $circ->target_copy->call_number->record->marc
1304 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
1307 last_grocery_billing => $last_billing,
1308 marc_xml => $marc_xml
1315 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
1319 sub prepare_fines_for_payment {
1320 # This assumes $self->prepare_fines has already been run
1324 if ($self->ctx->{fines}) {
1325 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
1326 @{$self->ctx->{fines}->{circulation}},
1327 @{$self->ctx->{fines}->{grocery}}
1334 sub load_myopac_main {
1336 my $limit = $self->cgi->param('limit') || 0;
1337 my $offset = $self->cgi->param('offset') || 0;
1338 $self->ctx->{search_ou} = $self->_get_search_lib();
1340 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
1343 sub load_myopac_update_email {
1345 my $e = $self->editor;
1346 my $ctx = $self->ctx;
1347 my $email = $self->cgi->param('email') || '';
1348 my $current_pw = $self->cgi->param('current_pw') || '';
1350 # needed for most up-to-date email address
1351 if (my $r = $self->prepare_extended_user_info) { return $r };
1353 return Apache2::Const::OK
1354 unless $self->cgi->request_method eq 'POST';
1356 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
1357 $ctx->{invalid_email} = $email;
1358 return Apache2::Const::OK;
1361 my $stat = $U->simplereq(
1363 'open-ils.actor.user.email.update',
1364 $e->authtoken, $email, $current_pw);
1366 if($U->event_equals($stat, 'INCORRECT_PASSWORD')) {
1367 $ctx->{password_incorrect} = 1;
1368 return Apache2::Const::OK;
1371 unless ($self->cgi->param("redirect_to")) {
1372 my $url = $self->apache->unparsed_uri;
1373 $url =~ s/update_email/prefs/;
1375 return $self->generic_redirect($url);
1378 return $self->generic_redirect;
1381 sub load_myopac_update_username {
1383 my $e = $self->editor;
1384 my $ctx = $self->ctx;
1385 my $username = $self->cgi->param('username') || '';
1386 my $current_pw = $self->cgi->param('current_pw') || '';
1388 $self->prepare_extended_user_info;
1390 my $allow_change = 1;
1392 my $lock_usernames = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.lock_usernames');
1393 if($lock_usernames == 1) {
1394 # Policy says no username changes
1397 # We want this further down.
1398 $regex_check = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.barcode_regex');
1399 my $username_unlimit = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.unlimit_usernames');
1400 if($username_unlimit != 1) {
1402 # Default is "starts with a number"
1403 $regex_check = '^\d+';
1405 # You already have a username?
1406 if($regex_check and $self->ctx->{user}->usrname !~ /$regex_check/) {
1411 if(!$allow_change) {
1412 my $url = $self->apache->unparsed_uri;
1413 $url =~ s/update_username/prefs/;
1415 return $self->generic_redirect($url);
1418 return Apache2::Const::OK
1419 unless $self->cgi->request_method eq 'POST';
1421 unless($username and $username !~ /\s/) { # any other username restrictions?
1422 $ctx->{invalid_username} = $username;
1423 return Apache2::Const::OK;
1426 # New username can't look like a barcode if we have a barcode regex
1427 if($regex_check and $username =~ /$regex_check/) {
1428 $ctx->{invalid_username} = $username;
1429 return Apache2::Const::OK;
1432 # New username has to look like a username if we have a username regex
1433 $regex_check = $ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.username_regex');
1434 if($regex_check and $username !~ /$regex_check/) {
1435 $ctx->{invalid_username} = $username;
1436 return Apache2::Const::OK;
1439 if($username ne $e->requestor->usrname) {
1441 my $evt = $U->simplereq(
1443 'open-ils.actor.user.username.update',
1444 $e->authtoken, $username, $current_pw);
1446 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
1447 $ctx->{password_incorrect} = 1;
1448 return Apache2::Const::OK;
1451 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
1452 $ctx->{username_exists} = $username;
1453 return Apache2::Const::OK;
1457 my $url = $self->apache->unparsed_uri;
1458 $url =~ s/update_username/prefs/;
1460 return $self->generic_redirect($url);
1463 sub load_myopac_update_password {
1465 my $e = $self->editor;
1466 my $ctx = $self->ctx;
1468 return Apache2::Const::OK
1469 unless $self->cgi->request_method eq 'POST';
1471 my $current_pw = $self->cgi->param('current_pw') || '';
1472 my $new_pw = $self->cgi->param('new_pw') || '';
1473 my $new_pw2 = $self->cgi->param('new_pw2') || '';
1475 unless($new_pw eq $new_pw2) {
1476 $ctx->{password_nomatch} = 1;
1477 return Apache2::Const::OK;
1480 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
1483 # This regex duplicates the JSPac's default "digit, letter, and 7 characters" rule
1484 $pw_regex = '(?=.*\d+.*)(?=.*[A-Za-z]+.*).{7,}';
1487 if($pw_regex and $new_pw !~ /$pw_regex/) {
1488 $ctx->{password_invalid} = 1;
1489 return Apache2::Const::OK;
1492 my $evt = $U->simplereq(
1494 'open-ils.actor.user.password.update',
1495 $e->authtoken, $new_pw, $current_pw);
1498 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
1499 $ctx->{password_incorrect} = 1;
1500 return Apache2::Const::OK;
1503 my $url = $self->apache->unparsed_uri;
1504 $url =~ s/update_password/prefs/;
1506 return $self->generic_redirect($url);
1509 sub _update_bookbag_metadata {
1510 my ($self, $bookbag) = @_;
1512 $bookbag->name($self->cgi->param("name"));
1513 $bookbag->description($self->cgi->param("description"));
1515 return 1 if $self->editor->update_container_biblio_record_entry_bucket($bookbag);
1519 sub load_myopac_bookbags {
1521 my $e = $self->editor;
1522 my $ctx = $self->ctx;
1524 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("sort");
1525 $e->xact_begin; # replication...
1527 my $rv = $self->load_mylist;
1528 unless($rv eq Apache2::Const::OK) {
1533 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket(
1535 {owner => $e->requestor->id, btype => 'bookbag'}, {
1536 order_by => {cbreb => 'name'},
1537 limit => $self->cgi->param('limit') || 10,
1538 offset => $self->cgi->param('offset') || 0
1544 if(!$ctx->{bookbags}) {
1546 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1549 # If the user wants a specific bookbag's items, load them.
1550 # XXX add bookbag item paging support
1552 if ($self->cgi->param("id")) {
1554 grep { $_->id eq $self->cgi->param("id") } @{$ctx->{bookbags}};
1558 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1561 if ($self->cgi->param("action") eq "editmeta") {
1562 if (!$self->_update_bookbag_metadata($bookbag)) {
1564 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1567 my $url = $self->ctx->{opac_root} . '/myopac/lists?id=' .
1570 # Keep it if we've got it
1571 if ($self->cgi->param("sort")) {
1572 $url .= ";sort=" . $self->cgi->param("sort");
1575 return $self->generic_redirect($url);
1579 my $query = $self->_prepare_bookbag_container_query(
1580 $bookbag->id, $sorter, $modifier
1583 # XXX we need to limit the number of records per bbag; use third arg
1584 # of bib_container_items_via_search() i think.
1585 my $items = $U->bib_container_items_via_search($bookbag->id, $query)
1586 or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1588 my (undef, @recs) = $self->get_records_and_facets(
1589 [ map {$_->target_biblio_record_entry->id} @$items ],
1594 $ctx->{bookbags_marc_xml}{$_->{id}} = $_->{marc_xml} for @recs;
1596 $bookbag->items($items);
1600 return Apache2::Const::OK;
1604 # actions are create, delete, show, hide, rename, add_rec, delete_item, place_hold
1605 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
1606 sub load_myopac_bookbag_update {
1607 my ($self, $action, $list_id, @hold_recs) = @_;
1608 my $e = $self->editor;
1609 my $cgi = $self->cgi;
1611 # save_notes is effectively another action, but is passed in a separate
1612 # CGI parameter for what are really just layout reasons.
1613 $action = 'save_notes' if $cgi->param('save_notes');
1614 $action ||= $cgi->param('action');
1616 $list_id ||= $cgi->param('list');
1618 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
1619 my @selected_item = $cgi->param('selected_item');
1620 my $shared = $cgi->param('shared');
1621 my $name = $cgi->param('name');
1622 my $description = $cgi->param('description');
1626 # This url intentionally leaves off the edit_notes parameter, but
1627 # may need to add some back in for paging.
1629 my $url = "https://" . $self->apache->hostname .
1630 $self->ctx->{opac_root} . "/myopac/lists?";
1632 $url .= 'sort=' . uri_escape($cgi->param("sort")) if $cgi->param("sort");
1634 if ($action eq 'create') {
1635 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
1637 $list->description($description);
1638 $list->owner($e->requestor->id);
1639 $list->btype('bookbag');
1640 $list->pub($shared ? 't' : 'f');
1641 $success = $U->simplereq('open-ils.actor',
1642 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
1644 } elsif($action eq 'place_hold') {
1646 # @hold_recs comes from anon lists redirect; selected_itesm comes from existing buckets
1647 unless (@hold_recs) {
1648 if (@selected_item) {
1649 my $items = $e->search_container_biblio_record_entry_bucket_item({id => \@selected_item});
1650 @hold_recs = map { $_->target_biblio_record_entry } @$items;
1654 return Apache2::Const::OK unless @hold_recs;
1655 $logger->info("placing holds from list page on: @hold_recs");
1657 my $url = $self->ctx->{opac_root} . '/place_hold?hold_type=T';
1658 $url .= ';hold_target=' . $_ for @hold_recs;
1659 return $self->generic_redirect($url);
1663 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
1665 return Apache2::Const::HTTP_BAD_REQUEST unless
1666 $list and $list->owner == $e->requestor->id;
1669 if($action eq 'delete') {
1670 $success = $U->simplereq('open-ils.actor',
1671 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
1673 } elsif($action eq 'show') {
1674 unless($U->is_true($list->pub)) {
1676 $success = $U->simplereq('open-ils.actor',
1677 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1680 } elsif($action eq 'hide') {
1681 if($U->is_true($list->pub)) {
1683 $success = $U->simplereq('open-ils.actor',
1684 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1687 } elsif($action eq 'rename') {
1690 $success = $U->simplereq('open-ils.actor',
1691 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1694 } elsif($action eq 'add_rec') {
1695 foreach my $add_rec (@add_rec) {
1696 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
1697 $item->bucket($list_id);
1698 $item->target_biblio_record_entry($add_rec);
1699 $success = $U->simplereq('open-ils.actor',
1700 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
1701 last unless $success;
1704 } elsif($action eq 'del_item') {
1705 foreach (@selected_item) {
1706 $success = $U->simplereq(
1708 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
1710 last unless $success;
1712 } elsif ($action eq 'save_notes') {
1713 $success = $self->update_bookbag_item_notes;
1714 $url .= "&id=" . uri_escape($cgi->param("id")) if $cgi->param("id");
1717 return $self->generic_redirect($url) if $success;
1719 # XXX FIXME Bucket failure doesn't have a page to show the user anything
1720 # right now. User just sees a 404 currently.
1722 $self->ctx->{bucket_action} = $action;
1723 $self->ctx->{bucket_action_failed} = 1;
1724 return Apache2::Const::OK;
1727 sub update_bookbag_item_notes {
1729 my $e = $self->editor;
1731 my @note_keys = grep /^note-\d+/, keys(%{$self->cgi->Vars});
1732 my @item_keys = grep /^item-\d+/, keys(%{$self->cgi->Vars});
1734 # We're going to leverage an API call that's already been written to check
1735 # permissions appropriately.
1737 my $a = create OpenSRF::AppSession("open-ils.actor");
1738 my $method = "open-ils.actor.container.item_note.cud";
1740 for my $note_key (@note_keys) {
1743 my $id = ($note_key =~ /(\d+)/)[0];
1746 $e->retrieve_container_biblio_record_entry_bucket_item_note($id))) {
1747 my $event = $e->die_event;
1748 $self->apache->log->warn(
1749 "error retrieving cbrebin id $id, got event " .
1753 $self->ctx->{bucket_action_event} = $event;
1757 if (length($self->cgi->param($note_key))) {
1758 $note->ischanged(1);
1759 $note->note($self->cgi->param($note_key));
1761 $note->isdeleted(1);
1764 my $r = $a->request($method, $e->authtoken, "biblio", $note)->gather(1);
1766 if (defined $U->event_code($r)) {
1767 $self->apache->log->warn(
1768 "attempt to modify cbrebin " . $note->id .
1769 " returned event " . $r->{textcode}
1773 $self->ctx->{bucket_action_event} = $r;
1778 for my $item_key (@item_keys) {
1779 my $id = int(($item_key =~ /(\d+)/)[0]);
1780 my $text = $self->cgi->param($item_key);
1783 next unless length $text;
1785 my $note = new Fieldmapper::container::biblio_record_entry_bucket_item_note;
1790 my $r = $a->request($method, $e->authtoken, "biblio", $note)->gather(1);
1792 if (defined $U->event_code($r)) {
1793 $self->apache->log->warn(
1794 "attempt to create cbrebin for item " . $note->item .
1795 " returned event " . $r->{textcode}
1799 $self->ctx->{bucket_action_event} = $r;
1808 sub load_myopac_bookbag_print {
1811 $self->apache->content_type("text/plain; encoding=utf8");
1813 my $id = int($self->cgi->param("list"));
1815 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("sort");
1818 $self->_prepare_bookbag_container_query($id, $sorter, $modifier);
1822 # Get the bookbag object itself, assuming we're allowed to.
1823 if ($self->editor->allowed("VIEW_CONTAINER")) {
1825 $bbag = $self->editor->retrieve_container_biblio_record_entry_bucket($id) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1827 my $bookbags = $self->editor->search_container_biblio_record_entry_bucket(
1831 "owner" => $self->editor->requestor->id,
1835 ) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1837 $bbag = pop @$bookbags;
1840 # If we have a bookbag we're allowed to look at, issue the A/T event
1841 # to get CSV, passing as a user param that search query we built before.
1843 $self->ctx->{csv} = $U->fire_object_event(
1844 undef, "container.biblio_record_entry_bucket.csv",
1845 $bbag, $self->editor->requestor->home_ou,
1846 undef, {"item_search" => $item_search}
1850 # Create a reasonable filename and set the content disposition to
1851 # provoke browser download dialogs.
1852 (my $filename = $bbag->id . $bbag->name) =~ s/[^a-z0-9_ -]//gi;
1854 $self->apache->headers_out->add(
1855 "Content-Disposition",
1856 "attachment;filename=$filename.csv"
1859 return Apache2::Const::OK;
1862 sub load_password_reset {
1864 my $cgi = $self->cgi;
1865 my $ctx = $self->ctx;
1866 my $barcode = $cgi->param('barcode');
1867 my $username = $cgi->param('username');
1868 my $email = $cgi->param('email');
1869 my $pwd1 = $cgi->param('pwd1');
1870 my $pwd2 = $cgi->param('pwd2');
1871 my $uuid = $ctx->{page_args}->[0];
1875 $logger->info("patron password reset with uuid $uuid");
1877 if ($pwd1 and $pwd2) {
1879 if ($pwd1 eq $pwd2) {
1881 my $response = $U->simplereq(
1883 'open-ils.actor.patron.password_reset.commit',
1886 $logger->info("patron password reset response " . Dumper($response));
1888 if ($U->event_code($response)) { # non-success event
1890 my $code = $response->{textcode};
1892 if ($code eq 'PATRON_NOT_AN_ACTIVE_PASSWORD_RESET_REQUEST') {
1893 $ctx->{pwreset} = {style => 'error', status => 'NOT_ACTIVE'};
1896 if ($code eq 'PATRON_PASSWORD_WAS_NOT_STRONG') {
1897 $ctx->{pwreset} = {style => 'error', status => 'NOT_STRONG'};
1902 $ctx->{pwreset} = {style => 'success', status => 'SUCCESS'};
1905 } else { # passwords not equal
1907 $ctx->{pwreset} = {style => 'error', status => 'NO_MATCH'};
1910 } else { # 2 password values needed
1912 $ctx->{pwreset} = {status => 'TWO_PASSWORDS'};
1915 } elsif ($barcode or $username) {
1917 my @params = $barcode ? ('barcode', $barcode) : ('username', $username);
1918 push(@params, $email) if $email;
1922 'open-ils.actor.patron.password_reset.request', @params);
1924 $ctx->{pwreset} = {status => 'REQUEST_SUCCESS'};
1927 $logger->info("patron password reset resulted in " . Dumper($ctx->{pwreset}));
1928 return Apache2::Const::OK;