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