]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGKPacLoader.pm
kpac : repair hold retrieval code
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGKPacLoader.pm
1 package OpenILS::WWW::EGKPacLoader;
2 use base 'OpenILS::WWW::EGCatLoader';
3 use strict; use warnings;
4 use XML::Simple;
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';
10 my $kpac_config;
11
12 # -----------------------------------------------------------------------------
13 # Override our parent's load() sub so we can do kpac-specific path routing.
14 # -----------------------------------------------------------------------------
15 sub load {
16     my $self = shift;
17
18     $self->init_ro_object_cache; 
19
20     my $stat = $self->load_common; 
21     return $stat unless $stat == Apache2::Const::OK;
22
23     $self->load_kpac_config;
24
25     my $path = $self->apache->path_info;
26     ($self->ctx->{page} = $path) =~ s#.*/(.*)#$1#g;
27
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
33     # ----------------------------------------------------------------
34     #  Everything below here requires SSL
35     # ----------------------------------------------------------------
36     return $self->redirect_ssl unless $self->cgi->https;
37
38     return $self->load_getit_results if $path =~ m|kpac/getit_results|;
39     return $self->load_getit if $path =~ m|kpac/getit|;
40
41     # ----------------------------------------------------------------
42     #  Everything below here requires authentication
43     # ----------------------------------------------------------------
44     return $self->redirect_auth unless $self->editor->requestor;
45
46     # AUTH pages
47
48     return Apache2::Const::OK;
49 }
50
51 sub load_kpac_rresults {
52     my $self = shift;
53
54     # The redirect-to-record-details-on-single-hit logic
55     # leverages the opac_root to determine the record detail
56     # page.  Replace it temporarily for our purposes.
57     my $tpac_root = $self->ctx->{opac_root};
58     $self->ctx->{opac_root} = $self->ctx->{kpac_root};
59
60     my $stat = $self->load_rresults;
61     $self->ctx->{opac_root} = $tpac_root;
62
63     return $stat;
64 }
65
66 sub load_getit {
67     my $self = shift;
68     my $ctx = $self->ctx;
69     my $rec_id = $ctx->{page_args}->[0];
70     my $bbag_id = $self->cgi->param('bookbag');
71     my $action = $self->cgi->param('action') || '';
72
73     # first load the record
74     my $stat = $self->load_record(no_search => 1);
75     return $stat unless $stat == Apache2::Const::OK;
76
77     $self->ctx->{page} = 'getit'; # repair the page
78
79     return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
80     return $self->login_and_place_hold($rec_id) if $action eq 'hold';
81
82     # if the user is logged in, fetch his bookbags
83     if ($ctx->{user}) {
84         $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
85             {   owner => $ctx->{user}->id, 
86                 btype => 'bookbag' 
87             }, 
88             {   order_by => {cbreb => 'name'},
89                 limit => $self->cgi->param('bbag_limit') || 100 
90             }
91         ]);
92     }
93
94     $self->ctx->{page} = 'getit'; # repair the page
95     return Apache2::Const::OK;
96 }
97     
98 sub login_and_place_hold {
99     my $self = shift;
100     my $bre_id = shift;
101     my $ctx = $self->ctx;
102     my $username = $self->cgi->param('username');
103     my $password = $self->cgi->param('password');
104     my $pickup_lib = $self->cgi->param('pickup_lib');
105
106     return Apache2::Const::HTTP_BAD_REQUEST 
107         unless $pickup_lib =~ /^\d+$/;
108
109     my $new_uri = $self->apache->unparsed_uri;
110     my $sep = ($new_uri =~ /\?/) ? '&' : '?';
111
112     if (!$ctx->{user}) {
113         # First, log the user in and return to 
114         $self->apache->log->info("kpac: logging in $username");
115
116         # TODO: let user know username/password is required..
117         return Apache2::Const::OK unless $username and $password;
118
119         $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
120         $self->cgi->param('redirect_to', $new_uri);
121         return $self->load_login;
122
123     } else {
124
125         $self->apache->log->info("kpac: placing hold for $bre_id");
126
127         $new_uri =~ s/getit/getit_results/g;
128         $self->cgi->param('hold_target', $bre_id);
129         $self->cgi->param('hold_type', 'T');
130         $self->cgi->param('part', ''); # needed even if unused
131
132         my $stat = $self->load_place_hold;
133
134         $self->apache->log->info("kpac: place hold returned $stat");
135
136         return $stat unless $stat == Apache2::Const::OK;
137
138         my $hdata = $ctx->{hold_data}->[0]; # only 1 hold placed
139         if (my $hold_id = $hdata ? $hdata->{hold_success} : undef) {
140
141             $self->apache->log->info("kpac: place hold succeeded");
142             $new_uri .= "${sep}hold=$hold_id";
143
144         } else {
145             $self->apache->log->info("kpac: place hold failed : " . $ctx->{hold_failed_event});
146             $new_uri .= "${sep}hold_failed=1";
147         }
148     }
149
150     $self->apache->log->info("kpac: place hold redirecting to: $new_uri");
151     return $self->generic_redirect($new_uri);
152 }
153
154 sub save_item_to_bookbag {
155     my $self = shift;
156     my $rec_id = shift;
157     my $bookbag_id = shift;
158
159     if ($bookbag_id) { 
160         # save to existing bookbag
161         $self->cgi->param('record', $rec_id);
162         my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
163         # TODO: check for failure
164         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
165         $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
166         return $self->generic_redirect($new_uri);
167
168     } else { 
169         # save to anonymous list
170        
171         # set some params assumed to exist for load_mylist_add
172         $self->cgi->param('record', $rec_id);
173         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
174         $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
175         $self->cgi->param('redirect_to', $new_uri);
176
177         return $self->load_mylist_add;
178     }
179
180     return Apache2::Const::HTTP_BAD_REQUEST;
181 }
182
183
184 sub load_getit_results {
185     my $self = shift;
186     my $ctx = $self->ctx;
187     my $e = $self->editor;
188     my $list = $self->cgi->param('list');
189     my $hold_id = $self->cgi->param('hold');
190     my $rec_id = $ctx->{page_args}->[0];
191
192     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
193     $ctx->{bre_id} = $rec_data[0]->{id};
194     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
195
196     if ($list) {
197         if ($list eq 'anon') {
198             $ctx->{added_to_anon} = 1;
199         } else {
200             $ctx->{added_to_list} = $e->retrieve_container_biblio_record_entry_bucket($list);
201         }
202     } else { 
203         $e->xact_begin;
204         $ctx->{hold} = $e->retrieve_action_hold_request($hold_id);
205         $e->xact_rollback;
206     }
207
208     return Apache2::Const::OK;
209 }
210
211 sub load_kpac_config {
212     my $self = shift;
213     my $ctx = $self->ctx;
214
215     if (!$kpac_config) {
216         my $path = $self->apache->dir_config('KPacConfigFile');
217
218         if (!$path) {
219             $self->apache->log->error("KPacConfigFile required!");
220             return;
221         }
222         
223         $kpac_config = XMLin(
224             $path,
225             KeyAttr => ['id'],
226             ForceArray => ['layout', 'page', 'cell'],
227             NormaliseSpace => 2
228         );
229     }
230
231     my $ou = $ctx->{physical_loc} || $self->_get_search_lib;
232     my $layout;
233
234     # Search up the org tree to find the nearest config for the context org unit
235     while (my $org = $ctx->{get_aou}->($ou)) {
236         ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
237         last if $layout;
238         $ou = $org->parent_ou;
239     }
240
241     $ctx->{kpac_layout} = $layout;
242     $ctx->{kpac_config} = $kpac_config;
243     $ctx->{kpac_root} = $ctx->{base_path} . "/kpac"; 
244     $ctx->{home_page} = 'http://' . $self->apache->hostname . $ctx->{kpac_root} . "/home";
245 }
246
247
248 1;