From 09c144815bef912bf3463bf2d1400c85f18aeb2a Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 6 Dec 2018 09:43:42 +0100 Subject: [PATCH] Bug 21959: Batch item modification - ability to apply regular expression on text fields Test plan: - go to Tools > Batch item modification, - select items to modify, - for one or some text fields, check the second checkbox (Use regular expression) and type your regex, - check the regex has been applied. Signed-off-by: Pierre-Marc Thibault Signed-off-by: Mathilde Formery Signed-off-by: Jonathan Druart --- .../prog/en/modules/tools/batchMod-edit.tt | 30 +++++++++++++++++++ tools/batchMod.pl | 35 ++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt index 30afde8508..8097f4185f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt @@ -24,17 +24,37 @@ $(document).ready(function(){ var row = $(this).attr("id"); row = row.replace("row","hint"); var todisable = $(this).parent().find("[name='field_value'],[name='tag'],[name='subfield'],[name='mandatory']"); + var regex_input = $(this).parent().find("[name='field_regex']"); if ($(this).is(":checked")) { $(todisable).prop('disabled', true); $("#"+row).html(_("This subfield will be deleted")); + $(regex_input).prop('disabled', true); } else { $(todisable).prop('disabled', false); $("#"+row).html(""); + $(regex_input).prop('disabled', false); } }); $("#mainformsubmit").on("click",function(){ return submitBackgroundJob(this.form); }); + $('input[name="field_regex"]').change(function() { + var id = $(this).attr('id'); + var editor = $(this).parent().find("[name='field_value']"); + var regex = $(this).parent().find("[name='regex_fields']"); + var disable_input = $(this).parent().find("[name='disable_input']"); + if ($(this).is(':checked')) { + $(editor).hide(); + $(regex).show(); + $(this).parent().find('.hint').html(_("Enter a regular expression for this subflied")); + $(disable_input).prop('disabled', true); + } else { + $(editor).show(); + $(regex).hide(); + $(this).parent().find('.hint').html(_("")); + $(disable_input).prop('disabled', false); + } + }); }); //]]> @@ -285,12 +305,22 @@ $(document).ready(function(){ [%- END -%] + + s// + / + + + [% UNLESS ( ite.mandatory ) %] [% ELSE %] Required [% END %] + [% IF (mv.type == 'text' || mv.type == 'textarea' ) %] + + [% END %] + diff --git a/tools/batchMod.pl b/tools/batchMod.pl index 9ed3301471..7077ca277f 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -105,6 +105,9 @@ if ($op eq "action") { my @tags = $input->multi_param('tag'); my @subfields = $input->multi_param('subfield'); my @values = $input->multi_param('field_value'); + my @searches = $input->multi_param('regex_search'); + my @replaces = $input->multi_param('regex_replace'); + my @modifiers = $input->multi_param('regex_modifiers'); my @disabled = $input->multi_param('disable_input'); # build indicator hash. my @ind_tag = $input->multi_param('ind_tag'); @@ -112,8 +115,9 @@ if ($op eq "action") { # Is there something to modify ? # TODO : We shall use this var to warn the user in case no modification was done to the items - my $values_to_modify = scalar(grep {!/^$/} @values); + my $values_to_modify = scalar(grep {!/^$/} @values) || scalar(grep {!/^$/} @searches); my $values_to_blank = scalar(@disabled); + my $marcitem; # Once the job is done @@ -217,8 +221,32 @@ if ($op eq "action") { } else { if ($values_to_modify || $values_to_blank) { my $localmarcitem = Item2Marc($itemdata); + my $modified = 0; + + for ( my $i = 0 ; $i < @tags ; $i++ ) { + my $search = $searches[$i]; + next unless $search; + + my $tag = $tags[$i]; + my $subfield = $subfields[$i]; + my $replace = $replaces[$i]; + my $mod = $modifiers[$i]; + + my $value = $localmarcitem->field( $tag )->subfield( $subfield ); + my $old_value = $value; + ## no critic (StringyEval) + eval "\$value =~ s/$search/$replace/$mod"; + + my @fields_to = $localmarcitem->field($tag); + foreach my $field_to_update ( @fields_to ) { + unless ( $old_value eq $value ) { + $modified++; + $field_to_update->update( $subfield => $value ); + } + } + } - my $modified = UpdateMarcWith( $marcitem, $localmarcitem ); + $modified += UpdateMarcWith( $marcitem, $localmarcitem ); if ( $modified ) { eval { if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) { @@ -634,6 +662,9 @@ sub UpdateMarcWith { my $fieldfrom=$marcfrom->field($itemtag); my @fields_to=$marcto->field($itemtag); my $modified = 0; + + return $modified unless $fieldfrom; + foreach my $subfield ( $fieldfrom->subfields() ) { foreach my $field_to_update ( @fields_to ) { if ( $subfield->[1] ) { -- 2.11.0