Bug 17600: Standardize our EXPORT_OK
[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 qw( get_template_and_user );
25 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
26 use C4::Members;
27
28 use Koha::Patrons;
29 use Koha::Patron::Files;
30 use Koha::Patron::Categories;
31
32 my $cgi = CGI->new;
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "members/files.tt",
37         query           => $cgi,
38         type            => "intranet",
39         flagsrequired   => { borrowers => 'edit_borrowers' },
40     }
41 );
42 $template->param( 'borrower_files' => 1 );
43
44 my $borrowernumber = $cgi->param('borrowernumber');
45
46 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
47 my $patron         = Koha::Patrons->find($borrowernumber);
48 output_and_exit_if_error( $cgi, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
49
50 my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber ); # FIXME Should be $patron->get_files. Koha::Patron::Files needs to be Koha::Objects based first
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
67     my $patron_category = $patron->category;
68     $template->param( patron => $patron );
69
70     my %errors;
71
72     if ( $op eq 'upload' ) {
73         my $uploaded_file = $cgi->upload('uploadfile');
74
75         if ($uploaded_file) {
76             my $filename = $cgi->param('uploadfile');
77             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
78
79             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
80
81             if (%errors) {
82                 $template->param( errors => %errors );
83             }
84             else {
85                 my $file_content;
86                 while (<$uploaded_file>) {
87                     $file_content .= $_;
88                 }
89
90                 $bf->AddFile(
91                     name    => $filename,
92                     type    => $mimetype,
93                     content => $file_content,
94                     description => scalar $cgi->param('description'),
95                 );
96             }
97         }
98         else {
99             $errors{'no_file'} = 1;
100         }
101     } elsif ( $op eq 'delete' ) {
102         $bf->DelFile( id => scalar $cgi->param('file_id') );
103     }
104
105     $template->param(
106         files => Koha::Patron::Files->new( borrowernumber => $borrowernumber )
107           ->GetFilesInfo(),
108
109         errors => \%errors,
110     );
111     output_html_with_http_headers $cgi, $cookie, $template->output;
112 }
113
114 =head1 AUTHOR
115
116 Kyle M Hall <kyle@bywatersolutions.com>
117
118 =cut