X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=tools%2Fupload.pl;h=cf9a2c605363951d8323982666ab29e9b1b82170;hb=5866c88d840709bdae2b6ef7e573b6fa2890141d;hp=5d9c8e7719afd0bae298617ca58d24266a9719bd;hpb=83ea49a0095853ab414981ff11b93b6f97886fc9;p=koha-ffzg.git diff --git a/tools/upload.pl b/tools/upload.pl index 5d9c8e7719..cf9a2c6053 100755 --- a/tools/upload.pl +++ b/tools/upload.pl @@ -23,7 +23,11 @@ use JSON; use C4::Auth; use C4::Output; -use Koha::Upload; +use Koha::UploadedFiles; + +use constant ERR_READING => 'UPLERR_FILE_NOT_READ'; +use constant ALERT_DELETED => 'UPL_FILE_DELETED'; # alert, no error +use constant ERR_NOT_DELETED => 'UPLERR_FILE_NOT_DELETED'; my $input = CGI::->new; my $op = $input->param('op') // 'new'; @@ -32,67 +36,99 @@ my $index = $input->param('index'); # MARC editor input field id my $term = $input->param('term'); my $id = $input->param('id'); my $msg = $input->param('msg'); +my $browsecategory = $input->param('browsecategory'); my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { template_name => "tools/upload.tt", query => $input, type => "intranet", - authnotrequired => 0, - flagsrequired => { editcatalogue => '*' }, + flagsrequired => { tools => 'upload_general_files' }, } ); $template->param( - plugin => $plugin, - index => $index, + index => $index, + owner => $loggedinuser, + plugin => $plugin, + uploadcategories => Koha::UploadedFiles->getCategories, ); -my $upar = $plugin ? { public => 1 } : {}; if ( $op eq 'new' ) { $template->param( mode => 'new', - uploadcategories => Koha::Upload->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; + +} elsif ( $op eq 'browse' ) { + my $uploads; + if ($browsecategory){ + $uploads = Koha::UploadedFiles->search({ + uploadcategorycode => $browsecategory, + $plugin? ( public => 1 ): (), + })->unblessed; + } + + $template->param( + mode => 'report', + msg => $msg, + uploads => $uploads, + ); + output_html_with_http_headers $input, $cookie, $template->output; + } elsif ( $op eq 'search' ) { - my $h = $id ? { id => $id } : { term => $term }; - my @uploads = Koha::Upload->new($upar)->get($h); + my $uploads; + if( $id ) { # might be a comma separated list + my @id = split /,/, $id; + foreach my $recid (@id) { + my $rec = Koha::UploadedFiles->find( $recid ); + push @$uploads, $rec->unblessed + if $rec && ( $rec->public || !$plugin ); + # Do not show private uploads in the plugin mode (:editor) + } + } else { + $uploads = Koha::UploadedFiles->search_term({ + term => $term, + $plugin? (): ( include_private => 1 ), + })->unblessed; + } + $template->param( mode => 'report', msg => $msg, - uploads => \@uploads, + uploads => $uploads, ); output_html_with_http_headers $input, $cookie, $template->output; -} elsif ( $op eq 'delete' ) { +} elsif ( $op eq 'delete' ) { # delete only takes the id parameter - my $upl = Koha::Upload->new($upar); - my ($fn) = $upl->delete( { id => $id } ); - my $e = $upl->err; - my $msg = - $fn ? JSON::to_json( { $fn => 6 } ) - : $e ? JSON::to_json($e) - : undef; + my $rec = Koha::UploadedFiles->find($id); + undef $rec if $rec && $plugin && !$rec->public; + my $fn = $rec ? $rec->filename : ''; + my $delete = $rec ? $rec->delete : undef; + #TODO Improve error handling + my $msg = $delete + ? JSON::to_json({ $fn => { code => ALERT_DELETED }}) + : $id + ? JSON::to_json({ $fn || $id, { code => ERR_NOT_DELETED }}) + : ''; $template->param( mode => 'deleted', msg => $msg, - uploadcategories => $upl->getCategories, ); output_html_with_http_headers $input, $cookie, $template->output; + } elsif ( $op eq 'download' ) { - my $upl = Koha::Upload->new($upar); - my $rec = $upl->get( { id => $id, filehandle => 1 } ); - my $fh = $rec->{fh}; + my $rec = Koha::UploadedFiles->find( $id ); + undef $rec if $rec && $plugin && !$rec->public; + my $fh = $rec? $rec->file_handle: undef; if ( !$rec || !$fh ) { $template->param( mode => 'new', - msg => JSON::to_json( { $id => 5 } ), - uploadcategories => $upl->getCategories, + msg => JSON::to_json({ $id => { code => ERR_READING }}), ); output_html_with_http_headers $input, $cookie, $template->output; } else { - my @hdr = $upl->httpheaders( $rec->{name} ); - print Encode::encode_utf8( $input->header(@hdr) ); + print Encode::encode_utf8( $input->header( $rec->httpheaders ) ); while (<$fh>) { print $_; }