Bug 14737: wrong permissions required for cn_browser.pl plugin
[koha.git] / tools / viewlog.pl
bloba13ec5faf27e8692f11684e8c7b0075590d7595d
1 #!/usr/bin/perl
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use Text::CSV::Encoded;
26 use C4::Context;
27 use C4::Koha;
28 use C4::Dates;
29 use C4::Output;
30 use C4::Log;
31 use C4::Items;
32 use C4::Branch;
33 use C4::Debug;
34 use C4::Search; # enabled_staff_search_views
36 use vars qw($debug $cgi_debug);
38 =head1 viewlog.pl
40 plugin that shows stats
42 =cut
44 my $input = new CGI;
46 $debug or $debug = $cgi_debug;
47 my $do_it = $input->param('do_it');
48 my @modules = $input->param("modules");
49 my $user = $input->param("user") // '';
50 my @actions = $input->param("actions");
51 my $object = $input->param("object");
52 my $info = $input->param("info");
53 my $datefrom = $input->param("from");
54 my $dateto = $input->param("to");
55 my $basename = $input->param("basename");
56 my $output = $input->param("output") || "screen";
57 my $src = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61 template_name => "tools/viewlog.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { tools => 'view_system_logs' },
66 debug => 1,
70 if ( $src eq 'circ' ) {
72 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
73 use C4::Members;
74 use C4::Members::Attributes qw(GetBorrowerAttributes);
75 my $borrowernumber = $object;
76 my $data = GetMember( 'borrowernumber' => $borrowernumber );
77 my ( $picture, $dberror ) = GetPatronImage( $data->{'borrowernumber'} );
78 $template->param( picture => 1 ) if $picture;
80 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
81 my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
82 $template->param(
83 ExtendedPatronAttributes => 1,
84 extendedattributes => $attributes
88 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
89 $template->param(%$data);
91 $template->param(
92 menu => 1,
93 borrowernumber => $borrowernumber,
94 categoryname => $data->{'description'},
95 roadtype => $roadtype,
96 branchname => GetBranchName( $data->{'branchcode'} ),
97 RoutingSerials => C4::Context->preference('RoutingSerials'),
101 $template->param(
102 debug => $debug,
103 C4::Search::enabled_staff_search_views,
104 object => $object,
107 if ($do_it) {
109 my @data;
110 my ( $results, $modules, $actions );
111 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \@actions; } # match All means no limit
112 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
113 $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
114 @data = @$results;
115 foreach my $result (@data) {
117 # Init additional columns for CSV export
118 $result->{'biblionumber'} = q{};
119 $result->{'biblioitemnumber'} = q{};
120 $result->{'barcode'} = q{};
121 $result->{'userfirstname'} = q{};
122 $result->{'usersurname'} = q{};
123 $result->{'borrowerfirstname'} = q{};
124 $result->{'borrowersurname'} = q{};
126 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
128 # get item information so we can create a working link
129 my $itemnumber = $result->{'object'};
130 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
131 my $item = GetItem($itemnumber);
132 if ($item) {
133 $result->{'biblionumber'} = $item->{'biblionumber'};
134 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
135 $result->{'barcode'} = $item->{'barcode'};
139 #always add firstname and surname for librarian/user
140 if ( $result->{'user'} ) {
141 my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
142 if ($userdetails) {
143 $result->{'userfirstname'} = $userdetails->{'firstname'};
144 $result->{'usersurname'} = $userdetails->{'surname'};
148 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
149 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
150 if ( $result->{'object'} ) {
151 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
152 if ($borrowerdetails) {
153 $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
154 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
160 if ( $output eq "screen" ) {
162 # Printing results to screen
163 $template->param(
164 logview => 1,
165 total => scalar @data,
166 looprow => \@data,
167 do_it => 1,
168 datefrom => $datefrom,
169 dateto => $dateto,
170 user => $user,
171 info => $info,
172 src => $src,
173 modules => \@modules,
174 actions => \@actions,
177 # Used modules
178 foreach my $module (@modules) {
179 $template->param( $module => 1 );
182 output_html_with_http_headers $input, $cookie, $template->output;
184 else {
186 # Printing to a csv file
187 my $content = q{};
188 my $delimiter = C4::Context->preference('delimiter') || ',';
189 if (@data) {
190 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
191 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
193 # First line with heading
194 # Exporting bd id seems useless
195 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
196 if ( $csv->combine(@headings) ) {
197 $content .= $csv->string() . "\n";
200 # Lines of logs
201 foreach my $line (@data) {
202 my @cells = map { $line->{$_} } @headings;
203 if ( $csv->combine(@cells) ) {
204 $content .= $csv->string() . "\n";
209 # Output
210 print $input->header(
211 -type => 'text/csv',
212 -attachment => $basename . '.csv',
214 print $content;
216 exit;
218 else {
219 output_html_with_http_headers $input, $cookie, $template->output;