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 <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2021-07-26 18:29:16 +02:00
parent 103ae6edcc
commit e125ea2aa5
4 changed files with 160 additions and 295 deletions

View File

@ -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++;
}
}
}

View File

@ -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;

View File

@ -248,77 +248,7 @@
<h2>Edit items</h2>
<div class="hint">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.</div>
<fieldset class="rows">
<ol>
[% FOREACH ite IN item %]
<li>
<div class="subfield_line" style="[% ite.visibility | html %]" id="subfield[% ite.tag | html %][% ite.subfield | html %][% ite.random | html %]">
[% SET mv = ite.marc_value %]
[% IF ( ite.mandatory ) %]
<label class="required" for="[%- mv.id | html -%]">
[% ELSE %]
<label for="[%- mv.id | html -%]">
[% END %]
[% ite.subfield | html %] - [% ite.marc_lib | $raw %]
</label>
[% IF ( mv.type == 'select' ) -%]
<select name="[%- mv.name | html -%]" id="[%- mv.id | html -%]" tabindex="1" class="input_marceditor select2">
[%- FOREACH aval IN mv.values %]
[% ite.subfield | html %] -
[% IF aval == mv.default %]
<option value="[%- aval | html -%]" selected="selected">[%- mv.labels.$aval | html -%]</option>
[% ELSE %]
<option value="[%- aval | html -%]">[%- mv.labels.$aval | html -%]</option>
[% END %]
[%- END -%]
</select>
[% ELSIF ( mv.type == 'text1' ) %]
<input type="text" tabindex="1" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="255" value="[%- mv.value | html -%]" />
<a href="#" class="buttonDot" onclick="Dopop('/cgi-bin/koha/authorities/auth_finder.pl?authtypecode=[%- mv.authtypecode | uri -%]&index=[%- mv.id | uri -%]','[%- mv.id | uri -%]'); return false;" title="Tag editor">...</a>
[% ELSIF ( mv.type == 'text2' ) %]
<input type="text" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="255" value="[%- mv.value | html -%]" />
[% IF mv.noclick %]
<a href="#" class="buttonDot disabled" title="No popup">...</a>
[% ELSE %]
<a href="#" id="buttonDot_[% mv.id | html %]" class="buttonDot" title="Tag editor">...</a>
[% END %]
[% ELSIF ( mv.type == 'text' ) %]
<input type="text" tabindex="1" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="255" value="[%- mv.value | html -%]" />
[% ELSIF ( mv.type == 'hidden' ) %]
<input type="hidden" tabindex="1" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="255" value="[%- mv.value | html -%]" />
[% ELSIF ( mv.type == 'textarea' ) %]
<textarea tabindex="1" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="255">[%- mv.value | html -%]"</textarea>
[%- END # /IF mv.type == ... -%]
<span name="regex_fields" style="display: none;">
s/<input type="text" id="[% mv.id | html %]" name="regex_search" placeholder="regex pattern" />/
<input type="text" id="[% mv.id | html %]" name="regex_replace" placeholder="regex replacement" />/
<input type="text" id="[% mv.id | html %]" name="regex_modifiers" placeholder="ig" size="3" />
</span>
[% UNLESS ( ite.mandatory ) %]
<input type="checkbox" id="row[% ite.tag | html %][% ite.subfield | html %][% ite.random | html %]" title="Check to delete subfield [% ite.subfield | html %]" name="disable_input" value="[% ite.subfield | html %]" />
[% ELSE %]
<span class="required">Required</span>
[% END %]
[% IF (mv.type == 'text' || mv.type == 'text2' || mv.type == 'textarea' ) %]
<a href="#" name="field_regex" id="[% ite.id | html %]" >RegEx</a>
[% END %]
<input type="hidden" name="tag" value="[% ite.tag | html %]" />
<input type="hidden" name="subfield" value="[% ite.subfield | html %]" />
<input type="hidden" name="mandatory" value="[% ite.mandatory | html %]" />
[% IF ( ite.repeatable ) %]
<a href="#" class="buttonPlus" onclick="CloneItemSubfield(this.parentNode.parentNode); return false;">
<img src="[% interface | html %]/[% theme | html %]/img/clone-subfield.png" alt="Clone" title="Clone this subfield" />
</a>
[% END %]
<span class="hint" id="hint[% ite.tag | html %][% ite.subfield | html %][% ite.random | html %]"></span>
</div> <!-- /.subfield_line -->
</li>
[% END # /FOREACH ite %]
</ol>
[% PROCESS subfields_for_item subfields => subfields, add_regex => 1, add_delete_checkbox => 1 %]
</fieldset>
<fieldset class="rows">

View File

@ -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"} : "";
# 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.
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);
# 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');
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);
# Getting list of subfields to keep when restricted batchmod edit is enabled
my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod') : ();
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} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
$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};
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}="<input type=\"text\" name=\"field_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
);