Bugzilla – Attachment 100296 Details for
Bug 23925
Value builder plugin to check ISBN validity
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 23925: Value builder plugin to check ISBN validity
Bug-23925-Value-builder-plugin-to-check-ISBN-valid.patch (text/plain), 9.09 KB, created by
Andreas Roussos
on 2020-03-06 21:38:20 UTC
(
hide
)
Description:
Bug 23925: Value builder plugin to check ISBN validity
Filename:
MIME Type:
Creator:
Andreas Roussos
Created:
2020-03-06 21:38:20 UTC
Size:
9.09 KB
patch
obsolete
>From 4cd8062477aeaaee8af5573f613c591fb4018394 Mon Sep 17 00:00:00 2001 >From: Pasi Kallinen <pasi.kallinen@koha-suomi.fi> >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 <pasi.kallinen@koha-suomi.fi> >Signed-off-by: Devinim <kohadevinim@devinim.com.tr> >Signed-off-by: Andreas Roussos <a.roussos@dataly.gr> >--- > 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 a50328a1f6..90ffe483c8 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 d363e3f668..b02e652ad4 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; >+} >+ > </script>|; > }; > # 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 0000000000..5a766cd2fa >--- /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 <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use C4::Context; >+use C4::Languages; >+ >+my $builder = sub { >+ my ( $params ) = @_; >+ my $function_name = $params->{id}; >+ >+ my $js = <<END_OF_JS; >+<script type="text/javascript"> >+//<![CDATA[ >+ >+function CheckISBN$function_name(str) { >+ var sum, >+ weight, >+ digit, >+ check, >+ i; >+ str = str.replace(/[^0-9X]/gi, ''); >+ if (str.length != 10 && str.length != 13) { >+ return false; >+ } >+ >+ if (str.length == 13) { >+ sum = 0; >+ for (i = 0; i < 12; i++) { >+ digit = parseInt(str[i]); >+ if (i % 2 == 1) { >+ sum += 3*digit; >+ } else { >+ sum += digit; >+ } >+ } >+ check = (10 - (sum % 10)) % 10; >+ return (check == str[str.length-1]); >+ } >+ >+ if (str.length == 10) { >+ weight = 10; >+ sum = 0; >+ for (i = 0; i < 9; i++) { >+ digit = parseInt(str[i]); >+ sum += weight*digit; >+ weight--; >+ } >+ check = 11 - (sum % 11); >+ if (check == 10) { >+ check = 'X'; >+ } >+ return (check == str[str.length-1].toUpperCase()); >+ } >+} >+ >+function Blur$function_name(id) { >+ var v = \$('#' + id).val(); >+ \$('#' + id).val(v.trim()); >+ return false; >+} >+ >+function SaveCheck$function_name(id) { >+ var v = \$('#' + id).val(); >+ if (v && v != '') { >+ if (!CheckISBN$function_name(v)) { >+ return { funcname: id, msg: _("Illegal ISBN") }; >+ } >+ } >+ return undefined; >+} >+ >+//]]> >+</script> >+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 4fda2747a9..438bb5542e 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<len ; i++){ > var tag=subfields[i].substr(4,3); > var subfield=subfields[i].substr(17,1); >diff --git a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js >index a172f5f1c0..66436275dc 100644 >--- a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js >+++ b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js >@@ -112,6 +112,13 @@ function CloneField(index, hideMarc, advancedMARCEditor) { > > clone.setAttribute('id',new_id); // setting a new id for the parent li > >+ var oinputs = original.getElementsByTagName('input'); >+ for (var i=0; i < oinputs.length; i++) { >+ if ($(oinputs[i]).data('savecheck')) { >+ $(clone).find("#"+oinputs[i].getAttribute('id')).data('savecheck', $(oinputs[i]).data('savecheck')); >+ } >+ } >+ > var divs = Array.from(clone.getElementsByTagName('li')).concat(Array.from(clone.getElementsByTagName('div'))); > > // if hide_marc, indicators are hidden fields >-- >2.11.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 23925
:
94861
|
99977
|
100267
|
100296
|
100299
|
100300
|
100301
|
100318
|
100319