]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
LP#1452352: fix generation of locale-specific RO object fetchers
[working/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) = @_;
14
15     my $list = [];
16     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_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)->ANON_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) {
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
77 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
78 sub load_mylist_add {
79     my $self = shift;
80     my $rec_id = $self->cgi->param('record');
81
82     my ($cache_key, $list) = $self->fetch_mylist;
83     push(@$list, $rec_id);
84
85     $cache_key = $U->simplereq(
86         'open-ils.actor',
87         'open-ils.actor.anon_cache.set_value', 
88         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
89
90     # Check if we need to warn patron about adding to a "temporary"
91     # list:
92     if ($self->check_for_temp_list_warning) {
93         return $self->mylist_warning_redirect($cache_key);
94     }
95
96     return $self->mylist_action_redirect($cache_key);
97 }
98
99 sub load_mylist_delete {
100     my $self = shift;
101     my $rec_id = $self->cgi->param('record');
102
103     my ($cache_key, $list) = $self->fetch_mylist;
104     $list = [ grep { $_ ne $rec_id } @$list ];
105
106     $cache_key = $U->simplereq(
107         'open-ils.actor',
108         'open-ils.actor.anon_cache.set_value', 
109         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
110
111     return $self->mylist_action_redirect($cache_key);
112 }
113
114 sub load_mylist_move {
115     my $self = shift;
116     my @rec_ids = $self->cgi->param('record');
117     my $action = $self->cgi->param('action') || '';
118
119     return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
120         if $action eq 'place_hold';
121
122     my ($cache_key, $list) = $self->fetch_mylist;
123     return $self->mylist_action_redirect unless $cache_key;
124
125     my @keep;
126     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
127
128     $cache_key = $U->simplereq(
129         'open-ils.actor',
130         'open-ils.actor.anon_cache.set_value', 
131         $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
132     );
133
134     if ($self->ctx->{user} and $action =~ /^\d+$/) {
135         # in this case, action becomes list_id
136         $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
137         # XXX TODO: test for failure of above
138     }
139
140     return $self->mylist_action_redirect($cache_key);
141 }
142
143 sub load_cache_clear {
144     my $self = shift;
145     $self->clear_anon_cache;
146     return $self->mylist_action_redirect;
147 }
148
149 # Wipes the entire anonymous cache, including My List
150 sub clear_anon_cache {
151     my $self = shift;
152     my $field = shift;
153
154     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
155
156     $U->simplereq(
157         'open-ils.actor',
158         'open-ils.actor.anon_cache.delete_session', $cache_key)
159         if $cache_key;
160
161 }
162
163 # Called after an anon-cache / My List action occurs.  Redirect
164 # to the redirect_url (cgi param) or referrer or home.
165 sub mylist_action_redirect {
166     my $self = shift;
167     my $cache_key = shift;
168
169     my $url;
170     if( my $anchor = $self->cgi->param('anchor') ) {
171         # on the results page, we want to redirect 
172         # back to record that was affected
173         $url = $self->ctx->{referer};
174         $url =~ s/#.*|$/#$anchor/;
175     } 
176
177     return $self->generic_redirect(
178         $url,
179         $self->cgi->cookie(
180             -name => (ref $self)->COOKIE_ANON_CACHE,
181             -path => '/',
182             -value => ($cache_key) ? $cache_key : '',
183             -expires => ($cache_key) ? undef : '-1h'
184         )
185     );
186 }
187
188 # called after an anon-cache / my list addition when we are configured
189 # to show a warning to the user.
190
191 sub mylist_warning_redirect {
192     my $self = shift;
193     my $cache_key = shift;
194
195     my $base_url = sprintf(
196         "%s://%s%s/temp_warn",
197         $self->ctx->{proto},
198         $self->ctx->{hostname},
199         $self->ctx->{opac_root}
200     );
201
202     my $redirect = $self->ctx->{referer};
203     if (my $anchor = $self->cgi->param('anchor')) {
204         $redirect =~ s/#.*|$/#$anchor/;
205     }
206
207     $base_url .= '?redirect_to=' . uri_escape_utf8($redirect);
208
209     return $self->generic_redirect(
210         $base_url,
211         $self->cgi->cookie(
212             -name => (ref $self)->COOKIE_ANON_CACHE,
213             -path => '/',
214             -value => ($cache_key) ? $cache_key : '',
215             -expires => ($cache_key) ? undef : '-1h'
216         )
217     );
218 }
219
220 sub load_mylist {
221     my ($self) = shift;
222     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
223         $self->fetch_mylist(1);
224
225     return Apache2::Const::OK;
226 }
227
228 sub load_temp_warn_post {
229     my $self = shift;
230     my $url = $self->cgi->param('redirect_to');
231     return $self->generic_redirect(
232         $url,
233         $self->cgi->cookie(
234             -name => 'no_temp_list_warn',
235             -path => '/',
236             -value => ($self->cgi->param('no_temp_list_warn')) ? '1' : '',
237             -expires => ($self->cgi->param('no_temp_list_warn')) ? undef : '-1h'
238         )
239     );
240 }
241
242 sub load_temp_warn {
243     my $self = shift;
244     $self->ctx->{'redirect_to'} = $self->cgi->param('redirect_to');
245     return Apache2::Const::OK;
246 }
247
248 1;