Merge remote branch 'kc/new/pending_qa/enh/bug_3644' into kcmaster
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Dates;
27 use C4::Output;
28 use C4::Log;
29 use C4::Items;
30 use C4::Branch;
31 use C4::Debug;
32 # use Data::Dumper;
33 use C4::Search;         # enabled_staff_search_views
34
35 use vars qw($debug $cgi_debug);
36
37 =head1 viewlog.pl
38
39 plugin that shows stats
40
41 =cut
42
43 my $input    = new CGI;
44
45 $debug or $debug = $cgi_debug;
46 my $do_it    = $input->param('do_it');
47 my @modules   = $input->param("modules");
48 my $user     = $input->param("user");
49 my @action   = $input->param("action");
50 my $object   = $input->param("object");
51 my $info     = $input->param("info");
52 my $datefrom = $input->param("from");
53 my $dateto   = $input->param("to");
54 my $basename = $input->param("basename");
55 #my $del      = $input->param("sep");
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
58
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
60     {
61         template_name   => "tools/viewlog.tmpl",
62         query           => $input,
63         type            => "intranet",
64         authnotrequired => 0,
65         flagsrequired   => { tools => 'view_system_logs' },
66         debug           => 1,
67     }
68 );
69
70 if ($src eq 'circ') {   # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
71     use C4::Members;
72     my $borrowernumber = $object;
73     my $data = GetMember('borrowernumber'=>$borrowernumber);
74     my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
75     $template->param( picture => 1 ) if $picture;
76     $template->param(   menu            => 1,
77                         title           => $data->{'title'},
78                         initials        => $data->{'initials'},
79                         surname         => $data->{'surname'},
80                         borrowernumber  => $borrowernumber,
81                         firstname       => $data->{'firstname'},
82                         cardnumber      => $data->{'cardnumber'},
83                         categorycode    => $data->{'categorycode'},
84                         categoryname    => $data->{'description'},
85                         address         => $data->{'address'},
86                         address2        => $data->{'address2'},
87                         city            => $data->{'city'},
88                         state           => $data->{'state'},
89                         zipcode         => $data->{'zipcode'},
90                         phone           => $data->{'phone'},
91                         phonepro        => $data->{'phonepro'},
92                         email           => $data->{'email'},
93                         branchcode      => $data->{'branchcode'},
94                         branchname              => GetBranchName($data->{'branchcode'}),
95     );
96 }
97
98 $template->param(
99         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
100                       dateformat => C4::Dates->new()->format(),
101                                        debug => $debug,
102         C4::Search::enabled_staff_search_views,
103 );
104
105 if ($do_it) {
106
107     my @data;
108     my $results = GetLogs($datefrom,$dateto,$user,\@modules,\@action,$object,$info);
109     @data=@$results;
110     my $total = scalar @data;
111     foreach my $result (@data){
112         if ($result->{'info'} eq 'item'||$result->{module} eq "CIRCULATION"){
113             # get item information so we can create a working link
114         my $itemnumber=$result->{'object'};
115         $itemnumber=$result->{'info'} if ($result->{module} eq "CIRCULATION");
116             my $item=GetItem($itemnumber);
117             $result->{'biblionumber'}=$item->{'biblionumber'};
118             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
119         }
120     }
121     
122     if ( $output eq "screen" ) {
123         # Printing results to screen
124         $template->param (
125                         logview => 1,
126             total    => $total,
127             looprow  => \@data,
128             do_it    => 1,
129             datefrom => $datefrom,
130             dateto   => $dateto,
131             user     => $user,
132             object   => $object,
133             action   => \@action,
134             info     => $info,
135             src      => $src,
136         );
137             #module   => 'fix this', #this seems unused in actual code
138         foreach my $module (@modules) {
139                 $template->param($module  => 1);
140         }
141
142         output_html_with_http_headers $input, $cookie, $template->output;
143     } else {
144         # Printing to a csv file
145         print $input->header(
146             -type       => 'text/csv',
147             -attachment => "$basename.csv",
148             -filename   => "$basename.csv"
149         );
150         my $sep = C4::Context->preference("delimiter");
151         foreach my $line (@data) {
152             #next unless $modules[0] eq "catalogue";
153                 foreach (qw(timestamp firstname surname action info title author)) {
154                         print $line->{$_} . $sep;
155                 }       
156         }
157     }
158         exit;
159 } else {
160     #my @values;
161     #my %labels;
162     #my %select;
163         #initialize some paramaters that might not be used in the template - it seems to evaluate EXPR even if a false TMPL_IF
164         $template->param(
165                 total => 0,
166                 module => "",
167                 info => ""
168         );
169         output_html_with_http_headers $input, $cookie, $template->output;
170 }