Bug 19074: Fix category display in Batch patron modification.
[koha.git] / tools / upload.pl
index 45b8d1c..fd70b13 100755 (executable)
@@ -23,7 +23,7 @@ use JSON;
 
 use C4::Auth;
 use C4::Output;
-use Koha::Upload;
+use Koha::UploadedFiles;
 
 my $input  = CGI::->new;
 my $op     = $input->param('op') // 'new';
@@ -46,56 +46,65 @@ $template->param(
     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 'search' ) {
-    my $h = $id ? { id => $id } : { term => $term };
-    my @uploads = Koha::Upload->new($upar)->get($h);
+    my $uploads;
+    if( $id ) {
+        my $rec = Koha::UploadedFiles->find( $id );
+        undef $rec if $rec && $plugin && !$rec->public;
+        push @$uploads, $rec->unblessed if $rec;
+    } 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' ) {
     # 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 => 6 })
+        : $id
+        ? JSON::to_json({ $fn || $id, 7 })
+        : '';
     $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,
         );
         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 $_;
         }