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