]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGKPacLoader.pm
5c80f92ea37c0c65486b6d49d07935f3b38c2206
[working/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_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_getit {
52     my $self = shift;
53     my $ctx = $self->ctx;
54     my $rec_id = $ctx->{page_args}->[0];
55     my $bbag_id = $self->cgi->param('bookbag');
56     my $action = $self->cgi->param('action') || '';
57
58     # first load the record
59     my $stat = $self->load_record(no_search => 1);
60     return $stat unless $stat == Apache2::Const::OK;
61
62     $self->ctx->{page} = 'getit'; # repair the page
63
64     return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
65     return $self->login_and_place_hold($rec_id) if $action eq 'hold';
66
67     # if the user is logged in, fetch his bookbags
68     if ($ctx->{user}) {
69         $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
70             {   owner => $ctx->{user}->id, 
71                 btype => 'bookbag' 
72             }, 
73             {   order_by => {cbreb => 'name'},
74                 limit => $self->cgi->param('bbag_limit') || 100 
75             }
76         ]);
77     }
78
79     $self->ctx->{page} = 'getit'; # repair the page
80     return Apache2::Const::OK;
81 }
82     
83 sub login_and_place_hold {
84     my $self = shift;
85     my $bre_id = shift;
86     my $ctx = $self->ctx;
87     my $username = $self->cgi->param('username');
88     my $password = $self->cgi->param('password');
89     my $pickup_lib = $self->cgi->param('pickup_lib');
90
91     return Apache2::Const::HTTP_BAD_REQUEST 
92         unless $pickup_lib =~ /^\d+$/;
93
94     my $new_uri = $self->apache->unparsed_uri;
95     my $sep = ($new_uri =~ /\?/) ? '&' : '?';
96
97     if (!$ctx->{user}) {
98         # First, log the user in and return to 
99         $self->apache->log->info("kpac: logging in $username");
100
101         # TODO: let user know username/password is required..
102         return Apache2::Const::OK unless $username and $password;
103
104         $new_uri .= "${sep}pickup_lib=$pickup_lib&action=hold";
105         $self->cgi->param('redirect_to', $new_uri);
106         return $self->load_login;
107
108     } else {
109
110         $self->apache->log->info("kpac: placing hold for $bre_id");
111
112         $new_uri =~ s/getit/getit_results/g;
113         $self->cgi->param('hold_target', $bre_id);
114         $self->cgi->param('hold_type', 'T');
115         $self->cgi->param('part', ''); # needed even if unused
116
117         my $stat = $self->load_place_hold;
118
119         $self->apache->log->info("kpac: place hold returned $stat");
120
121         return $stat unless $stat == Apache2::Const::OK;
122
123         my $hdata = $ctx->{hold_data}->[0]; # only 1 hold placed
124         if (my $hold_id = $hdata ? $hdata->{hold_success} : undef) {
125
126             $self->apache->log->info("kpac: place hold succeeded");
127             $new_uri .= "${sep}hold=$hold_id";
128
129         } else {
130             $self->apache->log->info("kpac: place hold failed : " . $ctx->{hold_failed_event});
131             $new_uri .= "${sep}hold_failed=1";
132         }
133     }
134
135     $self->apache->log->info("kpac: place hold redirecting to: $new_uri");
136     return $self->generic_redirect($new_uri);
137 }
138
139 sub save_item_to_bookbag {
140     my $self = shift;
141     my $rec_id = shift;
142     my $bookbag_id = shift;
143
144     if ($bookbag_id) { 
145         # save to existing bookbag
146         $self->cgi->param('record', $rec_id);
147         my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
148         # TODO: check for failure
149         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
150         $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
151         return $self->generic_redirect($new_uri);
152
153     } else { 
154         # save to anonymous list
155        
156         # set some params assumed to exist for load_mylist_add
157         $self->cgi->param('record', $rec_id);
158         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
159         $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
160         $self->cgi->param('redirect_to', $new_uri);
161
162         return $self->load_mylist_add;
163     }
164
165     return Apache2::Const::HTTP_BAD_REQUEST;
166 }
167
168
169 sub load_getit_results {
170     my $self = shift;
171     my $ctx = $self->ctx;
172     my $e = $self->editor;
173     my $list = $self->cgi->param('list');
174     my $hold_id = $self->cgi->param('hold');
175     my $rec_id = $ctx->{page_args}->[0];
176
177     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
178     $ctx->{bre_id} = $rec_data[0]->{id};
179     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
180
181     if ($list) {
182         if ($list eq 'anon') {
183             $ctx->{added_to_anon} = 1;
184         } else {
185             $ctx->{added_to_list} = $e->retrieve_container_biblio_record_entry_bucket($list);
186         }
187     } else { 
188         $e->xact_begin;
189         $ctx->{hold} = $e->retrieve_action_hold_request(8);
190         $e->xact_rollback;
191     }
192
193     return Apache2::Const::OK;
194 }
195
196 sub load_kpac_config {
197     my $self = shift;
198     my $ctx = $self->ctx;
199
200     if (!$kpac_config) {
201         my $path = $self->apache->dir_config('KPacConfigFile');
202
203         if (!$path) {
204             $self->apache->log->error("KPacConfigFile required!");
205             return;
206         }
207         
208         $kpac_config = XMLin(
209             $path,
210             KeyAttr => ['id'],
211             ForceArray => ['layout', 'page', 'cell'],
212             NormaliseSpace => 2
213         );
214     }
215
216     my $ou = $ctx->{physical_loc} || $self->_get_search_lib;
217     my $layout;
218
219     # Search up the org tree to find the nearest config for the context org unit
220     while (my $org = $ctx->{get_aou}->($ou)) {
221         ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
222         last if $layout;
223         $ou = $org->parent_ou;
224     }
225
226     $ctx->{kpac_layout} = $layout;
227     $ctx->{kpac_config} = $kpac_config;
228     $ctx->{kpac_root} = $ctx->{base_path} . "/kpac"; 
229     $ctx->{home_page} = 'http://' . $self->apache->hostname . $ctx->{kpac_root} . "/home";
230 }
231
232
233 1;