From e125ea2aa5da2c31edef197c04a4630c273c4c7a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 26 Jul 2021 18:29:16 +0200 Subject: [PATCH] Bug 28445: Use Koha::UI::Form::Builder::Item from batchmod as well Now that it's reusable, let use it somewhere else! Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- Koha/UI/Form/Builder/Item.pm | 157 +++++++++--- cataloguing/additem.pl | 11 +- .../prog/en/modules/tools/batchMod-edit.tt | 72 +----- tools/batchMod.pl | 225 +++--------------- 4 files changed, 165 insertions(+), 300 deletions(-) diff --git a/Koha/UI/Form/Builder/Item.pm b/Koha/UI/Form/Builder/Item.pm index 49683b8c02..f0794decb0 100644 --- a/Koha/UI/Form/Builder/Item.pm +++ b/Koha/UI/Form/Builder/Item.pm @@ -24,6 +24,34 @@ use C4::ClassSource qw( GetClassSources ); use Koha::DateUtils qw( dt_from_string ); use Koha::Libraries; +=head1 NAME + +Koha::UI::Form::Builder::Item + +Helper to build a form to add or edit a new item. + +=head1 API + +=head2 Class methods + +=cut + +=head3 new + + my $form = Koha::UI::Form::Builder::Item->new( + { + biblionumber => $biblionumber, + item => $current_item, + } + ); + +Constructor. +biblionumber should be passed if we are creating a new item. +For edition, an hashref representing the item to edit item must be passed. + +=cut + + sub new { my ( $class, $params ) = @_; @@ -35,6 +63,12 @@ sub new { return $self; } +=head3 generate_subfield_form + +Generate subfield's info for given tag, subfieldtag, etc. + +=cut + sub generate_subfield_form { my ($self, $params) = @_; @@ -45,8 +79,10 @@ sub generate_subfield_form { my $libraries = $params->{libraries}; my $marc_record = $params->{marc_record}; my $restricted_edition = $params->{restricted_editition}; + my $prefill_with_default_values = $params->{prefill_with_default_values}; + my $branch_limit = $params->{branch_limit}; - my $item = $self->{item}; + my $item = $self->{item}; my $subfield = $tagslib->{$tag}{$subfieldtag}; my $biblionumber = $self->{biblionumber}; @@ -79,7 +115,7 @@ sub generate_subfield_form { $subfield_data{kohafield} = $subfield->{kohafield} || 'items.more_subfields_xml'; - if ( !defined($value) || $value eq '' ) { + if ( $prefill_with_default_values && ( !defined($value) || $value eq '' ) ) { $value = $subfield->{defaultvalue}; if ($value) { @@ -108,7 +144,9 @@ sub generate_subfield_form { if ( ( $subfield->{hidden} > 4 ) || ( $subfield->{hidden} <= -4 ) ); my $pref_itemcallnumber = C4::Context->preference('itemcallnumber'); - if ( !$value + if ( $prefill_with_default_values + && !$value + && $subfield->{kohafield} && $subfield->{kohafield} eq 'items.itemcallnumber' && $pref_itemcallnumber ) { @@ -128,22 +166,6 @@ sub generate_subfield_form { } } - my $default_location = C4::Context->preference('NewItemsDefaultLocation'); - if ( !$value - && $subfield->{kohafield} eq 'items.location' - && $default_location ) - { - $value = $default_location; - } - - if ( $frameworkcode eq 'FA' - && $subfield->{kohafield} eq 'items.barcode' - && !$value ) - { - my $input = CGI->new; - $value = $input->param('barcode'); - } - if ( $subfield->{authorised_value} ) { my @authorised_values; my %authorised_lib; @@ -178,8 +200,6 @@ sub generate_subfield_form { } elsif ( $subfield->{authorised_value} eq "itemtypes" ) { push @authorised_values, ""; - my $branch_limit = - C4::Context->userenv && C4::Context->userenv->{"branch"}; my $itemtypes; if ($branch_limit) { $itemtypes = Koha::ItemTypes->search_with_localization( @@ -194,7 +214,7 @@ sub generate_subfield_form { $itemtype->translated_description; } - unless ($value) { + if (!$value && $biblionumber) { my $itype_sth = $dbh->prepare( "SELECT itemtype FROM biblioitems WHERE biblionumber = ?"); $itype_sth->execute($biblionumber); @@ -219,7 +239,7 @@ sub generate_subfield_form { $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'}; } - $value = $default_source unless ($value); + $value = $default_source if !$value && $prefill_with_default_values; #---- "true" authorised value } @@ -380,16 +400,82 @@ sub generate_subfield_form { return \%subfield_data; } + my $subfields = + Koha::UI::Form::Builder::Item->new( + { biblionumber => $biblionumber, item => $current_item } )->edit_form( + { + branchcode => $branchcode, + restricted_editition => $restrictededition, + ( + @subfields_to_prefill + ? ( subfields_to_prefill => \@subfields_to_prefill ) + : () + ), + prefill_with_default_values => 1, + branch_limit => C4::Context->userenv->{"branch"}, + } + ); + +Returns the list of subfields to display on the add/edit item form. + +Use it in the view with: + [% PROCESS subfields_for_item subfields => subfields %] + +Parameters: + +=over + +=item branchcode + +Pre-select a library (for logged in user) + +=item restricted_editition + +Flag to restrict the edition if the user does not have necessary permissions. + +=item subfields_to_prefill + +List of subfields to prefill (value of syspref SubfieldsToUseWhenPrefill) + +=item subfields_to_allow + +List of subfields to allow (value of syspref SubfieldsToAllowForRestrictedBatchmod) + +=item subfields_to_ignore + +List of subfields to ignore/skip + +=item prefill_with_default_values + +Flag to prefill with the default values defined in the framework. + +=item branch_limit + +Limit info depending on the library (so far only item types). + +=item default_branches_empty + +Flag to add an empty option to the library list. + +=back + +=cut + sub edit_form { my ( $self, $params ) = @_; my $branchcode = $params->{branchcode}; my $restricted_edition = $params->{restricted_editition}; my $subfields_to_prefill = $params->{subfields_to_prefill} || []; + my $subfields_to_allow = $params->{subfields_to_allow} || []; + my $subfields_to_ignore= $params->{subfields_to_ignore} || []; + my $prefill_with_default_values = $params->{prefill_with_default_values}; + my $branch_limit = $params->{branch_limit}; + my $libraries = Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed; for my $library (@$libraries) { - $library->{selected} = 1 if $library->{branchcode} eq $branchcode; + $library->{selected} = 1 if $branchcode && $library->{branchcode} eq $branchcode; } my $item = $self->{item}; @@ -405,7 +491,11 @@ sub edit_form { my $subfield = $tagslib->{$tag}{$subfieldtag}; next if IsMarcStructureInternal($subfield); - next if ( $subfield->{tab} ne "10" ); + next if $subfield->{tab} ne "10"; + next if @$subfields_to_allow && !grep { $subfield->{kohafield} eq $_ } @$subfields_to_allow; + next + if grep { $subfield->{kohafield} && $subfield->{kohafield} eq $_ } + @$subfields_to_ignore; my @values = (); @@ -442,12 +532,19 @@ sub edit_form { for my $value (@values) { my $subfield_data = $self->generate_subfield_form( - {tag => $tag, subfieldtag => $subfieldtag, value => $value, - tagslib => $tagslib, libraries => $libraries, - marc_record => $marc_record, restricted_edition => $restricted_edition, - }); + { + tag => $tag, + subfieldtag => $subfieldtag, + value => $value, + tagslib => $tagslib, + libraries => $libraries, + marc_record => $marc_record, + restricted_edition => $restricted_edition, + prefill_with_default_values => $prefill_with_default_values, + branch_limit => $branch_limit, + } + ); push @subfields, $subfield_data; - $i++; } } } diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index fcfb0549af..c9d3b7852d 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -572,9 +572,16 @@ my $subfields = @subfields_to_prefill ? ( subfields_to_prefill => \@subfields_to_prefill ) : () - ) + ), + prefill_with_default_values => 1, + branch_limit => C4::Context->userenv->{"branch"}, } - ); +); + +if ( $frameworkcode eq 'FA' ) { + my ( $barcode_field ) = grep {$_->{kohafield} eq 'items.barcode'} @$subfields; + $barcode_field->{marc_value}->{value} ||= $input->param('barcode'); +} if( my $default_location = C4::Context->preference('NewItemsDefaultLocation') ) { my ( $location_field ) = grep {$_->{kohafield} eq 'items.location'} @$subfields; 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 7c444d47c3..af9c38414d 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 @@ -248,77 +248,7 @@

Edit items

Checking the box right next to the subfield label will disable the entry and delete the subfield on all selected items. Leave fields blank to make no change.
-
    - [% FOREACH ite IN item %] -
  1. -
    - [% SET mv = ite.marc_value %] - [% IF ( ite.mandatory ) %] -
    -
  2. - [% END # /FOREACH ite %] -
+ [% PROCESS subfields_for_item subfields => subfields, add_regex => 1, add_delete_checkbox => 1 %]
diff --git a/tools/batchMod.pl b/tools/batchMod.pl index a753f72b00..13ecde7347 100755 --- a/tools/batchMod.pl +++ b/tools/batchMod.pl @@ -50,6 +50,7 @@ use Koha::Items; use Koha::ItemTypes; use Koha::Patrons; use Koha::SearchEngine::Indexer; +use Koha::UI::Form::Builder::Item; my $input = CGI->new; my $dbh = C4::Context->dbh; @@ -393,209 +394,39 @@ if ($op eq "show"){ # Even if we do not display the items, we need the itemnumbers $template->param(itemnumbers_array => \@itemnumbers); } -# now, build the item form for entering a new item -my @loop_data =(); -my $i=0; -my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; - -my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later. - -# Adding a default choice, in case the user does not want to modify the branch -my $nochange_branch = { branchname => '', value => '', selected => 1 }; -unshift (@$libraries, $nochange_branch); - -my $pref_itemcallnumber = C4::Context->preference('itemcallnumber'); - -# Getting list of subfields to keep when restricted batchmod edit is enabled -my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod'); -my $allowAllSubfields = ( - not defined $subfieldsToAllowForBatchmod - or $subfieldsToAllowForBatchmod eq q|| -) ? 1 : 0; -my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod); - -foreach my $tag (sort keys %{$tagslib}) { - # loop through each subfield - foreach my $subfield (sort keys %{$tagslib->{$tag}}) { - next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} ); - next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow ); - next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10"); - # barcode is not meant to be batch-modified - next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode'; - my %subfield_data; - - my $index_subfield = int(rand(1000000)); - if ($subfield eq '@'){ - $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield; - } else { - $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield; - } - $subfield_data{tag} = $tag; - $subfield_data{subfield} = $subfield; - $subfield_data{marc_lib} ="{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}.""; - $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory}; - $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable}; - my $value; - if ( $use_default_values) { - $value = $tagslib->{$tag}->{$subfield}->{defaultvalue}; - # get today date & replace YYYY, MM, DD if provided in the default value - my $today = dt_from_string; - my $year = $today->year; - my $month = $today->month; - my $day = $today->day; - $value =~ s/YYYY/$year/g; - $value =~ s/MM/$month/g; - $value =~ s/DD/$day/g; - } - $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4)); - # testing branch value if IndependentBranches. - - if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) { - my @authorised_values; - my %authorised_lib; - # builds list, depending on authorised value... - - if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) { - foreach my $library (@$libraries) { - push @authorised_values, $library->{branchcode}; - $authorised_lib{$library->{branchcode}} = $library->{branchname}; + # now, build the item form for entering a new item + my @loop_data =(); + my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; + + my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later. + + # Adding a default choice, in case the user does not want to modify the branch + my $nochange_branch = { branchname => '', value => '', selected => 1 }; + unshift (@$libraries, $nochange_branch); + + my $pref_itemcallnumber = C4::Context->preference('itemcallnumber'); + + # Getting list of subfields to keep when restricted batchmod edit is enabled + my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod') : (); + + my $subfields = Koha::UI::Form::Builder::Item->new->edit_form( + { + restricted_editition => $restrictededition, + ( + @subfields_to_allow + ? ( subfields_to_allow => \@subfields_to_allow ) + : () + ), + subfields_to_ignore => ['items.barcode'], + prefill_with_default_values => $use_default_values, } - $value = ""; - } - elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) { - push @authorised_values, ""; - my $itemtypes = Koha::ItemTypes->search_with_localization; - while ( my $itemtype = $itemtypes->next ) { - push @authorised_values, $itemtype->itemtype; - $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description; - } - $value = ""; - - #---- class_sources - } - elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) { - push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} ); - - my $class_sources = GetClassSources(); - my $default_source = C4::Context->preference("DefaultClassificationSource"); - - foreach my $class_source (sort keys %$class_sources) { - next unless $class_sources->{$class_source}->{'used'} or - ($value and $class_source eq $value) or - ($class_source eq $default_source); - push @authorised_values, $class_source; - $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'}; - } - $value = ''; - - #---- "true" authorised value - } - else { - push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} ); - - my @avs = Koha::AuthorisedValues->search_with_library_limits( - { - category => $tagslib->{$tag}->{$subfield}->{authorised_value} - }, - { order_by => 'lib' }, - $branch_limit - ); - for my $av ( @avs ) { - push @authorised_values, $av->authorised_value; - $authorised_lib{$av->authorised_value} = $av->lib; - } - $value=""; - } - $subfield_data{marc_value} = { - type => 'select', - id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield, - name => "field_value", - values => \@authorised_values, - labels => \%authorised_lib, - default => $value, - }; - # it's a thesaurus / authority field - } - elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) { - $subfield_data{marc_value} = { - type => 'text1', - id => $subfield_data{id}, - value => $value, - authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode}, - } - } - elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { # plugin - require Koha::FrameworkPlugin; - my $plugin = Koha::FrameworkPlugin->new( { - name => $tagslib->{$tag}->{$subfield}->{'value_builder'}, - item_style => 1, - }); - my $temp; - my $pars= { dbh => $dbh, record => $temp, tagslib => $tagslib, - id => $subfield_data{id} }; - $plugin->build( $pars ); - if( !$plugin->errstr ) { - $subfield_data{marc_value} = { - type => 'text2', - id => $subfield_data{id}, - value => $value, - javascript => $plugin->javascript, - noclick => $plugin->noclick, - }; - } else { - warn $plugin->errstr; - $subfield_data{marc_value} = { # supply default input form - type => 'text', - id => $subfield_data{id}, - value => $value, - }; - } - } - elsif ( $tag eq '' ) { # it's an hidden field - $subfield_data{marc_value} = { - type => 'hidden', - id => $subfield_data{id}, - value => $value, - }; - } - elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ? - $subfield_data{marc_value} = { - type => 'text', - id => $subfield_data{id}, - value => $value, - }; - } - elsif ( length($value) > 100 - or (C4::Context->preference("marcflavour") eq "UNIMARC" and - 300 <= $tag && $tag < 400 && $subfield eq 'a' ) - or (C4::Context->preference("marcflavour") eq "MARC21" and - 500 <= $tag && $tag < 600 ) - ) { - # oversize field (textarea) - $subfield_data{marc_value} = { - type => 'textarea', - id => $subfield_data{id}, - value => $value, - }; - } else { - # it's a standard field - $subfield_data{marc_value} = { - type => 'text', - id => $subfield_data{id}, - value => $value, - }; - } -# $subfield_data{marc_value}=""; - push (@loop_data, \%subfield_data); - $i++ - } -} # -- End foreach tag + ); # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit. $template->param( - item => \@loop_data, + subfields => $subfields, notfoundbarcodes => \@notfoundbarcodes, notfounditemnumbers => \@notfounditemnumbers ); -- 2.39.5