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 my $U = 'OpenILS::Application::AppUtils';
12 sub prepare_extended_user_info {
15 $self->ctx->{user} = $self->editor->retrieve_actor_user([
16 $self->ctx->{user}->id,
20 au => [qw/card home_ou addresses ident_type billing_address/]
24 ]) or return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
30 # user : au object, fleshed
31 sub load_myopac_prefs {
33 return $self->prepare_extended_user_info || Apache2::Const::OK;
36 sub load_myopac_prefs_notify {
38 my $e = $self->editor;
40 my $user_prefs = $self->fetch_optin_prefs;
41 $user_prefs = $self->update_optin_prefs($user_prefs)
42 if $self->cgi->request_method eq 'POST';
44 $self->ctx->{opt_in_settings} = $user_prefs;
46 return Apache2::Const::OK;
49 sub fetch_optin_prefs {
51 my $e = $self->editor;
53 # fetch all of the opt-in settings the user has access to
54 # XXX: user's should in theory have options to opt-in to notices
55 # for remote locations, but that opens the door for a large
56 # set of generally un-used opt-ins.. needs discussion
57 my $opt_ins = $U->simplereq(
59 'open-ils.actor.event_def.opt_in.settings.atomic',
60 $e->authtoken, $e->requestor->home_ou);
62 # fetch user setting values for each of the opt-in settings
63 my $user_set = $U->simplereq(
65 'open-ils.actor.patron.settings.retrieve',
68 [map {$_->name} @$opt_ins]
71 return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
74 sub update_optin_prefs {
76 my $user_prefs = shift;
77 my $e = $self->editor;
78 my @settings = $self->cgi->param('setting');
81 # apply now-true settings
82 for my $applied (@settings) {
83 # see if setting is already applied to this user
84 next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
85 $newsets{$applied} = OpenSRF::Utils::JSON->true;
88 # remove now-false settings
89 for my $pref (grep { $_->{value} } @$user_prefs) {
90 $newsets{$pref->{cust}->name} = undef
91 unless grep { $_ eq $pref->{cust}->name } @settings;
96 'open-ils.actor.patron.settings.update',
97 $e->authtoken, $e->requestor->id, \%newsets);
99 # update the local prefs to match reality
100 for my $pref (@$user_prefs) {
101 $pref->{value} = $newsets{$pref->{cust}->name}
102 if exists $newsets{$pref->{cust}->name};
108 sub load_myopac_prefs_settings {
110 return $self->prepare_extended_user_info || Apache2::Const::OK;
113 sub fetch_user_holds {
115 my $hold_ids = shift;
116 my $ids_only = shift;
118 my $available = shift;
122 my $e = $self->editor;
124 my $circ = OpenSRF::AppSession->create('open-ils.circ');
128 $hold_ids = $circ->request(
129 'open-ils.circ.holds.id_list.retrieve.authoritative',
134 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
138 return $hold_ids if $ids_only or @$hold_ids == 0;
141 suppress_notices => 1,
142 suppress_transits => 1,
144 suppress_patron_details => 1,
145 include_bre => $flesh ? 1 : 0
148 # ----------------------------------------------------------------
149 # Collect holds in batches of $batch_size for faster retrieval
153 my $mk_req_batch = sub {
155 my $top_idx = $batch_idx + $batch_size;
156 while($batch_idx < $top_idx) {
157 my $hold_id = $hold_ids->[$batch_idx++];
158 last unless $hold_id;
159 my $ses = OpenSRF::AppSession->create('open-ils.circ');
160 my $req = $ses->request(
161 'open-ils.circ.hold.details.retrieve',
162 $e->authtoken, $hold_id, $args);
163 push(@ses, {ses => $ses, req => $req});
169 my(@collected, @holds, @ses);
172 @ses = $mk_req_batch->() if $first;
173 last if $first and not @ses;
176 # If desired by the caller, filter any holds that are not available.
178 @collected = grep { $_->{hold}->{status} == 4 } @collected;
180 while(my $blob = pop(@collected)) {
181 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
186 for my $req_data (@ses) {
187 push(@collected, {hold => $req_data->{req}->gather(1)});
188 $req_data->{ses}->kill_me;
191 @ses = $mk_req_batch->();
192 last unless @collected or @ses;
196 # put the holds back into the original server sort order
198 for my $id (@$hold_ids) {
199 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
205 sub handle_hold_update {
208 my $e = $self->editor;
211 my @hold_ids = $self->cgi->param('hold_id'); # for non-_all actions
212 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
214 my $circ = OpenSRF::AppSession->create('open-ils.circ');
216 if($action =~ /cancel/) {
218 for my $hold_id (@hold_ids) {
219 my $resp = $circ->request(
220 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
223 } elsif ($action =~ /activate|suspend/) {
226 for my $hold_id (@hold_ids) {
227 my $vals = {id => $hold_id};
229 if($action =~ /activate/) {
230 $vals->{frozen} = 'f';
231 $vals->{thaw_date} = undef;
233 } elsif($action =~ /suspend/) {
234 $vals->{frozen} = 't';
235 # $vals->{thaw_date} = TODO;
237 push(@$vlist, $vals);
240 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
241 } elsif ($action eq 'edit') {
244 my $val = {"id" => $_};
245 $val->{"frozen"} = $self->cgi->param("frozen");
246 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
248 for my $field (qw/expire_time thaw_date/) {
249 # XXX TODO make this support other date formats, not just
251 next unless $self->cgi->param($field) =~
252 m:^(\d{2})/(\d{2})/(\d{4})$:;
253 $val->{$field} = "$3-$1-$2";
259 'open-ils.circ.hold.update.batch.atomic',
260 $e->authtoken, undef, \@vals
261 )->gather(1); # LFW XXX test for failure
262 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
266 return defined($url) ? $self->generic_redirect($url) : undef;
269 sub load_myopac_holds {
271 my $e = $self->editor;
272 my $ctx = $self->ctx;
275 my $limit = $self->cgi->param('limit') || 0;
276 my $offset = $self->cgi->param('offset') || 0;
277 my $action = $self->cgi->param('action') || '';
278 my $available = int($self->cgi->param('available') || 0);
280 my $hold_handle_result;
281 $hold_handle_result = $self->handle_hold_update($action) if $action;
283 $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
285 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
288 sub load_place_hold {
290 my $ctx = $self->ctx;
291 my $e = $self->editor;
292 my $cgi = $self->cgi;
293 $self->ctx->{page} = 'place_hold';
295 $ctx->{hold_target} = $cgi->param('hold_target');
296 $ctx->{hold_type} = $cgi->param('hold_type');
297 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
299 if ($ctx->{hold_type} eq 'T') {
300 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
301 } elsif ($ctx->{hold_type} eq 'I') {
302 my $iss = $e->retrieve_serial_issuance([
303 $ctx->{hold_target}, {
306 "siss" => ["subscription"], "ssub" => ["record_entry"]
310 $ctx->{record} = $iss->subscription->record_entry;
314 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
316 if(my $pickup_lib = $cgi->param('pickup_lib')) {
319 patronid => $e->requestor->id,
320 titleid => $ctx->{hold_target}, # XXX
321 pickup_lib => $pickup_lib,
325 my $allowed = $U->simplereq(
327 'open-ils.circ.title_hold.is_possible',
331 if($allowed->{success} == 1) {
332 my $hold = Fieldmapper::action::hold_request->new;
334 $hold->pickup_lib($pickup_lib);
335 $hold->requestor($e->requestor->id);
336 $hold->usr($e->requestor->id); # XXX staff
337 $hold->target($ctx->{hold_target});
338 $hold->hold_type($ctx->{hold_type});
339 # frozen, expired, etc..
341 my $stat = $U->simplereq(
343 'open-ils.circ.holds.create',
347 if($stat and $stat > 0) {
348 # if successful, return the user to the requesting page
349 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
350 return $self->generic_redirect;
353 $ctx->{hold_failed} = 1;
355 } else { # hold *check* failed
356 $ctx->{hold_failed} = 1; # XXX process the events, etc
357 $ctx->{hold_failed_event} = $allowed->{last_event};
361 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
364 return Apache2::Const::OK;
368 sub fetch_user_circs {
370 my $flesh = shift; # flesh bib data, etc.
371 my $circ_ids = shift;
375 my $e = $self->editor;
380 @circ_ids = @$circ_ids;
384 my $circ_data = $U->simplereq(
386 'open-ils.actor.user.checked_out',
391 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
393 if($limit or $offset) {
394 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
398 return [] unless @circ_ids;
400 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
405 circ => ['target_copy'],
406 acp => ['call_number'],
412 my $circs = $e->search_action_circulation(
413 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
416 for my $circ (@$circs) {
419 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
420 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
421 undef # pre-cat copy, use the dummy title/author instead
426 # make sure the final list is in the correct order
428 for my $id (@circ_ids) {
431 (grep { $_->{circ}->id == $id } @circs)
435 return \@sorted_circs;
439 sub handle_circ_renew {
442 my $ctx = $self->ctx;
444 my @renew_ids = $self->cgi->param('circ');
446 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
448 # TODO: fire off renewal calls in batches to speed things up
450 for my $circ (@$circs) {
452 my $evt = $U->simplereq(
454 'open-ils.circ.renew',
455 $self->editor->authtoken,
457 patron_id => $self->editor->requestor->id,
458 copy_id => $circ->{circ}->target_copy,
463 # TODO return these, then insert them into the circ data
464 # blob that is shoved into the template for each circ
465 # so the template won't have to match them
466 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
473 sub load_myopac_circs {
475 my $e = $self->editor;
476 my $ctx = $self->ctx;
479 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
480 my $offset = $self->cgi->param('offset') || 0;
481 my $action = $self->cgi->param('action') || '';
483 # perform the renewal first if necessary
484 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
486 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
488 my $success_renewals = 0;
489 my $failed_renewals = 0;
490 for my $data (@{$ctx->{circs}}) {
491 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
494 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
495 $data->{renewal_response} = $evt;
496 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
497 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
501 $ctx->{success_renewals} = $success_renewals;
502 $ctx->{failed_renewals} = $failed_renewals;
504 return Apache2::Const::OK;
507 sub load_myopac_circ_history {
509 my $e = $self->editor;
510 my $ctx = $self->ctx;
511 my $limit = $self->cgi->param('limit') || 15;
512 my $offset = $self->cgi->param('offset') || 0;
514 $ctx->{circ_history_limit} = $limit;
515 $ctx->{circ_history_offset} = $offset;
517 my $circs = $e->json_query({
518 from => ['action.usr_visible_circs', $e->requestor->id],
519 #limit => $limit || 25,
520 #offset => $offset || 0,
523 # XXX: order-by in the json_query above appears to do nothing, so in-query
524 # paging is not reallly an option. do the sorting/paging here
526 # sort newest to oldest
527 $circs = [ sort { $b->{xact_start} cmp $a->{xact_start} } @$circs ];
528 my @ids = map { $_->{id} } @$circs;
530 # find the selected page and trim cruft
531 @ids = @ids[$offset..($offset + $limit - 1)] if $limit;
532 @ids = grep { defined $_ } @ids;
534 $ctx->{circs} = $self->fetch_user_circs(1, \@ids);
535 #$ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circs], $limit, $offset);
537 return Apache2::Const::OK;
540 # TODO: action.usr_visible_holds does not return cancelled holds. Should it?
541 sub load_myopac_hold_history {
543 my $e = $self->editor;
544 my $ctx = $self->ctx;
545 my $limit = $self->cgi->param('limit') || 15;
546 my $offset = $self->cgi->param('offset') || 0;
547 $ctx->{hold_history_limit} = $limit;
548 $ctx->{hold_history_offset} = $offset;
551 my $holds = $e->json_query({
552 from => ['action.usr_visible_holds', $e->requestor->id],
553 limit => $limit || 25,
554 offset => $offset || 0
557 $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$holds], 0, 1, 0, $limit, $offset);
559 return Apache2::Const::OK;
562 sub load_myopac_payment_form {
566 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact')]) and return $r;
567 $r = $self->prepare_extended_user_info and return $r;
569 return Apache2::Const::OK;
572 # TODO: add other filter options as params/configs/etc.
573 sub load_myopac_payments {
575 my $limit = $self->cgi->param('limit') || 20;
576 my $offset = $self->cgi->param('offset') || 0;
577 my $e = $self->editor;
579 $self->ctx->{payment_history_limit} = $limit;
580 $self->ctx->{payment_history_offset} = $offset;
583 $args->{limit} = $limit if $limit;
584 $args->{offset} = $offset if $offset;
586 $self->ctx->{payments} = $U->simplereq(
588 'open-ils.actor.user.payments.retrieve.atomic',
589 $e->authtoken, $e->requestor->id, $args);
591 return Apache2::Const::OK;
594 sub load_myopac_pay {
598 $r = $self->prepare_fines(undef, undef, [$self->cgi->param('xact')]) and
601 # balance_owed is computed specifically from the fines we're trying
602 # to pay in this case.
603 if ($self->ctx->{fines}->{balance_owed} <= 0) {
604 $self->apache->log->info(
605 sprintf("Can't pay non-positive balance. xacts selected: (%s)",
606 join(", ", map(int, $self->cgi->param("xact"))))
608 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
611 my $cc_args = {"where_process" => 1};
613 $cc_args->{$_} = $self->cgi->param($_) for (qw/
614 number cvv2 expire_year expire_month billing_first
615 billing_last billing_address billing_city billing_state
620 "cc_args" => $cc_args,
621 "userid" => $self->ctx->{user}->id,
622 "payment_type" => "credit_card_payment",
623 "payments" => $self->prepare_fines_for_payment # should be safe after self->prepare_fines
626 my $resp = $U->simplereq("open-ils.circ", "open-ils.circ.money.payment",
627 $self->editor->authtoken, $args, $self->ctx->{user}->last_xact_id
630 $self->ctx->{"payment_response"} = $resp;
632 unless ($resp->{"textcode"}) {
633 $self->ctx->{printable_receipt} = $U->simplereq(
634 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
635 $self->editor->authtoken, $resp->{payments}
639 return Apache2::Const::OK;
642 sub load_myopac_receipt_print {
645 $self->ctx->{printable_receipt} = $U->simplereq(
646 "open-ils.circ", "open-ils.circ.money.payment_receipt.print",
647 $self->editor->authtoken, [$self->cgi->param("payment")]
650 return Apache2::Const::OK;
653 sub load_myopac_receipt_email {
656 # The following ML method doesn't actually check whether the user in
657 # question has an email address, so we do.
658 if ($self->ctx->{user}->email) {
659 $self->ctx->{email_receipt_result} = $U->simplereq(
660 "open-ils.circ", "open-ils.circ.money.payment_receipt.email",
661 $self->editor->authtoken, [$self->cgi->param("payment")]
664 $self->ctx->{email_receipt_result} =
665 new OpenILS::Event("PATRON_NO_EMAIL_ADDRESS");
668 return Apache2::Const::OK;
672 my ($self, $limit, $offset, $id_list) = @_;
674 # XXX TODO: check for failure after various network calls
676 # It may be unclear, but this result structure lumps circulation and
677 # reservation fines together, and keeps grocery fines separate.
678 $self->ctx->{"fines"} = {
686 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
688 # TODO: This should really be a ML call, but the existing calls
689 # return an excessive amount of data and don't offer streaming
691 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
693 my $req = $cstore->request(
694 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
696 usr => $self->editor->requestor->id,
697 balance_owed => {'!=' => 0},
698 ($id_list && @$id_list ? ("id" => $id_list) : ()),
703 mobts => [qw/grocery circulation reservation/],
704 bresv => ['target_resource_type'],
708 circ => ['target_copy'],
709 acp => ['call_number'],
712 order_by => { mobts => 'xact_start' },
717 my @total_keys = qw/total_paid total_owed balance_owed/;
718 $self->ctx->{"fines"}->{@total_keys} = (0, 0, 0);
720 while(my $resp = $req->recv) {
721 my $mobts = $resp->content;
722 my $circ = $mobts->circulation;
725 if($mobts->grocery) {
726 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
727 $last_billing = pop(@billings);
730 # XXX TODO confirm that the following, and the later division by 100.0
731 # to get a floating point representation once again, is sufficiently
733 $self->ctx->{"fines"}->{$_} += int($mobts->$_ * 100) for (@total_keys);
735 my $marc_xml = undef;
736 if ($mobts->xact_type eq 'reservation' and
737 $mobts->reservation->target_resource_type->record) {
738 $marc_xml = XML::LibXML->new->parse_string(
739 $mobts->reservation->target_resource_type->record->marc
741 } elsif ($mobts->xact_type eq 'circulation' and
742 $circ->target_copy->call_number->id != -1) {
743 $marc_xml = XML::LibXML->new->parse_string(
744 $circ->target_copy->call_number->record->marc
749 @{$self->ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
752 last_grocery_billing => $last_billing,
753 marc_xml => $marc_xml
758 $self->ctx->{"fines"}->{$_} /= 100.0 for (@total_keys);
762 sub prepare_fines_for_payment {
763 # This assumes $self->prepare_fines has already been run
767 if ($self->ctx->{fines}) {
768 push @results, [$_->{xact}->id, $_->{xact}->balance_owed] foreach (
769 @{$self->ctx->{fines}->{circulation}},
770 @{$self->ctx->{fines}->{grocery}}
777 sub load_myopac_main {
779 my $limit = $self->cgi->param('limit') || 0;
780 my $offset = $self->cgi->param('offset') || 0;
782 return $self->prepare_fines($limit, $offset) || Apache2::Const::OK;
785 sub load_myopac_update_email {
787 my $e = $self->editor;
788 my $ctx = $self->ctx;
789 my $email = $self->cgi->param('email') || '';
791 return Apache2::Const::OK
792 unless $self->cgi->request_method eq 'POST';
794 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
795 $ctx->{invalid_email} = $email;
796 return Apache2::Const::OK;
799 my $stat = $U->simplereq(
801 'open-ils.actor.user.email.update',
802 $e->authtoken, $email);
804 my $url = $self->apache->unparsed_uri;
805 $url =~ s/update_email/prefs/;
807 return $self->generic_redirect($url);
810 sub load_myopac_update_username {
812 my $e = $self->editor;
813 my $ctx = $self->ctx;
814 my $username = $self->cgi->param('username') || '';
816 return Apache2::Const::OK
817 unless $self->cgi->request_method eq 'POST';
819 unless($username and $username !~ /\s/) { # any other username restrictions?
820 $ctx->{invalid_username} = $username;
821 return Apache2::Const::OK;
824 if($username ne $e->requestor->usrname) {
826 my $evt = $U->simplereq(
828 'open-ils.actor.user.username.update',
829 $e->authtoken, $username);
831 if($U->event_equals($evt, 'USERNAME_EXISTS')) {
832 $ctx->{username_exists} = $username;
833 return Apache2::Const::OK;
837 my $url = $self->apache->unparsed_uri;
838 $url =~ s/update_username/prefs/;
840 return $self->generic_redirect($url);
843 sub load_myopac_update_password {
845 my $e = $self->editor;
846 my $ctx = $self->ctx;
848 return Apache2::Const::OK
849 unless $self->cgi->request_method eq 'POST';
851 my $current_pw = $self->cgi->param('current_pw') || '';
852 my $new_pw = $self->cgi->param('new_pw') || '';
853 my $new_pw2 = $self->cgi->param('new_pw2') || '';
855 unless($new_pw eq $new_pw2) {
856 $ctx->{password_nomatch} = 1;
857 return Apache2::Const::OK;
860 my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
862 if($pw_regex and $new_pw !~ /$pw_regex/) {
863 $ctx->{password_invalid} = 1;
864 return Apache2::Const::OK;
867 my $evt = $U->simplereq(
869 'open-ils.actor.user.password.update',
870 $e->authtoken, $new_pw, $current_pw);
873 if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
874 $ctx->{password_incorrect} = 1;
875 return Apache2::Const::OK;
878 my $url = $self->apache->unparsed_uri;
879 $url =~ s/update_password/prefs/;
881 return $self->generic_redirect($url);
884 sub load_myopac_bookbags {
886 my $e = $self->editor;
887 my $ctx = $self->ctx;
889 $e->xact_begin; # replication...
891 my $rv = $self->load_mylist;
892 unless($rv eq Apache2::Const::OK) {
898 order_by => {cbreb => 'name'},
899 limit => $self->cgi->param('limit') || 10,
900 offset => $self->cgi->param('offset') || 0
903 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
904 {owner => $self->editor->requestor->id, btype => 'bookbag'},
905 # XXX what to do about the possibility of really large bookbags here?
906 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
909 if(!$ctx->{bookbags}) {
911 return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
914 # get unique record IDs
916 foreach my $bbag (@{$ctx->{bookbags}}) {
918 map { $_->target_biblio_record_entry } @{$bbag->items}
920 $rec_ids{$rec_id} = 1;
924 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
927 return Apache2::Const::OK;
931 # actions are create, delete, show, hide, rename, add_rec, delete_item
932 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
933 sub load_myopac_bookbag_update {
934 my ($self, $action, $list_id) = @_;
935 my $e = $self->editor;
936 my $cgi = $self->cgi;
938 $action ||= $cgi->param('action');
939 $list_id ||= $cgi->param('list');
941 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
942 my @del_item = $cgi->param('del_item');
943 my $shared = $cgi->param('shared');
944 my $name = $cgi->param('name');
948 if($action eq 'create') {
949 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
951 $list->owner($e->requestor->id);
952 $list->btype('bookbag');
953 $list->pub($shared ? 't' : 'f');
954 $success = $U->simplereq('open-ils.actor',
955 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
959 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
961 return Apache2::Const::HTTP_BAD_REQUEST unless
962 $list and $list->owner == $e->requestor->id;
965 if($action eq 'delete') {
966 $success = $U->simplereq('open-ils.actor',
967 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
969 } elsif($action eq 'show') {
970 unless($U->is_true($list->pub)) {
972 $success = $U->simplereq('open-ils.actor',
973 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
976 } elsif($action eq 'hide') {
977 if($U->is_true($list->pub)) {
979 $success = $U->simplereq('open-ils.actor',
980 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
983 } elsif($action eq 'rename') {
986 $success = $U->simplereq('open-ils.actor',
987 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
990 } elsif($action eq 'add_rec') {
991 foreach my $add_rec (@add_rec) {
992 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
993 $item->bucket($list_id);
994 $item->target_biblio_record_entry($add_rec);
995 $success = $U->simplereq('open-ils.actor',
996 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
997 last unless $success;
1000 } elsif($action eq 'del_item') {
1001 foreach (@del_item) {
1002 $success = $U->simplereq(
1004 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
1006 last unless $success;
1010 return $self->generic_redirect if $success;
1012 $self->ctx->{bucket_action} = $action;
1013 $self->ctx->{bucket_action_failed} = 1;
1014 return Apache2::Const::OK;