Bug 13991: (QA followup) uninitialized value and [0] in /tools/viewlog.pl
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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::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
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->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
58
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/viewlog.tt",
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'view_system_logs' },
66         debug           => 1,
67     }
68 );
69
70 if ( $src eq 'circ' ) {
71
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;
79
80     if ( C4::Context->preference('ExtendedPatronAttributes') ) {
81         my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
82         $template->param(
83             ExtendedPatronAttributes => 1,
84             extendedattributes       => $attributes
85         );
86     }
87
88     # Computes full borrower address
89     my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
90     my $address = $data->{'streetnumber'} . " $roadtype " . $data->{'address'};
91
92     $template->param(
93         menu           => 1,
94         title          => $data->{'title'},
95         initials       => $data->{'initials'},
96         surname        => $data->{'surname'},
97         othernames     => $data->{'othernames'},
98         borrowernumber => $borrowernumber,
99         firstname      => $data->{'firstname'},
100         cardnumber     => $data->{'cardnumber'},
101         categorycode   => $data->{'categorycode'},
102         category_type  => $data->{'category_type'},
103         categoryname   => $data->{'description'},
104         address        => $address,
105         address2       => $data->{'address2'},
106         city           => $data->{'city'},
107         state          => $data->{'state'},
108         zipcode        => $data->{'zipcode'},
109         country        => $data->{'country'},
110         phone          => $data->{'phone'},
111         phonepro       => $data->{'phonepro'},
112         mobile         => $data->{'mobile'},
113         email          => $data->{'email'},
114         emailpro       => $data->{'emailpro'},
115         branchcode     => $data->{'branchcode'},
116         branchname     => GetBranchName( $data->{'branchcode'} ),
117         RoutingSerials => C4::Context->preference('RoutingSerials'),
118     );
119 }
120
121 $template->param(
122     debug => $debug,
123     C4::Search::enabled_staff_search_views,
124 );
125
126 if ($do_it) {
127
128     my @data;
129     my ( $results, $modules, $actions );
130     if ( defined $actions[0] && $actions[0] ne '' ) { $actions  = \@actions; }     # match All means no limit
131     if ( $modules[0] ne '' ) { $modules = \@modules; }    # match All means no limit
132     $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
133     @data = @$results;
134     foreach my $result (@data) {
135
136         # Init additional columns for CSV export
137         $result->{'biblionumber'}      = q{};
138         $result->{'biblioitemnumber'}  = q{};
139         $result->{'barcode'}           = q{};
140         $result->{'userfirstname'}     = q{};
141         $result->{'usersurname'}       = q{};
142         $result->{'borrowerfirstname'} = q{};
143         $result->{'borrowersurname'}   = q{};
144
145         if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
146
147             # get item information so we can create a working link
148             my $itemnumber = $result->{'object'};
149             $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
150             my $item = GetItem($itemnumber);
151             if ($item) {
152                 $result->{'biblionumber'}     = $item->{'biblionumber'};
153                 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
154                 $result->{'barcode'}          = $item->{'barcode'};
155             }
156         }
157
158         #always add firstname and surname for librarian/user
159         if ( $result->{'user'} ) {
160             my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
161             if ($userdetails) {
162                 $result->{'userfirstname'} = $userdetails->{'firstname'};
163                 $result->{'usersurname'}   = $userdetails->{'surname'};
164             }
165         }
166
167         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
168         if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
169             if ( $result->{'object'} ) {
170                 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
171                 if ($borrowerdetails) {
172                     $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
173                     $result->{'borrowersurname'}   = $borrowerdetails->{'surname'};
174                 }
175             }
176         }
177     }
178
179     if ( $output eq "screen" ) {
180
181         # Printing results to screen
182         $template->param(
183             logview  => 1,
184             total    => scalar @data,
185             looprow  => \@data,
186             do_it    => 1,
187             datefrom => $datefrom,
188             dateto   => $dateto,
189             user     => $user,
190             object   => $object,
191             info     => $info,
192             src      => $src,
193             modules  => \@modules,
194             actions  => \@actions,
195         );
196
197         # Used modules
198         foreach my $module (@modules) {
199             $template->param( $module => 1 );
200         }
201
202         output_html_with_http_headers $input, $cookie, $template->output;
203     }
204     else {
205
206         # Printing to a csv file
207         my $content = q{};
208         my $delimiter = C4::Context->preference('delimiter') || ',';
209         if (@data) {
210             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
211             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
212
213             # First line with heading
214             # Exporting bd id seems useless
215             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
216             if ( $csv->combine(@headings) ) {
217                 $content .= $csv->string() . "\n";
218             }
219
220             # Lines of logs
221             foreach my $line (@data) {
222                 my @cells = map { $line->{$_} } @headings;
223                 if ( $csv->combine(@cells) ) {
224                     $content .= $csv->string() . "\n";
225                 }
226             }
227         }
228
229         # Output
230         print $input->header(
231             -type       => 'text/csv',
232             -attachment => $basename . '.csv',
233         );
234         print $content;
235     }
236     exit;
237 }
238 else {
239     output_html_with_http_headers $input, $cookie, $template->output;
240 }