Followup : fb4366cdad0ad96d0427810581763dc2fc189af1
[koha.git] / tools / viewlog.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
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 $mime     = $input->param("MIME");
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->{'cardnumber'});
76     $template->param( picture => 1 ) if $picture;
77     $template->param(   menu            => 1,
78                         title           => $data->{'title'},
79                         initials        => $data->{'initials'},
80                         surname         => $data->{'surname'},
81                         borrowernumber  => $borrowernumber,
82                         firstname       => $data->{'firstname'},
83                         cardnumber      => $data->{'cardnumber'},
84                         categorycode    => $data->{'categorycode'},
85                         categoryname    => $data->{'description'},
86                         address         => $data->{'address'},
87                         address2        => $data->{'address2'},
88                         city            => $data->{'city'},
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 #### This code was never really used - maybe some day some will fix it ###
106 #my @mime = ( C4::Context->preference("MIME") );
107 #my $CGIextChoice = CGI::scrolling_list(
108 #        -name     => 'MIME',
109 #        -id       => 'MIME',
110 #        -values   => \@mime,
111 #        -size     => 1,
112 #        -multiple => 0
113 #);
114 #my @dels         = ( C4::Context->preference("delimiter") );
115 #my $CGIsepChoice = CGI::scrolling_list(
116 #        -name     => 'sep',
117 #        -id       => 'sep',
118 #        -values   => \@dels,
119 #        -size     => 1,
120 #        -multiple => 0
121 #);
122 #$template->param(
123 #        CGIextChoice => $CGIextChoice,
124 #        CGIsepChoice => $CGIsepChoice,
125 #);
126 #
127 if ($do_it) {
128
129     my $results = GetLogs($datefrom,$dateto,$user,\@modules,$action,$object,$info);
130     my $total = scalar @$results;
131     foreach my $result (@$results){
132         if ($result->{'info'} eq 'item'){
133             # get item information so we can create a working link
134             my $item=GetItem($result->{'object'});
135             $result->{'biblionumber'}=$item->{'biblionumber'};
136             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
137         }
138     }
139     
140     if ( $output eq "screen" ) {
141         # Printing results to screen
142         $template->param (
143                         logview => 1,
144             total    => $total,
145             looprow  => $results,
146             do_it    => 1,
147             datefrom => $datefrom,
148             dateto   => $dateto,
149             user     => $user,
150             object   => $object,
151             action   => $action,
152             info     => $info,
153             src      => $src,
154         );
155             #module   => 'fix this', #this seems unused in actual code
156         foreach my $module (@modules) {
157                 $template->param($module  => 1);
158         }
159
160         output_html_with_http_headers $input, $cookie, $template->output;
161     } else {
162         # Printing to a csv file
163         print $input->header(
164             -type       => 'text/csv',
165             -attachment => "$basename.csv",
166             -filename   => "$basename.csv"
167         );
168         my $sep = C4::Context->preference("delimiter");
169         foreach my $line (@$results) {
170             #next unless $modules[0] eq "catalogue";
171                 foreach (qw(timestamp firstname surname action info title author)) {
172                         print $line->{$_} . $sep;
173                 }       
174         }
175     }
176         exit;
177 } else {
178     #my @values;
179     #my %labels;
180     #my %select;
181         #initialize some paramaters that might not be used in the template - it seems to evaluate EXPR even if a false TMPL_IF
182         $template->param(
183                 total => 0,
184                 module => "",
185                 info => ""
186         );
187         output_html_with_http_headers $input, $cookie, $template->output;
188 }