1 package OpenILS::WWW::EGKPacLoader;
2 use base 'OpenILS::WWW::EGCatLoader';
3 use strict; use warnings;
5 use Apache2::Const -compile => qw(OK HTTP_BAD_REQUEST);
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 my $U = 'OpenILS::Application::AppUtils';
12 # -----------------------------------------------------------------------------
13 # Override our parent's load() sub so we can do kpac-specific path routing.
14 # -----------------------------------------------------------------------------
18 $self->init_ro_object_cache;
20 my $stat = $self->load_common;
21 return $stat unless $stat == Apache2::Const::OK;
23 $self->load_kpac_config;
25 my $path = $self->apache->path_info;
26 ($self->ctx->{page} = $path) =~ s#.*/(.*)#$1#g;
28 return $self->load_simple("home") if $path =~ m|kpac/home|;
29 return $self->load_simple("category") if $path =~ m|kpac/category|;
30 return $self->load_kpac_rresults if $path =~ m|kpac/results|;
31 return $self->load_record(no_search => 1) if $path =~ m|kpac/record|;
32 return $self->load_library if $path =~ m|kpac/library|;
34 # ----------------------------------------------------------------
35 # Everything below here requires SSL
36 # ----------------------------------------------------------------
37 return $self->redirect_ssl unless $self->cgi->https;
39 return $self->load_getit_results if $path =~ m|kpac/getit_results|;
40 return $self->load_getit if $path =~ m|kpac/getit|;
42 # ----------------------------------------------------------------
43 # Everything below here requires authentication
44 # ----------------------------------------------------------------
45 return $self->redirect_auth unless $self->editor->requestor;
49 return Apache2::Const::OK;
52 sub load_kpac_rresults {
55 # The redirect-to-record-details-on-single-hit logic
56 # leverages the opac_root to determine the record detail
57 # page. Replace it temporarily for our purposes.
58 my $tpac_root = $self->ctx->{opac_root};
59 $self->ctx->{opac_root} = $self->ctx->{kpac_root};
61 my $stat = $self->load_rresults;
62 $self->ctx->{opac_root} = $tpac_root;
70 my $rec_id = $ctx->{page_args}->[0];
71 my $bbag_id = $self->cgi->param('bookbag');
72 my $action = $self->cgi->param('action') || '';
74 # first load the record
75 my $stat = $self->load_record(no_search => 1);
76 return $stat unless $stat == Apache2::Const::OK;
78 $self->ctx->{page} = 'getit'; # repair the page
80 return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
81 return $self->login_and_place_hold($rec_id) if $action eq 'hold';
83 # if the user is logged in, fetch his bookbags
85 $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
86 { owner => $ctx->{user}->id,
89 { order_by => {cbreb => 'name'},
90 limit => $self->cgi->param('bbag_limit') || 100
95 $self->ctx->{page} = 'getit'; # repair the page
96 return Apache2::Const::OK;
99 sub login_and_place_hold {
102 my $ctx = $self->ctx;
103 my $username = $self->cgi->param('username');
104 my $password = $self->cgi->param('password');
105 my $pickup_lib = $self->cgi->param('pickup_lib');
107 return Apache2::Const::HTTP_BAD_REQUEST
108 unless $pickup_lib =~ /^\d+$/;
110 my $new_uri = $self->apache->unparsed_uri;
111 my $sep = ($new_uri =~ /\?/) ? '&' : '?';
114 # First, log the user in and return to
115 $self->apache->log->info("kpac: logging in $username");
117 # TODO: let user know username/password is required..
118 return Apache2::Const::OK unless $username and $password;
120 $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
121 $self->cgi->param('redirect_to', $new_uri);
122 return $self->load_login;
126 $self->apache->log->info("kpac: placing hold for $bre_id");
128 $new_uri =~ s/getit/getit_results/g;
129 $self->cgi->param('hold_target', $bre_id);
130 $self->cgi->param('hold_type', 'T');
131 $self->cgi->param('part', ''); # needed even if unused
133 my $stat = $self->load_place_hold;
135 $self->apache->log->info("kpac: place hold returned $stat");
137 return $stat unless $stat == Apache2::Const::OK;
139 my $hdata = $ctx->{hold_data}->[0]; # only 1 hold placed
140 if (my $hold_id = $hdata ? $hdata->{hold_success} : undef) {
142 $self->apache->log->info("kpac: place hold succeeded");
143 $new_uri .= "${sep}hold=$hold_id";
146 $self->apache->log->info("kpac: place hold failed : " . $ctx->{hold_failed_event});
147 $new_uri .= "${sep}hold_failed=1";
151 $self->apache->log->info("kpac: place hold redirecting to: $new_uri");
152 return $self->generic_redirect($new_uri);
155 sub save_item_to_bookbag {
158 my $bookbag_id = shift;
161 # save to existing bookbag
162 $self->cgi->param('record', $rec_id);
163 my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
164 # TODO: check for failure
165 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
166 $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
167 return $self->generic_redirect($new_uri);
170 # save to anonymous list
172 # set some params assumed to exist for load_mylist_add
173 $self->cgi->param('record', $rec_id);
174 (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
175 $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
176 $self->cgi->param('redirect_to', $new_uri);
178 return $self->load_mylist_add;
181 return Apache2::Const::HTTP_BAD_REQUEST;
185 sub load_getit_results {
187 my $ctx = $self->ctx;
188 my $e = $self->editor;
189 my $list = $self->cgi->param('list');
190 my $hold_id = $self->cgi->param('hold');
191 my $rec_id = $ctx->{page_args}->[0];
193 my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
194 $ctx->{bre_id} = $rec_data[0]->{id};
195 $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
198 if ($list eq 'anon') {
199 $ctx->{added_to_anon} = 1;
201 $ctx->{added_to_list} = $e->retrieve_container_biblio_record_entry_bucket($list);
205 $ctx->{hold} = $e->retrieve_action_hold_request($hold_id);
209 return Apache2::Const::OK;
212 sub load_kpac_config {
214 my $ctx = $self->ctx;
217 my $path = $self->apache->dir_config('KPacConfigFile');
220 $self->apache->log->error("KPacConfigFile required!");
224 $kpac_config = XMLin(
227 ForceArray => ['layout', 'page', 'cell'],
232 my $ou = $ctx->{physical_loc} || $self->_get_search_lib;
235 # Search up the org tree to find the nearest config for the context org unit
236 while (my $org = $ctx->{get_aou}->($ou)) {
237 ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
239 $ou = $org->parent_ou;
242 $ctx->{kpac_layout} = $layout;
243 $ctx->{kpac_config} = $kpac_config;
244 $ctx->{kpac_root} = $ctx->{base_path} . "/kpac";
245 $ctx->{home_page} = $ctx->{proto} . '://' . $ctx->{hostname} . $ctx->{kpac_root} . "/home";
246 $ctx->{global_search_filter} = $kpac_config->{global_filter};