]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
Ability to add records to permanent bookbags in TPAC.
[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     my $marc_xml;
32     if ($with_marc_xml) {
33         $marc_xml = $self->fetch_marc_xml_by_id($list);
34     }
35
36     # Leverage QueryParser to sort the items by values of config.metabib_fields
37     # from the items' marc records.
38     if (@$list) {
39         my ($sorter, $modifier) = $self->_get_bookbag_sort_params("anonsort");
40         my $query = $self->_prepare_anonlist_sorting_query($list, $sorter, $modifier);
41         my $args = {
42             "limit" => 1000,
43             "offset" => 0
44         };
45         $list = $U->bib_record_list_via_search($query, $args) or
46             return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
47     }
48
49     return ($cache_key, $list, $marc_xml);
50 }
51
52
53 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
54 sub load_mylist_add {
55     my $self = shift;
56     my $rec_id = $self->cgi->param('record');
57
58     my ($cache_key, $list) = $self->fetch_mylist;
59     push(@$list, $rec_id);
60
61     $cache_key = $U->simplereq(
62         'open-ils.actor',
63         'open-ils.actor.anon_cache.set_value', 
64         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
65
66     # Check if we need to warn patron about adding to a "temporary"
67     # list:
68     if ($self->check_for_temp_list_warning) {
69         return $self->mylist_warning_redirect($cache_key);
70     }
71
72     return $self->mylist_action_redirect($cache_key);
73 }
74
75 sub load_mylist_delete {
76     my $self = shift;
77     my $rec_id = $self->cgi->param('record');
78
79     my ($cache_key, $list) = $self->fetch_mylist;
80     $list = [ grep { $_ ne $rec_id } @$list ];
81
82     $cache_key = $U->simplereq(
83         'open-ils.actor',
84         'open-ils.actor.anon_cache.set_value', 
85         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
86
87     return $self->mylist_action_redirect($cache_key);
88 }
89
90 sub load_mylist_move {
91     my $self = shift;
92     my @rec_ids = $self->cgi->param('record');
93     my $action = $self->cgi->param('action') || '';
94
95     return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
96         if $action eq 'place_hold';
97
98     my ($cache_key, $list) = $self->fetch_mylist;
99     return $self->mylist_action_redirect unless $cache_key;
100
101     my @keep;
102     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
103
104     $cache_key = $U->simplereq(
105         'open-ils.actor',
106         'open-ils.actor.anon_cache.set_value', 
107         $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
108     );
109
110     if ($self->ctx->{user} and $action =~ /^\d+$/) {
111         # in this case, action becomes list_id
112         $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
113         # XXX TODO: test for failure of above
114     }
115
116     return $self->mylist_action_redirect($cache_key);
117 }
118
119 sub load_cache_clear {
120     my $self = shift;
121     $self->clear_anon_cache;
122     return $self->mylist_action_redirect;
123 }
124
125 # Wipes the entire anonymous cache, including My List
126 sub clear_anon_cache {
127     my $self = shift;
128     my $field = shift;
129
130     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
131
132     $U->simplereq(
133         'open-ils.actor',
134         'open-ils.actor.anon_cache.delete_session', $cache_key)
135         if $cache_key;
136
137 }
138
139 # Called after an anon-cache / My List action occurs.  Redirect
140 # to the redirect_url (cgi param) or referrer or home.
141 sub mylist_action_redirect {
142     my $self = shift;
143     my $cache_key = shift;
144
145     my $url;
146     if( my $anchor = $self->cgi->param('anchor') ) {
147         # on the results page, we want to redirect 
148         # back to record that was affected
149         $url = $self->ctx->{referer};
150         $url =~ s/#.*|$/#$anchor/;
151     } 
152
153     return $self->generic_redirect(
154         $url,
155         $self->cgi->cookie(
156             -name => (ref $self)->COOKIE_ANON_CACHE,
157             -path => '/',
158             -value => ($cache_key) ? $cache_key : '',
159             -expires => ($cache_key) ? undef : '-1h'
160         )
161     );
162 }
163
164 # called after an anon-cache / my list addition when we are configured
165 # to show a warning to the user.
166
167 sub mylist_warning_redirect {
168     my $self = shift;
169     my $cache_key = shift;
170
171     my $base_url = sprintf(
172         "%s://%s%s/temp_warn",
173         $self->cgi->https ? 'https' : 'http',
174         $self->apache->hostname,
175         $self->ctx->{opac_root}
176     );
177
178     my $redirect = $self->ctx->{referer};
179     if (my $anchor = $self->cgi->param('anchor')) {
180         $redirect =~ s/#.*|$/#$anchor/;
181     }
182
183     $base_url .= '?redirect_to=' . uri_escape($redirect);
184
185     return $self->generic_redirect(
186         $base_url,
187         $self->cgi->cookie(
188             -name => (ref $self)->COOKIE_ANON_CACHE,
189             -path => '/',
190             -value => ($cache_key) ? $cache_key : '',
191             -expires => ($cache_key) ? undef : '-1h'
192         )
193     );
194 }
195
196 sub load_mylist {
197     my ($self) = shift;
198     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
199         $self->fetch_mylist(1);
200
201     return Apache2::Const::OK;
202 }
203
204 sub load_temp_warn_post {
205     my $self = shift;
206     my $url = $self->cgi->param('redirect_to');
207     return $self->generic_redirect(
208         $url,
209         $self->cgi->cookie(
210             -name => 'no_temp_list_warn',
211             -path => '/',
212             -value => ($self->cgi->param('no_temp_list_warn')) ? '1' : '',
213             -expires => ($self->cgi->param('no_temp_list_warn')) ? undef : '-1h'
214         )
215     );
216 }
217
218 sub load_temp_warn {
219     my $self = shift;
220     $self->ctx->{'redirect_to'} = $self->cgi->param('redirect_to');
221     return Apache2::Const::OK;
222 }
223
224 1;