Bug 9367: Code optimization: CheckReserves is too often called
[koha.git] / members / files.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 ByWater Solutions
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;
22
23 use CGI;
24
25 use C4::Auth;
26 use C4::Branch;
27 use C4::Output;
28 use C4::Members;
29 use C4::Members::Attributes qw(GetBorrowerAttributes);
30 use C4::Debug;
31
32 use Koha::DateUtils;
33 use Koha::Borrower::Files;
34
35 my $cgi = CGI->new;
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {
39         template_name   => "members/files.tmpl",
40         query           => $cgi,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { borrowers => 1 },
44         debug           => 1,
45     }
46 );
47 $template->param( 'borrower_files' => 1 );
48
49 my $borrowernumber = $cgi->param('borrowernumber');
50 my $bf = Koha::Borrower::Files->new( borrowernumber => $borrowernumber );
51
52 my $op = $cgi->param('op') || '';
53
54 if ( $op eq 'download' ) {
55     my $file_id = $cgi->param('file_id');
56     my $file = $bf->GetFile( id => $file_id );
57
58     print $cgi->header(
59         -type       => $file->{'file_type'},
60         -charset    => 'utf-8',
61         -attachment => $file->{'file_name'}
62     );
63     print $file->{'file_content'};
64 }
65 else {
66     my $data = GetMember( borrowernumber => $borrowernumber );
67     $template->param(%$data);
68
69     my %errors;
70
71     if ( $op eq 'upload' ) {
72         my $uploaded_file = $cgi->upload('uploadfile');
73
74         if ($uploaded_file) {
75             my $filename = $cgi->param('uploadfile');
76             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
77
78             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
79
80             if (%errors) {
81                 $template->param( errors => %errors );
82             }
83             else {
84                 my $file_content;
85                 while (<$uploaded_file>) {
86                     $file_content .= $_;
87                 }
88
89                 $bf->AddFile(
90                     name    => $filename,
91                     type    => $mimetype,
92                     content => $file_content,
93                     description => $cgi->param('description'),
94                 );
95             }
96         }
97         else {
98             $errors{'no_file'} = 1;
99         }
100     } elsif ( $op eq 'delete' ) {
101         $bf->DelFile( id => $cgi->param('file_id') );
102     }
103
104     $template->param(
105         categoryname    => $data->{'description'},
106         branchname      => GetBranchName($data->{'branchcode'}),
107     );
108
109     if (C4::Context->preference('ExtendedPatronAttributes')) {
110         my $attributes = GetBorrowerAttributes($borrowernumber);
111         $template->param(
112             ExtendedPatronAttributes => 1,
113             extendedattributes => $attributes
114         );
115     }
116
117     my ($picture, $dberror) = GetPatronImage($data->{'cardnumber'});
118     $template->param( picture => 1 ) if $picture;
119
120     $template->param(
121         files => Koha::Borrower::Files->new( borrowernumber => $borrowernumber )
122           ->GetFilesInfo(),
123
124         errors => \%errors
125     );
126     output_html_with_http_headers $cgi, $cookie, $template->output;
127 }
128
129 =head1 AUTHOR
130
131 Kyle M Hall <kyle@bywatersolutions.com>
132
133 =cut