Bug 31420: Managing funds: Labels of statistic fields overlap with pull downs
[koha-ffzg.git] / tools / upload.pl
index dafb8dd..509e99b 100755 (executable)
@@ -21,10 +21,14 @@ use Modern::Perl;
 use CGI qw/-utf8/;
 use JSON;
 
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 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';
 my $plugin = $input->param('plugin');
@@ -32,12 +36,12 @@ 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   => { tools => 'upload_general_files' },
     }
 );
@@ -55,12 +59,33 @@ if ( $op eq 'new' ) {
     );
     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,
+        browsecategory => $browsecategory,
+    );
+    output_html_with_http_headers $input, $cookie, $template->output;
+
 } elsif ( $op eq 'search' ) {
     my $uploads;
-    if( $id ) {
-        my $rec = Koha::UploadedFiles->find( $id );
-        undef $rec if $rec && $plugin && !$rec->public;
-        push @$uploads, $rec->unblessed if $rec;
+    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,
@@ -83,9 +108,9 @@ if ( $op eq 'new' ) {
     my $delete = $rec ? $rec->delete : undef;
     #TODO Improve error handling
     my $msg = $delete
-        ? JSON::to_json({ $fn => { code => 6 }})
+        ? JSON::to_json({ $fn => { code => ALERT_DELETED }})
         : $id
-        ? JSON::to_json({ $fn || $id, { code => 7 }})
+        ? JSON::to_json({ $fn || $id, { code => ERR_NOT_DELETED }})
         : '';
     $template->param(
         mode             => 'deleted',
@@ -100,7 +125,7 @@ if ( $op eq 'new' ) {
     if ( !$rec || !$fh ) {
         $template->param(
             mode             => 'new',
-            msg              => JSON::to_json({ $id => { code => 5 }}),
+            msg              => JSON::to_json({ $id => { code => ERR_READING }}),
         );
         output_html_with_http_headers $input, $cookie, $template->output;
     } else {