Merge remote branch 'kc/master' into new/bug_5817
[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                         zipcode         => $data->{'zipcode'},
89                         phone           => $data->{'phone'},
90                         phonepro        => $data->{'phonepro'},
91                         email           => $data->{'email'},
92                         branchcode      => $data->{'branchcode'},
93                         branchname              => GetBranchName($data->{'branchcode'}),
94     );
95 }
96
97 $template->param(
98         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
99                       dateformat => C4::Dates->new()->format(),
100                                        debug => $debug,
101         C4::Search::enabled_staff_search_views,
102 );
103
104 if ($do_it) {
105
106     my $results = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
107     my $total = scalar @$results;
108     foreach my $result (@$results){
109         if ($result->{'info'} eq 'item'){
110             # get item information so we can create a working link
111             my $item=GetItem($result->{'object'});
112             $result->{'biblionumber'}=$item->{'biblionumber'};
113             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
114         }
115     }
116     
117     if ( $output eq "screen" ) {
118         # Printing results to screen
119         $template->param (
120                         logview => 1,
121             total    => $total,
122             looprow  => $results,
123             do_it    => 1,
124             datefrom => $datefrom,
125             dateto   => $dateto,
126             user     => $user,
127             object   => $object,
128             action   => $action,
129             info     => $info,
130             src      => $src,
131         );
132             #module   => 'fix this', #this seems unused in actual code
133         foreach my $module (@modules) {
134                 $template->param($module  => 1);
135         }
136
137         output_html_with_http_headers $input, $cookie, $template->output;
138     } else {
139         # Printing to a csv file
140         print $input->header(
141             -type       => 'text/csv',
142             -attachment => "$basename.csv",
143             -filename   => "$basename.csv"
144         );
145         my $sep = C4::Context->preference("delimiter");
146         foreach my $line (@$results) {
147             #next unless $modules[0] eq "catalogue";
148                 foreach (qw(timestamp firstname surname action info title author)) {
149                         print $line->{$_} . $sep;
150                 }       
151         }
152     }
153         exit;
154 } else {
155     #my @values;
156     #my %labels;
157     #my %select;
158         #initialize some paramaters that might not be used in the template - it seems to evaluate EXPR even if a false TMPL_IF
159         $template->param(
160                 total => 0,
161                 module => "",
162                 info => ""
163         );
164         output_html_with_http_headers $input, $cookie, $template->output;
165 }