Bug 15684: Fix encoding issues with quote upload
[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
35 use vars qw($debug $cgi_debug);
36
37 =head1 viewlog.pl
38
39 plugin that shows stats
40
41 =cut
42
43 my $input = new CGI;
44
45 $debug or $debug = $cgi_debug;
46 my $do_it    = $input->param('do_it');
47 my @modules  = $input->param("modules");
48 my $user     = $input->param("user") // '';
49 my @actions  = $input->param("actions");
50 my $object   = $input->param("object");
51 my $info     = $input->param("info");
52 my $datefrom = $input->param("from");
53 my $dateto   = $input->param("to");
54 my $basename = $input->param("basename");
55 my $output   = $input->param("output") || "screen";
56 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
57
58 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
59     {
60         template_name   => "tools/viewlog.tt",
61         query           => $input,
62         type            => "intranet",
63         authnotrequired => 0,
64         flagsrequired   => { tools => 'view_system_logs' },
65         debug           => 1,
66     }
67 );
68
69 if ( $src eq 'circ' ) {
70
71     # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
72     use C4::Members;
73     use C4::Members::Attributes qw(GetBorrowerAttributes);
74     my $borrowernumber = $object;
75     my $data = GetMember( 'borrowernumber' => $borrowernumber );
76     my ( $picture, $dberror ) = GetPatronImage( $data->{'borrowernumber'} );
77     $template->param( picture => 1 ) if $picture;
78
79     if ( C4::Context->preference('ExtendedPatronAttributes') ) {
80         my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
81         $template->param(
82             ExtendedPatronAttributes => 1,
83             extendedattributes       => $attributes
84         );
85     }
86
87     $template->param(%$data);
88
89     $template->param(
90         menu           => 1,
91         borrowernumber => $borrowernumber,
92         categoryname   => $data->{'description'},
93         branchname     => GetBranchName( $data->{'branchcode'} ),
94         RoutingSerials => C4::Context->preference('RoutingSerials'),
95     );
96 }
97
98 $template->param(
99     debug => $debug,
100     C4::Search::enabled_staff_search_views,
101     object => $object,
102 );
103
104 if ($do_it) {
105
106     my @data;
107     my ( $results, $modules, $actions );
108     if ( defined $actions[0] && $actions[0] ne '' ) { $actions  = \@actions; }     # match All means no limit
109     if ( $modules[0] ne '' ) { $modules = \@modules; }    # match All means no limit
110     $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
111     @data = @$results;
112     foreach my $result (@data) {
113
114         # Init additional columns for CSV export
115         $result->{'biblionumber'}      = q{};
116         $result->{'biblioitemnumber'}  = q{};
117         $result->{'barcode'}           = q{};
118         $result->{'userfirstname'}     = q{};
119         $result->{'usersurname'}       = q{};
120         $result->{'borrowerfirstname'} = q{};
121         $result->{'borrowersurname'}   = q{};
122
123         if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
124
125             # get item information so we can create a working link
126             my $itemnumber = $result->{'object'};
127             $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
128             my $item = GetItem($itemnumber);
129             if ($item) {
130                 $result->{'biblionumber'}     = $item->{'biblionumber'};
131                 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
132                 $result->{'barcode'}          = $item->{'barcode'};
133             }
134         }
135
136         #always add firstname and surname for librarian/user
137         if ( $result->{'user'} ) {
138             my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
139             if ($userdetails) {
140                 $result->{'userfirstname'} = $userdetails->{'firstname'};
141                 $result->{'usersurname'}   = $userdetails->{'surname'};
142             }
143         }
144
145         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
146         if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
147             if ( $result->{'object'} ) {
148                 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
149                 if ($borrowerdetails) {
150                     $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
151                     $result->{'borrowersurname'}   = $borrowerdetails->{'surname'};
152                 }
153             }
154         }
155     }
156
157     if ( $output eq "screen" ) {
158
159         # Printing results to screen
160         $template->param(
161             logview  => 1,
162             total    => scalar @data,
163             looprow  => \@data,
164             do_it    => 1,
165             datefrom => $datefrom,
166             dateto   => $dateto,
167             user     => $user,
168             info     => $info,
169             src      => $src,
170             modules  => \@modules,
171             actions  => \@actions,
172         );
173
174         # Used modules
175         foreach my $module (@modules) {
176             $template->param( $module => 1 );
177         }
178
179         output_html_with_http_headers $input, $cookie, $template->output;
180     }
181     else {
182
183         # Printing to a csv file
184         my $content = q{};
185         my $delimiter = C4::Context->preference('delimiter') || ',';
186         if (@data) {
187             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
188             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
189
190             # First line with heading
191             # Exporting bd id seems useless
192             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
193             if ( $csv->combine(@headings) ) {
194                 $content .= $csv->string() . "\n";
195             }
196
197             # Lines of logs
198             foreach my $line (@data) {
199                 my @cells = map { $line->{$_} } @headings;
200                 if ( $csv->combine(@cells) ) {
201                     $content .= $csv->string() . "\n";
202                 }
203             }
204         }
205
206         # Output
207         print $input->header(
208             -type       => 'text/csv',
209             -attachment => $basename . '.csv',
210         );
211         print $content;
212     }
213     exit;
214 }
215 else {
216     output_html_with_http_headers $input, $cookie, $template->output;
217 }