Bug 14141: Fix copy action
[koha.git] / tools / viewlog.pl
blobf1bd06c26485a6e4694c734415c40364806f2294
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;
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
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->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
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61 template_name => "tools/viewlog.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { tools => 'view_system_logs' },
66 debug => 1,
70 if ( $src eq 'circ' ) {
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;
80 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
81 my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
82 $template->param(
83 ExtendedPatronAttributes => 1,
84 extendedattributes => $attributes
88 # Computes full borrower address
89 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
90 my $address = $data->{'streetnumber'} . " $roadtype " . $data->{'address'};
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'),
121 $template->param(
122 debug => $debug,
123 C4::Search::enabled_staff_search_views,
124 object => $object,
127 if ($do_it) {
129 my @data;
130 my ( $results, $modules, $actions );
131 if ( defined $actions[0] && $actions[0] ne '' ) { $actions = \@actions; } # match All means no limit
132 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
133 $results = GetLogs( $datefrom, $dateto, $user, $modules, $actions, $object, $info );
134 @data = @$results;
135 foreach my $result (@data) {
137 # Init additional columns for CSV export
138 $result->{'biblionumber'} = q{};
139 $result->{'biblioitemnumber'} = q{};
140 $result->{'barcode'} = q{};
141 $result->{'userfirstname'} = q{};
142 $result->{'usersurname'} = q{};
143 $result->{'borrowerfirstname'} = q{};
144 $result->{'borrowersurname'} = q{};
146 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
148 # get item information so we can create a working link
149 my $itemnumber = $result->{'object'};
150 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
151 my $item = GetItem($itemnumber);
152 if ($item) {
153 $result->{'biblionumber'} = $item->{'biblionumber'};
154 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
155 $result->{'barcode'} = $item->{'barcode'};
159 #always add firstname and surname for librarian/user
160 if ( $result->{'user'} ) {
161 my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
162 if ($userdetails) {
163 $result->{'userfirstname'} = $userdetails->{'firstname'};
164 $result->{'usersurname'} = $userdetails->{'surname'};
168 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
169 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
170 if ( $result->{'object'} ) {
171 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
172 if ($borrowerdetails) {
173 $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
174 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
180 if ( $output eq "screen" ) {
182 # Printing results to screen
183 $template->param(
184 logview => 1,
185 total => scalar @data,
186 looprow => \@data,
187 do_it => 1,
188 datefrom => $datefrom,
189 dateto => $dateto,
190 user => $user,
191 object => $object,
192 action => \@actions,
193 info => $info,
194 src => $src,
195 modules => \@modules,
198 output_html_with_http_headers $input, $cookie, $template->output;
200 else {
202 # Printing to a csv file
203 my $content = q{};
204 my $delimiter = C4::Context->preference('delimiter') || ',';
205 if (@data) {
206 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
207 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
209 # First line with heading
210 # Exporting bd id seems useless
211 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
212 if ( $csv->combine(@headings) ) {
213 $content .= $csv->string() . "\n";
216 # Lines of logs
217 foreach my $line (@data) {
218 my @cells = map { $line->{$_} } @headings;
219 if ( $csv->combine(@cells) ) {
220 $content .= $csv->string() . "\n";
225 # Output
226 print $input->header(
227 -type => 'text/csv',
228 -attachment => $basename . '.csv',
230 print $content;
232 exit;
234 else {
235 output_html_with_http_headers $input, $cookie, $template->output;