3 # This file is part of Koha.
5 # Copyright (C) 2015 Rijksmuseum
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.
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.
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>.
26 use Koha::UploadedFiles;
28 use constant ERR_READING => 'UPLERR_FILE_NOT_READ';
29 use constant ALERT_DELETED => 'UPL_FILE_DELETED'; # alert, no error
30 use constant ERR_NOT_DELETED => 'UPLERR_FILE_NOT_DELETED';
32 my $input = CGI::->new;
33 my $op = $input->param('op') // 'new';
34 my $plugin = $input->param('plugin');
35 my $index = $input->param('index'); # MARC editor input field id
36 my $term = $input->param('term');
37 my $id = $input->param('id');
38 my $msg = $input->param('msg');
39 my $browsecategory = $input->param('browsecategory');
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42 { template_name => "tools/upload.tt",
45 flagsrequired => { tools => 'upload_general_files' },
51 owner => $loggedinuser,
53 uploadcategories => Koha::UploadedFiles->getCategories,
60 output_html_with_http_headers $input, $cookie, $template->output;
62 } elsif ( $op eq 'browse' ) {
65 $uploads = Koha::UploadedFiles->search({
66 uploadcategorycode => $browsecategory,
67 $plugin? ( public => 1 ): (),
76 output_html_with_http_headers $input, $cookie, $template->output;
78 } elsif ( $op eq 'search' ) {
80 if( $id ) { # might be a comma separated list
81 my @id = split /,/, $id;
82 foreach my $recid (@id) {
83 my $rec = Koha::UploadedFiles->find( $recid );
84 push @$uploads, $rec->unblessed
85 if $rec && ( $rec->public || !$plugin );
86 # Do not show private uploads in the plugin mode (:editor)
89 $uploads = Koha::UploadedFiles->search_term({
91 $plugin? (): ( include_private => 1 ),
100 output_html_with_http_headers $input, $cookie, $template->output;
102 } elsif ( $op eq 'delete' ) {
103 # delete only takes the id parameter
104 my $rec = Koha::UploadedFiles->find($id);
105 undef $rec if $rec && $plugin && !$rec->public;
106 my $fn = $rec ? $rec->filename : '';
107 my $delete = $rec ? $rec->delete : undef;
108 #TODO Improve error handling
110 ? JSON::to_json({ $fn => { code => ALERT_DELETED }})
112 ? JSON::to_json({ $fn || $id, { code => ERR_NOT_DELETED }})
118 output_html_with_http_headers $input, $cookie, $template->output;
120 } elsif ( $op eq 'download' ) {
121 my $rec = Koha::UploadedFiles->find( $id );
122 undef $rec if $rec && $plugin && !$rec->public;
123 my $fh = $rec? $rec->file_handle: undef;
124 if ( !$rec || !$fh ) {
127 msg => JSON::to_json({ $id => { code => ERR_READING }}),
129 output_html_with_http_headers $input, $cookie, $template->output;
131 print Encode::encode_utf8( $input->header( $rec->httpheaders ) );