@@ -, +, @@ structure admin. about the ISBN (Because the plugin will not complain about empty field) receive a complaint about illegal ISBN. there should be no complaints about the ISBN. --- 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 --- a/Koha/FrameworkPlugin.pm +++ a/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 ); --- a/cataloguing/value_builder/EXAMPLE.pl +++ a/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! --- a/cataloguing/value_builder/ISBN.pl +++ a/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 }; --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt +++ a/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