Modifications to viewlog.pl to keep it in the context from which it was called.
[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 Data::Dumper;
31
32 use vars qw($debug);
33
34 BEGIN {
35         $debug = $ENV{DEBUG} || 0;
36 }
37
38 =head1 viewlog.pl
39
40 plugin that shows a stats on borrowers
41
42 =cut
43
44 my $input    = new CGI;
45
46 $debug or $debug = $input->param('debug') || 0;
47 my $do_it    = $input->param('do_it');
48 my $module   = $input->param("module");
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 $mime     = $input->param("MIME");
57 my $del      = $input->param("sep");
58 my $output   = $input->param("output") || "screen";
59 my $src      = $input->param("src");    # this param allows us to be told where we were called from -fbcit
60
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62     {
63         template_name   => "tools/viewlog.tmpl",
64         query           => $input,
65         type            => "intranet",
66         authnotrequired => 0,
67         flagsrequired   => { tools => 1 },
68         debug           => 1,
69     }
70 );
71
72 if ($src eq 'circ') {   # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
73     use C4::Members;
74     my $borrowernumber = $object;
75     my $data = GetMember($borrowernumber,'borrowernumber');
76     my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
77     $template->param( picture => 1 ) if $picture;
78     $template->param(   menu            => 1,
79                         title           => $data->{'title'},
80                         initials        => $data->{'initials'},
81                         surname         => $data->{'surname'},
82                         borrowernumber  => $borrowernumber,
83                         firstname       => $data->{'firstname'},
84                         cardnumber      => $data->{'cardnumber'},
85                         categorycode    => $data->{'categorycode'},
86                         address         => $data->{'address'},
87                         address2        => $data->{'address2'},
88                         city            => $data->{'city'},
89                         phone           => $data->{'phone'},
90                         phonepro        => $data->{'phonepro'},
91                         email           => $data->{'email'},
92                         branchcode      => $data->{'branchcode'},
93     );
94 }
95
96 $template->param(
97         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
98                       dateformat => C4::Dates->new()->format(),
99                                        debug => $debug,
100 );
101
102 if ($do_it) {
103
104     my $results = GetLogs($datefrom,$dateto,$user,$module,$action,$object,$info);
105     my $total = scalar @$results;
106     warn "Total records retrieved = $total";
107     foreach my $result (@$results){
108         if ($result->{'info'} eq 'item'){
109             # get item information so we can create a working link
110             my $item=GetItem($result->{'object'});
111             $result->{'biblionumber'}=$item->{'biblionumber'};
112             $result->{'biblioitemnumber'}=$item->{'biblionumber'};              
113         }
114     }
115     
116     if ( $output eq "screen" ) {
117         # Printing results to screen
118         $template->param (
119                         logview => 1,
120             total    => $total,
121             $module  => 1,
122             looprow  => $results,
123             do_it    => 1,
124             datefrom => $datefrom,
125             dateto   => $dateto,
126             user     => $user,
127             module   => $module,
128             object   => $object,
129             action   => $action,
130             info     => $info,
131             src      => $src,
132         );
133         output_html_with_http_headers $input, $cookie, $template->output;
134     } else {
135         # Printing to a csv file
136         print $input->header(
137             -type       => 'application/vnd.sun.xml.calc',
138             -attachment => "$basename.csv",
139             -filename   => "$basename.csv"
140         );
141         my $sep = C4::Context->preference("delimiter");
142         foreach my $line (@$results) {
143             ($module eq "catalogue") or next;
144                         foreach (qw(timestamp firstname surname action info title author)) {
145                                 print $line->{$_} . $sep;
146                         }       
147                 }
148     }
149         exit;
150 } else {
151     my @values;
152     my %labels;
153     my %select;
154     my @mime = ( C4::Context->preference("MIME") );
155     my $CGIextChoice = CGI::scrolling_list(
156         -name     => 'MIME',
157         -id       => 'MIME',
158         -values   => \@mime,
159         -size     => 1,
160         -multiple => 0
161     );
162     my @dels         = ( C4::Context->preference("delimiter") );
163     my $CGIsepChoice = CGI::scrolling_list(
164         -name     => 'sep',
165         -id       => 'sep',
166         -values   => \@dels,
167         -size     => 1,
168         -multiple => 0
169     );
170     $template->param(
171         total        => 0,
172         CGIextChoice => $CGIextChoice,
173         CGIsepChoice => $CGIsepChoice,
174     );
175     output_html_with_http_headers $input, $cookie, $template->output;
176 }