]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGKPacLoader.pm
76223742477fe460c77787a5d61aeb915a2e01f5
[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 DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT 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_simple("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
55     # first load the record
56     my $stat = $self->load_record(no_search => 1);
57     return $stat unless $stat == Apache2::Const::OK;
58
59     $self->ctx->{page} = 'getit'; # repair the page
60
61     # if the user is logged in, fetch his bookbags
62     if ($ctx->{user}) {
63         $ctx->{bookbags} = $self->editor->search_container_biblio_record_entry_bucket(
64             [{
65                     owner => $ctx->{user}->id, 
66                     btype => 'bookbag'
67                 }, {
68                     order_by => {cbreb => 'name'},
69                     limit => $self->cgi->param('bbag_limit') || 100,
70             }],
71             {substream => 1}
72         );
73     }
74
75     $self->ctx->{page} = 'getit'; # repair the page
76     return Apache2::Const::OK;
77 }
78
79
80 sub load_kpac_config {
81     my $self = shift;
82
83     if (!$kpac_config) {
84         my $path = $self->apache->dir_config('KPacConfigFile');
85
86         if (!$path) {
87             $self->apache->log->error("KPacConfigFile required!");
88             return;
89         }
90         
91         $kpac_config = XMLin(
92             $path,
93             KeyAttr => ['id'],
94             ForceArray => ['layout', 'page', 'cell'],
95             NormaliseSpace => 2
96         );
97     }
98
99     my $ou = $self->ctx->{physical_loc} || $self->_get_search_lib;
100     my $layout;
101
102     # Search up the org tree to find the nearest config for the context org unit
103     while (my $org = $self->ctx->{get_aou}->($ou)) {
104         ($layout) = grep {$_->{owner} eq $org->id} @{$kpac_config->{layout}};
105         last if $layout;
106         $ou = $org->parent_ou;
107     }
108
109     $self->ctx->{kpac_layout} = $layout;
110     $self->ctx->{kpac_config} = $kpac_config;
111     $self->ctx->{kpac_root} = $self->ctx->{base_path} . "/kpac"; 
112 }
113
114
115 1;