]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGKPacLoader.pm
c0b1f5ea306acfa7a9c8e47fd25699e6b7d9d230
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGKPacLoader.pm
1 package OpenILS::WWW::EGKPacLoader;
2 use base 'OpenILS::WWW::EGCatLoader';
3 use strict; use warnings;
4 use XML::Simple;
5 use Apache2::Const -compile => qw(OK HTTP_BAD_REQUEST);
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 my $U = 'OpenILS::Application::AppUtils';
10 my $kpac_config;
11
12 # -----------------------------------------------------------------------------
13 # Override our parent's load() sub so we can do kpac-specific path routing.
14 # -----------------------------------------------------------------------------
15 sub load {
16     my $self = shift;
17
18     $self->init_ro_object_cache; 
19
20     my $stat = $self->load_common; 
21     return $stat unless $stat == Apache2::Const::OK;
22
23     $self->load_kpac_config;
24
25     my $path = $self->apache->path_info;
26     ($self->ctx->{page} = $path) =~ s#.*/(.*)#$1#g;
27
28     return $self->load_simple("home") if $path =~ m|kpac/home|;
29     return $self->load_simple("category") if $path =~ m|kpac/category|;
30     return $self->load_rresults if $path =~ m|kpac/results|;
31     return $self->load_record(no_search => 1) if $path =~ m|kpac/record|; 
32
33     # ----------------------------------------------------------------
34     #  Everything below here requires SSL
35     # ----------------------------------------------------------------
36     return $self->redirect_ssl unless $self->cgi->https;
37
38     return $self->load_getit_results if $path =~ m|kpac/getit_results|;
39     return $self->load_getit if $path =~ m|kpac/getit|;
40
41     # ----------------------------------------------------------------
42     #  Everything below here requires authentication
43     # ----------------------------------------------------------------
44     return $self->redirect_auth unless $self->editor->requestor;
45
46     # AUTH pages
47
48     return Apache2::Const::OK;
49 }
50
51 sub load_getit {
52     my $self = shift;
53     my $ctx = $self->ctx;
54     my $rec_id = $ctx->{page_args}->[0];
55     my $bbag_id = $self->cgi->param('bookbag');
56     my $action = $self->cgi->param('action') || '';
57
58     # first load the record
59     my $stat = $self->load_record(no_search => 1);
60     return $stat unless $stat == Apache2::Const::OK;
61
62     $self->ctx->{page} = 'getit'; # repair the page
63
64     return $self->save_item_to_bookbag($rec_id, $bbag_id) if $action eq 'save';
65
66     # if the user is logged in, fetch his bookbags
67     if ($ctx->{user}) {
68         $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket([
69             {   owner => $ctx->{user}->id, 
70                 btype => 'bookbag' 
71             }, 
72             {   order_by => {cbreb => 'name'},
73                 limit => $self->cgi->param('bbag_limit') || 100 
74             }
75         ]);
76     }
77
78     $self->ctx->{page} = 'getit'; # repair the page
79     return Apache2::Const::OK;
80 }
81
82 sub save_item_to_bookbag {
83     my $self = shift;
84     my $rec_id = shift;
85     my $bookbag_id = shift;
86
87     if ($bookbag_id) { 
88         # save to existing bookbag
89         $self->cgi->param('record', $rec_id);
90         my $stat = $self->load_myopac_bookbag_update('add_rec', $bookbag_id);
91         # TODO: check for failure
92         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
93         $new_uri .= ($new_uri =~ /\?/) ? "&list=$bookbag_id" : "?list=$bookbag_id";
94         return $self->generic_redirect($new_uri);
95
96     } else { 
97         # save to anonymous list
98        
99         # set some params assumed to exist for load_mylist_add
100         $self->cgi->param('record', $rec_id);
101         (my $new_uri = $self->apache->unparsed_uri) =~ s/getit/getit_results/g;
102         $new_uri .= ($new_uri =~ /\?/) ? '&list=anon' : '?list=anon';
103         $self->cgi->param('redirect_to', $new_uri);
104
105         return $self->load_mylist_add;
106     }
107
108     return Apache2::Const::HTTP_BAD_REQUEST;
109 }
110
111
112 sub load_getit_results {
113     my $self = shift;
114     my $ctx = $self->ctx;
115     my $e = $self->editor;
116     my $list = $self->cgi->param('list');
117     my $hold_id = $self->cgi->param('hold');
118     my $rec_id = $ctx->{page_args}->[0];
119
120     my (undef, @rec_data) = $self->get_records_and_facets([$rec_id]);
121     $ctx->{bre_id} = $rec_data[0]->{id};
122     $ctx->{marc_xml} = $rec_data[0]->{marc_xml};
123
124     if ($list) {
125         if ($list eq 'anon') {
126             $ctx->{anon_list} = 1;
127         } else {
128             $ctx->{list} = $e->retrieve_container_biblio_record_entry_bucket($list);
129         }
130
131     } elsif ($hold_id) {
132
133         # new hold means potential for replication lag
134         $e->xact_start; 
135         # fetch the hold...
136         $e->xact_rollback;
137     }
138
139     return Apache2::Const::OK;
140 }
141
142 sub load_kpac_config {
143     my $self = shift;
144
145     if (!$kpac_config) {
146         my $path = $self->apache->dir_config('KPacConfigFile');
147
148         if (!$path) {
149             $self->apache->log->error("KPacConfigFile required!");
150             return;
151         }
152         
153         $kpac_config = XMLin(
154             $path,
155             KeyAttr => ['id'],
156             ForceArray => ['layout', 'page', 'cell'],
157             NormaliseSpace => 2
158         );
159     }
160
161     my $ou = $self->ctx->{physical_loc} || $self->_get_search_lib;
162     my $layout;
163
164     # Search up the org tree to find the nearest config for the context org unit
165     while (my $org = $self->ctx->{get_aou}->($ou)) {
166         ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
167         last if $layout;
168         $ou = $org->parent_ou;
169     }
170
171     $self->ctx->{kpac_layout} = $layout;
172     $self->ctx->{kpac_config} = $kpac_config;
173     $self->ctx->{kpac_root} = $self->ctx->{base_path} . "/kpac"; 
174 }
175
176
177 1;