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 my $U = 'OpenILS::Application::AppUtils';
12 # user : au object, fleshed
15 $self->ctx->{page} = 'myopac';
17 $self->ctx->{user} = $self->editor->retrieve_actor_user([
18 $self->ctx->{user}->id,
22 au => [qw/card home_ou addresses ident_type/]
28 return Apache2::Const::OK;
32 sub fetch_user_holds {
37 my $available = shift;
41 my $e = $self->editor;
43 my $circ = OpenSRF::AppSession->create('open-ils.circ');
47 $hold_ids = $circ->request(
48 'open-ils.circ.holds.id_list.retrieve.authoritative',
53 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ] if $limit or $offset;
57 return $hold_ids if $ids_only or @$hold_ids == 0;
60 suppress_notices => 1,
61 suppress_transits => 1,
63 suppress_patron_details => 1,
64 include_bre => $flesh ? 1 : 0
67 # ----------------------------------------------------------------
68 # Collect holds in batches of $batch_size for faster retrieval
72 my $mk_req_batch = sub {
74 my $top_idx = $batch_idx + $batch_size;
75 while($batch_idx < $top_idx) {
76 my $hold_id = $hold_ids->[$batch_idx++];
78 my $ses = OpenSRF::AppSession->create('open-ils.circ');
79 my $req = $ses->request(
80 'open-ils.circ.hold.details.retrieve',
81 $e->authtoken, $hold_id, $args);
82 push(@ses, {ses => $ses, req => $req});
88 my(@collected, @holds, @ses);
91 @ses = $mk_req_batch->() if $first;
92 last if $first and not @ses;
95 # If desired by the caller, filter any holds that are not available.
97 @collected = grep { $_->{hold}->{status} == 4 } @collected;
99 while(my $blob = pop(@collected)) {
100 $blob->{marc_xml} = XML::LibXML->new->parse_string($blob->{hold}->{bre}->marc) if $flesh;
105 for my $req_data (@ses) {
106 push(@collected, {hold => $req_data->{req}->gather(1)});
107 $req_data->{ses}->kill_me;
110 @ses = $mk_req_batch->();
111 last unless @collected or @ses;
115 # put the holds back into the original server sort order
117 for my $id (@$hold_ids) {
118 push @sorted, grep { $_->{hold}->{hold}->id == $id } @holds;
124 sub handle_hold_update {
127 my $e = $self->editor;
130 my @hold_ids = $self->cgi->param('hold_id'); # for non-_all actions
131 @hold_ids = @{$self->fetch_user_holds(undef, 1)} if $action =~ /_all/;
133 my $circ = OpenSRF::AppSession->create('open-ils.circ');
135 if($action =~ /cancel/) {
137 for my $hold_id (@hold_ids) {
138 my $resp = $circ->request(
139 'open-ils.circ.hold.cancel', $e->authtoken, $hold_id, 6 )->gather(1); # 6 == patron-cancelled-via-opac
142 } elsif ($action =~ /activate|suspend/) {
145 for my $hold_id (@hold_ids) {
146 my $vals = {id => $hold_id};
148 if($action =~ /activate/) {
149 $vals->{frozen} = 'f';
150 $vals->{thaw_date} = undef;
152 } elsif($action =~ /suspend/) {
153 $vals->{frozen} = 't';
154 # $vals->{thaw_date} = TODO;
156 push(@$vlist, $vals);
159 $circ->request('open-ils.circ.hold.update.batch.atomic', $e->authtoken, undef, $vlist)->gather(1);
160 } elsif ($action eq 'edit') {
163 my $val = {"id" => $_};
164 $val->{"frozen"} = $self->cgi->param("frozen");
165 $val->{"pickup_lib"} = $self->cgi->param("pickup_lib");
167 for my $field (qw/expire_time thaw_date/) {
168 # XXX TODO make this support other date formats, not just
170 next unless $self->cgi->param($field) =~
171 m:^(\d{2})/(\d{2})/(\d{4})$:;
172 $val->{$field} = "$3-$1-$2";
178 'open-ils.circ.hold.update.batch.atomic',
179 $e->authtoken, undef, \@vals
180 )->gather(1); # LFW XXX test for failure
181 $url = 'https://' . $self->apache->hostname . $self->ctx->{opac_root} . '/myopac/holds';
185 return defined($url) ? $self->generic_redirect($url) : undef;
188 sub load_myopac_holds {
190 my $e = $self->editor;
191 my $ctx = $self->ctx;
194 my $limit = $self->cgi->param('limit') || 0;
195 my $offset = $self->cgi->param('offset') || 0;
196 my $action = $self->cgi->param('action') || '';
197 my $available = int($self->cgi->param('available') || 0);
199 my $hold_handle_result;
200 $hold_handle_result = $self->handle_hold_update($action) if $action;
202 $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
204 return defined($hold_handle_result) ? $hold_handle_result : Apache2::Const::OK;
207 sub load_place_hold {
209 my $ctx = $self->ctx;
210 my $e = $self->editor;
211 my $cgi = $self->cgi;
212 $self->ctx->{page} = 'place_hold';
214 $ctx->{hold_target} = $cgi->param('hold_target');
215 $ctx->{hold_type} = $cgi->param('hold_type');
216 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
218 if($ctx->{hold_type} eq 'T') {
219 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
223 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
225 if(my $pickup_lib = $cgi->param('pickup_lib')) {
228 patronid => $e->requestor->id,
229 titleid => $ctx->{hold_target}, # XXX
230 pickup_lib => $pickup_lib,
234 my $allowed = $U->simplereq(
236 'open-ils.circ.title_hold.is_possible',
240 if($allowed->{success} == 1) {
241 my $hold = Fieldmapper::action::hold_request->new;
243 $hold->pickup_lib($pickup_lib);
244 $hold->requestor($e->requestor->id);
245 $hold->usr($e->requestor->id); # XXX staff
246 $hold->target($ctx->{hold_target});
247 $hold->hold_type($ctx->{hold_type});
248 # frozen, expired, etc..
250 my $stat = $U->simplereq(
252 'open-ils.circ.holds.create',
256 if($stat and $stat > 0) {
257 # if successful, return the user to the requesting page
258 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
259 return $self->generic_redirect;
262 $ctx->{hold_failed} = 1;
264 } else { # hold *check* failed
265 $ctx->{hold_failed} = 1; # XXX process the events, etc
266 $ctx->{hold_failed_event} = $allowed->{last_event};
270 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
273 return Apache2::Const::OK;
277 sub fetch_user_circs {
279 my $flesh = shift; # flesh bib data, etc.
280 my $circ_ids = shift;
284 my $e = $self->editor;
289 @circ_ids = @$circ_ids;
293 my $circ_data = $U->simplereq(
295 'open-ils.actor.user.checked_out',
300 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
302 if($limit or $offset) {
303 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
307 return [] unless @circ_ids;
309 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
314 circ => ['target_copy'],
315 acp => ['call_number'],
321 my $circs = $e->search_action_circulation(
322 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
325 for my $circ (@$circs) {
328 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
329 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
330 undef # pre-cat copy, use the dummy title/author instead
335 # make sure the final list is in the correct order
337 for my $id (@circ_ids) {
340 (grep { $_->{circ}->id == $id } @circs)
344 return \@sorted_circs;
348 sub handle_circ_renew {
351 my $ctx = $self->ctx;
353 my @renew_ids = $self->cgi->param('circ');
355 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
357 # TODO: fire off renewal calls in batches to speed things up
359 for my $circ (@$circs) {
361 my $evt = $U->simplereq(
363 'open-ils.circ.renew',
364 $self->editor->authtoken,
366 patron_id => $self->editor->requestor->id,
367 copy_id => $circ->{circ}->target_copy,
372 # TODO return these, then insert them into the circ data
373 # blob that is shoved into the template for each circ
374 # so the template won't have to match them
375 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
382 sub load_myopac_circs {
384 my $e = $self->editor;
385 my $ctx = $self->ctx;
388 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
389 my $offset = $self->cgi->param('offset') || 0;
390 my $action = $self->cgi->param('action') || '';
392 # perform the renewal first if necessary
393 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
395 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
397 my $success_renewals = 0;
398 my $failed_renewals = 0;
399 for my $data (@{$ctx->{circs}}) {
400 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
403 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
404 $data->{renewal_response} = $evt;
405 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
406 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
410 $ctx->{success_renewals} = $success_renewals;
411 $ctx->{failed_renewals} = $failed_renewals;
413 return Apache2::Const::OK;
416 sub load_myopac_fines {
418 my $e = $self->editor;
419 my $ctx = $self->ctx;
428 my $limit = $self->cgi->param('limit') || 0;
429 my $offset = $self->cgi->param('offset') || 0;
431 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
433 # TODO: This should really be a ML call, but the existing calls
434 # return an excessive amount of data and don't offer streaming
436 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
438 my $req = $cstore->request(
439 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
441 usr => $e->requestor->id,
442 balance_owed => {'!=' => 0}
447 mobts => ['circulation', 'grocery'],
450 circ => ['target_copy'],
451 acp => ['call_number'],
454 order_by => { mobts => 'xact_start' },
459 while(my $resp = $req->recv) {
460 my $mobts = $resp->content;
461 my $circ = $mobts->circulation;
464 if($mobts->grocery) {
465 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
466 $last_billing = pop(@billings);
469 # XXX TODO switch to some money-safe non-fp library for math
470 $ctx->{"fines"}->{$_} += $mobts->$_ for (
471 qw/total_paid total_owed balance_owed/
475 @{$ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
478 last_grocery_billing => $last_billing,
479 marc_xml => ($mobts->xact_type ne 'circulation' or $circ->target_copy->call_number->id == -1) ?
481 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
486 return Apache2::Const::OK;
489 sub load_myopac_update_email {
491 my $e = $self->editor;
492 my $ctx = $self->ctx;
493 my $email = $self->cgi->param('email') || '';
495 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
496 $ctx->{invalid_email} = $email;
497 return Apache2::Const::OK;
500 my $stat = $U->simplereq(
502 'open-ils.actor.user.email.update',
503 $e->authtoken, $email);
505 my $url = $self->apache->unparsed_uri;
506 $url =~ s/update_email/prefs/;
508 return $self->generic_redirect($url);
511 sub load_myopac_bookbags {
513 my $e = $self->editor;
514 my $ctx = $self->ctx;
516 my $rv = $self->load_mylist;
517 return $rv if $rv ne Apache2::Const::OK;
520 order_by => {cbreb => 'name'},
521 limit => $self->cgi->param('limit') || 10,
522 offset => $self->cgi->param('limit') || 0
525 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
526 {owner => $self->editor->requestor->id, btype => 'bookbag'},
527 # XXX what to do about the possibility of really large bookbags here?
528 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
529 ]) or return $e->die_event;
531 # get unique record IDs
533 foreach my $bbag (@{$ctx->{bookbags}}) {
535 map { $_->target_biblio_record_entry } @{$bbag->items}
537 $rec_ids{$rec_id} = 1;
541 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id([keys %rec_ids]);
543 return Apache2::Const::OK;
547 # actions are create, delete, show, hide, rename, add_rec, delete_item
548 # CGI is action, list=list_id, add_rec/record=bre_id, del_item=bucket_item_id, name=new_bucket_name
549 sub load_myopac_bookbag_update {
550 my ($self, $action, $list_id) = @_;
551 my $e = $self->editor;
552 my $cgi = $self->cgi;
554 $action ||= $cgi->param('action');
555 $list_id ||= $cgi->param('list');
557 my @add_rec = $cgi->param('add_rec') || $cgi->param('record');
558 my @del_item = $cgi->param('del_item');
559 my $shared = $cgi->param('shared');
560 my $name = $cgi->param('name');
564 if($action eq 'create') {
565 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
567 $list->owner($e->requestor->id);
568 $list->btype('bookbag');
569 $list->pub($shared ? 't' : 'f');
570 $success = $U->simplereq('open-ils.actor',
571 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
575 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
577 return Apache2::Const::HTTP_BAD_REQUEST unless
578 $list and $list->owner == $e->requestor->id;
581 if($action eq 'delete') {
582 $success = $U->simplereq('open-ils.actor',
583 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
585 } elsif($action eq 'show') {
586 unless($U->is_true($list->pub)) {
588 $success = $U->simplereq('open-ils.actor',
589 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
592 } elsif($action eq 'hide') {
593 if($U->is_true($list->pub)) {
595 $success = $U->simplereq('open-ils.actor',
596 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
599 } elsif($action eq 'rename') {
602 $success = $U->simplereq('open-ils.actor',
603 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
606 } elsif($action eq 'add_rec') {
607 foreach my $add_rec (@add_rec) {
608 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
609 $item->bucket($list_id);
610 $item->target_biblio_record_entry($add_rec);
611 $success = $U->simplereq('open-ils.actor',
612 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
613 last unless $success;
616 } elsif($action eq 'del_item') {
617 foreach (@del_item) {
618 $success = $U->simplereq(
620 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
622 last unless $success;
626 return $self->generic_redirect if $success;
628 $self->ctx->{bucket_action} = $action;
629 $self->ctx->{bucket_action_failed} = 1;
630 return Apache2::Const::OK;