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
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);
166 sub load_myopac_holds {
168 my $e = $self->editor;
169 my $ctx = $self->ctx;
172 my $limit = $self->cgi->param('limit') || 0;
173 my $offset = $self->cgi->param('offset') || 0;
174 my $action = $self->cgi->param('action') || '';
175 my $available = int($self->cgi->param('available') || 0);
177 $self->handle_hold_update($action) if $action;
179 $ctx->{holds} = $self->fetch_user_holds(undef, 0, 1, $available, $limit, $offset);
181 return Apache2::Const::OK;
184 sub load_place_hold {
186 my $ctx = $self->ctx;
187 my $e = $self->editor;
188 my $cgi = $self->cgi;
189 $self->ctx->{page} = 'place_hold';
191 $ctx->{hold_target} = $cgi->param('hold_target');
192 $ctx->{hold_type} = $cgi->param('hold_type');
193 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
195 if($ctx->{hold_type} eq 'T') {
196 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
200 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
202 if(my $pickup_lib = $cgi->param('pickup_lib')) {
205 patronid => $e->requestor->id,
206 titleid => $ctx->{hold_target}, # XXX
207 pickup_lib => $pickup_lib,
211 my $allowed = $U->simplereq(
213 'open-ils.circ.title_hold.is_possible',
217 if($allowed->{success} == 1) {
218 my $hold = Fieldmapper::action::hold_request->new;
220 $hold->pickup_lib($pickup_lib);
221 $hold->requestor($e->requestor->id);
222 $hold->usr($e->requestor->id); # XXX staff
223 $hold->target($ctx->{hold_target});
224 $hold->hold_type($ctx->{hold_type});
225 # frozen, expired, etc..
227 my $stat = $U->simplereq(
229 'open-ils.circ.holds.create',
233 if($stat and $stat > 0) {
234 # if successful, return the user to the requesting page
235 $self->apache->log->info("Redirecting back to " . $cgi->param('redirect_to'));
236 return $self->generic_redirect;
239 $ctx->{hold_failed} = 1;
241 } else { # hold *check* failed
242 $ctx->{hold_failed} = 1; # XXX process the events, etc
243 $ctx->{hold_failed_event} = $allowed->{last_event};
247 $logger->info('hold permit result ' . OpenSRF::Utils::JSON->perl2JSON($allowed));
250 return Apache2::Const::OK;
254 sub fetch_user_circs {
256 my $flesh = shift; # flesh bib data, etc.
257 my $circ_ids = shift;
261 my $e = $self->editor;
266 @circ_ids = @$circ_ids;
270 my $circ_data = $U->simplereq(
272 'open-ils.actor.user.checked_out',
277 @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
279 if($limit or $offset) {
280 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
284 return [] unless @circ_ids;
286 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
291 circ => ['target_copy'],
292 acp => ['call_number'],
298 my $circs = $e->search_action_circulation(
299 [{id => \@circ_ids}, ($flesh) ? $qflesh : {}], {substream => 1});
302 for my $circ (@$circs) {
305 marc_xml => ($flesh and $circ->target_copy->call_number->id != -1) ?
306 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc) :
307 undef # pre-cat copy, use the dummy title/author instead
312 # make sure the final list is in the correct order
314 for my $id (@circ_ids) {
317 (grep { $_->{circ}->id == $id } @circs)
321 return \@sorted_circs;
325 sub handle_circ_renew {
328 my $ctx = $self->ctx;
330 my @renew_ids = $self->cgi->param('circ');
332 my $circs = $self->fetch_user_circs(0, ($action eq 'renew') ? [@renew_ids] : undef);
334 # TODO: fire off renewal calls in batches to speed things up
336 for my $circ (@$circs) {
338 my $evt = $U->simplereq(
340 'open-ils.circ.renew',
341 $self->editor->authtoken,
343 patron_id => $self->editor->requestor->id,
344 copy_id => $circ->{circ}->target_copy,
349 # TODO return these, then insert them into the circ data
350 # blob that is shoved into the template for each circ
351 # so the template won't have to match them
352 push(@responses, {copy => $circ->{circ}->target_copy, evt => $evt});
359 sub load_myopac_circs {
361 my $e = $self->editor;
362 my $ctx = $self->ctx;
365 my $limit = $self->cgi->param('limit') || 0; # 0 == unlimited
366 my $offset = $self->cgi->param('offset') || 0;
367 my $action = $self->cgi->param('action') || '';
369 # perform the renewal first if necessary
370 my @results = $self->handle_circ_renew($action) if $action =~ /renew/;
372 $ctx->{circs} = $self->fetch_user_circs(1, undef, $limit, $offset);
374 my $success_renewals = 0;
375 my $failed_renewals = 0;
376 for my $data (@{$ctx->{circs}}) {
377 my ($resp) = grep { $_->{copy} == $data->{circ}->target_copy->id } @results;
380 my $evt = ref($resp->{evt}) eq 'ARRAY' ? $resp->{evt}->[0] : $resp->{evt};
381 $data->{renewal_response} = $evt;
382 $success_renewals++ if $evt->{textcode} eq 'SUCCESS';
383 $failed_renewals++ if $evt->{textcode} ne 'SUCCESS';
387 $ctx->{success_renewals} = $success_renewals;
388 $ctx->{failed_renewals} = $failed_renewals;
390 return Apache2::Const::OK;
393 sub load_myopac_fines {
395 my $e = $self->editor;
396 my $ctx = $self->ctx;
405 my $limit = $self->cgi->param('limit') || 0;
406 my $offset = $self->cgi->param('offset') || 0;
408 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
410 # TODO: This should really be a ML call, but the existing calls
411 # return an excessive amount of data and don't offer streaming
413 my %paging = ($limit or $offset) ? (limit => $limit, offset => $offset) : ();
415 my $req = $cstore->request(
416 'open-ils.cstore.direct.money.open_billable_transaction_summary.search',
418 usr => $e->requestor->id,
419 balance_owed => {'!=' => 0}
424 mobts => ['circulation', 'grocery'],
427 circ => ['target_copy'],
428 acp => ['call_number'],
431 order_by => { mobts => 'xact_start' },
436 while(my $resp = $req->recv) {
437 my $mobts = $resp->content;
438 my $circ = $mobts->circulation;
441 if($mobts->grocery) {
442 my @billings = sort { $a->billing_ts cmp $b->billing_ts } @{$mobts->grocery->billings};
443 $last_billing = pop(@billings);
446 # XXX TODO switch to some money-safe non-fp library for math
447 $ctx->{"fines"}->{$_} += $mobts->$_ for (
448 qw/total_paid total_owed balance_owed/
452 @{$ctx->{"fines"}->{$mobts->grocery ? "grocery" : "circulation"}},
455 last_grocery_billing => $last_billing,
456 marc_xml => ($mobts->xact_type ne 'circulation' or $circ->target_copy->call_number->id == -1) ?
458 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
463 return Apache2::Const::OK;
466 sub load_myopac_update_email {
468 my $e = $self->editor;
469 my $ctx = $self->ctx;
470 my $email = $self->cgi->param('email') || '';
472 unless($email =~ /.+\@.+\..+/) { # TODO better regex?
473 $ctx->{invalid_email} = $email;
474 return Apache2::Const::OK;
477 my $stat = $U->simplereq(
479 'open-ils.actor.user.email.update',
480 $e->authtoken, $email);
482 my $url = $self->apache->unparsed_uri;
483 $url =~ s/update_email/prefs/;
485 return $self->generic_redirect($url);
488 sub load_myopac_bookbags {
490 my $e = $self->editor;
491 my $ctx = $self->ctx;
493 my $rv = $self->load_mylist;
494 return $rv if $rv ne Apache2::Const::OK;
497 order_by => {cbreb => 'name'},
498 limit => $self->cgi->param('limit') || 10,
499 offset => $self->cgi->param('limit') || 0
502 $ctx->{bookbags} = $e->search_container_biblio_record_entry_bucket([
503 {owner => $self->editor->requestor->id, btype => 'bookbag'},
504 # XXX what to do about the possibility of really large bookbags here?
505 {"flesh" => 1, "flesh_fields" => {"cbreb" => ["items"]}, %$args}
506 ]) or return $e->die_event;
508 # get unique record IDs
510 foreach my $bbag (@{$ctx->{bookbags}}) {
511 foreach my $item_id (map { $_->id } @{$bbag->items}) {
512 $rec_ids{$item_id} = 1;
516 $ctx->{bookbags_marc_xml} = $self->fetch_marc_xml_by_id(keys %rec_ids);
518 return Apache2::Const::OK;
522 # actions are create, delete, show, hide, rename, add_rec, delete_item
523 # CGI is action, list=list_id, add_rec=bre_id, del_item=bucket_item_id, name=new_bucket_name
524 sub load_myopac_bookbag_update {
526 my $e = $self->editor;
527 my $cgi = $self->cgi;
528 my $action = $cgi->param('action');
529 my $list_id = $cgi->param('list');
530 my $add_rec = $cgi->param('add_rec');
531 my @del_item = $cgi->param('del_item');
532 my $shared = $cgi->param('shared');
533 my $name = $cgi->param('name');
537 if($action eq 'create') {
538 $list = Fieldmapper::container::biblio_record_entry_bucket->new;
540 $list->owner($e->requestor->id);
541 $list->btype('bookbag');
542 $list->pub($shared ? 't' : 'f');
543 $success = $U->simplereq('open-ils.actor',
544 'open-ils.actor.container.create', $e->authtoken, 'biblio', $list)
548 $list = $e->retrieve_container_biblio_record_entry_bucket($list_id);
550 return Apache2::Const::HTTP_BAD_REQUEST unless
551 $list and $list->owner == $e->requestor->id;
554 if($action eq 'delete') {
555 $success = $U->simplereq('open-ils.actor',
556 'open-ils.actor.container.full_delete', $e->authtoken, 'biblio', $list_id);
558 } elsif($action eq 'show') {
559 unless($U->is_true($list->pub)) {
561 $success = $U->simplereq('open-ils.actor',
562 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
565 } elsif($action eq 'hide') {
566 if($U->is_true($list->pub)) {
568 $success = $U->simplereq('open-ils.actor',
569 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
572 } elsif($action eq 'rename') {
575 $success = $U->simplereq('open-ils.actor',
576 'open-ils.actor.container.update', $e->authtoken, 'biblio', $list);
579 } elsif($action eq 'add_rec') {
580 my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new;
581 $item->bucket($list_id);
582 $item->target_biblio_record_entry($add_rec);
583 $success = $U->simplereq('open-ils.actor',
584 'open-ils.actor.container.item.create', $e->authtoken, 'biblio', $item);
586 } elsif($action eq 'del_item') {
587 foreach (@del_item) {
588 $success = $U->simplereq(
590 'open-ils.actor.container.item.delete', $e->authtoken, 'biblio', $_
592 last unless $success;
596 return $self->generic_redirect if $success;
598 $self->ctx->{bucket_action} = $action;
599 $self->ctx->{bucket_action_failed} = 1;
600 return Apache2::Const::OK;