Bug 12497: Fix search history non-accessible when OPAC was private
[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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24
25 use C4::Auth;
26 use C4::Output;
27 use C4::Members;
28 use C4::Members::Attributes qw(GetBorrowerAttributes);
29 use C4::Debug;
30
31 use Koha::DateUtils;
32 use Koha::Patrons;
33 use Koha::Patron::Files;
34
35 my $cgi = CGI->new;
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {
39         template_name   => "members/files.tt",
40         query           => $cgi,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { borrowers => 1 },
44         debug           => 1,
45     }
46 );
47 $template->param( 'borrower_files' => 1 );
48
49 my $borrowernumber = $cgi->param('borrowernumber');
50 my $bf = Koha::Patron::Files->new( borrowernumber => $borrowernumber );
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     my $patron = Koha::Patrons->find( $borrowernumber );
67     unless ( $patron ) {
68         print $cgi->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
69         exit;
70     }
71
72     my $patron_category = $patron->category;
73     $template->param(%{ $patron->unblessed});
74
75     my %errors;
76
77     if ( $op eq 'upload' ) {
78         my $uploaded_file = $cgi->upload('uploadfile');
79
80         if ($uploaded_file) {
81             my $filename = $cgi->param('uploadfile');
82             my $mimetype = $cgi->uploadInfo($filename)->{'Content-Type'};
83
84             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
85
86             if (%errors) {
87                 $template->param( errors => %errors );
88             }
89             else {
90                 my $file_content;
91                 while (<$uploaded_file>) {
92                     $file_content .= $_;
93                 }
94
95                 $bf->AddFile(
96                     name    => $filename,
97                     type    => $mimetype,
98                     content => $file_content,
99                     description => scalar $cgi->param('description'),
100                 );
101             }
102         }
103         else {
104             $errors{'no_file'} = 1;
105         }
106     } elsif ( $op eq 'delete' ) {
107         $bf->DelFile( id => scalar $cgi->param('file_id') );
108     }
109
110     $template->param(
111         categoryname    => $patron_category->description,
112         RoutingSerials => C4::Context->preference('RoutingSerials'),
113     );
114
115     if (C4::Context->preference('ExtendedPatronAttributes')) {
116         my $attributes = GetBorrowerAttributes($borrowernumber);
117         $template->param(
118             ExtendedPatronAttributes => 1,
119             extendedattributes => $attributes
120         );
121     }
122
123     $template->param( picture => 1 ) if $patron->image;
124
125     $template->param( adultborrower => 1 )
126         if ( $patron_category->category_type eq 'A' || $patron_category->category_type eq 'I' );
127
128     $template->param(
129         files => Koha::Patron::Files->new( borrowernumber => $borrowernumber )
130           ->GetFilesInfo(),
131
132         errors => \%errors,
133     );
134     output_html_with_http_headers $cgi, $cookie, $template->output;
135 }
136
137 =head1 AUTHOR
138
139 Kyle M Hall <kyle@bywatersolutions.com>
140
141 =cut