Bug 15023: (followup) Remove warnings
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use Modern::Perl;
22
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::Output;
29 use C4::Log;
30 use C4::Items;
31 use C4::Branch;
32 use C4::Debug;
33 use C4::Search;    # enabled_staff_search_views
34 use Koha::Patron::Images;
35
36 use vars qw($debug $cgi_debug);
37
38 =head1 viewlog.pl
39
40 plugin that shows stats
41
42 =cut
43
44 my $input = new CGI;
45
46 $debug or $debug = $cgi_debug;
47 my $do_it    = $input->param('do_it');
48 my @modules  = $input->multi_param("modules");
49 my $user     = $input->param("user") // '';
50 my @actions  = $input->multi_param("actions");
51 my @interfaces  = $input->multi_param("interfaces");
52 my $object   = $input->param("object");
53 my $info     = $input->param("info");
54 my $datefrom = $input->param("from");
55 my $dateto   = $input->param("to");
56 my $basename = $input->param("basename");
57 my $output   = $input->param("output") || "screen";
58 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
59
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61     {
62         template_name   => "tools/viewlog.tt",
63         query           => $input,
64         type            => "intranet",
65         authnotrequired => 0,
66         flagsrequired   => { tools => 'view_system_logs' },
67         debug           => 1,
68     }
69 );
70
71 if ( $src eq 'circ' ) {
72
73     # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
74     use C4::Members;
75     use C4::Members::Attributes qw(GetBorrowerAttributes);
76     my $borrowernumber = $object;
77     my $data = GetMember( 'borrowernumber' => $borrowernumber );
78     my $patron_image = Koha::Patron::Images->find($data->{borrowernumber});
79     $template->param( picture => 1 ) if $patron_image;
80
81     if ( C4::Context->preference('ExtendedPatronAttributes') ) {
82         my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
83         $template->param(
84             ExtendedPatronAttributes => 1,
85             extendedattributes       => $attributes
86         );
87     }
88
89     $template->param(%$data);
90
91     $template->param(
92         menu           => 1,
93         borrowernumber => $borrowernumber,
94         categoryname   => $data->{'description'},
95         branchname     => GetBranchName( $data->{'branchcode'} ),
96         RoutingSerials => C4::Context->preference('RoutingSerials'),
97     );
98 }
99
100 $template->param(
101     debug => $debug,
102     C4::Search::enabled_staff_search_views,
103     object => $object,
104 );
105
106 if ($do_it) {
107
108     my @data;
109     my ( $results, $modules, $actions, $interfaces );
110     if ( defined $actions[0] && $actions[0] ne '' ) { $actions  = \@actions; }     # match All means no limit
111     if ( $modules[0] ne '' ) { $modules = \@modules; }    # match All means no limit
112     if ( defined $interfaces[0] && $interfaces[0] ne '' ) { $interfaces = \@interfaces; }    # match All means no limit
113     $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info, $interfaces );
114     @data = @$results;
115     foreach my $result (@data) {
116
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{};
125
126         if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
127
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'};
136             }
137         }
138
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'};
145             }
146         }
147
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'};
155                 }
156             }
157         }
158     }
159
160     if ( $output eq "screen" ) {
161
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,
175             interfaces => \@interfaces
176         );
177
178         # Used modules
179         foreach my $module (@modules) {
180             $template->param( $module => 1 );
181         }
182
183         output_html_with_http_headers $input, $cookie, $template->output;
184     }
185     else {
186
187         # Printing to a csv file
188         my $content = q{};
189         my $delimiter = C4::Context->preference('delimiter') || ',';
190         if (@data) {
191             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
192             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
193
194             # First line with heading
195             # Exporting bd id seems useless
196             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
197             if ( $csv->combine(@headings) ) {
198                 $content .= $csv->string() . "\n";
199             }
200
201             # Lines of logs
202             foreach my $line (@data) {
203                 my @cells = map { $line->{$_} } @headings;
204                 if ( $csv->combine(@cells) ) {
205                     $content .= $csv->string() . "\n";
206                 }
207             }
208         }
209
210         # Output
211         print $input->header(
212             -type       => 'text/csv',
213             -attachment => $basename . '.csv',
214         );
215         print $content;
216     }
217     exit;
218 }
219 else {
220     output_html_with_http_headers $input, $cookie, $template->output;
221 }