]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[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     return $self->mylist_action_redirect($cache_key);
63 }
64
65 sub load_mylist_delete {
66     my $self = shift;
67     my $rec_id = $self->cgi->param('record');
68
69     my ($cache_key, $list) = $self->fetch_mylist;
70     $list = [ grep { $_ ne $rec_id } @$list ];
71
72     $cache_key = $U->simplereq(
73         'open-ils.actor',
74         'open-ils.actor.anon_cache.set_value', 
75         $cache_key, (ref $self)->ANON_CACHE_MYLIST, $list);
76
77     return $self->mylist_action_redirect($cache_key);
78 }
79
80 sub load_mylist_move {
81     my $self = shift;
82     my @rec_ids = $self->cgi->param('record');
83     my $action = $self->cgi->param('action') || '';
84
85     return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
86         if $action eq 'place_hold';
87
88     my ($cache_key, $list) = $self->fetch_mylist;
89     return $self->mylist_action_redirect unless $cache_key;
90
91     my @keep;
92     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
93
94     $cache_key = $U->simplereq(
95         'open-ils.actor',
96         'open-ils.actor.anon_cache.set_value', 
97         $cache_key, (ref $self)->ANON_CACHE_MYLIST, \@keep
98     );
99
100     if ($self->ctx->{user} and $action =~ /^\d+$/) {
101         # in this case, action becomes list_id
102         $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
103         # XXX TODO: test for failure of above
104     }
105
106     return $self->mylist_action_redirect($cache_key);
107 }
108
109 sub load_cache_clear {
110     my $self = shift;
111     $self->clear_anon_cache;
112     return $self->mylist_action_redirect;
113 }
114
115 # Wipes the entire anonymous cache, including My List
116 sub clear_anon_cache {
117     my $self = shift;
118     my $field = shift;
119
120     my $cache_key = $self->cgi->cookie((ref $self)->COOKIE_ANON_CACHE) or return;
121
122     $U->simplereq(
123         'open-ils.actor',
124         'open-ils.actor.anon_cache.delete_session', $cache_key)
125         if $cache_key;
126
127 }
128
129 # Called after an anon-cache / My List action occurs.  Redirect
130 # to the redirect_url (cgi param) or referrer or home.
131 sub mylist_action_redirect {
132     my $self = shift;
133     my $cache_key = shift;
134
135     my $url;
136     if( my $anchor = $self->cgi->param('anchor') ) {
137         # on the results page, we want to redirect 
138         # back to record that was affected
139         $url = $self->ctx->{referer};
140         $url =~ s/#.*|$/#$anchor/;
141     } 
142
143     return $self->generic_redirect(
144         $url,
145         $self->cgi->cookie(
146             -name => (ref $self)->COOKIE_ANON_CACHE,
147             -path => '/',
148             -value => ($cache_key) ? $cache_key : '',
149             -expires => ($cache_key) ? undef : '-1h'
150         )
151     );
152 }
153
154 sub load_mylist {
155     my ($self) = shift;
156     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
157         $self->fetch_mylist(1);
158
159     return Apache2::Const::OK;
160 }
161
162 1;