Bug 17501: Remove Koha::Upload::get from Koha::Upload
[koha.git] / tools / upload.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2015 Rijksmuseum
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 use CGI qw/-utf8/;
22 use JSON;
23
24 use C4::Auth;
25 use C4::Output;
26 use Koha::Upload;
27 use Koha::UploadedFiles;
28
29 my $input  = CGI::->new;
30 my $op     = $input->param('op') // 'new';
31 my $plugin = $input->param('plugin');
32 my $index  = $input->param('index');         # MARC editor input field id
33 my $term   = $input->param('term');
34 my $id     = $input->param('id');
35 my $msg    = $input->param('msg');
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {   template_name   => "tools/upload.tt",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { tools => 'upload_general_files' },
43     }
44 );
45
46 $template->param(
47     index      => $index,
48     owner      => $loggedinuser,
49     plugin     => $plugin,
50 );
51
52 if ( $op eq 'new' ) {
53     $template->param(
54         mode             => 'new',
55         uploadcategories => Koha::Upload->getCategories,
56     );
57     output_html_with_http_headers $input, $cookie, $template->output;
58
59 } elsif ( $op eq 'search' ) {
60     my $uploads;
61     if( $id ) {
62         my $rec = Koha::UploadedFiles->search({
63             id => $id,
64             $plugin? ( public => 1 ) : (),
65         })->next;
66         push @$uploads, $rec->unblessed if $rec;
67     } else {
68         $uploads = Koha::UploadedFiles->search_term({
69             term => $term,
70             $plugin? (): ( include_private => 1 ),
71         })->unblessed;
72     }
73
74     $template->param(
75         mode    => 'report',
76         msg     => $msg,
77         uploads => $uploads,
78     );
79     output_html_with_http_headers $input, $cookie, $template->output;
80
81 } elsif ( $op eq 'delete' ) {
82     # delete only takes the id parameter
83     my $upload = $plugin?
84          Koha::UploadedFiles->search({ public => 1, id => $id })->next:
85          Koha::UploadedFiles->find($id);
86     my $fn = $upload? $upload->delete: undef;
87     #TODO Improve error handling
88     my $msg = $fn?
89         JSON::to_json({ $fn => 6 }):
90         JSON::to_json({
91             $upload? $upload->filename: ( $id? "id $id": '[No id]' ), 7,
92         });
93     $template->param(
94         mode             => 'deleted',
95         msg              => $msg,
96         uploadcategories => Koha::Upload->getCategories,
97     );
98     output_html_with_http_headers $input, $cookie, $template->output;
99
100 } elsif ( $op eq 'download' ) {
101     my $rec = Koha::UploadedFiles->search({
102         id => $id,
103         $plugin? ( public => 1 ) : (),
104     })->next;
105     my $fh  = $rec? $rec->file_handle:  undef;
106     if ( !$rec || !$fh ) {
107         $template->param(
108             mode             => 'new',
109             msg              => JSON::to_json( { $id => 5 } ),
110             uploadcategories => Koha::Upload->getCategories,
111         );
112         output_html_with_http_headers $input, $cookie, $template->output;
113     } else {
114         my @hdr = Koha::Upload->httpheaders( $rec->filename );
115         print Encode::encode_utf8( $input->header(@hdr) );
116         while (<$fh>) {
117             print $_;
118         }
119         $fh->close;
120     }
121 }