From 9ee70fb3daeab9ab4f96c2c9f4bf6e25345a7bdd Mon Sep 17 00:00:00 2001 From: Adrien Saurat Date: Wed, 18 Jan 2012 11:40:35 +0100 Subject: [PATCH] Bug 7422: name control for new vendor 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 | 54 ++++++++++++++++++++ .../prog/en/modules/acqui/supplier.tt | 47 +++++++++++++++-- 2 files changed, 96 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..da5c8ea --- /dev/null +++ b/acqui/check_duplicate_bookseller_ajax.pl @@ -0,0 +1,54 @@ +#!/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(''); +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..7dbf09b 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,48 @@ 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 (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 { + // 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