Bug 28572: Remove C4::Debug
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
24 use C4::Auth;
25 use C4::Output;
26 use C4::Members;
27
28 use Koha::DateUtils;
29 use Koha::Patrons;
30 use Koha::Patron::Files;
31 use Koha::Patron::Categories;
32
33 my $cgi = CGI->new;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "members/files.tt",
38         query           => $cgi,
39         type            => "intranet",
40         flagsrequired   => { borrowers => 'edit_borrowers' },
41     }
42 );
43 $template->param( 'borrower_files' => 1 );
44
45 my $borrowernumber = $cgi->param('borrowernumber');
46
47 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
48 my $patron         = Koha::Patrons->find($borrowernumber);
49 output_and_exit_if_error( $cgi, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
50
51 my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber ); # FIXME Should be $patron->get_files. Koha::Patron::Files needs to be Koha::Objects based first
52
53 my $op = $cgi->param('op') || '';
54
55 if ( $op eq 'download' ) {
56     my $file_id = $cgi->param('file_id');
57     my $file = $bf->GetFile( id => $file_id );
58
59     print $cgi->header(
60         -type       => $file->{'file_type'},
61         -charset    => 'utf-8',
62         -attachment => $file->{'file_name'}
63     );
64     print $file->{'file_content'};
65 }
66 else {
67
68     my $patron_category = $patron->category;
69     $template->param( patron => $patron );
70
71     my %errors;
72
73     if ( $op eq 'upload' ) {
74         my $uploaded_file = $cgi->upload('uploadfile');
75
76         if ($uploaded_file) {
77             my $filename = $cgi->param('uploadfile');
78             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
79
80             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
81
82             if (%errors) {
83                 $template->param( errors => %errors );
84             }
85             else {
86                 my $file_content;
87                 while (<$uploaded_file>) {
88                     $file_content .= $_;
89                 }
90
91                 $bf->AddFile(
92                     name    => $filename,
93                     type    => $mimetype,
94                     content => $file_content,
95                     description => scalar $cgi->param('description'),
96                 );
97             }
98         }
99         else {
100             $errors{'no_file'} = 1;
101         }
102     } elsif ( $op eq 'delete' ) {
103         $bf->DelFile( id => scalar $cgi->param('file_id') );
104     }
105
106     $template->param(
107         files => Koha::Patron::Files->new( borrowernumber => $borrowernumber )
108           ->GetFilesInfo(),
109
110         errors => \%errors,
111     );
112     output_html_with_http_headers $cgi, $cookie, $template->output;
113 }
114
115 =head1 AUTHOR
116
117 Kyle M Hall <kyle@bywatersolutions.com>
118
119 =cut