Bug 23473: Allow overwrite of passwords during import
[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::Items;
30 use C4::Serials;
31 use C4::Debug;
32 use C4::Search;    # enabled_staff_search_views
33
34 use Koha::ActionLogs;
35 use Koha::Database;
36 use Koha::DateUtils;
37 use Koha::Items;
38 use Koha::Patrons;
39
40 use vars qw($debug $cgi_debug);
41
42 =head1 viewlog.pl
43
44 plugin that shows stats
45
46 =cut
47
48 my $input = new CGI;
49
50 $debug or $debug = $cgi_debug;
51 my $do_it    = $input->param('do_it');
52 my @modules  = $input->multi_param("modules");
53 my $user     = $input->param("user") // '';
54 my @actions  = $input->multi_param("actions");
55 my @interfaces  = $input->multi_param("interfaces");
56 my $object   = $input->param("object");
57 my $object_type = $input->param("object_type");
58 my $info     = $input->param("info");
59 my $datefrom = $input->param("from");
60 my $dateto   = $input->param("to");
61 my $basename = $input->param("basename");
62 my $output   = $input->param("output") || "screen";
63 my $src      = $input->param("src") || ""; # this param allows us to be told where we were called from -fbcit
64
65 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
66     {
67         template_name   => "tools/viewlog.tt",
68         query           => $input,
69         type            => "intranet",
70         authnotrequired => 0,
71         flagsrequired   => { tools => 'view_system_logs' },
72         debug           => 1,
73     }
74 );
75
76 if ( $src eq 'circ' ) {
77
78     my $borrowernumber = $object;
79     my $patron = Koha::Patrons->find( $borrowernumber );
80     my $circ_info = 1;
81     unless ( $patron ) {
82          $circ_info = 0;
83     }
84
85     $template->param(
86         patron      => $patron,
87         circulation => $circ_info,
88     );
89 }
90
91 $template->param(
92     debug => $debug,
93     C4::Search::enabled_staff_search_views,
94     subscriptionsnumber => CountSubscriptionFromBiblionumber(scalar $input->param('object')),
95     object => $object,
96 );
97
98 if ($do_it) {
99     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
100     my %search_params;
101
102     if ($datefrom and $dateto ) {
103         my $dateto_endday = dt_from_string($dateto);
104         $dateto_endday->set( # We set last second of day to see all log from that day
105             hour => 23,
106             minute => 59,
107             second => 59
108         );
109         $search_params{'timestamp'} = {
110             -between => [
111                 $dtf->format_datetime( dt_from_string($datefrom) ),
112                 $dtf->format_datetime( $dateto_endday ),
113             ]
114         };
115     } elsif ($datefrom) {
116         $search_params{'timestamp'} = {
117             '>=' => $dtf->format_datetime( dt_from_string($datefrom) )
118         };
119     } elsif ($dateto) {
120         my $dateto_endday = dt_from_string($dateto);
121         $dateto_endday->set( # We set last second of day to see all log from that day
122             hour => 23,
123             minute => 59,
124             second => 59
125         );
126         $search_params{'timestamp'} = {
127             '<=' => $dtf->format_datetime( $dateto_endday )
128         };
129     }
130     # Circulation uses RENEWAL, but Patrons uses RENEW, this helps to find both
131     if ( grep { $_ eq 'RENEW'} @actions ) { push @actions, 'RENEWAL' };
132
133     $search_params{user} = $user if $user;
134     $search_params{module} = { -in => [ @modules ] } if ( defined $modules[0] and $modules[0] ne '' ) ;
135     $search_params{action} = { -in => [ @actions ] } if ( defined $actions[0] && $actions[0] ne '' );
136     $search_params{interface} = { -in => [ @interfaces ] } if ( defined $interfaces[0] && $interfaces[0] ne '' );
137
138
139     if ( @modules == 1 && $object_type eq 'biblio' ) {
140         # Handle 'Modification log' from cataloguing
141         my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
142         $search_params{'-or'} = [
143             { -and => { object => $object, info => { -like => 'biblio%' }}},
144             { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
145         ];
146     } else {
147         $search_params{info} = $info if $info;
148         $search_params{object} = $object if $object;
149     }
150
151     my @logs = Koha::ActionLogs->search(\%search_params);
152
153     my @data;
154     foreach my $log (@logs) {
155         my $result = $log->unblessed;
156         # Init additional columns for CSV export
157         $result->{'biblionumber'}      = q{};
158         $result->{'biblioitemnumber'}  = q{};
159         $result->{'barcode'}           = q{};
160
161         if ( substr( $log->info, 0, 4 ) eq 'item' || $log->module eq "CIRCULATION" ) {
162
163             # get item information so we can create a working link
164             my $itemnumber = $log->object;
165             $itemnumber = $log->info if ( $log->module eq "CIRCULATION" );
166             my $item = Koha::Items->find($itemnumber);
167             if ($item) {
168                 $result->{'biblionumber'}     = $item->biblionumber;
169                 $result->{'biblioitemnumber'} = $item->biblionumber;
170                 $result->{'barcode'}          = $item->barcode;
171             }
172         }
173
174         #always add firstname and surname for librarian/user
175         if ( $log->user ) {
176             my $patron = Koha::Patrons->find( $log->user );
177             if ($patron) {
178                 $result->{librarian} = $patron;
179             }
180         }
181
182         #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
183         if ( $log->module eq "CIRCULATION" || $log->module eq "MEMBERS" || $log->module eq "FINES" ) {
184             if ( $log->object ) {
185                 my $patron = Koha::Patrons->find( $log->object );
186                 if ($patron) {
187                     $result->{patron} = $patron;
188                 }
189             }
190         }
191         push @data, $result;
192     }
193     if ( $output eq "screen" ) {
194
195         # Printing results to screen
196         $template->param(
197             logview  => 1,
198             total    => scalar @data,
199             looprow  => \@data,
200             do_it    => 1,
201             datefrom => $datefrom,
202             dateto   => $dateto,
203             user     => $user,
204             info     => $info,
205             src      => $src,
206             modules  => \@modules,
207             actions  => \@actions,
208             interfaces => \@interfaces
209         );
210
211         # Used modules
212         foreach my $module (@modules) {
213             $template->param( $module => 1 );
214         }
215         output_html_with_http_headers $input, $cookie, $template->output;
216     }
217     else {
218
219         # Printing to a csv file
220         my $content = q{};
221         my $delimiter = C4::Context->preference('delimiter') || ',';
222         if (@data) {
223             my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
224             $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
225
226             # First line with heading
227             # Exporting bd id seems useless
228             my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
229             if ( $csv->combine(@headings) ) {
230                 $content .= $csv->string() . "\n";
231             }
232
233             # Lines of logs
234             foreach my $line (@data) {
235                 my @cells = map { $line->{$_} } @headings;
236                 if ( $csv->combine(@cells) ) {
237                     $content .= $csv->string() . "\n";
238                 }
239             }
240         }
241
242         # Output
243         print $input->header(
244             -type       => 'text/csv',
245             -attachment => $basename . '.csv',
246         );
247         print $content;
248     }
249     exit;
250 }
251 else {
252     output_html_with_http_headers $input, $cookie, $template->output;
253 }