From 2b07d491b5b55aa21a5df78f78f157369d24ccf0 Mon Sep 17 00:00:00 2001 From: Thibaud Guillot Date: Thu, 6 Apr 2023 17:16:22 +0200 Subject: [PATCH] Bug 32748: Prevent data erasing when editing items When we set an authorised value linked to one library, users who do not belong to this library do not get the value and can therefore overwrite it. This patch add current value to the dropdown list even if it comes from an authorized value of another library. Test plan : 1) Edit an item with one of these fields has an authorized value from another library. 2) See that you don't get this value on 'edit' 3) Apply this patch 4) Refresh and do step 1 5) Now you have the value in dropdown list and a tooltip info --- C4/Koha.pm | 21 ++++++++++++------- Koha/UI/Form/Builder/Item.pm | 8 +++++-- .../prog/en/includes/html_helpers.inc | 6 +++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index bee096db9fc..6fe447e6e9e 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -509,8 +509,10 @@ C<$opac> If set to a true value, displays OPAC descriptions rather than normal o =cut sub GetAuthorisedValues { - my $category = shift // ''; # optional parameter - my $opac = shift ? 1 : 0; # normalise to be safe + my $category = shift // ''; # optional parameter + my $opac = shift ? 1 : 0; # normalise to be safe + my $options = shift // ''; + my $no_limit = $options->{'no_limit'} if $options; #optional parameter to ignore library limitation # Is this cached already? my $branch_limit = C4::Context::mybranch(); @@ -521,21 +523,26 @@ sub GetAuthorisedValues { my @results; my $dbh = C4::Context->dbh; + + my $select_clause = 'SELECT av.*'; + my @where_args; + if ($branch_limit && $no_limit) { + $select_clause .= ', IF (branchcode = ? OR branchcode IS NULL, 0, 1) as restricted'; + push @where_args, $branch_limit; + } my $query = qq{ - SELECT DISTINCT av.* + $select_clause FROM authorised_values av }; $query .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id ) } if $branch_limit; my @where_strings; - my @where_args; - - if ($category) { + if($category) { push @where_strings, "category = ?"; push @where_args, $category; } - if ($branch_limit) { + if($branch_limit && !defined $no_limit) { push @where_strings, "( branchcode = ? OR branchcode IS NULL )"; push @where_args, $branch_limit; } diff --git a/Koha/UI/Form/Builder/Item.pm b/Koha/UI/Form/Builder/Item.pm index 7a2f54466f6..6b031704187 100644 --- a/Koha/UI/Form/Builder/Item.pm +++ b/Koha/UI/Form/Builder/Item.pm @@ -169,6 +169,7 @@ sub generate_subfield_form { if ( $subfield->{authorised_value} ) { my @authorised_values; my %authorised_lib; + my %authorised_restricted; # builds list, depending on authorised value... if ( $subfield->{authorised_value} eq "LOST" ) { @@ -245,10 +246,12 @@ sub generate_subfield_form { #---- "true" authorised value } else { push @authorised_values, qq{}; - my $av = GetAuthorisedValues( $subfield->{authorised_value} ); + my $av = GetAuthorisedValues( $subfield->{authorised_value}, undef, { 'no_limit' => 1 } ); for my $r (@$av) { push @authorised_values, $r->{authorised_value}; - $authorised_lib{ $r->{authorised_value} } = $r->{lib}; + + $authorised_lib{ $r->{authorised_value} } = $r->{lib}; + $authorised_restricted{ $r->{authorised_value} } = $r->{restricted}; } } @@ -270,6 +273,7 @@ sub generate_subfield_form { id => "tag_" . $tag . "_subfield_" . $subfieldtag . "_" . $index_subfield, values => \@authorised_values, labels => \%authorised_lib, + restricted => \%authorised_restricted, default => $value, ( ( grep { $_ eq $subfield->{authorised_value} } (qw(branches itemtypes cn_source)) ) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc index 81a5d33da79..46813a0561f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc @@ -124,7 +124,11 @@ [% FOREACH aval IN mv.values %] [% IF aval == mv.default %] [% SET matched = 1 %] - + [% IF mv.restricted.$aval %] + + [% ELSE %] + + [% END %] [% ELSE %] [% IF subfield.IS_LOST_AV && Koha.Preference("ClaimReturnedLostValue") && aval == Koha.Preference("ClaimReturnedLostValue") %] -- 2.39.5