1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
5 use Digest::MD5 qw(md5_hex);
6 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
7 use OpenSRF::AppSession;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::Utils qw/:datetime/;
10 use OpenSRF::Utils::Logger qw/$logger/;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::CStoreEditor qw/:funcs/;
13 use OpenILS::Utils::Fieldmapper;
14 use DateTime::Format::ISO8601;
15 my $U = 'OpenILS::Application::AppUtils';
17 my %cache; # proc-level cache
20 my($class, $apache, $ctx) = @_;
22 my $self = bless({}, ref($class) || $class);
24 $self->apache($apache);
28 OpenILS::Utils::CStoreEditor->init; # just in case
29 $self->editor(new_editor());
35 # current Apache2::RequestRec;
37 my($self, $apache) = @_;
38 $self->{apache} = $apache if $apache;
39 return $self->{apache};
42 # runtime / template context
45 $self->{ctx} = $ctx if $ctx;
51 my($self, $editor) = @_;
52 $self->{editor} = $editor if $editor;
53 return $self->{editor};
59 $self->{cgi} = $cgi if $cgi;
64 # load common data, then load page data
69 my $stat = $self->load_common;
70 return $stat unless $stat == Apache2::Const::OK;
72 my $path = $self->apache->path_info;
73 return $self->load_home if $path =~ /opac\/home/;
74 return $self->load_login if $path =~ /opac\/login/;
75 return $self->load_logout if $path =~ /opac\/logout/;
76 return $self->load_rresults if $path =~ /opac\/results/;
77 return $self->load_record if $path =~ /opac\/record/;
78 return $self->load_place_hold if $path =~ /opac\/place_hold/;
80 # XXX here is where we need to add a check for session timeout, either that
81 # or have load_* check something...
83 return $self->load_myopac_holds if $path =~ /opac\/myopac\/holds/;
84 return $self->load_myopac_circs if $path =~ /opac\/myopac\/circs/;
85 return $self->load_myopac if $path =~ /opac\/myopac/;
87 return Apache2::Const::OK;
90 # general purpose utility functions added to the environment
93 my $e = $self->editor;
96 $cache{map} = {}; # public object maps
97 $cache{list} = {}; # public object lists
99 # fetch-on-demand-and-cache subs for commonly used public data
100 my @public_classes = qw/ccs aout/;
102 for my $hint (@public_classes) {
105 $Fieldmapper::fieldmap->{$_}->{hint} eq $hint
106 } keys %{ $Fieldmapper::fieldmap };
108 $class =~ s/Fieldmapper:://o;
112 my $list_key = $hint . '_list';
113 my $find_key = "find_$hint";
115 $ctx->{$list_key} = sub {
116 my $method = "retrieve_all_$class";
117 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
118 return $cache{list}{$hint};
121 $cache{map}{$hint} = {};
123 $ctx->{$find_key} = sub {
125 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
126 ($cache{map}{$hint}{$id}) = grep { $_->id == $id } @{$ctx->{$list_key}->()};
127 return $cache{map}{$hint}{$id};
132 $ctx->{aou_tree} = sub {
134 # fetch the org unit tree
135 unless($cache{aou_tree}) {
136 my $tree = $e->search_actor_org_unit([
137 { parent_ou => undef},
139 flesh_fields => {aou => ['children']},
140 order_by => {aou => 'name'}
144 # flesh the org unit type for each org unit
145 # and simultaneously set the id => aou map cache
149 $node->ou_type( $ctx->{find_aout}->($node->ou_type) );
150 $cache{map}{aou}{$node->id} = $node;
151 flesh_aout($_, $ctx) foreach @{$node->children};
153 flesh_aout($tree, $ctx);
155 $cache{aou_tree} = $tree;
158 return $cache{aou_tree};
161 # Add a special handler for the tree-shaped org unit cache
162 $cache{map}{aou} = {};
163 $ctx->{find_aou} = sub {
165 $ctx->{aou_tree}->(); # force the org tree to load
166 return $cache{map}{aou}{$org_id};
169 # turns an ISO date into something TT can understand
170 $ctx->{parse_datetime} = sub {
172 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
174 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
188 # user_status : hash of user circ numbers
192 my $e = $self->editor;
193 my $ctx = $self->ctx;
195 if($e->authtoken($self->cgi->cookie('ses'))) {
199 $ctx->{authtoken} = $e->authtoken;
200 $ctx->{user} = $e->requestor;
201 $ctx->{user_stats} = $U->simplereq(
203 'open-ils.actor.user.opac.vital_stats',
204 $e->authtoken, $e->requestor->id);
208 return $self->load_logout;
212 return Apache2::Const::OK;
217 $self->ctx->{page} = 'home';
218 return Apache2::Const::OK;
224 my $cgi = $self->cgi;
226 $self->ctx->{page} = 'login';
228 my $username = $cgi->param('username');
229 my $password = $cgi->param('password');
231 return Apache2::Const::OK unless $username and $password;
233 my $seed = $U->simplereq(
235 'open-ils.auth.authenticate.init',
238 my $response = $U->simplereq(
240 'open-ils.auth.authenticate.complete',
241 { username => $username,
242 password => md5_hex($seed . md5_hex($password)),
247 # XXX check event, redirect as necessary
249 my $home = $self->apache->unparsed_uri;
250 $home =~ s/\/login/\/home/;
252 $self->apache->print(
254 -url => $cgi->param('origin') || $home,
255 -cookie => $cgi->cookie(
259 -value => $response->{payload}->{authtoken},
260 -expires => CORE::time + $response->{payload}->{authtime}
265 return Apache2::Const::REDIRECT;
271 my $path = $self->apache->uri;
272 $path =~ s/(\/[^\/]+$)/\/home/;
273 my $url = 'http://' . $self->apache->hostname . "$path";
275 $self->apache->print(
276 $self->cgi->redirect(
278 -cookie => $self->cgi->cookie(
287 return Apache2::Const::REDIRECT;
293 # records : list of bre's and copy-count objects
296 my $cgi = $self->cgi;
297 my $ctx = $self->ctx;
298 my $e = $self->editor;
300 $ctx->{page} = 'rresult';
301 my $page = $cgi->param('page') || 0;
302 my $facet = $cgi->param('facet');
303 my $query = $cgi->param('query');
304 my $limit = $cgi->param('limit') || 10; # XXX user settings
305 my $args = {limit => $limit, offset => $page * $limit};
306 $query = "$query $facet" if $facet;
310 $results = $U->simplereq(
312 'open-ils.search.biblio.multiclass.query.staff',
317 $logger->error("multiclass search error: $err");
318 $results = {count => 0, ids => []};
321 my $rec_ids = [map { $_->[0] } @{$results->{ids}}];
323 $ctx->{records} = [];
324 $ctx->{search_facets} = {};
325 $ctx->{page_size} = $limit;
326 $ctx->{hit_count} = $results->{count};
328 return Apache2::Const::OK if @$rec_ids == 0;
330 my $cstore1 = OpenSRF::AppSession->create('open-ils.cstore');
331 my $bre_req = $cstore1->request(
332 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids});
334 my $search = OpenSRF::AppSession->create('open-ils.search');
335 my $facet_req = $search->request('open-ils.search.facet_cache.retrieve', $results->{facet_key}, 10);
337 unless($cache{cmf}) {
338 $cache{cmf} = $e->search_config_metabib_field({id => {'!=' => undef}});
339 $ctx->{metabib_field} = $cache{cmf};
340 #$cache{cmc} = $e->search_config_metabib_class({name => {'!=' => undef}});
341 #$ctx->{metabib_class} = $cache{cmc};
345 while(my $resp = $bre_req->recv) {
346 my $bre = $resp->content;
348 # XXX farm out to multiple cstore sessions before loop, then collect after
349 my $copy_counts = $e->json_query(
350 {from => ['asset.record_copy_count', 1, $bre->id, 0]})->[0];
355 marc_xml => XML::LibXML->new->parse_string($bre->marc),
356 copy_counts => $copy_counts
363 # shove recs into context in search results order
364 for my $rec_id (@$rec_ids) {
367 grep { $_->{bre}->id == $rec_id } @data
371 my $facets = $facet_req->gather(1);
373 for my $cmf_id (keys %$facets) { # quick-n-dirty
374 my ($cmf) = grep { $_->id eq $cmf_id } @{$cache{cmf}};
375 $facets->{$cmf_id} = {cmf => $cmf, data => $facets->{$cmf_id}};
377 $ctx->{search_facets} = $facets;
379 return Apache2::Const::OK;
383 # record : bre object
386 $self->ctx->{page} = 'record';
388 my $rec_id = $self->ctx->{page_args}->[0]
389 or return Apache2::Const::HTTP_BAD_REQUEST;
391 $self->ctx->{record} = $self->editor->retrieve_biblio_record_entry([
396 bre => ['call_numbers'],
397 acn => ['copies'] # limit, paging, etc.
402 $self->ctx->{marc_xml} = XML::LibXML->new->parse_string($self->ctx->{record}->marc);
404 return Apache2::Const::OK;
408 # user : au object, fleshed
411 $self->ctx->{page} = 'myopac';
413 $self->ctx->{user} = $self->editor->retrieve_actor_user([
414 $self->ctx->{user}->id,
424 return Apache2::Const::OK;
427 sub load_myopac_holds {
429 my $e = $self->editor;
430 my $ctx = $self->ctx;
432 my $limit = $self->cgi->param('limit') || 10;
433 my $offset = $self->cgi->param('offset') || 0;
435 my $circ = OpenSRF::AppSession->create('open-ils.circ');
436 my $hold_ids = $circ->request(
437 'open-ils.circ.holds.id_list.retrieve',
442 $hold_ids = [ grep { defined $_ } @$hold_ids[$offset..($offset + $limit - 1)] ];
444 my $req = $circ->request(
445 'open-ils.circ.hold.details.batch.retrieve',
449 suppress_notices => 1,
450 suppress_transits => 1,
452 suppress_patron_details => 1,
457 # any requests we can fire off here?
460 while(my $resp = $req->recv) {
461 my $hold = $resp->content;
462 push(@{$ctx->{holds}}, {
464 marc_xml => XML::LibXML->new->parse_string($hold->{bre}->marc)
469 return Apache2::Const::OK;
472 sub load_place_hold {
474 my $ctx = $self->ctx;
475 my $e = $self->editor;
476 $self->ctx->{page} = 'place_hold';
478 $ctx->{hold_target} = $self->cgi->param('hold_target');
479 $ctx->{hold_type} = $self->cgi->param('hold_type');
480 $ctx->{default_pickup_lib} = $e->requestor->home_ou; # XXX staff
482 if($ctx->{hold_type} eq 'T') {
483 $ctx->{record} = $e->retrieve_biblio_record_entry($ctx->{hold_target});
487 $ctx->{marc_xml} = XML::LibXML->new->parse_string($ctx->{record}->marc);
489 if(my $pickup_lib = $self->cgi->param('pickup_lib')) {
492 patronid => $e->requestor->id,
493 titleid => $ctx->{hold_target}, # XXX
494 pickup_lib => $pickup_lib,
498 my $allowed = $U->simplereq(
500 'open-ils.circ.title_hold.is_possible',
504 if($allowed->{success} == 1) {
505 my $hold = Fieldmapper::action::hold_request->new;
507 $hold->pickup_lib($pickup_lib);
508 $hold->requestor($e->requestor->id);
509 $hold->usr($e->requestor->id); # XXX staff
510 $hold->target($ctx->{hold_target});
511 $hold->hold_type($ctx->{hold_type});
512 # frozen, expired, etc..
514 my $stat = $U->simplereq(
516 'open-ils.circ.holds.create',
520 if($stat and $stat > 0) {
521 $ctx->{hold_success} = 1;
523 $ctx->{hold_failed} = 1; # XXX process the events, etc
527 # place the hold and deliver results
530 return Apache2::Const::OK;
534 sub load_myopac_circs {
536 my $e = $self->editor;
537 my $ctx = $self->ctx;
540 my $limit = $self->cgi->param('limit') || 10;
541 my $offset = $self->cgi->param('offset') || 0;
543 my $circ_data = $U->simplereq(
545 'open-ils.actor.user.checked_out',
550 my @circ_ids = ( @{$circ_data->{overdue}}, @{$circ_data->{out}} );
551 @circ_ids = grep { defined $_ } @circ_ids[0..($offset + $limit - 1)];
553 return Apache2::Const::OK unless @circ_ids;
555 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
556 my $req = $cstore->request(
557 'open-ils.cstore.direct.action.circulation.search',
562 circ => ['target_copy'],
563 acp => ['call_number'],
570 while(my $resp = $req->recv) {
571 my $circ = $resp->content;
574 marc_xml => ($circ->target_copy->call_number->id == -1) ?
575 undef : # pre-cat copy, use the dummy title/author instead
576 XML::LibXML->new->parse_string($circ->target_copy->call_number->record->marc),
580 # make sure the final list is in the correct order
581 for my $id (@circ_ids) {
584 (grep { $_->{circ}->id == $id } @circs)
588 return Apache2::Const::OK;