From b3d670d43152faa5c7b066bb8be5ace8d40b46e7 Mon Sep 17 00:00:00 2001 From: Adrien Saurat Date: Fri, 2 Mar 2012 11:36:06 +0100 Subject: [PATCH] Bug 7422: prevent duplicates on vendor names Will ensure that: - a name is present and not made of spaces - the name does not contain quote characters - the name is unique (ajax script added) --- acqui/check_duplicate_bookseller_ajax.pl | 56 ++++++++++++++++++ .../prog/en/modules/acqui/supplier.tt | 60 ++++++++++++++++++-- 2 files changed, 111 insertions(+), 5 deletions(-) create mode 100755 acqui/check_duplicate_bookseller_ajax.pl diff --git a/acqui/check_duplicate_bookseller_ajax.pl b/acqui/check_duplicate_bookseller_ajax.pl new file mode 100755 index 0000000..fbe48ad --- /dev/null +++ b/acqui/check_duplicate_bookseller_ajax.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# BibLibre, 2012 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use warnings; +use CGI; +use CGI::Cookie; +use JSON; +use C4::Auth; +use C4::Bookseller qw/GetBookSeller/; +use C4::Context; + +my $input = new CGI; +print $input->header('application/json'); + +# Check the user's permissions +my %cookies = fetch CGI::Cookie; +my $sessid = $cookies{'CGISESSID'}->value || $input->param('CGISESSID'); +my ( $auth_status, $auth_sessid ) = + C4::Auth::check_cookie_auth( $sessid, { acquisition => 'order_manage' } ); +if ( $auth_status ne "ok" ) { + print to_json( { status => 'UNAUTHORIZED' } ); + exit 0; +} + +my $json; + +# Check if the bookseller already exists. +my @booksellers = GetBookSeller($input->param('companyName')); +if ( scalar(@booksellers) > 0 ) { + foreach my $bookseller (@booksellers) { + if ( $input->param('companyName') eq $bookseller->{'name'} ) { + $json->{status} = "DUPLICATES"; + } + } +} + +$json->{status} = 'OK' unless defined $json->{status}; +print to_json($json); + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tt index f30d839..3f874c7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/supplier.tt @@ -9,11 +9,61 @@ function confirm_deletion() { } } function check(f) { -if (f.company.value == "") { - alert(_("You must specify a name for this vendor.")); - return false; -} - f.submit(); +// invalid characters + var patt=/"/g; + var companyName = f.company.value; + companyName = companyName.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); + [% IF ( id ) %] + var id = "[% id %]"; + var originalName = "[% name %]"; // vendor name at the loading of the page (useful when editing) + [% ELSE %] + var id = ""; + var originalName = ""; + [% END %] + + if (companyName == "") { + alert(_("You must specify a name for this vendor.")); + return false; + } + else if (patt.test(companyName) ) { + alert(_("The vendor name contains invalid characters.")); + return false; + } + else { + if ( (id != "") && (companyName == originalName) ) { + // vendor has been modified but name stayed the same -> no need to check for duplicates + f.submit(); + } + else { + // AJAX check for duplicate vendor names + $.ajax({ + url: "/cgi-bin/koha/acqui/check_duplicate_bookseller_ajax.pl", + async:false, + method: "post", + data: {companyName : companyName}, + dataType: 'json', + + error: function(xhr) { + alert("Error: \n" + xhr.responseText); + }, + success: function(json) { + switch(json.status) { + case 'UNAUTHORIZED': + alert(_("Error: Duplicate bookseller verification failed. Insufficient user permissions.")); + return false; + break; + case 'DUPLICATES': + alert(_("This vendor name already exists in the database.")); + return false; + break; + case 'OK': + f.submit(); + break; + } + }, + }); + } + } } //]]> -- 1.7.4.1