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