yay! the reporter spits out csv and excel now ... html tomorrow (with any luck)
[Evergreen.git] / Open-ILS / src / perlmods / 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 ();
7 use Apache2::Log;
8 use Apache2::Const -compile => qw(OK REDIRECT :log);
9 use APR::Const    -compile => qw(:error SUCCESS);
10 use Apache2::RequestRec ();
11 use Apache2::RequestIO ();
12 use Apache2::RequestUtil;
13 use CGI;
14
15 use Template qw(: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
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         if(! ($user = verify_login($cgi->cookie("ses"))) ) {
74                 $ttk = "login";
75         }
76
77
78         print "Content-type: text/html; charset=utf-8\n\n";
79
80         _process_template(
81                         apache          => $apache,
82                         template                => "$ttk.ttk",
83                         params          => { 
84                                 user => $user, 
85                                 stage_dir => $ttk, 
86                                 config_xml => $base_xml, 
87                                 },
88                         );
89
90         return Apache2::Const::OK;
91 }
92
93
94 sub _process_template {
95
96         my %params = @_;
97         my $ttk                         = $params{template}             || return undef;
98         my $apache                      = $params{apache}                       || undef;
99         my $param_hash          = $params{params}                       || {};
100         $$param_hash{dtype_xform_map} = $OpenILS::WWW::Reporter::dtype_xform_map;
101         $$param_hash{dtype_xforms} = $OpenILS::WWW::Reporter::dtype_xforms;
102
103         my $template;
104
105         $template = Template->new( { 
106                 OUTPUT                  => $apache, 
107                 ABSOLUTE                => 1, 
108                 RELATIVE                => 1,
109                 PLUGIN_BASE             => $plugin_base,
110                 INCLUDE_PATH    => $includes, 
111                 PRE_CHOMP               => 1,
112                 POST_CHOMP              => 1,
113                 #LOAD_PERL              => 1,
114                 } 
115         );
116
117         try {
118
119                 if( ! $template->process( $ttk, $param_hash ) ) { 
120                         warn  "Error Processing Template: " . $template->error();
121                         my $err = $template->error();
122                         $err =~ s/\n/\<br\/\>/g;
123                         warn "Error processing template $ttk\n";        
124                         my $string =  "<br><b>Unable to process template:<br/><br/> " . $err . "</b>";
125                         print "ERROR: $string";
126                         #$template->process( $error_ttk , { error => $string } );
127                 }
128
129         } catch Error with {
130                 my $e = shift;
131                 warn "Error processing template $ttk:  $e - $@ \n";     
132                 print "<center><br/><br/><b>Error<br/><br/> $e <br/><br/> $@ </b><br/></center>";
133                 return;
134         };
135
136 }
137
138 # returns the user object if the session is valid, 0 otherwise
139 sub verify_login {
140         my $auth_token = shift;
141         return 0 unless $auth_token;
142
143         my $session = OpenSRF::AppSession->create("open-ils.auth");
144         my $req = $session->request(
145                 "open-ils.auth.session.retrieve", $auth_token );
146         my $user = $req->gather(1);
147
148         return $user if ref($user);
149         return 0;
150 }
151
152
153
154 1;