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