]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Account.pm
Merge branch 'master' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen-equin...
[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') || 15;
510     my $offset = $self->cgi->param('offset') || 0;
511
512     $ctx->{circ_history_limit} = $limit;
513     $ctx->{circ_history_offset} = $offset;
514
515     my $circs = $e->json_query({
516         from => ['action.usr_visible_circs', $e->requestor->id],
517         #limit => $limit || 25,
518         #offset => $offset || 0,
519     });
520
521     # XXX: order-by in the json_query above appears to do nothing, so in-query 
522     # paging is not reallly an option.  do the sorting/paging here
523
524     # sort newest to oldest
525     $circs = [ sort { $b->{xact_start} cmp $a->{xact_start} } @$circs ];
526     my @ids = map { $_->{id} } @$circs;
527
528     # find the selected page and trim cruft
529     @ids = @ids[$offset..($offset + $limit - 1)] if $limit;
530     @ids = grep { defined $_ } @ids;
531
532     $ctx->{circs} = $self->fetch_user_circs(1, \@ids);
533     #$ctx->{circs} = $self->fetch_user_circs(1, [map { $_->{id} } @$circs], $limit, $offset);
534
535     return Apache2::Const::OK;
536 }
537
538 # TODO: action.usr_visible_holds does not return cancelled holds.  Should it?
539 sub load_myopac_hold_history {
540     my $self = shift;
541     my $e = $self->editor;
542     my $ctx = $self->ctx;
543     my $limit = $self->cgi->param('limit') || 15;
544     my $offset = $self->cgi->param('offset') || 0;
545     $ctx->{hold_history_limit} = $limit;
546     $ctx->{hold_history_offset} = $offset;
547
548
549     my $holds = $e->json_query({
550         from => ['action.usr_visible_holds', $e->requestor->id],
551         limit => $limit || 25,
552         offset => $offset || 0
553     });
554
555     $ctx->{holds} = $self->fetch_user_holds([map { $_->{id} } @$holds], 0, 1, 0, $limit, $offset);
556
557     return Apache2::Const::OK;
558 }
559
560 # TODO: add other filter options as params/configs/etc.
561 sub load_myopac_payments {
562     my $self = shift;
563     my $limit = $self->cgi->param('limit') || 20;
564     my $offset = $self->cgi->param('offset') || 0;
565     my $e = $self->editor;
566
567     $self->ctx->{payment_history_limit} = $limit;
568     $self->ctx->{payment_history_offset} = $offset;
569
570     my $args = {};
571     $args->{limit} = $limit if $limit;
572     $args->{offset} = $offset if $offset;
573
574     $self->ctx->{payments} = $U->simplereq(
575         'open-ils.actor',
576         'open-ils.actor.user.payments.retrieve.atomic',
577         $e->authtoken, $e->requestor->id, $args);
578
579     return Apache2::Const::OK;
580 }
581
582
583
584 sub load_myopac_main {
585     my $self = shift;
586     my $limit = $self->cgi->param('limit') || 0;
587     my $offset = $self->cgi->param('offset') || 0;
588     my $e = $self->editor;
589     my $ctx = $self->ctx;
590
591     $ctx->{"fines"} = {
592         "circulation" => [],
593         "grocery" => [],
594         "total_paid" => 0,
595         "total_owed" => 0,
596         "balance_owed" => 0
597     };
598
599
600     my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
601
602     # TODO: This should really be a ML call, but the existing calls 
603     # return an excessive amount of data and don't offer streaming
604
605     my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
606
607     my $req = $cstore->request(
608         'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
609         {
610             usr => $e->requestor->id,
611             balance_owed => {'!=' => 0}
612         },
613         {
614             flesh => 4,
615             flesh_fields => {
616                 mobts => [qw/grocery circulation reservation/],
617                 bresv => ['target_resource_type'],
618                 brt => ['record'],
619                 mg => ['billings'],
620                 mb => ['btype'],
621                 circ => ['target_copy'],
622                 acp => ['call_number'],
623                 acn => ['record']
624             },
625             order_by => { mobts => 'xact_start' },
626             %paging
627         }
628     );
629
630     while(my $resp = $req->recv) {
631         my $mobts = $resp->content;
632         my $circ = $mobts->circulation;
633
634         my $last_billing;
635         if($mobts->grocery) {
636             my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
637             $last_billing = pop(@billings);
638         }
639
640         # XXX TODO switch to some money-safe non-fp library for math
641         $ctx->{"fines"}->{$_} += $mobts->$_ for (
642             qw/total_paid total_owed balance_owed/
643         );
644
645         my $marc_xml = undef;
646         if ($mobts->xact_type eq 'reservation' and
647             $mobts->reservation->target_resource_type->record) {
648             $marc_xml = XML::LibXML->new->parse_string(
649                 $mobts->reservation->target_resource_type->record->marc
650             );
651         } elsif ($mobts->xact_type eq 'circulation' and
652             $circ->target_copy->call_number->id != -1) {
653             $marc_xml = XML::LibXML->new->parse_string(
654                 $circ->target_copy->call_number->record->marc
655             );
656         }
657
658         push(
659             @{$ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
660             {
661                 xact => $mobts,
662                 last_grocery_billing => $last_billing,
663                 marc_xml => $marc_xml
664             } 
665         );
666     }
667
668      return Apache2::Const::OK;
669 }       
670
671 sub load_myopac_update_email {
672     my $self = shift;
673     my $e = $self->editor;
674     my $ctx = $self->ctx;
675     my $email = $self->cgi->param('email') || '';
676
677     return Apache2::Const::OK 
678         unless $self->cgi->request_method eq 'POST';
679
680     unless($email =~ /.+\@.+\..+/) { # TODO better regex?
681         $ctx->{invalid_email} = $email;
682         return Apache2::Const::OK;
683     }
684
685     my $stat = $U->simplereq(
686         'open-ils.actor', 
687         'open-ils.actor.user.email.update', 
688         $e->authtoken, $email);
689
690     my $url = $self->apache->unparsed_uri;
691     $url =~ s/update_email/prefs/;
692
693     return $self->generic_redirect($url);
694 }
695
696 sub load_myopac_update_username {
697     my $self = shift;
698     my $e = $self->editor;
699     my $ctx = $self->ctx;
700     my $username = $self->cgi->param('username') || '';
701
702     return Apache2::Const::OK 
703         unless $self->cgi->request_method eq 'POST';
704
705     unless($username and $username !~ /\s/) { # any other username restrictions?
706         $ctx->{invalid_username} = $username;
707         return Apache2::Const::OK;
708     }
709
710     if($username ne $e->requestor->usrname) {
711
712         my $evt = $U->simplereq(
713             'open-ils.actor', 
714             'open-ils.actor.user.username.update', 
715             $e->authtoken, $username);
716
717         if($U->event_equals($evt, 'USERNAME_EXISTS')) {
718             $ctx->{username_exists} = $username;
719             return Apache2::Const::OK;
720         }
721     }
722
723     my $url = $self->apache->unparsed_uri;
724     $url =~ s/update_username/prefs/;
725
726     return $self->generic_redirect($url);
727 }
728
729 sub load_myopac_update_password {
730     my $self = shift;
731     my $e = $self->editor;
732     my $ctx = $self->ctx;
733
734     return Apache2::Const::OK 
735         unless $self->cgi->request_method eq 'POST';
736
737     my $current_pw = $self->cgi->param('current_pw') || '';
738     my $new_pw = $self->cgi->param('new_pw') || '';
739     my $new_pw2 = $self->cgi->param('new_pw2') || '';
740
741     unless($new_pw eq $new_pw2) {
742         $ctx->{password_nomatch} = 1;
743         return Apache2::Const::OK;
744     }
745
746     my $pw_regex = $ctx->{get_org_setting}->($e->requestor->home_ou, 'global.password_regex');
747
748     if($pw_regex and $new_pw !~ /$pw_regex/) {
749         $ctx->{password_invalid} = 1;
750         return Apache2::Const::OK;
751     }
752
753     my $evt = $U->simplereq(
754         'open-ils.actor', 
755         'open-ils.actor.user.password.update', 
756         $e->authtoken, $new_pw, $current_pw);
757
758
759     if($U->event_equals($evt, 'INCORRECT_PASSWORD')) {
760         $ctx->{password_incorrect} = 1;
761         return Apache2::Const::OK;
762     }
763
764     my $url = $self->apache->unparsed_uri;
765     $url =~ s/update_password/prefs/;
766
767     return $self->generic_redirect($url);
768 }
769
770 sub load_myopac_bookbags {
771     my $self = shift;
772     my $e = $self->editor;
773     my $ctx = $self->ctx;
774
775     $e->xact_begin; # replication...
776
777     my $rv = $self->load_mylist;
778     unless($rv eq Apache2::Const::OK) {
779         $e->rollback;
780         return $rv;
781     }
782
783     my $args = {
784         order_by => {cbreb => 'name'},
785         limit => $self->cgi->param('limit') || 10,
786         offset => $self->cgi->param('offset') || 0
787     };
788
789     $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
790         {owner => $self->editor->requestor->id, btype => 'bookbag'},
791         # XXX what to do about the possibility of really large bookbags here?
792         {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
793     ]);
794
795     if(!$ctx->{bookbags}) {
796         $e->rollback;
797         return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
798     }
799     
800     # get unique record IDs
801     my %rec_ids = ();
802     foreach my $bbag (@{$ctx->{bookbags}}) {
803         foreach my $rec_id (
804             map { $_->target_biblio_record_entry } @{$bbag->items}
805         ) {
806             $rec_ids{$rec_id} = 1;
807         }
808     }
809
810     $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
811
812     $e->rollback;
813     return Apache2::Const::OK;
814 }
815
816
817 # actions are create, delete, show, hide, rename, add_rec, delete_item
818 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
819 sub load_myopac_bookbag_update {
820     my ($self, $action, $list_id) = @_;
821     my $e = $self->editor;
822     my $cgi = $self->cgi;
823
824     $action ||= $cgi->param('action');
825     $list_id ||= $cgi->param('list');
826
827     my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
828     my @del_item = $cgi->param('del_item');
829     my $shared = $cgi->param('shared');
830     my $name = $cgi->param('name');
831     my $success = 0;
832     my $list;
833
834     if($action eq 'create') {
835         $list = Fieldmapper::container::biblio_record_entry_bucket->new;
836         $list->name($name);
837         $list->owner($e->requestor->id);
838         $list->btype('bookbag');
839         $list->pub($shared ? 't' : 'f');
840         $success = $U->simplereq('open-ils.actor', 
841             'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
842
843     } else {
844
845         $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
846
847         return Apache2::Const::HTTP_BAD_REQUEST unless 
848             $list and $list->owner == $e->requestor->id;
849     }
850
851     if($action eq 'delete') {
852         $success = $U->simplereq('open-ils.actor', 
853             'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
854
855     } elsif($action eq 'show') {
856         unless($U->is_true($list->pub)) {
857             $list->pub('t');
858             $success = $U->simplereq('open-ils.actor', 
859                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
860         }
861
862     } elsif($action eq 'hide') {
863         if($U->is_true($list->pub)) {
864             $list->pub('f');
865             $success = $U->simplereq('open-ils.actor', 
866                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
867         }
868
869     } elsif($action eq 'rename') {
870         if($name) {
871             $list->name($name);
872             $success = $U->simplereq('open-ils.actor', 
873                 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
874         }
875
876     } elsif($action eq 'add_rec') {
877         foreach my $add_rec (@add_rec) {
878             my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
879             $item->bucket($list_id);
880             $item->target_biblio_record_entry($add_rec);
881             $success = $U->simplereq('open-ils.actor', 
882                 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
883             last unless $success;
884         }
885
886     } elsif($action eq 'del_item') {
887         foreach (@del_item) {
888             $success = $U->simplereq(
889                 'open-ils.actor',
890                 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
891             );
892             last unless $success;
893         }
894     }
895
896     return $self->generic_redirect if $success;
897
898     $self->ctx->{bucket_action} = $action;
899     $self->ctx->{bucket_action_failed} = 1;
900     return Apache2::Const::OK;
901 }
902
903 1