Merge remote-tracking branch 'origin/new/bug_7729'
[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::Output;
27 use C4::Members;
28 use C4::Debug;
29
30 use Koha::DateUtils;
31 use Koha::Borrower::Files;
32
33 my $cgi = CGI->new;
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "members/files.tmpl",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { borrowers => 1 },
42         debug           => 1,
43     }
44 );
45 $template->param( 'borrower_files' => 1 );
46
47 my $borrowernumber = $cgi->param('borrowernumber');
48 my $bf = Koha::Borrower::Files->new( borrowernumber => $borrowernumber );
49
50 my $op = $cgi->param('op') || '';
51
52 if ( $op eq 'download' ) {
53     my $file_id = $cgi->param('file_id');
54     my $file = $bf->GetFile( id => $file_id );
55
56     print $cgi->header(
57         -type       => $file->{'file_type'},
58         -charset    => 'utf-8',
59         -attachment => $file->{'file_name'}
60     );
61     print $file->{'file_content'};
62 }
63 else {
64     my $data = GetMember( borrowernumber => $borrowernumber );
65     $template->param(%$data);
66
67     my %errors;
68
69     if ( $op eq 'upload' ) {
70         my $uploaded_file = $cgi->upload('uploadfile');
71
72         if ($uploaded_file) {
73             my $filename = $cgi->param('uploadfile');
74             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
75
76             $errors{'empty_upload'} = 1 unless ( length($uploaded_file) > 0 );
77
78             if (%errors) {
79                 $template->param( errors => %errors );
80             }
81             else {
82                 my $file_content;
83                 while (<$uploaded_file>) {
84                     $file_content .= $_;
85                 }
86
87                 $bf->AddFile(
88                     name    => $filename,
89                     type    => $mimetype,
90                     content => $file_content,
91                     description => $cgi->param('description'),
92                 );
93             }
94         }
95         else {
96             $errors{'no_file'} = 1;
97         }
98     } elsif ( $op eq 'delete' ) {
99         $bf->DelFile( id => $cgi->param('file_id') );
100     }
101
102     $template->param(
103         files => Koha::Borrower::Files->new( borrowernumber => $borrowernumber )
104           ->GetFilesInfo(),
105
106         errors => %errors
107     );
108     output_html_with_http_headers $cgi, $cookie, $template->output;
109 }
110
111 =head1 AUTHOR
112
113 Kyle M Hall <kyle@bywatersolutions.com>
114
115 =cut