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;
10 use OpenSRF::Utils::Cache;
11 use Digest::MD5 qw(md5_hex);
13 $Data::Dumper::Indent = 0;
15 my $U = 'OpenILS::Application::AppUtils';
17 sub prepare_extended_user_info {
20 my $e = $self->editor;
22 # are we already in a transaction?
23 my $local_xact = !$e->{xact_id};
24 $e->xact_begin if $local_xact;
26 # keep the original user object so we can restore
27 # login-specific data (e.g. workstation)
28 my $usr = $self->ctx->{user};
30 $self->ctx->{user} = $self->editor->retrieve_actor_user([
31 $self->ctx->{user}->id,
35 au => [qw/card home_ou addresses ident_type billing_address/, @extra_flesh]
41 $e->rollback if $local_xact;
43 $self->ctx->{user}->wsid($usr->wsid);
44 $self->ctx->{user}->ws_ou($usr->ws_ou);
46 # discard replaced (negative-id) addresses.
47 $self->ctx->{user}->addresses([
48 grep {$_->id > 0} @{$self->ctx->{user}->addresses} ]);
50 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR
51 unless $self->ctx->{user};
56 # Given an event returned by a failed attempt to create a hold, do we have
57 # permission to override? XXX Should the permission check be scoped to a
58 # given org_unit context?
59 sub test_could_override {
60 my ($self, $event) = @_;
62 return 0 unless $event;
63 return 1 if $self->editor->allowed($event->{textcode} . ".override");
64 return 1 if $event->{"fail_part"} and
65 $self->editor->allowed($event->{"fail_part"} . ".override");
69 # Find out whether we care that local copies are available
70 sub local_avail_concern {
71 my ($self, $hold_target, $hold_type, $pickup_lib) = @_;
73 my $would_block = $self->ctx->{get_org_setting}->
74 ($pickup_lib, "circ.holds.hold_has_copy_at.block");
76 $self->ctx->{get_org_setting}->
77 ($pickup_lib, "circ.holds.hold_has_copy_at.alert") and
78 not $self->cgi->param("override")
79 ) unless $would_block;
81 if ($would_block or $would_alert) {
83 "hold_target" => $hold_target,
84 "hold_type" => $hold_type,
85 "org_unit" => $pickup_lib
87 my $local_avail = $U->simplereq(
89 "open-ils.circ.hold.has_copy_at", $self->editor->authtoken, $args
92 "copy availability information for " . Dumper($args) .
93 " is " . Dumper($local_avail)
95 if (%$local_avail) { # if hash not empty
96 $self->ctx->{hold_copy_available} = $local_avail;
97 return ($would_block, $would_alert);
105 # user : au object, fleshed
106 sub load_myopac_prefs {
108 my $cgi = $self->cgi;
109 my $e = $self->editor;
110 my $pending_addr = $cgi->param('pending_addr');
111 my $replace_addr = $cgi->param('replace_addr');
112 my $delete_pending = $cgi->param('delete_pending');
114 $self->prepare_extended_user_info;
115 my $user = $self->ctx->{user};
117 my $lock_usernames = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.lock_usernames');
118 if(defined($lock_usernames) and $lock_usernames == 1) {
119 # Policy says no username changes
120 $self->ctx->{username_change_disallowed} = 1;
122 my $username_unlimit = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.unlimit_usernames');
123 if(!$username_unlimit) {
124 my $regex_check = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.barcode_regex');
126 # Default is "starts with a number"
127 $regex_check = '^\d+';
129 # You already have a username?
130 if($regex_check and $self->ctx->{user}->usrname !~ /$regex_check/) {
131 $self->ctx->{username_change_disallowed} = 1;
136 return Apache2::Const::OK unless
137 $pending_addr or $replace_addr or $delete_pending;
139 my @form_fields = qw/address_type street1 street2 city county state country post_code/;
142 if( $pending_addr ) { # update an existing pending address
144 ($paddr) = grep { $_->id == $pending_addr } @{$user->addresses};
145 return Apache2::Const::HTTP_BAD_REQUEST unless $paddr;
146 $paddr->$_( $cgi->param($_) ) for @form_fields;
148 } elsif( $replace_addr ) { # create a new pending address for 'replace_addr'
150 $paddr = Fieldmapper::actor::user_address->new;
152 $paddr->usr($user->id);
153 $paddr->pending('t');
154 $paddr->replaces($replace_addr);
155 $paddr->$_( $cgi->param($_) ) for @form_fields;
157 } elsif( $delete_pending ) {
158 $paddr = $e->retrieve_actor_user_address($delete_pending);
159 return Apache2::Const::HTTP_BAD_REQUEST unless
160 $paddr and $paddr->usr == $user->id and $U->is_true($paddr->pending);
161 $paddr->isdeleted(1);
164 my $resp = $U->simplereq(
166 'open-ils.actor.user.address.pending.cud',
167 $e->authtoken, $paddr);
169 if( $U->event_code($resp) ) {
170 $logger->error("Error updating pending address: $resp");
171 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
174 # in light of these changes, re-fetch latest data
176 $self->prepare_extended_user_info;
179 return Apache2::Const::OK;
182 sub load_myopac_prefs_notify {
184 my $e = $self->editor;
187 my $stat = $self->_load_user_with_prefs;
188 return $stat if $stat;
190 my $user_prefs = $self->fetch_optin_prefs;
191 $user_prefs = $self->update_optin_prefs($user_prefs)
192 if $self->cgi->request_method eq 'POST';
194 $self->ctx->{opt_in_settings} = $user_prefs;
196 return Apache2::Const::OK
197 unless $self->cgi->request_method eq 'POST';
200 my $set_map = $self->ctx->{user_setting_map};
204 opac.default_sms_notify
206 my $val = $self->cgi->param($key);
207 $settings{$key}= $val unless $$set_map{$key} eq $val;
210 my $key = 'opac.default_sms_carrier';
211 my $val = $self->cgi->param('sms_carrier');
212 $settings{$key}= $val unless $$set_map{$key} eq $val;
214 $key = 'opac.hold_notify';
215 my @notify_methods = ();
216 if ($self->cgi->param($key . ".email") eq 'on') {
217 push @notify_methods, "email";
219 if ($self->cgi->param($key . ".phone") eq 'on') {
220 push @notify_methods, "phone";
222 if ($self->cgi->param($key . ".sms") eq 'on') {
223 push @notify_methods, "sms";
225 $val = join("|",@notify_methods);
226 $settings{$key}= $val unless $$set_map{$key} eq $val;
228 # Send the modified settings off to be saved
231 'open-ils.actor.patron.settings.update',
232 $self->editor->authtoken, undef, \%settings);
234 # re-fetch user prefs
235 $self->ctx->{updated_user_settings} = \%settings;
236 return $self->_load_user_with_prefs || Apache2::Const::OK;
239 sub fetch_optin_prefs {
241 my $e = $self->editor;
243 # fetch all of the opt-in settings the user has access to
244 # XXX: user's should in theory have options to opt-in to notices
245 # for remote locations, but that opens the door for a large
246 # set of generally un-used opt-ins.. needs discussion
247 my $opt_ins = $U->simplereq(
249 'open-ils.actor.event_def.opt_in.settings.atomic',
250 $e->authtoken, $e->requestor->home_ou);
252 # some opt-ins are staff-only
253 $opt_ins = [ grep { $U->is_true($_->opac_visible) } @$opt_ins ];
255 # fetch user setting values for each of the opt-in settings
256 my $user_set = $U->simplereq(
258 'open-ils.actor.patron.settings.retrieve',
261 [map {$_->name} @$opt_ins]
264 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
267 sub load_myopac_messages {
269 my $e = $self->editor;
270 my $ctx = $self->ctx;
271 my $cgi = $self->cgi;
273 my $limit = $cgi->param('limit') || 20;
274 my $offset = $cgi->param('offset') || 0;
276 my $pcrud = OpenSRF::AppSession->create('open-ils.pcrud');
279 my $action = $cgi->param('action') || '';
281 my ($changed, $failed) = $self->_handle_message_action($pcrud, $action);
282 if ($changed > 0 || $failed > 0) {
283 $ctx->{message_update_action} = $action;
284 $ctx->{message_update_changed} = $changed;
285 $ctx->{message_update_failed} = $failed;
286 $self->update_dashboard_stats();
290 my $single = $cgi->param('single') || 0;
291 my $id = $cgi->param('message_id');
295 if (!$action && $single && $id) {
296 $messages = $self->_fetch_and_mark_read_single_message($pcrud, $id);
297 if (scalar(@$messages) == 1) {
298 $ctx->{display_single_message} = 1;
299 $ctx->{patron_message_id} = $id;
305 # fetch all the messages
306 ($ctx->{patron_messages_count}, $messages) =
307 $self->_fetch_user_messages($pcrud, $offset, $limit);
312 foreach my $aum (@$messages) {
314 push @{ $ctx->{patron_messages} }, {
316 title => $aum->title,
317 message => $aum->message,
318 create_date => $aum->create_date,
319 is_read => defined($aum->read_date) ? 1 : 0,
320 library => $aum->sending_lib->name,
324 $ctx->{patron_messages_limit} = $limit;
325 $ctx->{patron_messages_offset} = $offset;
327 return Apache2::Const::OK;
330 sub _fetch_and_mark_read_single_message {
335 $pcrud->request('open-ils.pcrud.transaction.begin', $self->editor->authtoken)->gather(1);
336 my $messages = $pcrud->request(
337 'open-ils.pcrud.search.auml.atomic',
338 $self->editor->authtoken,
340 usr => $self->editor->requestor->id,
346 flesh_fields => { auml => ['sending_lib'] },
350 $messages->[0]->read_date('now');
352 'open-ils.pcrud.update.auml',
353 $self->editor->authtoken,
357 $pcrud->request('open-ils.pcrud.transaction.commit', $self->editor->authtoken)->gather(1);
359 $self->update_dashboard_stats();
364 sub _fetch_user_messages {
370 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
372 my $all_messages = $pcrud->request(
373 'open-ils.pcrud.id_list.auml.atomic',
374 $self->editor->authtoken,
376 usr => $self->editor->requestor->id,
382 my $messages = $pcrud->request(
383 'open-ils.pcrud.search.auml.atomic',
384 $self->editor->authtoken,
386 usr => $self->editor->requestor->id,
391 flesh_fields => { auml => ['sending_lib'] },
392 order_by => { auml => 'create_date DESC' },
397 return scalar(@$all_messages), $messages;
400 sub _handle_message_action {
404 my $cgi = $self->cgi;
406 my @ids = $cgi->param('message_id');
407 return (0, 0) unless @ids;
411 $pcrud->request('open-ils.pcrud.transaction.begin', $self->editor->authtoken)->gather(1);
413 my $aum = $pcrud->request(
414 'open-ils.pcrud.retrieve.auml',
415 $self->editor->authtoken,
419 if ($action eq 'mark_read') {
420 $aum->read_date('now');
421 } elsif ($action eq 'mark_unread') {
422 $aum->clear_read_date();
423 } elsif ($action eq 'mark_deleted') {
426 $pcrud->request('open-ils.pcrud.update.auml', $self->editor->authtoken, $aum)->gather(1) ?
431 $pcrud->request('open-ils.pcrud.transaction.rollback', $self->editor->authtoken)->gather(1);
433 $failed = scalar(@ids);
435 $pcrud->request('open-ils.pcrud.transaction.commit', $self->editor->authtoken)->gather(1);
437 return ($changed, $failed);
440 sub _load_lists_and_settings {
442 my $e = $self->editor;
443 my $stat = $self->_load_user_with_prefs;
446 my $setting_map = $self->ctx->{user_setting_map};
447 $exclude = $$setting_map{'opac.default_list'} if ($$setting_map{'opac.default_list'});
448 $self->ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket(
450 {owner => $self->ctx->{user}->id, btype => 'bookbag', id => {'<>' => $exclude}}, {
451 order_by => {cbreb => 'name'},
452 limit => $self->cgi->param('limit') || 10,
453 offset => $self->cgi->param('offset') || 0
457 # We also want a total count of the user's bookbags.
459 'select' => { 'cbreb' => [ { 'column' => 'id', 'transform' => 'count', 'aggregate' => 'true', 'alias' => 'count' } ] },
461 'where' => { 'btype' => 'bookbag', 'owner' => $self->ctx->{user}->id }
463 my $r = $e->json_query($q);
464 $self->ctx->{bookbag_count} = $r->[0]->{'count'};
465 # Someone has requested that we use the default list's name
466 # rather than "Default List."
469 'select' => {'cbreb' => ['name']},
471 'where' => {'id' => $exclude}
473 $r = $e->json_query($q);
474 $self->ctx->{default_bookbag} = $r->[0]->{'name'};
482 sub update_optin_prefs {
484 my $user_prefs = shift;
485 my $e = $self->editor;
486 my @settings = $self->cgi->param('setting');
489 # apply now-true settings
490 for my $applied (@settings) {
491 # see if setting is already applied to this user
492 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
493 $newsets{$applied} = OpenSRF::Utils::JSON->true;
496 # remove now-false settings
497 for my $pref (grep { $_->{value} } @$user_prefs) {
498 $newsets{$pref->{cust}->name} = undef
499 unless grep { $_ eq $pref->{cust}->name } @settings;
504 'open-ils.actor.patron.settings.update',
505 $e->authtoken, $e->requestor->id, \%newsets);
507 # update the local prefs to match reality
508 for my $pref (@$user_prefs) {
509 $pref->{value} = $newsets{$pref->{cust}->name}
510 if exists $newsets{$pref->{cust}->name};
516 sub _load_user_with_prefs {
518 my $stat = $self->prepare_extended_user_info('settings');
519 return $stat if $stat; # not-OK
521 $self->ctx->{user_setting_map} = {
522 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
523 @{$self->ctx->{user}->settings}
529 sub _get_bookbag_sort_params {
530 my ($self, $param_name) = @_;
532 # The interface that feeds this cgi parameter will provide a single
533 # argument for a QP sort filter, and potentially a modifier after a period.
534 # In practice this means the "sort" parameter will be something like
535 # "titlesort" or "authorsort.descending".
536 my $sorter = $self->cgi->param($param_name) || "";
539 $sorter =~ s/^(.*?)\.(.*)/$1/;
540 $modifier = $2 || undef;
543 return ($sorter, $modifier);
546 sub _prepare_bookbag_container_query {
547 my ($self, $container_id, $sorter, $modifier) = @_;
550 "container(bre,bookbag,%d,%s)%s%s",
551 $container_id, $self->editor->authtoken,
552 ($sorter ? " sort($sorter)" : ""),
553 ($modifier ? "#$modifier" : "")
557 sub _prepare_anonlist_sorting_query {
558 my ($self, $list, $sorter, $modifier) = @_;
561 "record_list(%s)%s%s",
563 ($sorter ? " sort($sorter)" : ""),
564 ($modifier ? "#$modifier" : "")
569 sub load_myopac_prefs_settings {
574 opac.default_search_location
575 opac.default_pickup_location
576 opac.temporary_list_no_warn
579 my $stat = $self->_load_user_with_prefs;
580 return $stat if $stat;
582 # if behind-desk holds are supported and the user
583 # setting which controls the value is opac-visible,
584 # add the setting to the list of settings to manage.
585 # note: this logic may need to be changed later to
586 # check whether behind-the-desk holds are supported
587 # anywhere the patron may select as a pickup lib.
588 my $e = $self->editor;
589 my $bdous = $self->ctx->{get_org_setting}->(
590 $e->requestor->home_ou,
591 'circ.holds.behind_desk_pickup_supported');
595 $e->retrieve_config_usr_setting_type(
596 'circ.holds_behind_desk');
598 if ($U->is_true($setting->opac_visible)) {
599 push(@user_prefs, 'circ.holds_behind_desk');
600 $self->ctx->{behind_desk_supported} = 1;
604 return Apache2::Const::OK
605 unless $self->cgi->request_method eq 'POST';
607 # some setting values from the form don't match the
608 # required value/format for the db, so they have to be
609 # individually translated.
612 my $set_map = $self->ctx->{user_setting_map};
614 foreach my $key (@user_prefs) {
615 my $val = $self->cgi->param($key);
616 $settings{$key}= $val unless $$set_map{$key} eq $val;
619 my $now = DateTime->now->strftime('%F');
620 foreach my $key (qw/history.circ.retention_start history.hold.retention_start/) {
621 my $val = $self->cgi->param($key);
622 if($val and $val eq 'on') {
623 # Set the start time to 'now' unless a start time already exists for the user
624 $settings{$key} = $now unless $$set_map{$key};
626 # clear the start time if one previously existed for the user
627 $settings{$key} = undef if $$set_map{$key};
631 # Send the modified settings off to be saved
634 'open-ils.actor.patron.settings.update',
635 $self->editor->authtoken, undef, \%settings);
637 # re-fetch user prefs
638 $self->ctx->{updated_user_settings} = \%settings;
639 return $self->_load_user_with_prefs || Apache2::Const::OK;
642 sub load_myopac_prefs_my_lists {
647 opac.list_items_per_page
650 my $stat = $self->_load_user_with_prefs;
651 return $stat if $stat;
653 return Apache2::Const::OK
654 unless $self->cgi->request_method eq 'POST';
657 my $set_map = $self->ctx->{user_setting_map};
659 foreach my $key (@user_prefs) {
660 my $val = $self->cgi->param($key);
661 $settings{$key}= $val unless $$set_map{$key} eq $val;
664 if (keys %settings) { # we found a different setting value
665 # Send the modified settings off to be saved
668 'open-ils.actor.patron.settings.update',
669 $self->editor->authtoken, undef, \%settings);
671 # re-fetch user prefs
672 $self->ctx->{updated_user_settings} = \%settings;
673 $stat = $self->_load_user_with_prefs;
676 return $stat || Apache2::Const::OK;
679 sub fetch_user_holds {
681 my $hold_ids = shift;
682 my $ids_only = shift;
684 my $available = shift;
688 my $e = $self->editor;
689 my $all_ids; # to be used below.
692 my $circ = OpenSRF::AppSession->create('open-ils.circ');
694 $hold_ids = $circ->request(
695 'open-ils.circ.holds.id_list.retrieve.authoritative',
702 $all_ids = $hold_ids;
703 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
706 $all_ids = $hold_ids;
709 return { ids => $hold_ids, all_ids => $all_ids } if $ids_only or @$hold_ids == 0;
712 suppress_notices => 1,
713 suppress_transits => 1,
715 suppress_patron_details => 1
718 # ----------------------------------------------------------------
719 # Collect holds in batches of $batch_size for faster retrieval
723 my $mk_req_batch = sub {
725 my $top_idx = $batch_idx + $batch_size;
726 while($batch_idx < $top_idx) {
727 my $hold_id = $hold_ids->[$batch_idx++];
728 last unless $hold_id;
729 my $ses = OpenSRF::AppSession->create('open-ils.circ');
730 my $req = $ses->request(
731 'open-ils.circ.hold.details.retrieve',
732 $e->authtoken, $hold_id, $args);
733 push(@ses, {ses => $ses, req => $req});
739 my(@collected, @holds, @ses);
742 @ses = $mk_req_batch->() if $first;
743 last if $first and not @ses;
746 while(my $blob = pop(@collected)) {
749 # in the holds edit UI, we need to know what formats and
750 # languages the user selected for this hold, plus what
751 # formats/langs are available on the MR as a whole.
752 if ($blob->{hold}{hold}->hold_type eq 'M') {
753 my $hold = $blob->{hold}->{hold};
755 # for MR, fetch the combined MR unapi blob
756 (undef, @data) = $self->get_records_and_facets(
757 [$hold->target], undef, {flesh => '{mra}', metarecord => 1});
759 my $filter_org = $U->org_unit_ancestor_at_depth(
761 $hold->selection_depth);
763 my $filter_data = $U->simplereq(
765 'open-ils.circ.mmr.holds.filters.authoritative.atomic',
766 $hold->target, $filter_org, [$hold->id]
769 $blob->{metarecord_filters} =
770 $filter_data->[0]->{metarecord};
771 $blob->{metarecord_selected_filters} =
772 $filter_data->[1]->{hold};
775 (undef, @data) = $self->get_records_and_facets(
776 [$blob->{hold}->{bre_id}], undef, {flesh => '{mra}'}
780 $blob->{marc_xml} = $data[0]->{marc_xml};
785 for my $req_data (@ses) {
786 push(@collected, {hold => $req_data->{req}->gather(1)});
787 $req_data->{ses}->kill_me;
790 @ses = $mk_req_batch->();
791 last unless @collected or @ses;
795 # put the holds back into the original server sort order
797 for my $id (@$hold_ids) {
798 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
801 return { holds => \@sorted, ids => $hold_ids, all_ids => $all_ids };
804 sub handle_hold_update {
807 my $hold_ids = shift;
808 my $e = $self->editor;
811 my @hold_ids = ($hold_ids) ? @$hold_ids : $self->cgi->param('hold_id'); # for non-_all actions
812 @hold_ids = @{$self->fetch_user_holds(undef, 1)->{ids}} if $action =~ /_all/;
814 my $circ = OpenSRF::AppSession->create('open-ils.circ');
816 if($action =~ /cancel/) {
818 for my $hold_id (@hold_ids) {
819 my $resp = $circ->request(
820 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
823 } elsif ($action =~ /activate|suspend/) {
826 for my $hold_id (@hold_ids) {
827 my $vals = {id => $hold_id};
829 if($action =~ /activate/) {
830 $vals->{frozen} = 'f';
831 $vals->{thaw_date} = undef;
833 } elsif($action =~ /suspend/) {
834 $vals->{frozen} = 't';
835 # $vals->{thaw_date} = TODO;
837 push(@$vlist, $vals);
840 my $resp = $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
841 $self->ctx->{hold_suspend_post_capture} = 1 if
842 grep {$U->event_equals($_, 'HOLD_SUSPEND_AFTER_CAPTURE')} @$resp;
844 } elsif ($action eq 'edit') {
847 my $val = {"id" => $_};
848 $val->{"frozen"} = $self->cgi->param("frozen");
849 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
851 for my $field (qw/expire_time thaw_date/) {
852 # XXX TODO make this support other date formats, not just
854 next unless $self->cgi->param($field) =~
855 m:^(\d{2})/(\d{2})/(\d{4})$:;
856 $val->{$field} = "$3-$1-$2";
859 $val->{holdable_formats} = # no-op for non-MR holds
860 $self->compile_holdable_formats(undef, $_);
866 'open-ils.circ.hold.update.batch.atomic',
867 $e->authtoken, undef, \@vals
868 )->gather(1); # LFW XXX test for failure
869 $url = $self->ctx->{proto} . '://' . $self->ctx->{hostname} . $self->ctx->{opac_root} . '/myopac/holds';
870 foreach my $param (('loc', 'qtype', 'query')) {
871 if ($self->cgi->param($param)) {
872 $url .= ";$param=" . uri_escape_utf8($self->cgi->param($param));
878 return defined($url) ? $self->generic_redirect($url) : undef;
881 sub load_myopac_holds {
883 my $e = $self->editor;
884 my $ctx = $self->ctx;
886 my $limit = $self->cgi->param('limit') || 15;
887 my $offset = $self->cgi->param('offset') || 0;
888 my $action = $self->cgi->param('action') || '';
889 my $hold_id = $self->cgi->param('id');
890 my $available = int($self->cgi->param('available') || 0);
892 my $hold_handle_result;
893 $hold_handle_result = $self->handle_hold_update($action) if $action;
896 if ($self->cgi->param('sort') ne "") {
897 $holds_object = $self->fetch_user_holds($hold_id ? [$hold_id] : undef, 0, 1, $available);
900 $holds_object = $self->fetch_user_holds($hold_id ? [$hold_id] : undef, 0, 1, $available, $limit, $offset);
903 if($holds_object->{holds}) {
904 $ctx->{holds} = $holds_object->{holds};
906 $ctx->{holds_ids} = $holds_object->{all_ids};
907 $ctx->{holds_limit} = $limit;
908 $ctx->{holds_offset} = $offset;
910 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
915 sub load_place_hold {
917 my $ctx = $self->ctx;
918 my $gos = $ctx->{get_org_setting};
919 my $e = $self->editor;
920 my $cgi = $self->cgi;
922 $self->ctx->{page} = 'place_hold';
923 my @targets = $cgi->param('hold_target');
924 my @parts = $cgi->param('part');
926 $ctx->{hold_type} = $cgi->param('hold_type');
927 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # unless changed below
928 $ctx->{email_notify} = $cgi->param('email_notify');
929 if ($cgi->param('phone_notify_checkbox')) {
930 $ctx->{phone_notify} = $cgi->param('phone_notify');
932 if ($cgi->param('sms_notify_checkbox')) {
933 $ctx->{sms_notify} = $cgi->param('sms_notify');
934 $ctx->{sms_carrier} = $cgi->param('sms_carrier');
937 return $self->generic_redirect unless @targets;
939 $logger->info("Looking at hold_type: " . $ctx->{hold_type} . " and targets: @targets");
941 $ctx->{staff_recipient} = $self->editor->retrieve_actor_user([
946 au => ['settings', 'card']
949 ]) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
950 my $user_setting_map = {
951 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
953 $ctx->{staff_recipient}->settings
956 $ctx->{user_setting_map} = $user_setting_map;
958 my $default_notify = (defined $$user_setting_map{'opac.hold_notify'} ? $$user_setting_map{'opac.hold_notify'} : 'email:phone');
959 if ($default_notify =~ /email/) {
960 $ctx->{default_email_notify} = 'checked';
962 $ctx->{default_email_notify} = '';
964 if ($default_notify =~ /phone/) {
965 $ctx->{default_phone_notify} = 'checked';
967 $ctx->{default_phone_notify} = '';
969 if ($default_notify =~ /sms/) {
970 $ctx->{default_sms_notify} = 'checked';
972 $ctx->{default_sms_notify} = '';
975 # If we have a default pickup location, grab it
976 if ($$user_setting_map{'opac.default_pickup_location'}) {
977 $ctx->{default_pickup_lib} = $$user_setting_map{'opac.default_pickup_location'};
980 my $request_lib = $e->requestor->ws_ou;
982 $ctx->{hold_data} = \@hold_data;
986 if ($ctx->{email_notify}) { $hdata->{email_notify} = $ctx->{email_notify}; }
987 if ($ctx->{phone_notify}) { $hdata->{phone_notify} = $ctx->{phone_notify}; }
988 if ($ctx->{sms_notify}) { $hdata->{sms_notify} = $ctx->{sms_notify}; }
989 if ($ctx->{sms_carrier}) { $hdata->{sms_carrier} = $ctx->{sms_carrier}; }
993 my $type_dispatch = {
996 my $mrecs = $e->batch_retrieve_metabib_metarecord([
998 {flesh => 1, flesh_fields => {mmr => ['master_record']}}],
1002 for my $id (@targets) {
1003 my ($mr) = grep {$_->id eq $id} @$mrecs;
1005 my $ou_id = $cgi->param('pickup_lib') || $self->ctx->{search_ou};
1006 my $filter_data = $U->simplereq(
1008 'open-ils.circ.mmr.holds.filters.authoritative', $mr->id, $ou_id);
1010 my $holdable_formats =
1011 $self->compile_holdable_formats($mr->id);
1013 push(@hold_data, $data_filler->({
1015 record => $mr->master_record,
1016 holdable_formats => $holdable_formats,
1017 metarecord_filters => $filter_data->{metarecord}
1022 my $recs = $e->batch_retrieve_biblio_record_entry(
1023 [\@targets, {flesh => 1, flesh_fields => {bre => ['metarecord']}}],
1027 for my $id (@targets) { # force back into the correct order
1028 my ($rec) = grep {$_->id eq $id} @$recs;
1030 # NOTE: if tpac ever supports locked-down pickup libs,
1031 # we'll need to pass a pickup_lib param along with the
1032 # record to filter the set of monographic parts.
1033 my $parts = $U->simplereq(
1035 'open-ils.search.biblio.record_hold_parts',
1036 {record => $rec->id}
1039 # T holds on records that have parts are OK, but if the record has
1040 # no non-part copies, the hold will ultimately fail. When that
1041 # happens, require the user to select a part.
1042 my $part_required = 0;
1044 my $np_copies = $e->json_query({
1045 select => { acp => [{column => 'id', transform => 'count', alias => 'count'}]},
1046 from => {acp => {acn => {}, acpm => {type => 'left'}}},
1048 '+acp' => {deleted => 'f'},
1049 '+acn' => {deleted => 'f', record => $rec->id},
1050 '+acpm' => {id => undef}
1053 $part_required = 1 if $np_copies->[0]->{count} == 0;
1056 push(@hold_data, $data_filler->({
1060 part_required => $part_required
1065 my $vols = $e->batch_retrieve_asset_call_number([
1068 "flesh_fields" => {"acn" => ["record"]}
1070 ], {substream => 1});
1072 for my $id (@targets) {
1073 my ($vol) = grep {$_->id eq $id} @$vols;
1074 push(@hold_data, $data_filler->({target => $vol, record => $vol->record}));
1078 my $copies = $e->batch_retrieve_asset_copy([
1082 "acn" => ["record"],
1083 "acp" => ["call_number"]
1086 ], {substream => 1});
1088 for my $id (@targets) {
1089 my ($copy) = grep {$_->id eq $id} @$copies;
1090 push(@hold_data, $data_filler->({target => $copy, record => $copy->call_number->record}));
1094 my $isses = $e->batch_retrieve_serial_issuance([
1098 "siss" => ["subscription"], "ssub" => ["record_entry"]
1101 ], {substream => 1});
1103 for my $id (@targets) {
1104 my ($iss) = grep {$_->id eq $id} @$isses;
1105 push(@hold_data, $data_filler->({target => $iss, record => $iss->subscription->record_entry}));
1110 }->{$ctx->{hold_type}}->();
1112 # caller sent bad target IDs or the wrong hold type
1113 return Apache2::Const::HTTP_BAD_REQUEST unless @hold_data;
1115 # generate the MARC xml for each record
1116 $_->{marc_xml} = XML::LibXML->new->parse_string($_->{record}->marc) for @hold_data;
1118 my $pickup_lib = $cgi->param('pickup_lib');
1119 # no pickup lib means no holds placement
1120 return Apache2::Const::OK unless $pickup_lib;
1122 $ctx->{hold_attempt_made} = 1;
1124 # Give the original CGI params back to the user in case they
1125 # want to try to override something.
1126 $ctx->{orig_params} = $cgi->Vars;
1127 delete $ctx->{orig_params}{submit};
1128 delete $ctx->{orig_params}{hold_target};
1129 delete $ctx->{orig_params}{part};
1131 my $usr = $e->requestor->id;
1133 if ($ctx->{is_staff} and !$cgi->param("hold_usr_is_requestor")) {
1134 # find the real hold target
1136 $usr = $U->simplereq(
1138 "open-ils.actor.user.retrieve_id_by_barcode_or_username",
1139 $e->authtoken, $cgi->param("hold_usr"));
1141 if (defined $U->event_code($usr)) {
1142 $ctx->{hold_failed} = 1;
1143 $ctx->{hold_failed_event} = $usr;
1147 # target_id is the true target_id for holds placement.
1148 # needed for attempt_hold_placement()
1149 # With the exception of P-type holds, target_id == target->id.
1150 $_->{target_id} = $_->{target}->id for @hold_data;
1152 if ($ctx->{hold_type} eq 'T') {
1154 # Much like quantum wave-particles, P-type holds pop into
1155 # and out of existence at the user's whim. For our purposes,
1156 # we treat such holds as T(itle) holds with a selected_part
1157 # designation. When the time comes to pass the hold information
1158 # off for holds possibility testing and placement, make it look
1159 # like a real P-type hold.
1160 my (@p_holds, @t_holds);
1162 for my $idx (0..$#parts) {
1163 my $hdata = $hold_data[$idx];
1164 if (my $part = $parts[$idx]) {
1165 $hdata->{target_id} = $part;
1166 $hdata->{selected_part} = $part;
1167 push(@p_holds, $hdata);
1169 push(@t_holds, $hdata);
1173 $self->apache->log->warn("$#parts : @t_holds");
1175 $self->attempt_hold_placement($usr, $pickup_lib, 'P', @p_holds) if @p_holds;
1176 $self->attempt_hold_placement($usr, $pickup_lib, 'T', @t_holds) if @t_holds;
1179 $self->attempt_hold_placement($usr, $pickup_lib, $ctx->{hold_type}, @hold_data);
1182 # NOTE: we are leaving the staff-placed patron barcode cookie
1183 # in place. Otherwise, it's not possible to place more than
1184 # one hold for the patron within a staff/patron session. This
1185 # does leave the barcode to linger longer than is ideal, but
1186 # normal staff work flow will cause the cookie to be replaced
1187 # with each new patron anyway.
1188 # TODO: See about getting the staff client to clear the cookie
1190 # return to the place_hold page so the results of the hold
1191 # placement attempt can be reported to the user
1192 return Apache2::Const::OK;
1195 sub attempt_hold_placement {
1196 my ($self, $usr, $pickup_lib, $hold_type, @hold_data) = @_;
1197 my $cgi = $self->cgi;
1198 my $ctx = $self->ctx;
1199 my $e = $self->editor;
1201 # First see if we should warn/block for any holds that
1202 # might have locally available items.
1203 for my $hdata (@hold_data) {
1204 my ($local_block, $local_alert) = $self->local_avail_concern(
1205 $hdata->{target_id}, $hold_type, $pickup_lib);
1208 $hdata->{hold_failed} = 1;
1209 $hdata->{hold_local_block} = 1;
1210 } elsif ($local_alert) {
1211 $hdata->{hold_failed} = 1;
1212 $hdata->{hold_local_alert} = 1;
1216 my $method = 'open-ils.circ.holds.test_and_create.batch';
1218 if ($cgi->param('override')) {
1219 $method .= '.override';
1221 } elsif (!$ctx->{is_staff}) {
1223 $method .= '.override' if $self->ctx->{get_org_setting}->(
1224 $e->requestor->home_ou, "opac.patron.auto_overide_hold_events");
1227 my @create_targets = map {$_->{target_id}} (grep { !$_->{hold_failed} } @hold_data);
1229 if(@create_targets) {
1231 # holdable formats may be different for each MR hold.
1232 # map each set to the ID of the target.
1233 my $holdable_formats = {};
1234 if ($hold_type eq 'M') {
1235 $holdable_formats->{$_->{target_id}} =
1236 $_->{holdable_formats} for @hold_data;
1239 my $bses = OpenSRF::AppSession->create('open-ils.circ');
1240 my $breq = $bses->request(
1245 pickup_lib => $pickup_lib,
1246 hold_type => $hold_type,
1247 holdable_formats_map => $holdable_formats
1252 while (my $resp = $breq->recv) {
1254 $resp = $resp->content;
1255 $logger->info('batch hold placement result: ' . OpenSRF::Utils::JSON->perl2JSON($resp));
1257 if ($U->event_code($resp)) {
1258 $ctx->{general_hold_error} = $resp;
1262 my ($hdata) = grep {$_->{target_id} eq $resp->{target}} @hold_data;
1263 my $result = $resp->{result};
1265 if ($U->event_code($result)) {
1266 # e.g. permission denied
1267 $hdata->{hold_failed} = 1;
1268 $hdata->{hold_failed_event} = $result;
1272 if(not ref $result and $result > 0) {
1273 # successul hold returns the hold ID
1275 $hdata->{hold_success} = $result;
1278 # hold-specific failure event
1279 $hdata->{hold_failed} = 1;
1281 if (ref $result eq 'HASH') {
1282 $hdata->{hold_failed_event} = $result->{last_event};
1284 if ($result->{age_protected_copy}) {
1285 my %temp = %{$hdata->{hold_failed_event}};
1286 my $theTextcode = $temp{"textcode"};
1287 $theTextcode.=".override";
1288 $hdata->{could_override} = $self->editor->allowed( $theTextcode );
1289 $hdata->{age_protect} = 1;
1291 $hdata->{could_override} = $result->{place_unfillable} ||
1292 $self->test_could_override($hdata->{hold_failed_event});
1294 } elsif (ref $result eq 'ARRAY') {
1295 $hdata->{hold_failed_event} = $result->[0];
1297 if ($result->[3]) { # age_protect_only
1298 my %temp = %{$hdata->{hold_failed_event}};
1299 my $theTextcode = $temp{"textcode"};
1300 $theTextcode.=".override";
1301 $hdata->{could_override} = $self->editor->allowed( $theTextcode );
1302 $hdata->{age_protect} = 1;
1304 $hdata->{could_override} = $result->[4] || # place_unfillable
1305 $self->test_could_override($hdata->{hold_failed_event});
1316 # pull the selected formats and languages for metarecord holds
1317 # from the CGI params and map them into the JSON holdable
1318 # formats...er, format.
1319 # if no metarecord is provided, we'll pull it from the target
1320 # of the provided hold.
1321 sub compile_holdable_formats {
1322 my ($self, $mr_id, $hold_id) = @_;
1323 my $e = $self->editor;
1324 my $cgi = $self->cgi;
1326 # exit early if not needed
1328 grep /metarecord_formats_|metarecord_langs_/,
1331 # CGI params are based on the MR id, since during hold placement
1332 # we have no old ID. During hold edit, map the hold ID back to
1333 # the metarecod target.
1335 $e->retrieve_action_hold_request($hold_id)->target
1338 my $format_attr = $self->ctx->{get_cgf}->(
1339 'opac.metarecord.holds.format_attr');
1341 if (!$format_attr) {
1342 $logger->error("Missing config.global_flag: ".
1343 "opac.metarecord.holds.format_attr!");
1347 $format_attr = $format_attr->value;
1349 # during hold placement or edit submission, the user selects
1350 # which of the available formats/langs are acceptable.
1351 # Capture those here as the holdable_formats for the MR hold.
1352 my @selected_formats = $cgi->param("metarecord_formats_$mr_id");
1353 my @selected_langs = $cgi->param("metarecord_langs_$mr_id");
1355 # map the selected attrs into the JSON holdable_formats structure
1357 if (@selected_formats) {
1359 map { {_attr => $format_attr, _val => $_} }
1363 if (@selected_langs) {
1365 map { {_attr => 'item_lang', _val => $_} }
1370 return OpenSRF::Utils::JSON->perl2JSON($blob);
1373 sub fetch_user_circs {
1375 my $flesh = shift; # flesh bib data, etc.
1376 my $circ_ids = shift;
1380 my $e = $self->editor;
1385 @circ_ids = @$circ_ids;
1390 select => {circ => ['id']},
1394 usr => $e->requestor->id,
1395 checkin_time => undef,
1397 {stop_fines => undef},
1398 {stop_fines => {'not in' => ['LOST','CLAIMSRETURNED','LONGOVERDUE']}}
1402 order_by => {circ => ['due_date']}
1405 $query->{limit} = $limit if $limit;
1406 $query->{offset} = $offset if $offset;
1408 my $ids = $e->json_query($query);
1409 @circ_ids = map {$_->{id}} @$ids;
1412 return [] unless @circ_ids;
1417 circ => ['target_copy'],
1418 acp => ['call_number'],
1424 my $circs = $e->search_action_circulation(
1425 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
1428 for my $circ (@$circs) {
1431 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
1432 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
1433 undef # pre-cat copy, use the dummy title/author instead
1438 # make sure the final list is in the correct order
1440 for my $id (@circ_ids) {
1443 (grep { $_->{circ}->id == $id } @circs)
1447 return \@sorted_circs;
1451 sub handle_circ_renew {
1454 my $ctx = $self->ctx;
1456 my @renew_ids = $self->cgi->param('circ');
1458 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
1460 # TODO: fire off renewal calls in batches to speed things up
1462 for my $circ (@$circs) {
1464 my $evt = $U->simplereq(
1466 'open-ils.circ.renew',
1467 $self->editor->authtoken,
1469 patron_id => $self->editor->requestor->id,
1470 copy_id => $circ->{circ}->target_copy,
1475 # TODO return these, then insert them into the circ data
1476 # blob that is shoved into the template for each circ
1477 # so the template won't have to match them
1478 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
1485 sub load_myopac_circs {
1487 my $e = $self->editor;
1488 my $ctx = $self->ctx;
1491 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
1492 my $offset = $self->cgi->param('offset') || 0;
1493 my $action = $self->cgi->param('action') || '';
1495 # perform the renewal first if necessary
1496 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
1498 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
1500 my $success_renewals = 0;
1501 my $failed_renewals = 0;
1502 for my $data (@{$ctx->{circs}}) {
1503 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
1506 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
1507 $data->{renewal_response} = $evt;
1508 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
1509 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
1513 $ctx->{success_renewals} = $success_renewals;
1514 $ctx->{failed_renewals} = $failed_renewals;
1516 return Apache2::Const::OK;
1519 sub load_myopac_circ_history {
1521 my $e = $self->editor;
1522 my $ctx = $self->ctx;
1523 my $limit = $self->cgi->param('limit') || 15;
1524 my $offset = $self->cgi->param('offset') || 0;
1526 $ctx->{circ_history_limit} = $limit;
1527 $ctx->{circ_history_offset} = $offset;
1530 if ($self->cgi->param('sort') ne "") { # Defer limitation to circ_history.tt2
1531 $circ_ids = $e->json_query({
1535 transform => 'action.usr_visible_circs',
1536 result_field => 'id'
1540 where => {id => $e->requestor->id}
1544 $circ_ids = $e->json_query({
1548 transform => 'action.usr_visible_circs',
1549 result_field => 'id'
1553 where => {id => $e->requestor->id},
1559 $ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circ_ids]);
1560 return Apache2::Const::OK;
1563 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
1564 sub load_myopac_hold_history {
1566 my $e = $self->editor;
1567 my $ctx = $self->ctx;
1568 my $limit = $self->cgi->param('limit') || 15;
1569 my $offset = $self->cgi->param('offset') || 0;
1570 $ctx->{hold_history_limit} = $limit;
1571 $ctx->{hold_history_offset} = $offset;
1573 my $hold_ids = $e->json_query({
1577 transform => 'action.usr_visible_holds',
1578 result_field => 'id'
1582 where => {id => $e->requestor->id}
1585 my $holds_object = $self->fetch_user_holds([map { $_->{id} } @$hold_ids], 0, 1, 0, $limit, $offset);
1586 if($holds_object->{holds}) {
1587 $ctx->{holds} = $holds_object->{holds};
1589 $ctx->{hold_history_ids} = $holds_object->{all_ids};
1591 return Apache2::Const::OK;
1594 sub load_myopac_payment_form {
1598 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]) and return $r;
1599 $r = $self->prepare_extended_user_info and return $r;
1601 return Apache2::Const::OK;
1604 # TODO: add other filter options as params/configs/etc.
1605 sub load_myopac_payments {
1607 my $limit = $self->cgi->param('limit') || 20;
1608 my $offset = $self->cgi->param('offset') || 0;
1609 my $e = $self->editor;
1611 $self->ctx->{payment_history_limit} = $limit;
1612 $self->ctx->{payment_history_offset} = $offset;
1615 $args->{limit} = $limit if $limit;
1616 $args->{offset} = $offset if $offset;
1618 if (my $max_age = $self->ctx->{get_org_setting}->(
1619 $e->requestor->home_ou, "opac.payment_history_age_limit"
1621 my $min_ts = DateTime->now(
1622 "time_zone" => DateTime::TimeZone->new("name" => "local"),
1623 )->subtract("seconds" => interval_to_seconds($max_age))->iso8601();
1625 $logger->info("XXX min_ts: $min_ts");
1626 $args->{"where"} = {"payment_ts" => {">=" => $min_ts}};
1629 $self->ctx->{payments} = $U->simplereq(
1631 'open-ils.actor.user.payments.retrieve.atomic',
1632 $e->authtoken, $e->requestor->id, $args);
1634 return Apache2::Const::OK;
1637 # 1. caches the form parameters
1638 # 2. loads the credit card payment "Processing..." page
1639 sub load_myopac_pay_init {
1641 my $cache = OpenSRF::Utils::Cache->new('global');
1643 my @payment_xacts = ($self->cgi->param('xact'), $self->cgi->param('xact_misc'));
1645 if (!@payment_xacts) {
1646 # for consistency with load_myopac_payment_form() and
1647 # to preserve backwards compatibility, if no xacts are
1648 # selected, assume all (applicable) transactions are wanted.
1649 my $stat = $self->prepare_fines(undef, undef, [$self->cgi->param('xact'), $self->cgi->param('xact_misc')]);
1650 return $stat if $stat;
1652 map { $_->{xact}->id } (
1653 @{$self->ctx->{fines}->{circulation}},
1654 @{$self->ctx->{fines}->{grocery}}
1658 return $self->generic_redirect unless @payment_xacts;
1660 my $cc_args = {"where_process" => 1};
1662 $cc_args->{$_} = $self->cgi->param($_) for (qw/
1663 number cvv2 expire_year expire_month billing_first
1664 billing_last billing_address billing_city billing_state
1665 billing_zip stripe_token
1669 cc_args => $cc_args,
1670 user => $self->ctx->{user}->id,
1671 xacts => \@payment_xacts
1674 # generate a temporary cache token and cache the form data
1675 my $token = md5_hex($$ . time() . rand());
1676 $cache->put_cache($token, $cache_args, 30);
1678 $logger->info("tpac caching payment info with token $token and xacts [@payment_xacts]");
1680 # after we render the processing page, we quickly redirect to submit
1681 # the actual payment. The refresh url contains the payment token.
1682 # It also contains the list of xact IDs, which allows us to clear the
1683 # cache at the earliest possible time while leaving a trace of which
1684 # transactions we were processing, so the UI can bring the user back
1685 # to the payment form w/ the same xacts if the payment fails.
1687 my $refresh = "1; url=main_pay/$token?xact=" . pop(@payment_xacts);
1688 $refresh .= ";xact=$_" for @payment_xacts;
1689 $self->ctx->{refresh} = $refresh;
1691 return Apache2::Const::OK;
1694 # retrieve the cached CC payment info and send off for processing
1695 sub load_myopac_pay {
1697 my $token = $self->ctx->{page_args}->[0];
1698 return Apache2::Const::HTTP_BAD_REQUEST unless $token;
1700 my $cache = OpenSRF::Utils::Cache->new('global');
1701 my $cache_args = $cache->get_cache($token);
1702 $cache->delete_cache($token);
1704 # this page is loaded immediately after the token is created.
1705 # if the cached data is not there, it's because of an invalid
1706 # token (or cache failure) and not because of a timeout.
1707 return Apache2::Const::HTTP_BAD_REQUEST unless $cache_args;
1709 my @payment_xacts = @{$cache_args->{xacts}};
1710 my $cc_args = $cache_args->{cc_args};
1712 # as an added security check, verify the user submitting
1713 # the form is the same as the user whose data was cached
1714 return Apache2::Const::HTTP_BAD_REQUEST unless
1715 $cache_args->{user} == $self->ctx->{user}->id;
1717 $logger->info("tpac paying fines with token $token and xacts [@payment_xacts]");
1720 $r = $self->prepare_fines(undef, undef, \@payment_xacts) and return $r;
1722 # balance_owed is computed specifically from the fines we're paying
1723 if ($self->ctx->{fines}->{balance_owed} <= 0) {
1724 $logger->info("tpac can't pay non-positive balance. xacts selected: [@payment_xacts]");
1725 return Apache2::Const::HTTP_BAD_REQUEST;
1729 "cc_args" => $cc_args,
1730 "userid" => $self->ctx->{user}->id,
1731 "payment_type" => "credit_card_payment",
1732 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
1735 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
1736 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
1739 $self->ctx->{"payment_response"} = $resp;
1741 unless ($resp->{"textcode"}) {
1742 $self->ctx->{printable_receipt} = $U->simplereq(
1743 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
1744 $self->editor->authtoken, $resp->{payments}
1748 return Apache2::Const::OK;
1751 sub load_myopac_receipt_print {
1754 $self->ctx->{printable_receipt} = $U->simplereq(
1755 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
1756 $self->editor->authtoken, [$self->cgi->param("payment")]
1759 return Apache2::Const::OK;
1762 sub load_myopac_receipt_email {
1765 # The following ML method doesn't actually check whether the user in
1766 # question has an email address, so we do.
1767 if ($self->ctx->{user}->email) {
1768 $self->ctx->{email_receipt_result} = $U->simplereq(
1769 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
1770 $self->editor->authtoken, [$self->cgi->param("payment")]
1773 $self->ctx->{email_receipt_result} =
1774 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
1777 return Apache2::Const::OK;
1781 my ($self, $limit, $offset, $id_list) = @_;
1783 # XXX TODO: check for failure after various network calls
1785 # It may be unclear, but this result structure lumps circulation and
1786 # reservation fines together, and keeps grocery fines separate.
1787 $self->ctx->{"fines"} = {
1788 "circulation" => [],
1795 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
1797 # TODO: This should really be a ML call, but the existing calls
1798 # return an excessive amount of data and don't offer streaming
1800 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
1802 my $req = $cstore->request(
1803 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
1805 usr => $self->editor->requestor->id,
1806 balance_owed => {'!=' => 0},
1807 ($id_list && @$id_list ? ("id" => $id_list) : ()),
1812 mobts => [qw/grocery circulation reservation/],
1813 bresv => ['target_resource_type'],
1817 circ => ['target_copy'],
1818 acp => ['call_number'],
1821 order_by => { mobts => 'xact_start' },
1826 my @total_keys = qw/total_paid total_owed balance_owed/;
1827 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
1829 while(my $resp = $req->recv) {
1830 my $mobts = $resp->content;
1831 my $circ = $mobts->circulation;
1834 if($mobts->grocery) {
1835 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
1836 $last_billing = pop(@billings);
1839 # XXX TODO confirm that the following, and the later division by 100.0
1840 # to get a floating point representation once again, is sufficiently
1841 # "money-safe" math.
1842 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
1844 my $marc_xml = undef;
1845 if ($mobts->xact_type eq 'reservation' and
1846 $mobts->reservation->target_resource_type->record) {
1847 $marc_xml = XML::LibXML->new->parse_string(
1848 $mobts->reservation->target_resource_type->record->marc
1850 } elsif ($mobts->xact_type eq 'circulation' and
1851 $circ->target_copy->call_number->id != -1) {
1852 $marc_xml = XML::LibXML->new->parse_string(
1853 $circ->target_copy->call_number->record->marc
1858 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
1861 last_grocery_billing => $last_billing,
1862 marc_xml => $marc_xml
1869 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
1873 sub prepare_fines_for_payment {
1874 # This assumes $self->prepare_fines has already been run
1878 if ($self->ctx->{fines}) {
1879 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
1880 @{$self->ctx->{fines}->{circulation}},
1881 @{$self->ctx->{fines}->{grocery}}
1888 sub load_myopac_main {
1890 my $limit = $self->cgi->param('limit') || 0;
1891 my $offset = $self->cgi->param('offset') || 0;
1892 $self->ctx->{search_ou} = $self->_get_search_lib();
1893 $self->ctx->{user}->notes(
1894 $self->editor->search_actor_usr_note({
1895 usr => $self->ctx->{user}->id,
1899 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
1902 sub load_myopac_update_email {
1904 my $e = $self->editor;
1905 my $ctx = $self->ctx;
1906 my $email = $self->cgi->param('email') || '';
1907 my $current_pw = $self->cgi->param('current_pw') || '';
1909 # needed for most up-to-date email address
1910 if (my $r = $self->prepare_extended_user_info) { return $r };
1912 return Apache2::Const::OK
1913 unless $self->cgi->request_method eq 'POST';
1915 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
1916 $ctx->{invalid_email} = $email;
1917 return Apache2::Const::OK;
1920 my $stat = $U->simplereq(
1922 'open-ils.actor.user.email.update',
1923 $e->authtoken, $email, $current_pw);
1925 if($U->event_equals($stat, 'INCORRECT_PASSWORD')) {
1926 $ctx->{password_incorrect} = 1;
1927 return Apache2::Const::OK;
1930 unless ($self->cgi->param("redirect_to")) {
1931 my $url = $self->apache->unparsed_uri;
1932 $url =~ s/update_email/prefs/;
1934 return $self->generic_redirect($url);
1937 return $self->generic_redirect;
1940 sub load_myopac_update_username {
1942 my $e = $self->editor;
1943 my $ctx = $self->ctx;
1944 my $username = $self->cgi->param('username') || '';
1945 my $current_pw = $self->cgi->param('current_pw') || '';
1947 $self->prepare_extended_user_info;
1949 my $allow_change = 1;
1951 my $lock_usernames = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.lock_usernames');
1952 if(defined($lock_usernames) and $lock_usernames == 1) {
1953 # Policy says no username changes
1956 # We want this further down.
1957 $regex_check = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.barcode_regex');
1958 my $username_unlimit = $self->ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.unlimit_usernames');
1959 if(!$username_unlimit) {
1961 # Default is "starts with a number"
1962 $regex_check = '^\d+';
1964 # You already have a username?
1965 if($regex_check and $self->ctx->{user}->usrname !~ /$regex_check/) {
1970 if(!$allow_change) {
1971 my $url = $self->apache->unparsed_uri;
1972 $url =~ s/update_username/prefs/;
1974 return $self->generic_redirect($url);
1977 return Apache2::Const::OK
1978 unless $self->cgi->request_method eq 'POST';
1980 unless($username and $username !~ /\s/) { # any other username restrictions?
1981 $ctx->{invalid_username} = $username;
1982 return Apache2::Const::OK;
1985 # New username can't look like a barcode if we have a barcode regex
1986 if($regex_check and $username =~ /$regex_check/) {
1987 $ctx->{invalid_username} = $username;
1988 return Apache2::Const::OK;
1991 # New username has to look like a username if we have a username regex
1992 $regex_check = $ctx->{get_org_setting}->($e->requestor->home_ou, 'opac.username_regex');
1993 if($regex_check and $username !~ /$regex_check/) {
1994 $ctx->{invalid_username} = $username;
1995 return Apache2::Const::OK;
1998 if($username ne $e->requestor->usrname) {
2000 my $evt = $U->simplereq(
2002 'open-ils.actor.user.username.update',
2003 $e->authtoken, $username, $current_pw);
2005 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
2006 $ctx->{password_incorrect} = 1;
2007 return Apache2::Const::OK;
2010 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
2011 $ctx->{username_exists} = $username;
2012 return Apache2::Const::OK;
2016 my $url = $self->apache->unparsed_uri;
2017 $url =~ s/update_username/prefs/;
2019 return $self->generic_redirect($url);
2022 sub load_myopac_update_password {
2024 my $e = $self->editor;
2025 my $ctx = $self->ctx;
2027 return Apache2::Const::OK
2028 unless $self->cgi->request_method eq 'POST';
2030 my $current_pw = $self->cgi->param('current_pw') || '';
2031 my $new_pw = $self->cgi->param('new_pw') || '';
2032 my $new_pw2 = $self->cgi->param('new_pw2') || '';
2034 unless($new_pw eq $new_pw2) {
2035 $ctx->{password_nomatch} = 1;
2036 return Apache2::Const::OK;
2039 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
2042 # This regex duplicates the JSPac's default "digit, letter, and 7 characters" rule
2043 $pw_regex = '(?=.*\d+.*)(?=.*[A-Za-z]+.*).{7,}';
2046 if($pw_regex and $new_pw !~ /$pw_regex/) {
2047 $ctx->{password_invalid} = 1;
2048 return Apache2::Const::OK;
2051 my $evt = $U->simplereq(
2053 'open-ils.actor.user.password.update',
2054 $e->authtoken, $new_pw, $current_pw);
2057 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
2058 $ctx->{password_incorrect} = 1;
2059 return Apache2::Const::OK;
2062 my $url = $self->apache->unparsed_uri;
2063 $url =~ s/update_password/prefs/;
2065 return $self->generic_redirect($url);
2068 sub _update_bookbag_metadata {
2069 my ($self, $bookbag) = @_;
2071 $bookbag->name($self->cgi->param("name"));
2072 $bookbag->description($self->cgi->param("description"));
2074 return 1 if $self->editor->update_container_biblio_record_entry_bucket($bookbag);
2078 sub _get_lists_per_page {
2081 if($self->editor->requestor) {
2082 $self->timelog("Checking for opac.lists_per_page preference");
2083 # See if the user has a lists per page preference
2084 my $ipp = $self->editor->search_actor_user_setting({
2085 usr => $self->editor->requestor->id,
2086 name => 'opac.lists_per_page'
2088 $self->timelog("Got opac.lists_per_page preference");
2089 return OpenSRF::Utils::JSON->JSON2perl($ipp->value) if $ipp;
2091 return 10; # default
2094 sub _get_items_per_page {
2097 if($self->editor->requestor) {
2098 $self->timelog("Checking for opac.list_items_per_page preference");
2099 # See if the user has a list items per page preference
2100 my $ipp = $self->editor->search_actor_user_setting({
2101 usr => $self->editor->requestor->id,
2102 name => 'opac.list_items_per_page'
2104 $self->timelog("Got opac.list_items_per_page preference");
2105 return OpenSRF::Utils::JSON->JSON2perl($ipp->value) if $ipp;
2107 return 10; # default
2110 sub load_myopac_bookbags {
2112 my $e = $self->editor;
2113 my $ctx = $self->ctx;
2114 my $limit = $self->_get_lists_per_page || 10;
2115 my $offset = $self->cgi->param('offset') || 0;
2117 $ctx->{bookbags_limit} = $limit;
2118 $ctx->{bookbags_offset} = $offset;
2120 # for list item pagination
2121 my $item_limit = $self->_get_items_per_page;
2122 my $item_page = $self->cgi->param('item_page') || 1;
2123 my $item_offset = ($item_page - 1) * $item_limit;
2124 $ctx->{bookbags_item_page} = $item_page;
2126 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("sort");
2127 $e->xact_begin; # replication...
2129 my $rv = $self->load_mylist;
2130 unless($rv eq Apache2::Const::OK) {
2135 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket(
2137 {owner => $e->requestor->id, btype => 'bookbag'}, {
2138 order_by => {cbreb => 'name'},
2146 if(!$ctx->{bookbags}) {
2148 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
2151 # We load the user prefs to get their default bookbag.
2152 $self->_load_user_with_prefs;
2154 # We also want a total count of the user's bookbags.
2156 'select' => { 'cbreb' => [ { 'column' => 'id', 'transform' => 'count', 'aggregate' => 'true', 'alias' => 'count' } ] },
2158 'where' => { 'btype' => 'bookbag', 'owner' => $self->ctx->{user}->id }
2160 my $r = $e->json_query($q);
2161 $ctx->{bookbag_count} = $r->[0]->{'count'};
2163 # If the user wants a specific bookbag's items, load them.
2165 if ($self->cgi->param("bbid")) {
2167 grep { $_->id eq $self->cgi->param("bbid") } @{$ctx->{bookbags}};
2170 my $query = $self->_prepare_bookbag_container_query(
2171 $bookbag->id, $sorter, $modifier
2174 # Calculate total count of the items in selected bookbag.
2175 # This total includes record entries that have no assets available.
2176 my $bb_search_results = $U->simplereq(
2177 "open-ils.search", "open-ils.search.biblio.multiclass.query",
2178 {"limit" => 1, "offset" => 0}, $query
2179 ); # we only need the count, so do the actual search with limit=1
2181 if ($bb_search_results) {
2182 $ctx->{bb_item_count} = $bb_search_results->{count};
2184 $logger->warn("search failed in load_myopac_bookbags()");
2185 $ctx->{bb_item_count} = 0; # fallback value
2188 #calculate page count
2189 $ctx->{bb_page_count} = int ((($ctx->{bb_item_count} - 1) / $item_limit) + 1);
2191 if ( ($self->cgi->param("action") || '') eq "editmeta") {
2192 if (!$self->_update_bookbag_metadata($bookbag)) {
2194 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
2197 my $url = $self->ctx->{opac_root} . '/myopac/lists?bbid=' .
2200 foreach my $param (('loc', 'qtype', 'query', 'sort', 'offset', 'limit')) {
2201 if ($self->cgi->param($param)) {
2202 $url .= ";$param=" . uri_escape_utf8($self->cgi->param($param));
2206 return $self->generic_redirect($url);
2210 # we're done with our CStoreEditor. Rollback here so
2211 # later calls don't cause a timeout, resulting in a
2212 # transaction rollback under the covers.
2216 # For list items pagination
2218 "limit" => $item_limit,
2219 "offset" => $item_offset
2222 my $items = $U->bib_container_items_via_search($bookbag->id, $query, $args)
2223 or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
2225 # capture pref_ou for callnumber filter/display
2226 $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
2228 # search for local callnumbers for display
2229 my $focus_ou = $ctx->{physical_loc} || $ctx->{pref_ou};
2231 my (undef, @recs) = $self->get_records_and_facets(
2232 [ map {$_->target_biblio_record_entry->id} @$items ],
2235 flesh => '{mra,holdings_xml,acp,exclude_invisible_acn}',
2237 site => $ctx->{get_aou}->($focus_ou)->shortname,
2238 pref_lib => $ctx->{pref_ou}
2242 $ctx->{bookbags_marc_xml}{$_->{id}} = $_->{marc_xml} for @recs;
2244 $bookbag->items($items);
2248 # If we have add_rec, we got here from the "Add to new list"
2249 # or "See all" popmenu items.
2250 if (my $add_rec = $self->cgi->param('add_rec')) {
2251 $self->ctx->{add_rec} = $add_rec;
2252 # But not in the staff client, 'cause that breaks things.
2253 unless ($self->ctx->{is_staff}) {
2254 # allow caller to provide the where_from in cases where
2255 # the referer is an intermediate error page
2256 if ($self->cgi->param('where_from')) {
2257 $self->ctx->{where_from} = $self->cgi->param('where_from');
2259 $self->ctx->{where_from} = $self->ctx->{referer};
2260 if ( my $anchor = $self->cgi->param('anchor') ) {
2261 $self->ctx->{where_from} =~ s/#.*|$/#$anchor/;
2267 # this rollback may be a dupe, but that's OK because
2268 # cstoreditor ignores dupe rollbacks
2271 return Apache2::Const::OK;
2275 # actions are create, delete, show, hide, rename, add_rec, delete_item, place_hold
2276 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
2277 sub load_myopac_bookbag_update {
2278 my ($self, $action, $list_id, @hold_recs) = @_;
2279 my $e = $self->editor;
2280 my $cgi = $self->cgi;
2282 # save_notes is effectively another action, but is passed in a separate
2283 # CGI parameter for what are really just layout reasons.
2284 $action = 'save_notes' if $cgi->param('save_notes');
2285 $action ||= $cgi->param('action');
2287 $list_id ||= $cgi->param('list') || $cgi->param('bbid');
2289 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
2290 my @selected_item = $cgi->param('selected_item');
2291 my $shared = $cgi->param('shared');
2292 my $name = $cgi->param('name');
2293 my $description = $cgi->param('description');
2297 # This url intentionally leaves off the edit_notes parameter, but
2298 # may need to add some back in for paging.
2300 my $url = $self->ctx->{proto} . "://" . $self->ctx->{hostname} .
2301 $self->ctx->{opac_root} . "/myopac/lists?";
2303 foreach my $param (('loc', 'qtype', 'query', 'sort')) {
2304 if ($cgi->param($param)) {
2305 $url .= "$param=" . uri_escape_utf8($cgi->param($param)) . ";";
2309 if ($action eq 'create') {
2312 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
2314 $list->description($description);
2315 $list->owner($e->requestor->id);
2316 $list->btype('bookbag');
2317 $list->pub($shared ? 't' : 'f');
2318 $success = $U->simplereq('open-ils.actor',
2319 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list);
2320 if (ref($success) ne 'HASH' && scalar @add_rec) {
2321 $list_id = (ref($success)) ? $success->id : $success;
2322 foreach my $add_rec (@add_rec) {
2323 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
2324 $item->bucket($list_id);
2325 $item->target_biblio_record_entry($add_rec);
2326 $success = $U->simplereq('open-ils.actor',
2327 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
2328 last unless $success;
2331 $url = $cgi->param('where_from') if ($success && $cgi->param('where_from'));
2334 $self->ctx->{bucket_failure_noname} = 1;
2337 } elsif($action eq 'place_hold') {
2339 # @hold_recs comes from anon lists redirect; selected_itesm comes from existing buckets
2340 unless (@hold_recs) {
2341 if (@selected_item) {
2342 my $items = $e->search_container_biblio_record_entry_bucket_item({id => \@selected_item});
2343 @hold_recs = map { $_->target_biblio_record_entry } @$items;
2347 return Apache2::Const::OK unless @hold_recs;
2348 $logger->info("placing holds from list page on: @hold_recs");
2350 my $url = $self->ctx->{opac_root} . '/place_hold?hold_type=T';
2351 $url .= ';hold_target=' . $_ for @hold_recs;
2352 foreach my $param (('loc', 'qtype', 'query')) {
2353 if ($cgi->param($param)) {
2354 $url .= ";$param=" . uri_escape_utf8($cgi->param($param));
2357 return $self->generic_redirect($url);
2361 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
2363 return Apache2::Const::HTTP_BAD_REQUEST unless
2364 $list and $list->owner == $e->requestor->id;
2367 if($action eq 'delete') {
2368 $success = $U->simplereq('open-ils.actor',
2369 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
2371 # We check to see if we're deleting the user's default list.
2372 $self->_load_user_with_prefs;
2373 my $settings_map = $self->ctx->{user_setting_map};
2374 if ($$settings_map{'opac.default_list'} == $list_id) {
2375 # We unset the user's opac.default_list setting.
2376 $success = $U->simplereq(
2378 'open-ils.actor.patron.settings.update',
2381 { 'opac.default_list' => 0 }
2385 } elsif($action eq 'show') {
2386 unless($U->is_true($list->pub)) {
2388 $success = $U->simplereq('open-ils.actor',
2389 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
2392 } elsif($action eq 'hide') {
2393 if($U->is_true($list->pub)) {
2395 $success = $U->simplereq('open-ils.actor',
2396 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
2399 } elsif($action eq 'rename') {
2402 $success = $U->simplereq('open-ils.actor',
2403 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
2406 } elsif($action eq 'add_rec') {
2407 foreach my $add_rec (@add_rec) {
2408 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
2409 $item->bucket($list_id);
2410 $item->target_biblio_record_entry($add_rec);
2411 $success = $U->simplereq('open-ils.actor',
2412 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
2413 last unless $success;
2415 # Redirect back where we came from if we have an anchor parameter:
2416 if ( my $anchor = $cgi->param('anchor') && !$self->ctx->{is_staff}) {
2417 $url = $self->ctx->{referer};
2418 $url =~ s/#.*|$/#$anchor/;
2419 } elsif ($cgi->param('where_from')) {
2420 # Or, if we have a "where_from" parameter.
2421 $url = $cgi->param('where_from');
2423 } elsif ($action eq 'del_item') {
2424 foreach (@selected_item) {
2425 $success = $U->simplereq(
2427 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
2429 last unless $success;
2431 } elsif ($action eq 'save_notes') {
2432 $success = $self->update_bookbag_item_notes;
2433 $url .= "&bbid=" . uri_escape_utf8($cgi->param("bbid")) if $cgi->param("bbid");
2434 } elsif ($action eq 'make_default') {
2435 $success = $U->simplereq(
2437 'open-ils.actor.patron.settings.update',
2440 { 'opac.default_list' => $list_id }
2442 } elsif ($action eq 'remove_default') {
2443 $success = $U->simplereq(
2445 'open-ils.actor.patron.settings.update',
2448 { 'opac.default_list' => 0 }
2452 return $self->generic_redirect($url) if $success;
2454 $self->ctx->{where_from} = $cgi->param('where_from');
2455 $self->ctx->{bucket_action} = $action;
2456 $self->ctx->{bucket_action_failed} = 1;
2457 return Apache2::Const::OK;
2460 sub update_bookbag_item_notes {
2462 my $e = $self->editor;
2464 my @note_keys = grep /^note-\d+/, keys(%{$self->cgi->Vars});
2465 my @item_keys = grep /^item-\d+/, keys(%{$self->cgi->Vars});
2467 # We're going to leverage an API call that's already been written to check
2468 # permissions appropriately.
2470 my $a = create OpenSRF::AppSession("open-ils.actor");
2471 my $method = "open-ils.actor.container.item_note.cud";
2473 for my $note_key (@note_keys) {
2476 my $id = ($note_key =~ /(\d+)/)[0];
2479 $e->retrieve_container_biblio_record_entry_bucket_item_note($id))) {
2480 my $event = $e->die_event;
2481 $self->apache->log->warn(
2482 "error retrieving cbrebin id $id, got event " .
2486 $self->ctx->{bucket_action_event} = $event;
2490 if (length($self->cgi->param($note_key))) {
2491 $note->ischanged(1);
2492 $note->note($self->cgi->param($note_key));
2494 $note->isdeleted(1);
2497 my $r = $a->request($method, $e->authtoken, "biblio", $note)->gather(1);
2499 if (defined $U->event_code($r)) {
2500 $self->apache->log->warn(
2501 "attempt to modify cbrebin " . $note->id .
2502 " returned event " . $r->{textcode}
2506 $self->ctx->{bucket_action_event} = $r;
2511 for my $item_key (@item_keys) {
2512 my $id = int(($item_key =~ /(\d+)/)[0]);
2513 my $text = $self->cgi->param($item_key);
2516 next unless length $text;
2518 my $note = new Fieldmapper::container::biblio_record_entry_bucket_item_note;
2523 my $r = $a->request($method, $e->authtoken, "biblio", $note)->gather(1);
2525 if (defined $U->event_code($r)) {
2526 $self->apache->log->warn(
2527 "attempt to create cbrebin for item " . $note->item .
2528 " returned event " . $r->{textcode}
2532 $self->ctx->{bucket_action_event} = $r;
2541 sub load_myopac_bookbag_print {
2544 my $id = int($self->cgi->param("list"));
2546 my ($sorter, $modifier) = $self->_get_bookbag_sort_params("sort");
2549 $self->_prepare_bookbag_container_query($id, $sorter, $modifier);
2553 # Get the bookbag object itself, assuming we're allowed to.
2554 if ($self->editor->allowed("VIEW_CONTAINER")) {
2556 $bbag = $self->editor->retrieve_container_biblio_record_entry_bucket($id) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
2558 my $bookbags = $self->editor->search_container_biblio_record_entry_bucket(
2562 "owner" => $self->editor->requestor->id,
2566 ) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
2568 $bbag = pop @$bookbags;
2571 # If we have a bookbag we're allowed to look at, issue the A/T event
2572 # to get CSV, passing as a user param that search query we built before.
2574 $self->ctx->{csv} = $U->fire_object_event(
2575 undef, "container.biblio_record_entry_bucket.csv",
2576 $bbag, $self->editor->requestor->home_ou,
2577 undef, {"item_search" => $item_search}
2581 # Create a reasonable filename and set the content disposition to
2582 # provoke browser download dialogs.
2583 (my $filename = $bbag->id . $bbag->name) =~ s/[^a-z0-9_ -]//gi;
2585 return $self->set_file_download_headers("$filename.csv");
2588 sub load_myopac_circ_history_export {
2590 my $e = $self->editor;
2591 my $filename = $self->cgi->param('filename') || 'circ_history.csv';
2593 my $ids = $e->json_query({
2597 transform => 'action.usr_visible_circs',
2598 result_field => 'id'
2602 where => {id => $e->requestor->id}
2605 $self->ctx->{csv} = $U->fire_object_event(
2607 'circ.format.history.csv',
2608 $e->search_action_circulation({id => [map {$_->{id}} @$ids]}, {substream =>1}),
2609 $self->editor->requestor->home_ou
2612 return $self->set_file_download_headers($filename);
2615 sub load_password_reset {
2617 my $cgi = $self->cgi;
2618 my $ctx = $self->ctx;
2619 my $barcode = $cgi->param('barcode');
2620 my $username = $cgi->param('username');
2621 my $email = $cgi->param('email');
2622 my $pwd1 = $cgi->param('pwd1');
2623 my $pwd2 = $cgi->param('pwd2');
2624 my $uuid = $ctx->{page_args}->[0];
2628 $logger->info("patron password reset with uuid $uuid");
2630 if ($pwd1 and $pwd2) {
2632 if ($pwd1 eq $pwd2) {
2634 my $response = $U->simplereq(
2636 'open-ils.actor.patron.password_reset.commit',
2639 $logger->info("patron password reset response " . Dumper($response));
2641 if ($U->event_code($response)) { # non-success event
2643 my $code = $response->{textcode};
2645 if ($code eq 'PATRON_NOT_AN_ACTIVE_PASSWORD_RESET_REQUEST') {
2646 $ctx->{pwreset} = {style => 'error', status => 'NOT_ACTIVE'};
2649 if ($code eq 'PATRON_PASSWORD_WAS_NOT_STRONG') {
2650 $ctx->{pwreset} = {style => 'error', status => 'NOT_STRONG'};
2655 $ctx->{pwreset} = {style => 'success', status => 'SUCCESS'};
2658 } else { # passwords not equal
2660 $ctx->{pwreset} = {style => 'error', status => 'NO_MATCH'};
2663 } else { # 2 password values needed
2665 $ctx->{pwreset} = {status => 'TWO_PASSWORDS'};
2668 } elsif ($barcode or $username) {
2670 my @params = $barcode ? ('barcode', $barcode) : ('username', $username);
2671 push(@params, $email) if $email;
2675 'open-ils.actor.patron.password_reset.request', @params);
2677 $ctx->{pwreset} = {status => 'REQUEST_SUCCESS'};
2680 $logger->info("patron password reset resulted in " . Dumper($ctx->{pwreset}));
2681 return Apache2::Const::OK;