]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/Reporter.pm
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / Reporter.pm
1 package OpenILS::WWW::Reporter;
2 use strict; use warnings;
3
4 use vars qw/$dtype_xform_map $dtype_xform/;
5
6 use Apache2::Log;
7 use Apache2::Const -compile => qw(OK REDIRECT DECLINED :log);
8 use APR::Const    -compile => qw(:error SUCCESS);
9 use Apache2::RequestRec ();
10 use Apache2::RequestIO ();
11 use Apache2::RequestUtil;
12 use CGI;
13 use Data::Dumper;
14
15 use Template;
16
17 use OpenSRF::EX qw(:try);
18 use OpenSRF::System;
19 use XML::LibXML;
20
21 use OpenSRF::Utils::SettingsParser;
22 use OpenILS::Utils::Fieldmapper;
23 use OpenILS::WWW::Reporter::transforms;
24
25
26 # set the bootstrap config and template include directory when 
27 # this module is loaded
28 my $bootstrap;
29 my $includes = [];  
30 my $base_xml;
31 #my $base_xml_doc;
32
33 sub import {
34     my( $self, $bs_config, $core_xml, @incs ) = @_;
35     $bootstrap = $bs_config;
36     $base_xml = $core_xml;
37     $includes = [ @incs ];
38 }
39
40
41 # our templates plugins are here
42 my $plugin_base = 'OpenILS::Template::Plugin';
43
44 sub child_init {
45     OpenSRF::System->bootstrap_client( config_file => $bootstrap );
46
47     #parse the base xml file
48     #my $parser = XML::LibXML->new;
49     #$parser->expand_xinclude(1);
50
51     #$base_xml_doc = $parser->parse_file($base_xml);
52     return Apache2::Const::OK;
53 }
54
55 sub handler {
56
57     my $apache = shift;
58     return Apache2::Const::DECLINED if (-e $apache->filename);
59
60     my $cgi = CGI->new;
61
62     my $path = $apache->path_info;
63     (my $ttk = $path) =~ s{^/?([a-zA-Z0-9_]+).*?$}{$1}o;
64
65     $ttk = $apache->filename unless $ttk;
66     $ttk = "dashboard" unless $ttk;
67
68     $ttk = (split '/', $ttk)[-1];
69     
70     my $user;
71
72     # if the user is not logged in via cookie, route them to the login page
73     my $auth_ses = $cgi->param('ses') || $cgi->cookie('ses') || $cgi->cookie('eg.auth.token');
74     if ($auth_ses =~ /^"(.+)"$/) { # came from eg2 login, is json encoded
75         $auth_ses = $1;
76     }
77
78     if(! ($user = verify_login($auth_ses)) ) {
79         $ttk = "login";
80     }
81
82
83     print "Content-type: text/html; charset=utf-8\n\n";
84     #print "Content-type: text/html\n\n";
85
86     _process_template(
87             apache      => $apache,
88             template        => "$ttk.ttk",
89             params      => { 
90                 user => $user, 
91                 stage_dir => $ttk, 
92                 config_xml => $base_xml, 
93                 },
94             );
95
96     return Apache2::Const::OK;
97 }
98
99
100 sub _process_template {
101
102     my %params = @_;
103     my $ttk             = $params{template}     || return undef;
104     my $apache          = $params{apache}           || undef;
105     my $param_hash      = $params{params}           || {};
106     $$param_hash{dtype_xform_map} = $OpenILS::WWW::Reporter::dtype_xform_map;
107     $$param_hash{dtype_xforms} = $OpenILS::WWW::Reporter::dtype_xforms;
108
109     my $template;
110
111     $template = Template->new( { 
112         OUTPUT          => $apache, 
113         ABSOLUTE        => 1, 
114         RELATIVE        => 1,
115         PLUGIN_BASE     => $plugin_base,
116         INCLUDE_PATH    => $includes, 
117         PRE_CHOMP       => 1,
118         POST_CHOMP      => 1,
119         #LOAD_PERL      => 1,
120         } 
121     );
122
123     try {
124
125         if( ! $template->process( $ttk, $param_hash ) ) { 
126             warn  "Error Processing Template: " . $template->error();
127             my $err = $template->error();
128             $err =~ s/\n/\<br\/\>/g;
129             warn "Error processing template $ttk\n";    
130             my $string =  "<br><b>Unable to process template:<br/><br/> " . $err . "</b>";
131             print "ERROR: $string";
132             #$template->process( $error_ttk , { error => $string } );
133         }
134
135     } catch Error with {
136         my $e = shift;
137         warn "Error processing template $ttk:  $e - $@ \n"; 
138         print "<center><br/><br/><b>Error<br/><br/> $e <br/><br/> $@ </b><br/></center>";
139         return;
140     };
141
142 }
143
144 # returns the user object if the session is valid, 0 otherwise
145 sub verify_login {
146     my $auth_token = shift;
147     return 0 unless $auth_token;
148
149     my $session = OpenSRF::AppSession->create("open-ils.auth");
150     my $req = $session->request(
151         "open-ils.auth.session.retrieve", $auth_token );
152     my $user = $req->gather(1);
153
154     if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) {
155         return 0;
156     }
157
158     return $user if ref($user);
159     return 0;
160 }
161
162
163
164 1;