Translation updates for Koha 18.11.10
[koha.git] / tools / viewlog.pl
blobeab080615dde4ea66d0e393f8f3053b9ebc49c72
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::Output;
29 use C4::Log;
30 use C4::Items;
31 use C4::Serials;
32 use C4::Debug;
33 use C4::Search; # enabled_staff_search_views
34 use Koha::Patrons;
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->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
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62 template_name => "tools/viewlog.tt",
63 query => $input,
64 type => "intranet",
65 authnotrequired => 0,
66 flagsrequired => { tools => 'view_system_logs' },
67 debug => 1,
71 if ( $src eq 'circ' ) {
73 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
74 use C4::Members::Attributes qw(GetBorrowerAttributes);
75 my $borrowernumber = $object;
76 my $patron = Koha::Patrons->find( $borrowernumber );
77 my $circ_info = 1;
78 unless ( $patron ) {
79 $circ_info = 0;
81 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
82 my $attributes = GetBorrowerAttributes( $borrowernumber );
83 $template->param(
84 ExtendedPatronAttributes => 1,
85 extendedattributes => $attributes
89 $template->param(
90 patron => $patron,
91 circulation => $circ_info,
95 $template->param(
96 debug => $debug,
97 C4::Search::enabled_staff_search_views,
98 subscriptionsnumber => CountSubscriptionFromBiblionumber(scalar $input->param('object')),
99 object => $object,
102 if ($do_it) {
104 my @data;
105 my ( $results, $modules, $actions, $interfaces );
106 if ( grep { $_ eq 'RENEW' } @actions ) { push @actions, 'RENEWAL' }; # Circulation uses 'RENEWAL', Patrons uses 'RENEW'
107 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \@actions; } # match All means no limit
108 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
109 if ( defined $interfaces[0] && $interfaces[0] ne '' ) { $interfaces = \@interfaces; } # match All means no limit
110 $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info, $interfaces );
111 @data = @$results;
112 foreach my $result (@data) {
114 # Init additional columns for CSV export
115 $result->{'biblionumber'} = q{};
116 $result->{'biblioitemnumber'} = q{};
117 $result->{'barcode'} = q{};
119 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
121 # get item information so we can create a working link
122 my $itemnumber = $result->{'object'};
123 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
124 my $item = GetItem($itemnumber);
125 if ($item) {
126 $result->{'biblionumber'} = $item->{'biblionumber'};
127 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
128 $result->{'barcode'} = $item->{'barcode'};
132 #always add firstname and surname for librarian/user
133 if ( $result->{'user'} ) {
134 my $patron = Koha::Patrons->find( $result->{'user'} );
135 if ($patron) {
136 $result->{librarian} = $patron;
140 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
141 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
142 if ( $result->{'object'} ) {
143 my $patron = Koha::Patrons->find( $result->{'object'} );
144 if ($patron) {
145 $result->{patron} = $patron;
151 if ( $output eq "screen" ) {
153 # Printing results to screen
154 $template->param(
155 logview => 1,
156 total => scalar @data,
157 looprow => \@data,
158 do_it => 1,
159 datefrom => $datefrom,
160 dateto => $dateto,
161 user => $user,
162 info => $info,
163 src => $src,
164 modules => \@modules,
165 actions => \@actions,
166 interfaces => \@interfaces
169 # Used modules
170 foreach my $module (@modules) {
171 $template->param( $module => 1 );
174 output_html_with_http_headers $input, $cookie, $template->output;
176 else {
178 # Printing to a csv file
179 my $content = q{};
180 my $delimiter = C4::Context->preference('delimiter') || ',';
181 if (@data) {
182 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
183 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
185 # First line with heading
186 # Exporting bd id seems useless
187 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
188 if ( $csv->combine(@headings) ) {
189 $content .= $csv->string() . "\n";
192 # Lines of logs
193 foreach my $line (@data) {
194 my @cells = map { $line->{$_} } @headings;
195 if ( $csv->combine(@cells) ) {
196 $content .= $csv->string() . "\n";
201 # Output
202 print $input->header(
203 -type => 'text/csv',
204 -attachment => $basename . '.csv',
206 print $content;
208 exit;
210 else {
211 output_html_with_http_headers $input, $cookie, $template->output;