From 949b8fc3e5435da4d16d8167a8dc5b7f2956a7b3 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Wed, 30 Oct 2019 10:50:58 +0200 Subject: [PATCH] Bug 23925: Value builder plugin to check ISBN validity This patch adds a new value builder plugin ISBN.pl, which should be attached to field 020a. The plugin will perform a simple checksum checking when trying to save the record, and inform the user that the ISBN is illegal if the checksum doesn't match. The ISBN plugin is a test case for additional value builder plugin functionality that lets the plugins check field values and prevent saving if so desired. The example plugin has been updated with information on how to write a pre-save check function. To test: 1) Add the ISBN.pl plugin to the field 020a in MARC subfield structure admin. 2) Go to cataloguing and try to save the record, no complaints about the ISBN (Because the plugin will not complain about empty field) 3) Enter something in the 020a field and try to save, you should receive a complaint about illegal ISBN. 4) Enter a valid ISBN (eg. 978-951-1-27641-8) and try to save, there should be no complaints about the ISBN. Signed-off-by: Pasi Kallinen Signed-off-by: Devinim --- Koha/FrameworkPlugin.pm | 5 +- cataloguing/value_builder/EXAMPLE.pl | 11 +++ cataloguing/value_builder/ISBN.pl | 96 ++++++++++++++++++++++ .../prog/en/modules/cataloguing/addbiblio.tt | 32 ++++++++ koha-tmpl/intranet-tmpl/prog/js/cataloging.js | 7 ++ 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 cataloguing/value_builder/ISBN.pl diff --git a/Koha/FrameworkPlugin.pm b/Koha/FrameworkPlugin.pm index a50328a1f65..90ffe483c83 100644 --- a/Koha/FrameworkPlugin.pm +++ b/Koha/FrameworkPlugin.pm @@ -301,7 +301,7 @@ sub _process_javascript { my $bind = ''; my $clickfound = 0; my @events = qw|click focus blur change mouseover mouseout mousedown - mouseup mousemove keydown keypress keyup|; + mouseup mousemove keydown keypress keyup savecheck|; foreach my $ev ( @events ) { my $scan = $ev eq 'click' && $self->{oldschool}? 'clic': $ev; if( $script =~ /function\s+($scan\w+)\s*\(([^\)]*)\)/is ) { @@ -334,6 +334,9 @@ sub _add_binding { } elsif( $fname eq 'noclick' ) { # no click: return false, no scroll $bind= qq| \$("#$ctl").$ev(function () { return false; });\n|; $script=''; + } elsif( $fname =~ /^savecheck/i ) { + $bind= qq| \$("#$ctl").data('savecheck', '$fname');\n|; + $script=''; } else { # add real event handler calling the function found $bind=qq| \$("#$ctl").$ev(\{id: '$id'\}, ${fname}_handler);\n|; $script = $self->_add_handler( $ev, $fname ); diff --git a/cataloguing/value_builder/EXAMPLE.pl b/cataloguing/value_builder/EXAMPLE.pl index d363e3f6684..b02e652ad4f 100644 --- a/cataloguing/value_builder/EXAMPLE.pl +++ b/cataloguing/value_builder/EXAMPLE.pl @@ -94,6 +94,17 @@ function Click$id(event) { window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=EXAMPLE.pl&index=\"+event.data.id+\"&result=\"+fieldvalue,\"tag_editor\",'width=700,height=700,toolbar=false,scrollbars=yes'); return false; // prevents scrolling } + +/* SaveCheck-function is used to prevent saving. + Note: Parameter is id of the element, not an event. */ +function SaveCheck$id(id) { + var fieldvalue=\$('#'+id).val(); + if (v && v != '' && v != 'abc') { + return { funcname: id, msg: "Sorry, field must contain 'abc'" }; + } + return undefined; +} + |; }; # NOTE: Did you see the last semicolon? This was just an assignment! diff --git a/cataloguing/value_builder/ISBN.pl b/cataloguing/value_builder/ISBN.pl new file mode 100644 index 00000000000..5a766cd2fa8 --- /dev/null +++ b/cataloguing/value_builder/ISBN.pl @@ -0,0 +1,96 @@ +#!/usr/bin/perl + +# Copyright 2019 KohaSuomi oy +# +# 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 3 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, see . + +use Modern::Perl; + +use C4::Context; +use C4::Languages; + +my $builder = sub { + my ( $params ) = @_; + my $function_name = $params->{id}; + + my $js = < +// + +END_OF_JS + return $js; +}; + +return { builder => $builder }; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt index 4fda2747a9b..438bb5542ea 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt @@ -283,6 +283,17 @@ } } + function ValuePluginParseFunctionName(funcname) { + var re = /^tag_(...)_subfield_(.)_/; + var found = funcname.match(re); + return { field: found[1], subfield: found[2] }; + } + + function ValuePluginHighlightField(funcname) { + document.getElementById(funcname).setAttribute('class','subfield_not_filled'); + document.getElementById(funcname).focus(); + } + /** * check if mandatory/important subfields are written * @param mandatory true to check for mandatories, false for importants @@ -331,6 +342,27 @@ } StrAlert += "\n\n"; + + /* Allow value builder plugins to perform extra checks before saving the record */ + $('input').each(function() { + var d = $(this).data('savecheck'); + if (d && d != '') { + var fn = window[d]; + if (typeof fn === 'function') { + var err = fn(this.id); + if (err) { + var val = $(this).val(); + StrAlert += "\t* "; + var f = ValuePluginParseFunctionName(err.funcname); + StrAlert += _("tag %s subfield %s: %s (%s)").format(f.field, f.subfield, err.msg, val); + StrAlert += "\n"; + ValuePluginHighlightField(err.funcname); + flag=1; + } + } + } + }); + for(var i=0,len=subfields.length; i