From e1eb8e543a36fbfb9f30ae38fe9ed68883844b94 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Mon, 30 Sep 2013 09:34:01 +0200
Subject: [PATCH] Bug 6874: Force user to select a directory

This patch adds a javascript and a server-side checks

Test plan:
1/ Choose a file, leave the radios not selected and submit
2/ You have a javascript alert which prevents form to be submitted
3/ Disable javascript and repeat step 1
4/ Form is submitted but form is redisplayed with an error message
telling you to choose a directory.

Signed-off-by: Mark Tompsett <mtompset@hotmail.com>
---
 cataloguing/value_builder/upload.pl                     |  8 ++++++--
 .../prog/en/modules/cataloguing/value_builder/upload.tt | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/cataloguing/value_builder/upload.pl b/cataloguing/value_builder/upload.pl
index 4e6683a..966f0b3 100755
--- a/cataloguing/value_builder/upload.pl
+++ b/cataloguing/value_builder/upload.pl
@@ -80,9 +80,9 @@ sub plugin {
     );
 
     # Dealing with the uploaded file
-    if ($uploaded_file) {
+    my $dir = $input->param('dir');
+    if ($uploaded_file and $dir) {
         my $fh = $input->upload('uploaded_file');
-        my $dir = $input->param('dir');
 
         $id = C4::UploadedFiles::UploadFile($uploaded_file, $dir, $fh->handle);
         if($id) {
@@ -128,6 +128,10 @@ sub plugin {
         } else {
             $template->param( error_upload_path_not_configured => 1 );
         }
+
+        if ($uploaded_file and not $dir) {
+            $template->param(error_no_dir_selected => 1);
+        }
     }
 
     $template->param(
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt
index cd983ad..63849f8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/value_builder/upload.tt
@@ -6,6 +6,20 @@
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script type="text/javascript" src="[% interface %]/lib/jquery/jquery.js"></script>
     <link rel="stylesheet" type="text/css" href="[% themelang %]/css/staff-global.css" />
+    <script type="text/javascript">
+      function _(s) { return s; }
+      $(document).ready(function() {
+        $('form').each(function() {
+          $(this).submit(function() {
+            var value = $(this).find('input[type="radio"][name="dir"]:checked').val();
+            if (!value) {
+              alert(_("Please select the destination of file"));
+              return false;
+            }
+          });
+        })
+      });
+    </script>
 
 </head>
 <body>
@@ -59,6 +73,9 @@
           <p>Configuration variable 'upload_path' is not configured.</p>
           <p>Please configure it in your koha-conf.xml</p>
         [% ELSE %]
+          [% IF (error_no_dir_selected) %]
+              <p class="error">Error: You have to select the destination of uploaded file.<p>
+          [% END %]
           <h2>Please select the file to upload : </h2>
           <form method="post" enctype="multipart/form-data" action="/cgi-bin/koha/cataloguing/plugin_launcher.pl">
               [% filefield %]
-- 
1.9.1