Bug 12542: Tabs inconsistency in different circ-menu.inc uses
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Dates;
28 use C4::Output;
29 use C4::Log;
30 use C4::Items;
31 use C4::Branch;
32 use C4::Debug;
33 # use Data::Dumper;
34 use C4::Search;         # enabled_staff_search_views
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->param("modules");
49 my $user     = $input->param("user");
50 my @action   = $input->param("action");
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 $del      = $input->param("sep");
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') {   # 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     # Computes full borrower address
88     my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
89     my $address = $data->{'streetnumber'} . " $roadtype " . $data->{'address'};
90
91     $template->param(   menu            => 1,
92                         title           => $data->{'title'},
93                         initials        => $data->{'initials'},
94                         surname         => $data->{'surname'},
95                         othernames      => $data->{'othernames'},
96                         borrowernumber  => $borrowernumber,
97                         firstname       => $data->{'firstname'},
98                         cardnumber      => $data->{'cardnumber'},
99                         categorycode    => $data->{'categorycode'},
100                         category_type   => $data->{'category_type'},
101                         categoryname    => $data->{'description'},
102                         address         => $address,
103                         address2        => $data->{'address2'},
104                         city            => $data->{'city'},
105                         state           => $data->{'state'},
106                         zipcode         => $data->{'zipcode'},
107                         country         => $data->{'country'},
108                         phone           => $data->{'phone'},
109                         phonepro        => $data->{'phonepro'},
110                         mobile          => $data->{'mobile'},
111                         email           => $data->{'email'},
112                         emailpro        => $data->{'emailpro'},
113                         branchcode      => $data->{'branchcode'},
114                         branchname              => GetBranchName($data->{'branchcode'}),
115                         RoutingSerials => C4::Context->preference('RoutingSerials'),
116     );
117 }
118
119 $template->param(
120     debug => $debug,
121     C4::Search::enabled_staff_search_views,
122 );
123
124 if ($do_it) {
125
126     my @data;
127     my ($results,$modules,$action);
128     if ($action[0] ne '') { $action = \@action; } # match All means no limit
129     if ($modules[0] ne '') { $modules = \@modules; } # match All means no limit
130     $results = GetLogs($datefrom,$dateto,$user,$modules,$action,$object,$info);
131     @data=@$results;
132     my $total = scalar @data;
133     foreach my $result (@data){
134         if (substr($result->{'info'}, 0, 4) eq 'item' || $result->{module} eq "CIRCULATION"){
135             # get item information so we can create a working link
136         my $itemnumber=$result->{'object'};
137         $itemnumber=$result->{'info'} if ($result->{module} eq "CIRCULATION");
138             my $item=GetItem($itemnumber);
139             $result->{'biblionumber'}=$item->{'biblionumber'};
140             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
141         $result->{'barcode'}=$item->{'barcode'};
142         }
143     #always add firstname and surname for librarian/user
144     if ($result->{'user'}){
145         my $userdetails = C4::Members::GetMemberDetails($result->{'user'});
146         if ($userdetails->{'firstname'}){
147             $result->{'userfirstname'} = $userdetails->{'firstname'};
148         }
149         if ($userdetails->{'surname'}){
150             $result->{'usersurname'} = $userdetails->{'surname'};
151         }
152     }
153     #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
154     if ($result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES"){
155         if($result->{'object'}){
156             my $borrowerdetails = C4::Members::GetMemberDetails($result->{'object'});
157             if ($borrowerdetails->{'firstname'}){
158             $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
159             }
160             if ($borrowerdetails->{'surname'}){
161                 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
162             }
163         }
164     }
165     }
166     
167     if ( $output eq "screen" ) {
168         # Printing results to screen
169         $template->param (
170                         logview => 1,
171             total    => $total,
172             looprow  => \@data,
173             do_it    => 1,
174             datefrom => $datefrom,
175             dateto   => $dateto,
176             user     => $user,
177             object   => $object,
178             action   => \@action,
179             info     => $info,
180             src      => $src,
181         );
182             #module   => 'fix this', #this seems unused in actual code
183         foreach my $module (@modules) {
184                 $template->param($module  => 1);
185         }
186
187         output_html_with_http_headers $input, $cookie, $template->output;
188     } else {
189         # Printing to a csv file
190         print $input->header(
191             -type       => 'text/csv',
192             -attachment => "$basename.csv",
193             -filename   => "$basename.csv"
194         );
195         my $sep = C4::Context->preference("delimiter");
196         foreach my $line (@data) {
197             #next unless $modules[0] eq "catalogue";
198                 foreach (qw(timestamp firstname surname action info title author)) {
199                         print $line->{$_} . $sep;
200                 }       
201         }
202     }
203         exit;
204 } else {
205     #my @values;
206     #my %labels;
207     #my %select;
208         #initialize some paramaters that might not be used in the template - it seems to evaluate EXPR even if a false TMPL_IF
209         $template->param(
210                 total => 0,
211                 module => "",
212                 info => ""
213         );
214         output_html_with_http_headers $input, $cookie, $template->output;
215 }