From 9225eb320d03a7ebb247f14aaaf99a3787a9743c 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 item 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 | 11 ++++++++--- Koha/UI/Form/Builder/Item.pm | 10 ++++++++-- .../intranet-tmpl/prog/en/includes/html_helpers.inc | 10 +++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index 7132910adf..67d765771d 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -486,20 +486,24 @@ C<$opac> If set to a true value, displays OPAC descriptions rather than normal o sub GetAuthorisedValues { 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 paramater to ignore branch limitation # Is this cached already? my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : ""; my $cache_key = - "AuthorisedValues-$category-$opac-$branch_limit"; + "AuthorisedValues-$category-$opac-$branch_limit-$no_limit"; my $cache = Koha::Caches->get_instance(); my $result = $cache->get_from_cache($cache_key); return $result if $result; my @results; my $dbh = C4::Context->dbh; + my $select_clause = 'SELECT av.*'; + $select_clause .= ", GROUP_CONCAT( authorised_values_branches.branchcode SEPARATOR '-' ) as restricted_to" if $no_limit; my $query = qq{ - SELECT DISTINCT av.* + $select_clause FROM authorised_values av }; $query .= qq{ @@ -511,13 +515,14 @@ sub GetAuthorisedValues { 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; } if(@where_strings > 0) { $query .= " WHERE " . join(" AND ", @where_strings); } + $query .= ' GROUP BY av.id '; $query .= ' ORDER BY category, ' . ( $opac ? 'COALESCE(lib_opac, lib)' : 'lib, lib_opac' diff --git a/Koha/UI/Form/Builder/Item.pm b/Koha/UI/Form/Builder/Item.pm index 3a478ee05e..c0783ce893 100644 --- a/Koha/UI/Form/Builder/Item.pm +++ b/Koha/UI/Form/Builder/Item.pm @@ -251,11 +251,17 @@ sub generate_subfield_form { } else { push @authorised_values, qq{}; - my $av = GetAuthorisedValues( $subfield->{authorised_value} ); + my $av = GetAuthorisedValues( $subfield->{authorised_value}, undef, { 'no_limit' => 1 } ); + my @restricted_av; for my $r (@$av) { - push @authorised_values, $r->{authorised_value}; + if ( defined $r->{restricted_to} ) { + push @restricted_av, {code => $r->{authorised_value}, lib => $r->{lib}, restricted_to => $r->{restricted_to}}; + } else { + push @authorised_values, $r->{authorised_value}; + } $authorised_lib{ $r->{authorised_value} } = $r->{lib}; } + $subfield_data{restricted_av} = \@restricted_av if scalar(@restricted_av) > 0; } if ( $subfield->{hidden} > 4 or $subfield->{hidden} <= -4 ) { 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 f0d695bc9c..477bbbbbd6 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/html_helpers.inc @@ -124,6 +124,15 @@ [% END %] [% END %] [% END %] + [% FOREACH r_av IN subfield.restricted_av %] + [% SET r_av_lib = r_av.lib %] + [% SET r_av_code = r_av.code %] + [% SET r_av_from = r_av.restricted_to %] + [% IF r_av_code == mv.default %] + [% SET matched = 1 %] + [% END %] + + [% END %] [% UNLESS matched || ( ( kohafield == 'items.damaged' || kohafield == 'items.itemlost' || kohafield == 'items.withdrawn' || kohafield == 'items.notforloan' ) && mv.default == '0' ) %] [%# If the current value is not in the authorised list and is not a field where 0 means unset #%] @@ -210,7 +219,6 @@ [% END %] - [% END %] -- 2.25.1