Bug 766: Create a plugin routine to build dropdown list
[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.tmpl",
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     my $borrowernumber = $object;
74     my $data = GetMember('borrowernumber'=>$borrowernumber);
75     my ($picture, $dberror) = GetPatronImage($data->{'borrowernumber'});
76     $template->param( picture => 1 ) if $picture;
77     
78     $template->param(   menu            => 1,
79                         title           => $data->{'title'},
80                         initials        => $data->{'initials'},
81                         surname         => $data->{'surname'},
82                         othernames      => $data->{'othernames'},
83                         borrowernumber  => $borrowernumber,
84                         firstname       => $data->{'firstname'},
85                         cardnumber      => $data->{'cardnumber'},
86                         categorycode    => $data->{'categorycode'},
87                         category_type   => $data->{'category_type'},
88                         categoryname    => $data->{'description'},
89                         address         => $data->{'address'},
90                         address2        => $data->{'address2'},
91                         city            => $data->{'city'},
92                         state           => $data->{'state'},
93                         zipcode         => $data->{'zipcode'},
94                         phone           => $data->{'phone'},
95                         phonepro        => $data->{'phonepro'},
96                         email           => $data->{'email'},
97                         branchcode      => $data->{'branchcode'},
98                         branchname              => GetBranchName($data->{'branchcode'}),
99                         RoutingSerials => C4::Context->preference('RoutingSerials'),
100     );
101 }
102
103 $template->param(
104     debug => $debug,
105     C4::Search::enabled_staff_search_views,
106 );
107
108 if ($do_it) {
109
110     my @data;
111     my ($results,$modules,$action);
112     if ($action[0] ne '') { $action = \@action; } # match All means no limit
113     if ($modules[0] ne '') { $modules = \@modules; } # match All means no limit
114     $results = GetLogs($datefrom,$dateto,$user,$modules,$action,$object,$info);
115     @data=@$results;
116     my $total = scalar @data;
117     foreach my $result (@data){
118         if (substr($result->{'info'}, 0, 4) eq 'item' || $result->{module} eq "CIRCULATION"){
119             # get item information so we can create a working link
120         my $itemnumber=$result->{'object'};
121         $itemnumber=$result->{'info'} if ($result->{module} eq "CIRCULATION");
122             my $item=GetItem($itemnumber);
123             $result->{'biblionumber'}=$item->{'biblionumber'};
124             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
125         $result->{'barcode'}=$item->{'barcode'};
126         }
127     #always add firstname and surname for librarian/user
128     if ($result->{'user'}){
129         my $userdetails = C4::Members::GetMemberDetails($result->{'user'});
130         if ($userdetails->{'firstname'}){
131             $result->{'userfirstname'} = $userdetails->{'firstname'};
132         }
133         if ($userdetails->{'surname'}){
134             $result->{'usersurname'} = $userdetails->{'surname'};
135         }
136     }
137     #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
138     if ($result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES"){
139         if($result->{'object'}){
140             my $borrowerdetails = C4::Members::GetMemberDetails($result->{'object'});
141             if ($borrowerdetails->{'firstname'}){
142             $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
143             }
144             if ($borrowerdetails->{'surname'}){
145                 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
146             }
147         }
148     }
149     }
150     
151     if ( $output eq "screen" ) {
152         # Printing results to screen
153         $template->param (
154                         logview => 1,
155             total    => $total,
156             looprow  => \@data,
157             do_it    => 1,
158             datefrom => $datefrom,
159             dateto   => $dateto,
160             user     => $user,
161             object   => $object,
162             action   => \@action,
163             info     => $info,
164             src      => $src,
165         );
166             #module   => 'fix this', #this seems unused in actual code
167         foreach my $module (@modules) {
168                 $template->param($module  => 1);
169         }
170
171         output_html_with_http_headers $input, $cookie, $template->output;
172     } else {
173         # Printing to a csv file
174         print $input->header(
175             -type       => 'text/csv',
176             -attachment => "$basename.csv",
177             -filename   => "$basename.csv"
178         );
179         my $sep = C4::Context->preference("delimiter");
180         foreach my $line (@data) {
181             #next unless $modules[0] eq "catalogue";
182                 foreach (qw(timestamp firstname surname action info title author)) {
183                         print $line->{$_} . $sep;
184                 }       
185         }
186     }
187         exit;
188 } else {
189     #my @values;
190     #my %labels;
191     #my %select;
192         #initialize some paramaters that might not be used in the template - it seems to evaluate EXPR even if a false TMPL_IF
193         $template->param(
194                 total => 0,
195                 module => "",
196                 info => ""
197         );
198         output_html_with_http_headers $input, $cookie, $template->output;
199 }