Bug 20009: use Modern::Perl in Members perl scripts
[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 use C4::Members::Attributes qw(GetBorrowerAttributes);
28 use C4::Debug;
29
30 use Koha::DateUtils;
31 use Koha::Patrons;
32 use Koha::Patron::Files;
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         authnotrequired => 0,
42         flagsrequired   => { borrowers => 1 },
43         debug           => 1,
44     }
45 );
46 $template->param( 'borrower_files' => 1 );
47
48 my $borrowernumber = $cgi->param('borrowernumber');
49 my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
50
51 my $op = $cgi->param('op') || '';
52
53 if ( $op eq 'download' ) {
54     my $file_id = $cgi->param('file_id');
55     my $file = $bf->GetFile( id => $file_id );
56
57     print $cgi->header(
58         -type       => $file->{'file_type'},
59         -charset    => 'utf-8',
60         -attachment => $file->{'file_name'}
61     );
62     print $file->{'file_content'};
63 }
64 else {
65     my $patron = Koha::Patrons->find( $borrowernumber );
66     unless ( $patron ) {
67         print $cgi->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
68         exit;
69     }
70
71     my $patron_category = $patron->category;
72     $template->param(%{ $patron->unblessed});
73
74     my %errors;
75
76     if ( $op eq 'upload' ) {
77         my $uploaded_file = $cgi->upload('uploadfile');
78
79         if ($uploaded_file) {
80             my $filename = $cgi->param('uploadfile');
81             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
82
83             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
84
85             if (%errors) {
86                 $template->param( errors => %errors );
87             }
88             else {
89                 my $file_content;
90                 while (<$uploaded_file>) {
91                     $file_content .= $_;
92                 }
93
94                 $bf->AddFile(
95                     name    => $filename,
96                     type    => $mimetype,
97                     content => $file_content,
98                     description => scalar $cgi->param('description'),
99                 );
100             }
101         }
102         else {
103             $errors{'no_file'} = 1;
104         }
105     } elsif ( $op eq 'delete' ) {
106         $bf->DelFile( id => scalar $cgi->param('file_id') );
107     }
108
109     $template->param(
110         categoryname    => $patron_category->description,
111     );
112
113     if (C4::Context->preference('ExtendedPatronAttributes')) {
114         my $attributes = GetBorrowerAttributes($borrowernumber);
115         $template->param(
116             ExtendedPatronAttributes => 1,
117             extendedattributes => $attributes
118         );
119     }
120
121     $template->param( picture => 1 ) if $patron->image;
122
123     $template->param( adultborrower => 1 )
124         if ( $patron_category->category_type eq 'A' || $patron_category->category_type eq 'I' );
125
126     $template->param(
127         files => Koha::Patron::Files->new( borrowernumber => $borrowernumber )
128           ->GetFilesInfo(),
129
130         errors => \%errors,
131     );
132     output_html_with_http_headers $cgi, $cookie, $template->output;
133 }
134
135 =head1 AUTHOR
136
137 Kyle M Hall <kyle@bywatersolutions.com>
138
139 =cut