3 # Copyright 2010 BibLibre
4 # Copyright 2011 MJ Ray and software.coop
6 # This file is part of Koha.
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
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.
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.
25 use Text::CSV::Encoded;
34 use C4::Search; # enabled_staff_search_views
36 use vars qw($debug $cgi_debug);
40 plugin that shows stats
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 $output = $input->param("output") || "screen";
57 my $src = $input->param("src"); # this param allows us to be told where we were called from -fbcit
59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
61 template_name => "tools/viewlog.tmpl",
65 flagsrequired => { tools => 'view_system_logs' },
70 if ( $src eq 'circ' ) {
72 # if we were called from circulation, use the circulation menu and get data to populate it -fbcit
74 my $borrowernumber = $object;
75 my $data = GetMember( 'borrowernumber' => $borrowernumber );
76 my ( $picture, $dberror ) = GetPatronImage( $data->{'borrowernumber'} );
77 $template->param( picture => 1 ) if $picture;
79 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
80 my $attributes = GetBorrowerAttributes( $data->{'borrowernumber'} );
82 ExtendedPatronAttributes => 1,
83 extendedattributes => $attributes
87 # Computes full borrower address
88 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $data->{streettype} );
89 my $address = $data->{'streetnumber'} . " $roadtype " . $data->{'address'};
93 title => $data->{'title'},
94 initials => $data->{'initials'},
95 surname => $data->{'surname'},
96 othernames => $data->{'othernames'},
97 borrowernumber => $borrowernumber,
98 firstname => $data->{'firstname'},
99 cardnumber => $data->{'cardnumber'},
100 categorycode => $data->{'categorycode'},
101 category_type => $data->{'category_type'},
102 categoryname => $data->{'description'},
104 address2 => $data->{'address2'},
105 city => $data->{'city'},
106 state => $data->{'state'},
107 zipcode => $data->{'zipcode'},
108 country => $data->{'country'},
109 phone => $data->{'phone'},
110 phonepro => $data->{'phonepro'},
111 mobile => $data->{'mobile'},
112 email => $data->{'email'},
113 emailpro => $data->{'emailpro'},
114 branchcode => $data->{'branchcode'},
115 branchname => GetBranchName( $data->{'branchcode'} ),
116 RoutingSerials => C4::Context->preference('RoutingSerials'),
122 C4::Search::enabled_staff_search_views,
128 my ( $results, $modules, $action );
129 if ( $action[0] ne '' ) { $action = \@action; } # match All means no limit
130 if ( $modules[0] ne '' ) { $modules = \@modules; } # match All means no limit
131 $results = GetLogs( $datefrom, $dateto, $user, $modules, $action, $object, $info );
133 foreach my $result (@data) {
135 # Init additional columns for CSV export
136 $result->{'biblionumber'} = q{};
137 $result->{'biblioitemnumber'} = q{};
138 $result->{'barcode'} = q{};
139 $result->{'userfirstname'} = q{};
140 $result->{'usersurname'} = q{};
141 $result->{'borrowerfirstname'} = q{};
142 $result->{'borrowersurname'} = q{};
144 if ( substr( $result->{'info'}, 0, 4 ) eq 'item' || $result->{module} eq "CIRCULATION" ) {
146 # get item information so we can create a working link
147 my $itemnumber = $result->{'object'};
148 $itemnumber = $result->{'info'} if ( $result->{module} eq "CIRCULATION" );
149 my $item = GetItem($itemnumber);
151 $result->{'biblionumber'} = $item->{'biblionumber'};
152 $result->{'biblioitemnumber'} = $item->{'biblionumber'};
153 $result->{'barcode'} = $item->{'barcode'};
157 #always add firstname and surname for librarian/user
158 if ( $result->{'user'} ) {
159 my $userdetails = C4::Members::GetMemberDetails( $result->{'user'} );
161 $result->{'userfirstname'} = $userdetails->{'firstname'};
162 $result->{'usersurname'} = $userdetails->{'surname'};
166 #add firstname and surname for borrower, when using the CIRCULATION, MEMBERS, FINES
167 if ( $result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES" ) {
168 if ( $result->{'object'} ) {
169 my $borrowerdetails = C4::Members::GetMemberDetails( $result->{'object'} );
170 if ($borrowerdetails) {
171 $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'};
172 $result->{'borrowersurname'} = $borrowerdetails->{'surname'};
178 if ( $output eq "screen" ) {
180 # Printing results to screen
183 total => scalar @data,
186 datefrom => $datefrom,
196 foreach my $module (@modules) {
197 $template->param( $module => 1 );
200 output_html_with_http_headers $input, $cookie, $template->output;
204 # Printing to a csv file
206 my $delimiter = C4::Context->preference('delimiter') || ',';
208 my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } );
209 $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag();
211 # First line with heading
212 # Exporting bd id seems useless
213 my @headings = grep { $_ ne 'action_id' } sort keys %{$data[0]};
214 if ( $csv->combine(@headings) ) {
215 $content .= $csv->string() . "\n";
219 foreach my $line (@data) {
220 my @cells = map { $line->{$_} } @headings;
221 if ( $csv->combine(@cells) ) {
222 $content .= $csv->string() . "\n";
228 print $input->header(
230 -attachment => $basename . '.csv',
237 output_html_with_http_headers $input, $cookie, $template->output;