Bug 6874: kohastructure.sql, jquery.js, refocus, and more

Two problems were discovered while doing a fresh install
of Koha. These problems in the kohastructure.sql file are
addressed with this patch.

Clicking the plug-in icon should cause the popup window
to refocus.  This adds the refocus code to the upload.pl file.

The path to the jquery.js script was wrong in the
upload_delete_file.tt file. Changed [% themelang %] to
[% interface %].

If a user clones 856$u after uploading a file, deletes the file,
and then clicks the plugin icon on the first 856$u, this will go
immediately to the upload screen with an informative error
message.

After some validation was added, it was extended to include
other cases. This serves to patch 6874 to a state where sign
off should be possible.

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
This commit is contained in:
Mark Tompsett 2013-09-26 11:45:19 -04:00 committed by Tomas Cohen Arazi
parent 79168c011b
commit 64079ccaa3
6 changed files with 172 additions and 50 deletions

View file

@ -183,44 +183,109 @@ sub UploadFile {
return;
}
=head2 DanglingEntry
C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
Determine if a entry is dangling.
Returns: 2 == no db entry
1 == no file
0 == both a file and db entry.
-1 == N/A (undef id / non-file-upload URL)
=cut
sub DanglingEntry {
my ($id,$isfileuploadurl) = @_;
my $retval;
if (defined($id)) {
my $file = GetUploadedFile($id);
if($file) {
my $file_path = $file->{filepath};
my $file_deleted = 0;
unless( -f $file_path ) {
$retval = 1;
} else {
$retval = 0;
}
}
else {
if ( $isfileuploadurl ) {
$retval = 2;
}
else {
$retval = -1;
}
}
}
else {
$retval = -1;
}
return $retval;
}
=head2 DelUploadedFile
C4::UploadedFiles::DelUploadedFile($id);
Remove a previously uploaded file, given its id.
Returns a false value if an error occurs.
Returns: 1 == file deleted
0 == file not deleted
-1== no file to delete / no meaninful id passed
=cut
sub DelUploadedFile {
my ($id) = @_;
my $retval;
my $file = GetUploadedFile($id);
if($file) {
my $file_path = $file->{filepath};
my $file_deleted = 0;
unless( -f $file_path ) {
warn "Id $file->{id} is in database but not in filesystem, removing id from database";
$file_deleted = 1;
} else {
if(unlink $file_path) {
if ($id) {
my $file = GetUploadedFile($id);
if($file) {
my $file_path = $file->{filepath};
my $file_deleted = 0;
unless( -f $file_path ) {
warn "Id $file->{id} is in database but not in filesystem, removing id from database";
$file_deleted = 1;
} else {
if(unlink $file_path) {
$file_deleted = 1;
}
}
unless($file_deleted) {
warn "File $file_path cannot be deleted: $!";
}
my $dbh = C4::Context->dbh;
my $query = qq{
DELETE FROM uploaded_files
WHERE id = ?
};
my $sth = $dbh->prepare($query);
my $numrows = $sth->execute($id);
# if either a DB entry or file was deleted,
# then clearly we have a deletion.
if ($numrows>0 || $file_deleted==1) {
$retval = 1;
}
else {
$retval = 0;
}
}
unless($file_deleted) {
warn "File $file_path cannot be deleted: $!";
else {
warn "There was no file for id=($id)";
$retval = -1;
}
my $dbh = C4::Context->dbh;
my $query = qq{
DELETE FROM uploaded_files
WHERE id = ?
};
my $sth = $dbh->prepare($query);
return $sth->execute($id);
}
else {
warn "DelUploadFile called with no id.";
$retval = -1;
}
return $retval;
}
1;

View file

@ -46,10 +46,15 @@ sub plugin_javascript {
function Clic$function_name(index) {
var id = document.getElementById(index).value;
var IsFileUploadUrl=0;
if (id.match(/opac-retrieve-file/)) {
IsFileUploadUrl=1;
}
if(id.match(/id=([0-9a-f]+)/)){
id = RegExp.\$1;
}
window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
var newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=upload.pl&index=\"+index+\"&id=\"+id+\"&from_popup=0\"+\"&IsFileUploadUrl=\"+IsFileUploadUrl, 'upload', 'width=600,height=400,toolbar=false,scrollbars=no');
newin.focus();
}
</script>
@ -64,10 +69,16 @@ sub plugin {
my $id = $input->param('id');
my $delete = $input->param('delete');
my $uploaded_file = $input->param('uploaded_file');
my $template_name = ($id || $delete)
? "upload_delete_file.tt"
: "upload.tt";
my $from_popup = $input->param('from_popup');
my $isfileuploadurl = $input->param('IsFileUploadUrl');
my $dangling = C4::UploadedFiles::DanglingEntry($id,$isfileuploadurl);
my $template_name;
if ($delete || ($id && ($dangling==0 || $dangling==1))) {
$template_name = "upload_delete_file.tt";
}
else {
$template_name = "upload.tt";
}
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{ template_name => "cataloguing/value_builder/$template_name",
@ -79,6 +90,10 @@ sub plugin {
}
);
if ($dangling==2) {
$template->param( dangling => 1 );
}
# Dealing with the uploaded file
my $dir = $input->param('dir');
if ($uploaded_file and $dir) {
@ -97,14 +112,14 @@ sub plugin {
} else {
$template->param(error => 1);
}
} elsif ($delete || $id) {
} elsif ($delete || ($id && ($dangling==0 || $dangling==1))) {
# If there's already a file uploaded for this field,
# We handle its deletion
if ($delete) {
if(C4::UploadedFiles::DelUploadedFile($id)) {;
$template->param(success => 1);
} else {
if(C4::UploadedFiles::DelUploadedFile($id)==0) {;
$template->param(error => 1);
} else {
$template->param(success => 1);
}
}
} else {
@ -129,14 +144,21 @@ sub plugin {
$template->param( error_upload_path_not_configured => 1 );
}
if (!$uploaded_file && !$dir && $from_popup) {
$template->param(error_nothing_selected => 1);
}
elsif (!$uploaded_file && $dir) {
$template->param(error_no_file_selected => 1);
}
if ($uploaded_file and not $dir) {
$template->param(error_no_dir_selected => 1);
}
}
$template->param(
index => $index,
id => $id
id => $id,
);
output_html_with_http_headers $input, $cookie, $template->output;

View file

@ -3364,7 +3364,7 @@ CREATE TABLE IF NOT EXISTS `borrower_modifications` (
-- Table structure for table uploaded_files
--
DROP TABLE IF EXISTS uploaded_files
DROP TABLE IF EXISTS uploaded_files;
CREATE TABLE uploaded_files (
id CHAR(40) NOT NULL PRIMARY KEY,
filename TEXT NOT NULL,

View file

@ -7,18 +7,32 @@
<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() {
function ValidateForm() {
var filename = document.forms["UploadForm"]["uploaded_file"].value;
var selected = 0;
var value;
$('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;
}
});
})
});
value = $(this).find('input[type="radio"][name="dir"]:checked').val();
if (value) {
selected = 1;
}
});
if (!filename && !selected) {
alert("Please select a file and its destination.");
return false;
}
else if (!filename) {
alert("Please select a file to upload.");
return false;
}
else if (!selected) {
alert("Please select a file destination.");
return false;
}
else {
return true;
}
}
</script>
</head>
@ -73,17 +87,28 @@
<p>Configuration variable 'upload_path' is not configured.</p>
<p>Please configure it in your koha-conf.xml</p>
[% ELSE %]
[% IF (error_nothing_selected) %]
<p class="error">Error: You have to choose the file to upload and select where to upload the file.</p>
[% END %]
[% IF (error_no_file_selected) %]
<p class="error">Error: You have to choose the file to upload.</p>
[% END %]
[% IF (error_no_dir_selected) %]
<p class="error">Error: You have to select the destination of uploaded file.<p>
<p class="error">Error: You have to select where to upload the file.</p>
[% END %]
[% IF (dangling) %]
<p class="error">Error: The URL has no file to retrieve.</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">
<form name="UploadForm" method="post" enctype="multipart/form-data" action="/cgi-bin/koha/cataloguing/plugin_launcher.pl" onsubmit="return ValidateForm()">
[% filefield %]
<h3>Choose where to upload file</h3>
<h3>Choose where to upload the file</h3>
[% INCLUDE list_dirs dirs = dirs_tree %]
<input type="hidden" name="from_popup" value="1" />
<input type="hidden" name="plugin_name" value="upload.pl" />
<input type="hidden" name="index" value="[% index %]" />
<input type="submit">
<input type="submit" value="Upload file">
<input type="button" value="Cancel" onclick="window.close();" />
</form>
[% END %]
[% END %]

View file

@ -4,7 +4,7 @@
<head>
<title>Upload plugin</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="[% themelang %]/lib/jquery/jquery.js"></script>
<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">
//<![CDATA[

View file

@ -3,7 +3,7 @@
use Modern::Perl;
use File::Temp qw/ tempdir /;
use Test::CGI::Multipart;
use Test::More tests => 11;
use Test::More tests => 15;
use t::lib::Mocks;
@ -35,7 +35,17 @@ foreach my $key (qw(id filename filepath dir)) {
ok(-e $file->{filepath}, "File $file->{filepath} exists");
ok(C4::UploadedFiles::DelUploadedFile($id), "DelUploadedFile($id) returned true");
ok(C4::UploadedFiles::DanglingEntry()==-1, "DanglingEntry() returned -1 as expected.");
ok(C4::UploadedFiles::DanglingEntry($id)==0, "DanglingEntry($id) returned 0 as expected.");
unlink ($file->{filepath});
ok(C4::UploadedFiles::DanglingEntry($id)==1, "DanglingEntry($id) returned 1 as expected.");
open my $fh,">",($file->{filepath});
print $fh "";
close $fh;
ok(C4::UploadedFiles::DelUploadedFile($id)==1, "DelUploadedFile($id) returned 1.");
ok(C4::UploadedFiles::DelUploadedFile($id)==-1, "DelUploadedFile($id) returned -1 as expected.");
ok(! -e $file->{filepath}, "File $file->{filepath} does not exist anymore");
is(C4::UploadedFiles::UploadFile($testfilename, '../', $testfile_fh->handle), undef, 'UploadFile with $dir containing ".." return undef');