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 my $U = 'OpenILS::Application::AppUtils';
13 sub prepare_extended_user_info {
17 $self->ctx->{user} = $self->editor->retrieve_actor_user([
18 $self->ctx->{user}->id,
22 au => [qw/card home_ou addresses ident_type billing_address/, @extra_flesh]
26 ]) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
32 # user : au object, fleshed
33 sub load_myopac_prefs {
35 return $self->prepare_extended_user_info || Apache2::Const::OK;
38 sub load_myopac_prefs_notify {
40 my $e = $self->editor;
42 my $user_prefs = $self->fetch_optin_prefs;
43 $user_prefs = $self->update_optin_prefs($user_prefs)
44 if $self->cgi->request_method eq 'POST';
46 $self->ctx->{opt_in_settings} = $user_prefs;
48 return Apache2::Const::OK;
51 sub fetch_optin_prefs {
53 my $e = $self->editor;
55 # fetch all of the opt-in settings the user has access to
56 # XXX: user's should in theory have options to opt-in to notices
57 # for remote locations, but that opens the door for a large
58 # set of generally un-used opt-ins.. needs discussion
59 my $opt_ins = $U->simplereq(
61 'open-ils.actor.event_def.opt_in.settings.atomic',
62 $e->authtoken, $e->requestor->home_ou);
64 # fetch user setting values for each of the opt-in settings
65 my $user_set = $U->simplereq(
67 'open-ils.actor.patron.settings.retrieve',
70 [map {$_->name} @$opt_ins]
73 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
76 sub update_optin_prefs {
78 my $user_prefs = shift;
79 my $e = $self->editor;
80 my @settings = $self->cgi->param('setting');
83 # apply now-true settings
84 for my $applied (@settings) {
85 # see if setting is already applied to this user
86 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
87 $newsets{$applied} = OpenSRF::Utils::JSON->true;
90 # remove now-false settings
91 for my $pref (grep { $_->{value} } @$user_prefs) {
92 $newsets{$pref->{cust}->name} = undef
93 unless grep { $_ eq $pref->{cust}->name } @settings;
98 'open-ils.actor.patron.settings.update',
99 $e->authtoken, $e->requestor->id, \%newsets);
101 # update the local prefs to match reality
102 for my $pref (@$user_prefs) {
103 $pref->{value} = $newsets{$pref->{cust}->name}
104 if exists $newsets{$pref->{cust}->name};
110 sub _load_user_with_prefs {
112 my $stat = $self->prepare_extended_user_info('settings');
113 return $stat if $stat; # not-OK
115 $self->ctx->{user_setting_map} = {
116 map { $_->name => OpenSRF::Utils::JSON->JSON2perl($_->value) }
117 @{$self->ctx->{user}->settings}
123 sub load_myopac_prefs_settings {
126 my $stat = $self->_load_user_with_prefs;
127 return $stat if $stat;
129 return Apache2::Const::OK
130 unless $self->cgi->request_method eq 'POST';
132 # some setting values from the form don't match the
133 # required value/format for the db, so they have to be
134 # individually translated.
137 my $set_map = $self->ctx->{user_setting_map};
139 my $key = 'opac.hits_per_page';
140 my $val = $self->cgi->param($key);
141 $settings{$key}= $val unless $$set_map{$key} eq $val;
143 my $now = DateTime->now->strftime('%F');
144 for $key (qw/history.circ.retention_start history.hold.retention_start/) {
145 $val = $self->cgi->param($key);
146 if($val and $val eq 'on') {
147 # Set the start time to 'now' unless a start time already exists for the user
148 $settings{$key} = $now unless $$set_map{$key};
150 # clear the start time if one previously existed for the user
151 $settings{$key} = undef if $$set_map{$key};
155 # Send the modified settings off to be saved
158 'open-ils.actor.patron.settings.update',
159 $self->editor->authtoken, undef, \%settings);
161 # re-fetch user prefs
162 $self->ctx->{updated_user_settings} = \%settings;
163 return $self->_load_user_with_prefs || Apache2::Const::OK;
166 sub fetch_user_holds {
168 my $hold_ids = shift;
169 my $ids_only = shift;
171 my $available = shift;
175 my $e = $self->editor;
177 my $circ = OpenSRF::AppSession->create('open-ils.circ');
181 $hold_ids = $circ->request(
182 'open-ils.circ.holds.id_list.retrieve.authoritative',
187 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
191 return $hold_ids if $ids_only or @$hold_ids == 0;
194 suppress_notices => 1,
195 suppress_transits => 1,
197 suppress_patron_details => 1,
198 include_bre => $flesh ? 1 : 0
201 # ----------------------------------------------------------------
202 # Collect holds in batches of $batch_size for faster retrieval
206 my $mk_req_batch = sub {
208 my $top_idx = $batch_idx + $batch_size;
209 while($batch_idx < $top_idx) {
210 my $hold_id = $hold_ids->[$batch_idx++];
211 last unless $hold_id;
212 my $ses = OpenSRF::AppSession->create('open-ils.circ');
213 my $req = $ses->request(
214 'open-ils.circ.hold.details.retrieve',
215 $e->authtoken, $hold_id, $args);
216 push(@ses, {ses => $ses, req => $req});
222 my(@collected, @holds, @ses);
225 @ses = $mk_req_batch->() if $first;
226 last if $first and not @ses;
229 # If desired by the caller, filter any holds that are not available.
231 @collected = grep { $_->{hold}->{status} == 4 } @collected;
233 while(my $blob = pop(@collected)) {
234 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
239 for my $req_data (@ses) {
240 push(@collected, {hold => $req_data->{req}->gather(1)});
241 $req_data->{ses}->kill_me;
244 @ses = $mk_req_batch->();
245 last unless @collected or @ses;
249 # put the holds back into the original server sort order
251 for my $id (@$hold_ids) {
252 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
258 sub handle_hold_update {
261 my $e = $self->editor;
264 my @hold_ids = $self->cgi->param('hold_id'); # for non-_all actions
265 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
267 my $circ = OpenSRF::AppSession->create('open-ils.circ');
269 if($action =~ /cancel/) {
271 for my $hold_id (@hold_ids) {
272 my $resp = $circ->request(
273 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
276 } elsif ($action =~ /activate|suspend/) {
279 for my $hold_id (@hold_ids) {
280 my $vals = {id => $hold_id};
282 if($action =~ /activate/) {
283 $vals->{frozen} = 'f';
284 $vals->{thaw_date} = undef;
286 } elsif($action =~ /suspend/) {
287 $vals->{frozen} = 't';
288 # $vals->{thaw_date} = TODO;
290 push(@$vlist, $vals);
293 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
294 } elsif ($action eq 'edit') {
297 my $val = {"id" => $_};
298 $val->{"frozen"} = $self->cgi->param("frozen");
299 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
301 for my $field (qw/expire_time thaw_date/) {
302 # XXX TODO make this support other date formats, not just
304 next unless $self->cgi->param($field) =~
305 m:^(\d{2})/(\d{2})/(\d{4})$:;
306 $val->{$field} = "$3-$1-$2";
312 'open-ils.circ.hold.update.batch.atomic',
313 $e->authtoken, undef, \@vals
314 )->gather(1); # LFW XXX test for failure
315 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
319 return defined($url) ? $self->generic_redirect($url) : undef;
322 sub load_myopac_holds {
324 my $e = $self->editor;
325 my $ctx = $self->ctx;
328 my $limit = $self->cgi->param('limit') || 0;
329 my $offset = $self->cgi->param('offset') || 0;
330 my $action = $self->cgi->param('action') || '';
331 my $available = int($self->cgi->param('available') || 0);
333 my $hold_handle_result;
334 $hold_handle_result = $self->handle_hold_update($action) if $action;
336 $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
338 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
341 sub load_place_hold {
343 my $ctx = $self->ctx;
344 my $e = $self->editor;
345 my $cgi = $self->cgi;
346 $self->ctx->{page} = 'place_hold';
348 $ctx->{hold_target} = $cgi->param('hold_target');
349 $ctx->{hold_type} = $cgi->param('hold_type');
350 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
352 # XXX check for failure of the retrieve_* methods called below, and
353 # possibly replace all the if,elsif with a dispatch table (meh, elegance)
356 if ($ctx->{hold_type} eq 'T') {
357 $target_field = "titleid";
358 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
359 } elsif ($ctx->{hold_type} eq 'V') {
360 $target_field = "volume_id";
361 my $vol = $e->retrieve_asset_call_number([
362 $ctx->{hold_target}, {
364 "flesh_fields" => {"acn" => ["record"]}
367 $ctx->{record} = $vol->record;
368 } elsif ($ctx->{hold_type} eq 'C') {
369 $target_field = "copy_id";
370 my $copy = $e->retrieve_asset_copy([
371 $ctx->{hold_target}, {
375 "acp" => ["call_number"]
379 $ctx->{record} = $copy->call_number->record;
380 } elsif ($ctx->{hold_type} eq 'I') {
381 $target_field = "issuanceid";
382 my $iss = $e->retrieve_serial_issuance([
383 $ctx->{hold_target}, {
386 "siss" => ["subscription"], "ssub" => ["record_entry"]
390 $ctx->{record} = $iss->subscription->record_entry;
394 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
396 if (my $pickup_lib = $cgi->param('pickup_lib')) {
397 my $requestor = $e->requestor->id;
400 if ((not $ctx->{"is_staff"}) or
401 ($cgi->param("hold_usr_is_requestor"))) {
404 my $actor = create OpenSRF::AppSession("open-ils.actor");
405 $usr = $actor->request(
406 "open-ils.actor.user.retrieve_id_by_barcode_or_username",
407 $e->authtoken, $cgi->param("hold_usr")
410 if (defined $U->event_code($usr)) {
411 $ctx->{hold_failed} = 1;
412 $ctx->{hold_failed_event} = $usr;
418 $target_field => $ctx->{"hold_target"},
419 pickup_lib => $pickup_lib,
420 hold_type => $ctx->{"hold_type"},
424 my $allowed = $U->simplereq(
426 'open-ils.circ.title_hold.is_possible',
430 if($allowed->{success} == 1) {
431 my $hold = Fieldmapper::action::hold_request->new;
433 $hold->pickup_lib($pickup_lib);
434 $hold->requestor($requestor);
436 $hold->target($ctx->{hold_target});
437 $hold->hold_type($ctx->{hold_type});
438 # frozen, expired, etc..
440 my $stat = $U->simplereq(
442 'open-ils.circ.holds.create',
446 if($stat and $stat > 0) {
447 # if successful, return the user to the requesting page
448 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
449 return $self->generic_redirect;
452 $ctx->{hold_failed} = 1;
454 } else { # hold *check* failed
455 $ctx->{hold_failed} = 1; # XXX process the events, etc
456 $ctx->{hold_failed_event} = $allowed->{last_event};
460 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
463 return Apache2::Const::OK;
467 sub fetch_user_circs {
469 my $flesh = shift; # flesh bib data, etc.
470 my $circ_ids = shift;
474 my $e = $self->editor;
479 @circ_ids = @$circ_ids;
483 my $circ_data = $U->simplereq(
485 'open-ils.actor.user.checked_out',
490 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
492 if($limit or $offset) {
493 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
497 return [] unless @circ_ids;
499 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
504 circ => ['target_copy'],
505 acp => ['call_number'],
511 my $circs = $e->search_action_circulation(
512 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
515 for my $circ (@$circs) {
518 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
519 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
520 undef # pre-cat copy, use the dummy title/author instead
525 # make sure the final list is in the correct order
527 for my $id (@circ_ids) {
530 (grep { $_->{circ}->id == $id } @circs)
534 return \@sorted_circs;
538 sub handle_circ_renew {
541 my $ctx = $self->ctx;
543 my @renew_ids = $self->cgi->param('circ');
545 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
547 # TODO: fire off renewal calls in batches to speed things up
549 for my $circ (@$circs) {
551 my $evt = $U->simplereq(
553 'open-ils.circ.renew',
554 $self->editor->authtoken,
556 patron_id => $self->editor->requestor->id,
557 copy_id => $circ->{circ}->target_copy,
562 # TODO return these, then insert them into the circ data
563 # blob that is shoved into the template for each circ
564 # so the template won't have to match them
565 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
572 sub load_myopac_circs {
574 my $e = $self->editor;
575 my $ctx = $self->ctx;
578 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
579 my $offset = $self->cgi->param('offset') || 0;
580 my $action = $self->cgi->param('action') || '';
582 # perform the renewal first if necessary
583 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
585 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
587 my $success_renewals = 0;
588 my $failed_renewals = 0;
589 for my $data (@{$ctx->{circs}}) {
590 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
593 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
594 $data->{renewal_response} = $evt;
595 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
596 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
600 $ctx->{success_renewals} = $success_renewals;
601 $ctx->{failed_renewals} = $failed_renewals;
603 return Apache2::Const::OK;
606 sub load_myopac_circ_history {
608 my $e = $self->editor;
609 my $ctx = $self->ctx;
610 my $limit = $self->cgi->param('limit') || 15;
611 my $offset = $self->cgi->param('offset') || 0;
613 $ctx->{circ_history_limit} = $limit;
614 $ctx->{circ_history_offset} = $offset;
616 my $circs = $e->json_query({
617 from => ['action.usr_visible_circs', $e->requestor->id],
618 #limit => $limit || 25,
619 #offset => $offset || 0,
622 # XXX: order-by in the json_query above appears to do nothing, so in-query
623 # paging is not reallly an option. do the sorting/paging here
625 # sort newest to oldest
626 $circs = [ sort { $b->{xact_start} cmp $a->{xact_start} } @$circs ];
627 my @ids = map { $_->{id} } @$circs;
629 # find the selected page and trim cruft
630 @ids = @ids[$offset..($offset + $limit - 1)] if $limit;
631 @ids = grep { defined $_ } @ids;
633 $ctx->{circs} = $self->fetch_user_circs(1, \@ids);
634 #$ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circs], $limit, $offset);
636 return Apache2::Const::OK;
639 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
640 sub load_myopac_hold_history {
642 my $e = $self->editor;
643 my $ctx = $self->ctx;
644 my $limit = $self->cgi->param('limit') || 15;
645 my $offset = $self->cgi->param('offset') || 0;
646 $ctx->{hold_history_limit} = $limit;
647 $ctx->{hold_history_offset} = $offset;
650 my $holds = $e->json_query({
651 from => ['action.usr_visible_holds', $e->requestor->id],
652 limit => $limit || 25,
653 offset => $offset || 0
656 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$holds], 0, 1, 0, $limit, $offset);
658 return Apache2::Const::OK;
661 sub load_myopac_payment_form {
665 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact')]) and return $r;
666 $r = $self->prepare_extended_user_info and return $r;
668 return Apache2::Const::OK;
671 # TODO: add other filter options as params/configs/etc.
672 sub load_myopac_payments {
674 my $limit = $self->cgi->param('limit') || 20;
675 my $offset = $self->cgi->param('offset') || 0;
676 my $e = $self->editor;
678 $self->ctx->{payment_history_limit} = $limit;
679 $self->ctx->{payment_history_offset} = $offset;
682 $args->{limit} = $limit if $limit;
683 $args->{offset} = $offset if $offset;
685 $self->ctx->{payments} = $U->simplereq(
687 'open-ils.actor.user.payments.retrieve.atomic',
688 $e->authtoken, $e->requestor->id, $args);
690 return Apache2::Const::OK;
693 sub load_myopac_pay {
697 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact')]) and
700 # balance_owed is computed specifically from the fines we're trying
701 # to pay in this case.
702 if ($self->ctx->{fines}->{balance_owed} <= 0) {
703 $self->apache->log->info(
704 sprintf("Can't pay non-positive balance. xacts selected: (%s)",
705 join(", ", map(int, $self->cgi->param("xact"))))
707 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
710 my $cc_args = {"where_process" => 1};
712 $cc_args->{$_} = $self->cgi->param($_) for (qw/
713 number cvv2 expire_year expire_month billing_first
714 billing_last billing_address billing_city billing_state
719 "cc_args" => $cc_args,
720 "userid" => $self->ctx->{user}->id,
721 "payment_type" => "credit_card_payment",
722 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
725 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
726 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
729 $self->ctx->{"payment_response"} = $resp;
731 unless ($resp->{"textcode"}) {
732 $self->ctx->{printable_receipt} = $U->simplereq(
733 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
734 $self->editor->authtoken, $resp->{payments}
738 return Apache2::Const::OK;
741 sub load_myopac_receipt_print {
744 $self->ctx->{printable_receipt} = $U->simplereq(
745 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
746 $self->editor->authtoken, [$self->cgi->param("payment")]
749 return Apache2::Const::OK;
752 sub load_myopac_receipt_email {
755 # The following ML method doesn't actually check whether the user in
756 # question has an email address, so we do.
757 if ($self->ctx->{user}->email) {
758 $self->ctx->{email_receipt_result} = $U->simplereq(
759 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
760 $self->editor->authtoken, [$self->cgi->param("payment")]
763 $self->ctx->{email_receipt_result} =
764 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
767 return Apache2::Const::OK;
771 my ($self, $limit, $offset, $id_list) = @_;
773 # XXX TODO: check for failure after various network calls
775 # It may be unclear, but this result structure lumps circulation and
776 # reservation fines together, and keeps grocery fines separate.
777 $self->ctx->{"fines"} = {
785 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
787 # TODO: This should really be a ML call, but the existing calls
788 # return an excessive amount of data and don't offer streaming
790 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
792 my $req = $cstore->request(
793 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
795 usr => $self->editor->requestor->id,
796 balance_owed => {'!=' => 0},
797 ($id_list && @$id_list ? ("id" => $id_list) : ()),
802 mobts => [qw/grocery circulation reservation/],
803 bresv => ['target_resource_type'],
807 circ => ['target_copy'],
808 acp => ['call_number'],
811 order_by => { mobts => 'xact_start' },
816 my @total_keys = qw/total_paid total_owed balance_owed/;
817 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
819 while(my $resp = $req->recv) {
820 my $mobts = $resp->content;
821 my $circ = $mobts->circulation;
824 if($mobts->grocery) {
825 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
826 $last_billing = pop(@billings);
829 # XXX TODO confirm that the following, and the later division by 100.0
830 # to get a floating point representation once again, is sufficiently
832 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
834 my $marc_xml = undef;
835 if ($mobts->xact_type eq 'reservation' and
836 $mobts->reservation->target_resource_type->record) {
837 $marc_xml = XML::LibXML->new->parse_string(
838 $mobts->reservation->target_resource_type->record->marc
840 } elsif ($mobts->xact_type eq 'circulation' and
841 $circ->target_copy->call_number->id != -1) {
842 $marc_xml = XML::LibXML->new->parse_string(
843 $circ->target_copy->call_number->record->marc
848 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
851 last_grocery_billing => $last_billing,
852 marc_xml => $marc_xml
857 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
861 sub prepare_fines_for_payment {
862 # This assumes $self->prepare_fines has already been run
866 if ($self->ctx->{fines}) {
867 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
868 @{$self->ctx->{fines}->{circulation}},
869 @{$self->ctx->{fines}->{grocery}}
876 sub load_myopac_main {
878 my $limit = $self->cgi->param('limit') || 0;
879 my $offset = $self->cgi->param('offset') || 0;
881 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
884 sub load_myopac_update_email {
886 my $e = $self->editor;
887 my $ctx = $self->ctx;
888 my $email = $self->cgi->param('email') || '';
890 return Apache2::Const::OK
891 unless $self->cgi->request_method eq 'POST';
893 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
894 $ctx->{invalid_email} = $email;
895 return Apache2::Const::OK;
898 my $stat = $U->simplereq(
900 'open-ils.actor.user.email.update',
901 $e->authtoken, $email);
903 my $url = $self->apache->unparsed_uri;
904 $url =~ s/update_email/prefs/;
906 return $self->generic_redirect($url);
909 sub load_myopac_update_username {
911 my $e = $self->editor;
912 my $ctx = $self->ctx;
913 my $username = $self->cgi->param('username') || '';
915 return Apache2::Const::OK
916 unless $self->cgi->request_method eq 'POST';
918 unless($username and $username !~ /\s/) { # any other username restrictions?
919 $ctx->{invalid_username} = $username;
920 return Apache2::Const::OK;
923 if($username ne $e->requestor->usrname) {
925 my $evt = $U->simplereq(
927 'open-ils.actor.user.username.update',
928 $e->authtoken, $username);
930 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
931 $ctx->{username_exists} = $username;
932 return Apache2::Const::OK;
936 my $url = $self->apache->unparsed_uri;
937 $url =~ s/update_username/prefs/;
939 return $self->generic_redirect($url);
942 sub load_myopac_update_password {
944 my $e = $self->editor;
945 my $ctx = $self->ctx;
947 return Apache2::Const::OK
948 unless $self->cgi->request_method eq 'POST';
950 my $current_pw = $self->cgi->param('current_pw') || '';
951 my $new_pw = $self->cgi->param('new_pw') || '';
952 my $new_pw2 = $self->cgi->param('new_pw2') || '';
954 unless($new_pw eq $new_pw2) {
955 $ctx->{password_nomatch} = 1;
956 return Apache2::Const::OK;
959 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
961 if($pw_regex and $new_pw !~ /$pw_regex/) {
962 $ctx->{password_invalid} = 1;
963 return Apache2::Const::OK;
966 my $evt = $U->simplereq(
968 'open-ils.actor.user.password.update',
969 $e->authtoken, $new_pw, $current_pw);
972 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
973 $ctx->{password_incorrect} = 1;
974 return Apache2::Const::OK;
977 my $url = $self->apache->unparsed_uri;
978 $url =~ s/update_password/prefs/;
980 return $self->generic_redirect($url);
983 sub load_myopac_bookbags {
985 my $e = $self->editor;
986 my $ctx = $self->ctx;
988 $e->xact_begin; # replication...
990 my $rv = $self->load_mylist;
991 unless($rv eq Apache2::Const::OK) {
997 order_by => {cbreb => 'name'},
998 limit => $self->cgi->param('limit') || 10,
999 offset => $self->cgi->param('offset') || 0
1002 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
1003 {owner => $self->editor->requestor->id, btype => 'bookbag'},
1004 # XXX what to do about the possibility of really large bookbags here?
1005 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
1008 if(!$ctx->{bookbags}) {
1010 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
1013 # get unique record IDs
1015 foreach my $bbag (@{$ctx->{bookbags}}) {
1016 foreach my $rec_id (
1017 map { $_->target_biblio_record_entry } @{$bbag->items}
1019 $rec_ids{$rec_id} = 1;
1023 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
1026 return Apache2::Const::OK;
1030 # actions are create, delete, show, hide, rename, add_rec, delete_item
1031 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
1032 sub load_myopac_bookbag_update {
1033 my ($self, $action, $list_id) = @_;
1034 my $e = $self->editor;
1035 my $cgi = $self->cgi;
1037 $action ||= $cgi->param('action');
1038 $list_id ||= $cgi->param('list');
1040 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
1041 my @del_item = $cgi->param('del_item');
1042 my $shared = $cgi->param('shared');
1043 my $name = $cgi->param('name');
1047 if($action eq 'create') {
1048 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
1050 $list->owner($e->requestor->id);
1051 $list->btype('bookbag');
1052 $list->pub($shared ? 't' : 'f');
1053 $success = $U->simplereq('open-ils.actor',
1054 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
1058 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
1060 return Apache2::Const::HTTP_BAD_REQUEST unless
1061 $list and $list->owner == $e->requestor->id;
1064 if($action eq 'delete') {
1065 $success = $U->simplereq('open-ils.actor',
1066 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
1068 } elsif($action eq 'show') {
1069 unless($U->is_true($list->pub)) {
1071 $success = $U->simplereq('open-ils.actor',
1072 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1075 } elsif($action eq 'hide') {
1076 if($U->is_true($list->pub)) {
1078 $success = $U->simplereq('open-ils.actor',
1079 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1082 } elsif($action eq 'rename') {
1085 $success = $U->simplereq('open-ils.actor',
1086 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
1089 } elsif($action eq 'add_rec') {
1090 foreach my $add_rec (@add_rec) {
1091 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
1092 $item->bucket($list_id);
1093 $item->target_biblio_record_entry($add_rec);
1094 $success = $U->simplereq('open-ils.actor',
1095 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
1096 last unless $success;
1099 } elsif($action eq 'del_item') {
1100 foreach (@del_item) {
1101 $success = $U->simplereq(
1103 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
1105 last unless $success;
1109 return $self->generic_redirect if $success;
1111 $self->ctx->{bucket_action} = $action;
1112 $self->ctx->{bucket_action_failed} = 1;
1113 return Apache2::Const::OK;