]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Container.pm
Template Toolkit OPAC
[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 use constant COOKIE_ANON_CACHE => 'anoncache';
11 use constant ANON_CACHE_MYLIST => 'mylist';
12
13 # Retrieve the users cached records AKA 'My List'
14 # Returns an empty list if there are no cached records
15 sub fetch_mylist {
16     my ($self, $with_marc_xml) = @_;
17
18     my $list = [];
19     my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE);
20
21     if($cache_key) {
22
23         $list = $U->simplereq(
24             'open-ils.actor',
25             'open-ils.actor.anon_cache.get_value', 
26             $cache_key, ANON_CACHE_MYLIST);
27
28         if(!$list) {
29             $cache_key = undef;
30             $list = [];
31         }
32     }
33
34     my $marc_xml;
35     if ($with_marc_xml) {
36         $marc_xml = $self->fetch_marc_xml_by_id($list);
37     }
38
39     return ($cache_key, $list, $marc_xml);
40 }
41
42
43 # Adds a record (by id) to My List, creating a new anon cache + list if necessary.
44 sub load_mylist_add {
45     my $self = shift;
46     my $rec_id = $self->cgi->param('record');
47
48     my ($cache_key, $list) = $self->fetch_mylist;
49     push(@$list, $rec_id);
50
51     $cache_key = $U->simplereq(
52         'open-ils.actor',
53         'open-ils.actor.anon_cache.set_value', 
54         $cache_key, ANON_CACHE_MYLIST, $list);
55
56     return $self->mylist_action_redirect($cache_key);
57 }
58
59 sub load_mylist_delete {
60     my $self = shift;
61     my $rec_id = $self->cgi->param('record');
62
63     my ($cache_key, $list) = $self->fetch_mylist;
64     $list = [ grep { $_ ne $rec_id } @$list ];
65
66     $cache_key = $U->simplereq(
67         'open-ils.actor',
68         'open-ils.actor.anon_cache.set_value', 
69         $cache_key, ANON_CACHE_MYLIST, $list);
70
71     return $self->mylist_action_redirect($cache_key);
72 }
73
74 sub load_mylist_move {
75     my $self = shift;
76     my @rec_ids = $self->cgi->param('record');
77     my $action = $self->cgi->param('action') || '';
78
79     return $self->load_myopac_bookbag_update('place_hold', undef, @rec_ids)
80         if $action eq 'place_hold';
81
82     my ($cache_key, $list) = $self->fetch_mylist;
83     return $self->mylist_action_redirect unless $cache_key;
84
85     my @keep;
86     foreach my $id (@$list) { push(@keep, $id) unless grep { $id eq $_ } @rec_ids; }
87
88     $cache_key = $U->simplereq(
89         'open-ils.actor',
90         'open-ils.actor.anon_cache.set_value', 
91         $cache_key, ANON_CACHE_MYLIST, \@keep
92     );
93
94     if ($self->ctx->{user} and $action =~ /^\d+$/) {
95         # in this case, action becomes list_id
96         $self->load_myopac_bookbag_update('add_rec', $self->cgi->param('action'));
97         # XXX TODO: test for failure of above
98     }
99
100     return $self->mylist_action_redirect($cache_key);
101 }
102
103 sub load_cache_clear {
104     my $self = shift;
105     $self->clear_anon_cache;
106     return $self->mylist_action_redirect;
107 }
108
109 # Wipes the entire anonymous cache, including My List
110 sub clear_anon_cache {
111     my $self = shift;
112     my $field = shift;
113
114     my $cache_key = $self->cgi->cookie(COOKIE_ANON_CACHE) or return;
115
116     $U->simplereq(
117         'open-ils.actor',
118         'open-ils.actor.anon_cache.delete_session', $cache_key)
119         if $cache_key;
120
121 }
122
123 # Called after an anon-cache / My List action occurs.  Redirect
124 # to the redirect_url (cgi param) or referrer or home.
125 sub mylist_action_redirect {
126     my $self = shift;
127     my $cache_key = shift;
128
129     my $url;
130     if( my $anchor = $self->cgi->param('anchor') ) {
131         # on the results page, we want to redirect 
132         # back to record that was affected
133         $url = $self->ctx->{referer};
134         $url =~ s/#.*|$/#$anchor/g;
135     } 
136
137     return $self->generic_redirect(
138         $url,
139         $self->cgi->cookie(
140             -name => COOKIE_ANON_CACHE,
141             -path => '/',
142             -value => ($cache_key) ? $cache_key : '',
143             -expires => ($cache_key) ? undef : '-1h'
144         )
145     );
146 }
147
148 sub load_mylist {
149     my ($self) = shift;
150     (undef, $self->ctx->{mylist}, $self->ctx->{mylist_marc_xml}) =
151         $self->fetch_mylist(1);
152
153     return Apache2::Const::OK;
154 }
155
156 1;