]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Account.pm
Merge branch 'opac-tt-poc' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen...
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Account.pm
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;
8 use OpenSRF::Utils::JSON;
9 my $U = 'OpenILS::Application::AppUtils';
10
11
12 # context additions: 
13 #   user : au object, fleshed
14 sub load_myopac_prefs {
15     my $self = shift;
16
17     $self->ctx->{user} = $self->editor->retrieve_actor_user([
18         $self->ctx->{user}->id,
19         {
20             flesh => 1,
21             flesh_fields => {
22                 au => [qw/card home_ou addresses ident_type/]
23                 # ...
24             }
25         }
26     ]);
27
28     return Apache2::Const::OK;
29 }
30
31 sub load_myopac_prefs_notify {
32     my $self = shift;
33     my $e = $self->editor;
34
35     my $user_prefs = $self->fetch_optin_prefs;
36     $user_prefs = $self->update_optin_prefs($user_prefs)
37         if $self->cgi->request_method eq 'POST';
38
39     $self->ctx->{opt_in_settings} = $user_prefs; 
40
41     return Apache2::Const::OK;
42 }
43
44 sub fetch_optin_prefs {
45     my $self = shift;
46     my $e = $self->editor;
47
48     # fetch all of the opt-in settings the user has access to
49     # XXX: user's should in theory have options to opt-in to notices
50     # for remote locations, but that opens the door for a large
51     # set of generally un-used opt-ins.. needs discussion
52     my $opt_ins =  $U->simplereq(
53         'open-ils.actor',
54         'open-ils.actor.event_def.opt_in.settings.atomic',
55         $e->authtoken, $e->requestor->home_ou);
56
57     # fetch user setting values for each of the opt-in settings
58     my $user_set = $U->simplereq(
59         'open-ils.actor',
60         'open-ils.actor.patron.settings.retrieve',
61         $e->authtoken, 
62         $e->requestor->id, 
63         [map {$_->name} @$opt_ins]
64     );
65
66     return [map { {cust => $_, value => $user_set->{$_->name} } } @$opt_ins];
67 }
68
69 sub update_optin_prefs {
70     my $self = shift;
71     my $user_prefs = shift;
72     my $e = $self->editor;
73     my @settings = $self->cgi->param('setting');
74     my %newsets;
75
76     # apply now-true settings
77     for my $applied (@settings) {
78         # see if setting is already applied to this user
79         next if grep { $_->{cust}->name eq $applied and $_->{value} } @$user_prefs;
80         $newsets{$applied} = OpenSRF::Utils::JSON->true;
81     }
82
83     # remove now-false settings
84     for my $pref (grep { $_->{value} } @$user_prefs) {
85         $newsets{$pref->{cust}->name} = undef 
86             unless grep { $_ eq $pref->{cust}->name } @settings;
87     }
88
89     $U->simplereq(
90         'open-ils.actor',
91         'open-ils.actor.patron.settings.update',
92         $e->authtoken, $e->requestor->id, \%newsets);
93
94     # update the local prefs to match reality
95     for my $pref (@$user_prefs) {
96         $pref->{value} = $newsets{$pref->{cust}->name} 
97             if exists $newsets{$pref->{cust}->name};
98     }
99
100     return $user_prefs;
101 }
102
103 sub load_myopac_prefs_settings {
104     my $self = shift;
105
106     $self->ctx->{user} = $self->editor->retrieve_actor_user([
107         $self->ctx->{user}->id,
108         {
109             flesh => 1,
110             flesh_fields => {
111                 au => [qw/card home_ou addresses ident_type/]
112                 # ...
113             }
114         }
115     ]);
116
117     return Apache2::Const::OK;
118 }
119
120
121 sub fetch_user_holds {
122     my $self = shift;
123     my $hold_ids = shift;
124     my $ids_only = shift;
125     my $flesh = shift;
126     my $available = shift;
127     my $limit = shift;
128     my $offset = shift;
129
130     my $e = $self->editor;
131
132     my $circ = OpenSRF::AppSession->create('open-ils.circ');
133
134     if(!$hold_ids) {
135
136         $hold_ids = $circ->request(
137             'open-ils.circ.holds.id_list.retrieve.authoritative', 
138             $e->authtoken, 
139             $e->requestor->id
140         )->gather(1);
141     
142         $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
143     }
144
145
146     return $hold_ids if $ids_only or @$hold_ids == 0;
147
148     my $args = {
149         suppress_notices => 1,
150         suppress_transits => 1,
151         suppress_mvr => 1,
152         suppress_patron_details => 1,
153         include_bre => $flesh ? 1 : 0
154     };
155
156     # ----------------------------------------------------------------
157     # Collect holds in batches of $batch_size for faster retrieval
158
159     my $batch_size = 8;
160     my $batch_idx = 0;
161     my $mk_req_batch = sub {
162         my @ses;
163         my $top_idx = $batch_idx + $batch_size;
164         while($batch_idx < $top_idx) {
165             my $hold_id = $hold_ids->[$batch_idx++];
166             last unless $hold_id;
167             my $ses = OpenSRF::AppSession->create('open-ils.circ');
168             my $req = $ses->request(
169                 'open-ils.circ.hold.details.retrieve', 
170                 $e->authtoken, $hold_id, $args);
171             push(@ses, {ses => $ses, req => $req});
172         }
173         return @ses;
174     };
175
176     my $first = 1;
177     my(@collected, @holds, @ses);
178
179     while(1) {
180         @ses = $mk_req_batch->() if $first;
181         last if $first and not @ses;
182
183         if(@collected) {
184             # If desired by the caller, filter any holds that are not available.
185             if ($available) {
186                 @collected = grep { $_->{hold}->{status} == 4 } @collected;
187             }
188             while(my $blob = pop(@collected)) {
189                 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
190                 push(@holds, $blob);
191             }
192         }
193
194         for my $req_data (@ses) {
195             push(@collected, {hold => $req_data->{req}->gather(1)});
196             $req_data->{ses}->kill_me;
197         }
198
199         @ses = $mk_req_batch->();
200         last unless @collected or @ses;
201         $first = 0;
202     }
203
204     # put the holds back into the original server sort order
205     my @sorted;
206     for my $id (@$hold_ids) {
207         push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
208     }
209
210     return \@sorted;
211 }
212
213 sub handle_hold_update {
214     my $self = shift;
215     my $action = shift;
216     my $e = $self->editor;
217     my $url;
218
219     my @hold_ids = $self->cgi->param('hold_id'); # for non-_all actions
220     @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
221
222     my $circ = OpenSRF::AppSession->create('open-ils.circ');
223
224     if($action =~ /cancel/) {
225
226         for my $hold_id (@hold_ids) {
227             my $resp = $circ->request(
228                 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
229         }
230
231     } elsif ($action =~ /activate|suspend/) {
232         
233         my $vlist = [];
234         for my $hold_id (@hold_ids) {
235             my $vals = {id => $hold_id};
236
237             if($action =~ /activate/) {
238                 $vals->{frozen} = 'f';
239                 $vals->{thaw_date} = undef;
240
241             } elsif($action =~ /suspend/) {
242                 $vals->{frozen} = 't';
243                 # $vals->{thaw_date} = TODO;
244             }
245             push(@$vlist, $vals);
246         }
247
248         $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
249     } elsif ($action eq 'edit') {
250
251         my @vals = map {
252             my $val = {"id" => $_};
253             $val->{"frozen"} = $self->cgi->param("frozen");
254             $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
255
256             for my $field (qw/expire_time thaw_date/) {
257                 # XXX TODO make this support other date formats, not just
258                 # MM/DD/YYYY.
259                 next unless $self->cgi->param($field) =~
260                     m:^(\d{2})/(\d{2})/(\d{4})$:;
261                 $val->{$field} = "$3-$1-$2";
262             }
263             $val;
264         } @hold_ids;
265
266         $circ->request(
267             'open-ils.circ.hold.update.batch.atomic',
268             $e->authtoken, undef, \@vals
269         )->gather(1);   # LFW XXX test for failure
270         $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
271     }
272
273     $circ->kill_me;
274     return defined($url) ? $self->generic_redirect($url) : undef;
275 }
276
277 sub load_myopac_holds {
278     my $self = shift;
279     my $e = $self->editor;
280     my $ctx = $self->ctx;
281     
282
283     my $limit = $self->cgi->param('limit') || 0;
284     my $offset = $self->cgi->param('offset') || 0;
285     my $action = $self->cgi->param('action') || '';
286     my $available = int($self->cgi->param('available') || 0);
287
288     my $hold_handle_result;
289     $hold_handle_result = $self->handle_hold_update($action) if $action;
290
291     $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
292
293     return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
294 }
295
296 sub load_place_hold {
297     my $self = shift;
298     my $ctx = $self->ctx;
299     my $e = $self->editor;
300     my $cgi = $self->cgi;
301     $self->ctx->{page} = 'place_hold';
302
303     $ctx->{hold_target} = $cgi->param('hold_target');
304     $ctx->{hold_type} = $cgi->param('hold_type');
305     $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
306
307     if($ctx->{hold_type} eq 'T') {
308         $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
309     }
310     # ...
311
312     $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
313
314     if(my $pickup_lib = $cgi->param('pickup_lib')) {
315
316         my $args = {
317             patronid => $e->requestor->id,
318             titleid => $ctx->{hold_target}, # XXX
319             pickup_lib => $pickup_lib,
320             depth => 0, # XXX
321         };
322
323         my $allowed = $U->simplereq(
324             'open-ils.circ',
325             'open-ils.circ.title_hold.is_possible',
326             $e->authtoken, $args
327         );
328
329         if($allowed->{success} == 1) {
330             my $hold = Fieldmapper::action::hold_request->new;
331
332             $hold->pickup_lib($pickup_lib);
333             $hold->requestor($e->requestor->id);
334             $hold->usr($e->requestor->id); # XXX staff
335             $hold->target($ctx->{hold_target});
336             $hold->hold_type($ctx->{hold_type});
337             # frozen, expired, etc..
338
339             my $stat = $U->simplereq(
340                 'open-ils.circ',
341                 'open-ils.circ.holds.create',
342                 $e->authtoken, $hold
343             );
344
345             if($stat and $stat > 0) {
346                 # if successful, return the user to the requesting page
347                 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
348                 return $self->generic_redirect;
349
350             } else {
351                 $ctx->{hold_failed} = 1;
352             }
353         } else { # hold *check* failed
354             $ctx->{hold_failed} = 1; # XXX process the events, etc
355             $ctx->{hold_failed_event} = $allowed->{last_event};
356         }
357
358         # hold permit failed
359         $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
360     }
361
362     return Apache2::Const::OK;
363 }
364
365
366 sub fetch_user_circs {
367     my $self = shift;
368     my $flesh = shift; # flesh bib data, etc.
369     my $circ_ids = shift;
370     my $limit = shift;
371     my $offset = shift;
372
373     my $e = $self->editor;
374
375     my @circ_ids;
376
377     if($circ_ids) {
378         @circ_ids = @$circ_ids;
379
380     } else {
381
382         my $circ_data = $U->simplereq(
383             'open-ils.actor', 
384             'open-ils.actor.user.checked_out',
385             $e->authtoken, 
386             $e->requestor->id
387         );
388
389         @circ_ids =  ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
390
391         if($limit or $offset) {
392             @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
393         }
394     }
395
396     return [] unless @circ_ids;
397
398     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
399
400     my $qflesh = {
401         flesh => 3,
402         flesh_fields => {
403             circ => ['target_copy'],
404             acp => ['call_number'],
405             acn => ['record']
406         }
407     };
408
409     $e->xact_begin;
410     my $circs = $e->search_action_circulation(
411         [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
412
413     my @circs;
414     for my $circ (@$circs) {
415         push(@circs, {
416             circ => $circ, 
417             marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ? 
418                 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) : 
419                 undef  # pre-cat copy, use the dummy title/author instead
420         });
421     }
422     $e->xact_rollback;
423
424     # make sure the final list is in the correct order
425     my @sorted_circs;
426     for my $id (@circ_ids) {
427         push(
428             @sorted_circs,
429             (grep { $_->{circ}->id == $id } @circs)
430         );
431     }
432
433     return \@sorted_circs;
434 }
435
436
437 sub handle_circ_renew {
438     my $self = shift;
439     my $action = shift;
440     my $ctx = $self->ctx;
441
442     my @renew_ids = $self->cgi->param('circ');
443
444     my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
445
446     # TODO: fire off renewal calls in batches to speed things up
447     my @responses;
448     for my $circ (@$circs) {
449
450         my $evt = $U->simplereq(
451             'open-ils.circ', 
452             'open-ils.circ.renew',
453             $self->editor->authtoken,
454             {
455                 patron_id => $self->editor->requestor->id,
456                 copy_id => $circ->{circ}->target_copy,
457                 opac_renewal => 1
458             }
459         );
460
461         # TODO return these, then insert them into the circ data 
462         # blob that is shoved into the template for each circ
463         # so the template won't have to match them
464         push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
465     }
466
467     return @responses;
468 }
469
470
471 sub load_myopac_circs {
472     my $self = shift;
473     my $e = $self->editor;
474     my $ctx = $self->ctx;
475
476     $ctx->{circs} = [];
477     my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
478     my $offset = $self->cgi->param('offset') || 0;
479     my $action = $self->cgi->param('action') || '';
480
481     # perform the renewal first if necessary
482     my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
483
484     $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
485
486     my $success_renewals = 0;
487     my $failed_renewals = 0;
488     for my $data (@{$ctx->{circs}}) {
489         my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
490
491         if($resp) {
492             my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
493             $data->{renewal_response} = $evt;
494             $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
495             $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
496         }
497     }
498
499     $ctx->{success_renewals} = $success_renewals;
500     $ctx->{failed_renewals} = $failed_renewals;
501
502     return Apache2::Const::OK;
503 }
504
505 sub load_myopac_circ_history {
506     my $self = shift;
507     my $e = $self->editor;
508     my $ctx = $self->ctx;
509     my $limit = $self->cgi->param('limit');
510     my $offset = $self->cgi->param('offset') || 0;
511
512     my $circs = $e->json_query({
513         from => ['action.usr_visible_circs', $e->requestor->id],
514         #limit => $limit || 25,
515         #offset => $offset || 0,
516     });
517
518     # XXX: order-by in the json_query above appears to do nothing, so in-query 
519     # paging is not reallly an option.  do the sorting/paging here
520
521     # sort newest to oldest
522     $circs = [ sort { $b->{xact_start} cmp $a->{xact_start} } @$circs ];
523     my @ids = map { $_->{id} } @$circs;
524
525     # find the selected page and trim cruft
526     @ids = @ids[$offset..($offset + $limit - 1)] if $limit;
527     @ids = grep { defined $_ } @ids;
528
529     $ctx->{circs} = $self->fetch_user_circs(1, \@ids, $limit, $offset);
530     #$ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circs], $limit, $offset);
531
532     return Apache2::Const::OK;
533 }
534
535 # TODO: action.usr_visible_holds does not return cancelled holds.  Should it?
536 sub load_myopac_hold_history {
537     my $self = shift;
538     my $e = $self->editor;
539     my $ctx = $self->ctx;
540     my $limit = $self->cgi->param('limit');
541     my $offset = $self->cgi->param('offset');
542
543     my $holds = $e->json_query({
544         from => ['action.usr_visible_holds', $e->requestor->id],
545         limit => $limit || 25,
546         offset => $offset || 0
547     });
548
549     $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$holds], 0, 1, 0, $limit, $offset);
550
551     return Apache2::Const::OK;
552 }
553
554 sub load_myopac_main {
555     my $self = shift;
556     my $limit = $self->cgi->param('limit') || 0;
557     my $offset = $self->cgi->param('offset') || 0;
558     my $expand = $self->cgi->param('expand') || '';
559
560     return $self->load_myopac_payments($limit, $offset) 
561         if $expand eq 'payments';
562
563     return $self->load_myopac_fines($limit, $offset);
564 }
565
566 # TODO: add other filter options as params/configs/etc.
567 sub load_myopac_payments {
568     my $self = shift;
569     my $limit = shift || 0;
570     my $offset = shift || 0;
571     my $e = $self->editor;
572
573     my $args = {};
574     $args->{limit} = $limit if $limit;
575     $args->{offset} = $offset if $offset;
576
577     $self->ctx->{payments} = $U->simplereq(
578         'open-ils.actor',
579         'open-ils.actor.user.payments.retrieve.atomic',
580         $e->authtoken, $e->requestor->id, $args);
581
582     return Apache2::Const::OK;
583 }
584
585
586
587 sub load_myopac_fines {
588     my $self = shift;
589     my $limit = shift || 0;
590     my $offset = shift || 0;
591     my $e = $self->editor;
592     my $ctx = $self->ctx;
593
594     $ctx->{"fines"} = {
595         "circulation" => [],
596         "grocery" => [],
597         "total_paid" => 0,
598         "total_owed" => 0,
599         "balance_owed" => 0
600     };
601
602
603     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
604
605     # TODO: This should really be a ML call, but the existing calls 
606     # return an excessive amount of data and don't offer streaming
607
608     my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
609
610     my $req = $cstore->request(
611         'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
612         {
613             usr => $e->requestor->id,
614             balance_owed => {'!=' => 0}
615         },
616         {
617             flesh => 4,
618             flesh_fields => {
619                 mobts => ['circulation', 'grocery'],
620                 mg => ['billings'],
621                 mb => ['btype'],
622                 circ => ['target_copy'],
623                 acp => ['call_number'],
624                 acn => ['record']
625             },
626             order_by => { mobts => 'xact_start' },
627             %paging
628         }
629     );
630
631     while(my $resp = $req->recv) {
632         my $mobts = $resp->content;
633         my $circ = $mobts->circulation;
634
635         my $last_billing;
636         if($mobts->grocery) {
637             my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
638             $last_billing = pop(@billings);
639         }
640
641         # XXX TODO switch to some money-safe non-fp library for math
642         $ctx->{"fines"}->{$_} += $mobts->$_ for (
643             qw/total_paid total_owed balance_owed/
644         );
645
646         push(
647             @{$ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
648             {
649                 xact => $mobts,
650                 last_grocery_billing => $last_billing,
651                 marc_xml => ($mobts->xact_type ne 'circulation' or $circ->target_copy->call_number->id == -1) ?
652                     undef :
653                     XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
654             } 
655         );
656     }
657
658      return Apache2::Const::OK;
659 }       
660
661 sub load_myopac_update_email {
662     my $self = shift;
663     my $e = $self->editor;
664     my $ctx = $self->ctx;
665     my $email = $self->cgi->param('email') || '';
666
667     unless($email =~ /.+\@.+\..+/) { # TODO better regex?
668         $ctx->{invalid_email} = $email;
669         return Apache2::Const::OK;
670     }
671
672     my $stat = $U->simplereq(
673         'open-ils.actor', 
674         'open-ils.actor.user.email.update', 
675         $e->authtoken, $email);
676
677     my $url = $self->apache->unparsed_uri;
678     $url =~ s/update_email/prefs/;
679
680     return $self->generic_redirect($url);
681 }
682
683 sub load_myopac_bookbags {
684     my $self = shift;
685     my $e = $self->editor;
686     my $ctx = $self->ctx;
687
688     my $rv = $self->load_mylist;
689     return $rv if $rv ne Apache2::Const::OK;
690
691     my $args = {
692         order_by => {cbreb => 'name'},
693         limit => $self->cgi->param('limit') || 10,
694         offset => $self->cgi->param('limit') || 0
695     };
696
697     $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
698         {owner => $self->editor->requestor->id, btype => 'bookbag'},
699         # XXX what to do about the possibility of really large bookbags here?
700         {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
701     ]) or return $e->die_event;
702
703     # get unique record IDs
704     my %rec_ids = ();
705     foreach my $bbag (@{$ctx->{bookbags}}) {
706         foreach my $rec_id (
707             map { $_->target_biblio_record_entry } @{$bbag->items}
708         ) {
709             $rec_ids{$rec_id} = 1;
710         }
711     }
712
713     $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
714
715     return Apache2::Const::OK;
716 }
717
718
719 # actions are create, delete, show, hide, rename, add_rec, delete_item
720 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
721 sub load_myopac_bookbag_update {
722     my ($self, $action, $list_id) = @_;
723     my $e = $self->editor;
724     my $cgi = $self->cgi;
725
726     $action ||= $cgi->param('action');
727     $list_id ||= $cgi->param('list');
728
729     my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
730     my @del_item = $cgi->param('del_item');
731     my $shared = $cgi->param('shared');
732     my $name = $cgi->param('name');
733     my $success = 0;
734     my $list;
735
736     if($action eq 'create') {
737         $list = Fieldmapper::container::biblio_record_entry_bucket->new;
738         $list->name($name);
739         $list->owner($e->requestor->id);
740         $list->btype('bookbag');
741         $list->pub($shared ? 't' : 'f');
742         $success = $U->simplereq('open-ils.actor', 
743             'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
744
745     } else {
746
747         $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
748
749         return Apache2::Const::HTTP_BAD_REQUEST unless 
750             $list and $list->owner == $e->requestor->id;
751     }
752
753     if($action eq 'delete') {
754         $success = $U->simplereq('open-ils.actor', 
755             'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
756
757     } elsif($action eq 'show') {
758         unless($U->is_true($list->pub)) {
759             $list->pub('t');
760             $success = $U->simplereq('open-ils.actor', 
761                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
762         }
763
764     } elsif($action eq 'hide') {
765         if($U->is_true($list->pub)) {
766             $list->pub('f');
767             $success = $U->simplereq('open-ils.actor', 
768                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
769         }
770
771     } elsif($action eq 'rename') {
772         if($name) {
773             $list->name($name);
774             $success = $U->simplereq('open-ils.actor', 
775                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
776         }
777
778     } elsif($action eq 'add_rec') {
779         foreach my $add_rec (@add_rec) {
780             my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
781             $item->bucket($list_id);
782             $item->target_biblio_record_entry($add_rec);
783             $success = $U->simplereq('open-ils.actor', 
784                 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
785             last unless $success;
786         }
787
788     } elsif($action eq 'del_item') {
789         foreach (@del_item) {
790             $success = $U->simplereq(
791                 'open-ils.actor',
792                 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
793             );
794             last unless $success;
795         }
796     }
797
798     return $self->generic_redirect if $success;
799
800     $self->ctx->{bucket_action} = $action;
801     $self->ctx->{bucket_action_failed} = 1;
802     return Apache2::Const::OK;
803 }
804
805 1