]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Container.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
9
10 # Retrieve the users cached records AKA 'My List'
11 # Returns an empty list if there are no cached records
12 sub fetch_mylist {
13     my ($self, $with_marc_xml, $skip_sort) = @_;
14
15     my $list = [];
16     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
17
18     if($cache_key) {
19
20         $list = $U->simplereq(
21             'open-ils.actor',
22             'open-ils.actor.anon_cache.get_value', 
23             $cache_key, (ref $self)->CART_CACHE_MYLIST);
24
25         if(!$list) {
26             $cache_key = undef;
27             $list = [];
28         }
29     }
30
31     {   # sanitize
32         no warnings qw/numeric/;
33         $list = [map { int $_ } @$list];
34         $list = [grep { $_ > 0} @$list];
35     };
36
37     my $marc_xml;
38     if ($with_marc_xml) {
39         my $ctx = $self->ctx;
40
41         # capture pref_ou for callnumber filter/display
42         $ctx->{pref_ou} = $self->_get_pref_lib() || $ctx->{search_ou};
43
44         # search for local callnumbers for display
45         my $focus_ou = $ctx->{physical_loc} || $ctx->{pref_ou};
46
47         my (undef, @recs) = $self->get_records_and_facets(
48             $list, undef, {
49                 flesh => '{mra,holdings_xml,acp,exclude_invisible_acn}',
50                 flesh_depth => 1,
51                 site => $ctx->{get_aou}->($focus_ou)->shortname,
52                 pref_lib => $ctx->{pref_ou}
53             }
54         );
55
56         # make it look like the caller is expecting
57         $marc_xml = { map {$_->{id} => $_->{marc_xml}} @recs };
58     }
59
60     # Leverage QueryParser to sort the items by values of config.metabib_fields
61     # from the items' marc records.
62     if (@$list && !$skip_sort) {
63         my ($sorter, $modifier) = $self->_get_bookbag_sort_params("anonsort");
64         my $query = $self->_prepare_anonlist_sorting_query($list, $sorter, $modifier);
65         my $args = {
66             "limit" => 1000,
67             "offset" => 0
68         };
69         $list = $U->bib_record_list_via_search($query, $args) or
70             return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
71     }
72
73     return ($cache_key, $list, $marc_xml);
74 }
75
76 sub load_api_mylist_retrieve {
77     my $self = shift;
78
79     # this has the effect of instantiating an empty one if need be
80     my ($cache_key, $list) = $self->fetch_mylist(0, 1);
81
82     $self->ctx->{json_response} = {
83         mylist => [ map { 0 + $_ } @$list ], # force integers
84     };
85     $self->ctx->{json_reponse_cookie} =
86         $self->cgi->cookie(
87             -name => (ref $self)->COOKIE_CART_CACHE,
88             -path => '/',
89             -value => ($cache_key) ? $cache_key : '',
90             -expires => ($cache_key) ? undef : '-1h'
91         );
92
93     return Apache2::Const::OK;
94 }
95
96 sub load_api_mylist_clear {
97     my $self = shift;
98
99     $self->clear_anon_cache;
100
101     # and return fresh, empty cart
102     return $self->load_api_mylist_retrieve();
103 }
104
105 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
106 sub load_mylist_add {
107     my $self = shift;
108
109     my ($cache_key, $list) = $self->_do_mylist_add();
110
111     # Check if we need to warn patron about adding to a "temporary"
112     # list:
113     if ($self->check_for_temp_list_warning) {
114         return $self->mylist_warning_redirect($cache_key);
115     }
116
117     return $self->mylist_action_redirect($cache_key);
118 }
119
120 sub load_api_mylist_add {
121     my $self = shift;
122
123     my ($cache_key, $list) = $self->_do_mylist_add();
124
125     $self->ctx->{json_response} = {
126         mylist => [ map { 0 + $_ } @$list ], # force integers
127     };
128     $self->ctx->{json_reponse_cookie} =
129         $self->cgi->cookie(
130             -name => (ref $self)->COOKIE_CART_CACHE,
131             -path => '/',
132             -value => ($cache_key) ? $cache_key : '',
133             -expires => ($cache_key) ? undef : '-1h'
134         );
135
136     return Apache2::Const::OK;
137 }
138
139 sub _do_mylist_add {
140     my $self = shift;
141     my @rec_ids = $self->cgi->param('record');
142
143     my ($cache_key, $list) = $self->fetch_mylist(0, 1);
144     push(@$list, @rec_ids);
145
146     $cache_key = $U->simplereq(
147         'open-ils.actor',
148         'open-ils.actor.anon_cache.set_value', 
149         $cache_key, (ref $self)->CART_CACHE_MYLIST, $list);
150
151     return ($cache_key, $list);
152 }
153
154 sub load_mylist_delete {
155     my $self = shift;
156
157     my ($cache_key, $list) = $self->_do_mylist_delete;
158
159     return $self->mylist_action_redirect($cache_key);
160 }
161
162 sub load_api_mylist_delete {
163     my $self = shift;
164
165     my ($cache_key, $list) = $self->_do_mylist_delete();
166
167     $self->ctx->{json_response} = {
168         mylist => [ map { 0 + $_ } @$list ], # force integers
169     };
170     $self->ctx->{json_reponse_cookie} =
171         $self->cgi->cookie(
172             -name => (ref $self)->COOKIE_CART_CACHE,
173             -path => '/',
174             -value => ($cache_key) ? $cache_key : '',
175             -expires => ($cache_key) ? undef : '-1h'
176         );
177
178     return Apache2::Const::OK;
179 }
180
181 sub _do_mylist_delete {
182     my $self = shift;
183     my @rec_ids = $self->cgi->param('record');
184
185     my ($cache_key, $list) = $self->fetch_mylist(0, 1);
186     foreach my $rec_id (@rec_ids) {
187         $list = [ grep { $_ ne $rec_id } @$list ];
188     }
189
190     $cache_key = $U->simplereq(
191         'open-ils.actor',
192         'open-ils.actor.anon_cache.set_value', 
193         $cache_key, (ref $self)->CART_CACHE_MYLIST, $list);
194
195     return ($cache_key, $list);
196 }
197
198 sub load_mylist_print {
199     my $self = shift;
200
201     my $cache_key = shift // $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
202
203     if (!$cache_key) {
204         return $self->generic_redirect;
205     }
206
207     my $url = sprintf(
208         "%s://%s%s/record/print_preview/%s",
209         $self->ctx->{proto},
210         $self->ctx->{hostname},
211         $self->ctx->{opac_root},
212         $cache_key,
213     );
214
215     my $redirect = $self->cgi->param('redirect_to');
216     $url .= '?redirect_to=' . uri_escape_utf8($redirect);
217     my $clear_cart = $self->cgi->param('clear_cart');
218     $url .= '&clear_cart=1' if $clear_cart;
219     my $sort = $self->cgi->param('sort') || $self->cgi->param('anonsort');
220     my $sort_dir = $self->cgi->param('sort_dir');
221     $url .= '&sort='.$sort if $sort;
222     $url .= '&sort_dir='.$sort_dir if $sort_dir;
223     $url .= '&is_list=1';
224
225     return $self->generic_redirect($url);
226 }
227
228 sub load_mylist_email {
229     my $self = shift;
230
231     my $cache_key = shift // $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE);
232
233     if (!$cache_key) {
234         return $self->generic_redirect;
235     }
236
237     my $url = sprintf(
238         "%s://%s%s/record/email_preview/%s",
239         $self->ctx->{proto},
240         $self->ctx->{hostname},
241         $self->ctx->{opac_root},
242         $cache_key,
243     );
244
245     my $redirect = $self->cgi->param('redirect_to');
246     $url .= '?redirect_to=' . uri_escape_utf8($redirect);
247     my $clear_cart = $self->cgi->param('clear_cart');
248     $url .= '&clear_cart=1' if $clear_cart;
249     my $sort = $self->cgi->param('sort') || $self->cgi->param('anonsort');
250     my $sort_dir = $self->cgi->param('sort_dir');
251     $url .= '&sort='.$sort if $sort;
252     $url .= '&sort_dir='.$sort_dir if $sort_dir;
253     $url .= '&is_list=1';
254
255     return $self->generic_redirect($url);
256 }
257
258 sub _stash_record_list_in_anon_cache {
259     my $self = shift;
260     my @rec_ids = @_;
261
262     my $cache_key = $U->simplereq(
263         'open-ils.actor',
264         'open-ils.actor.anon_cache.set_value',
265         undef, (ref $self)->CART_CACHE_MYLIST, [ @rec_ids ]);
266     return $cache_key;
267 }
268
269 sub load_mylist_move {
270     my $self = shift;
271     my @rec_ids = $self->cgi->param('record');
272     my $action = $self->cgi->param('action') || '';
273
274     my ($cache_key, $list) = $self->fetch_mylist;
275
276     unless ((scalar(@rec_ids) > 0) ||
277         ($self->cgi->param('entire_list') && scalar(@$list) > 0)) {
278         my $url = $self->ctx->{referer};
279         $url .= ($url =~ /\?/ ? '&' : '?') . 'cart_none_selected=1';
280         return $self->generic_redirect($url);
281     }
282
283     if ($action eq 'place_hold') {
284         if ($self->cgi->param('entire_list')) {
285             @rec_ids = @$list;
286         }
287         return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids);
288     }
289     if ($action eq 'print') {
290         if ($self->cgi->param('entire_list')) {
291             @rec_ids = @$list;
292         }
293         my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
294         return $self->load_mylist_print($temp_cache_key);
295     }
296     if ($action eq 'email') {
297         if ($self->cgi->param('entire_list')) {
298             @rec_ids = @$list;
299         }
300         my $temp_cache_key = $self->_stash_record_list_in_anon_cache(@rec_ids);
301         return $self->load_mylist_email($temp_cache_key);
302     }
303     if ($action eq 'new_list') {
304         my $url = $self->apache->unparsed_uri;
305         $url =~ s!/mylist/move!/myopac/lists!;
306         return $self->generic_redirect($url);
307     }
308
309     return $self->mylist_action_redirect unless $cache_key;
310
311     my @keep;
312     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
313
314     $cache_key = $U->simplereq(
315         'open-ils.actor',
316         'open-ils.actor.anon_cache.set_value', 
317         $cache_key, (ref $self)->CART_CACHE_MYLIST, \@keep
318     );
319
320     if ($action eq 'delete' && scalar(@keep) == 0) {
321         my $url = $self->cgi->param('orig_referrer') // $self->ctx->{referer};
322         return $self->generic_redirect($url);
323     }
324
325     if ($self->ctx->{user} and $action =~ /^\d+$/) {
326         # in this case, action becomes list_id
327         $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
328         # XXX TODO: test for failure of above
329     }
330
331     return $self->mylist_action_redirect($cache_key);
332 }
333
334 sub load_cache_clear {
335     my $self = shift;
336     $self->clear_anon_cache;
337     return $self->mylist_action_redirect;
338 }
339
340 # Wipes the entire anonymous cache, including My List
341 sub clear_anon_cache {
342     my $self = shift;
343     my $field = shift;
344
345     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_CART_CACHE) or return;
346
347     $U->simplereq(
348         'open-ils.actor',
349         'open-ils.actor.anon_cache.delete_session', $cache_key)
350         if $cache_key;
351
352 }
353
354 # Called after an anon-cache / My List action occurs.  Redirect
355 # to the redirect_url (cgi param) or referrer or home.
356 sub mylist_action_redirect {
357     my $self = shift;
358     my $cache_key = shift;
359
360     my $url;
361     if( my $anchor = $self->cgi->param('anchor') ) {
362         # on the results page, we want to redirect 
363         # back to record that was affected
364         $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
365         $url =~ s/#.*|$/#$anchor/;
366     } else {
367         $url = $self->cgi->param('redirect_to') // $self->ctx->{referer};
368     }
369
370     return $self->generic_redirect(
371         $url,
372         $self->cgi->cookie(
373             -name => (ref $self)->COOKIE_CART_CACHE,
374             -path => '/',
375             -value => ($cache_key) ? $cache_key : '',
376             -expires => ($cache_key) ? undef : '-1h'
377         )
378     );
379 }
380
381 # called after an anon-cache / my list addition when we are configured
382 # to show a warning to the user.
383
384 sub mylist_warning_redirect {
385     my $self = shift;
386     my $cache_key = shift;
387
388     my $base_url = sprintf(
389         "%s://%s%s/temp_warn",
390         $self->ctx->{proto},
391         $self->ctx->{hostname},
392         $self->ctx->{opac_root}
393     );
394
395     my $redirect = $self->ctx->{referer};
396     if (my $anchor = $self->cgi->param('anchor')) {
397         $redirect =~ s/#.*|$/#$anchor/;
398     }
399
400     $base_url .= '?redirect_to=' . uri_escape_utf8($redirect);
401
402     return $self->generic_redirect(
403         $base_url,
404         $self->cgi->cookie(
405             -name => (ref $self)->COOKIE_CART_CACHE,
406             -path => '/',
407             -value => ($cache_key) ? $cache_key : '',
408             -expires => ($cache_key) ? undef : '-1h'
409         )
410     );
411 }
412
413 sub load_mylist {
414     my ($self) = shift;
415     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
416         $self->fetch_mylist(1);
417
418     # get list of bookbags in case user wants to move cart contents to
419     # one
420     if ($self->ctx->{user}) {
421         $self->_load_lists_and_settings;
422     }
423
424     return Apache2::Const::OK;
425 }
426
427 sub load_temp_warn_post {
428     my $self = shift;
429     my $url = $self->cgi->param('redirect_to');
430     return $self->generic_redirect(
431         $url,
432         $self->cgi->cookie(
433             -name => 'no_temp_list_warn',
434             -path => '/',
435             -value => ($self->cgi->param('no_temp_list_warn')) ? '1' : '',
436             -expires => ($self->cgi->param('no_temp_list_warn')) ? undef : '-1h'
437         )
438     );
439 }
440
441 sub load_temp_warn {
442     my $self = shift;
443     $self->ctx->{'redirect_to'} = $self->cgi->param('redirect_to');
444     return Apache2::Const::OK;
445 }
446
447 1;