From eec37fbd240e9df93fa140b68463d621a2e000ee Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 12 Jun 2020 18:11:41 +0200
Subject: [PATCH] Bug 19361: Add a tooltip warning if value not in AV list

(This patch depends on bug 25728)

When cataloguing it may happen that a previous value of a subfield is
not longer in the list of AVs.

Prior to this patch the value was removed.

This patch suggests to display a tooltip next to the problematic
subfield. If nothing is done, the value is kept.

This patch work when cataloguing a bibliographic record, and
adding/editing items.

It could be implemented easily for authority cataloguing.
---
 cataloguing/addbiblio.pl                                 | 1 +
 cataloguing/additem.pl                                   | 2 ++
 .../prog/en/modules/cataloguing/addbiblio.tt             | 5 ++++-
 .../intranet-tmpl/prog/en/modules/cataloguing/additem.tt | 8 ++++++--
 koha-tmpl/intranet-tmpl/prog/js/cataloging.js            | 9 +++++++++
 5 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl
index d8d376ea4f..6386cf9ef5 100755
--- a/cataloguing/addbiblio.pl
+++ b/cataloguing/addbiblio.pl
@@ -227,6 +227,7 @@ sub build_authorized_values_list {
         name     => "tag_".$tag."_subfield_".$subfield."_".$index_tag."_".$index_subfield,
         default  => $value,
         values   => \@authorised_values,
+        not_in_av => ( ( grep { $_ eq $value } @authorised_values ) ? 0 : 1 ),
         labels   => \%authorised_lib,
         ( ( grep { $_ eq $category } ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $category ) ),
     };
diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index 89b72690f5..e2e9cb5882 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -231,6 +231,7 @@ sub generate_subfield_form {
                     id          => $subfield_data{id},
                     maxlength   => $subfield_data{maxlength},
                     value       => $value,
+                    not_in_av => ( ( grep { $_ eq $value } @authorised_values ) ? 0 : 1 ),
                     ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
@@ -241,6 +242,7 @@ sub generate_subfield_form {
                     values   => \@authorised_values,
                     labels   => \%authorised_lib,
                     default  => $value,
+                    not_in_av => ( ( grep { $_ eq $value } @authorised_values ) ? 0 : 1 ),
                     ( ( grep { $_ eq $subfieldlib->{authorised_value}} ( qw(branches itemtypes cn_source) ) ) ? () : ( category => $subfieldlib->{authorised_value}) ),
                 };
             }
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
index e5fdff84de..3e9ee85982 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
@@ -843,7 +843,10 @@
                                                 [% ELSIF ( mv.type == 'textarea' ) %]
                                                     <textarea cols="70" rows="4" id="[%- mv.id | html -%]" name="[%- mv.name | html -%]" class="input_marceditor" tabindex="1">[%- mv.value | html -%]</textarea>
                                                 [% ELSIF ( mv.type == 'select' ) %]
-                                                    <select name="[%- mv.name | html -%]" tabindex="1" size="1" class="input_marceditor" id="[%- mv.id | html -%]" data-category="[% mv.category | html %]">
+                                                    <select name="[%- mv.name | html -%]" tabindex="1" size="1" class="input_marceditor" id="[%- mv.id | html -%]" data-category="[% mv.category | html %]" data-not_av="[% mv.not_in_av %]">
+                                                    [% IF mv.not_in_av %]
+                                                        <option value="[% mv.default %]">[% mv.default %]</option>
+                                                    [% END %]
                                                     [% FOREACH aval IN mv.values %]
                                                         [% IF aval == mv.default %]
                                                         <option value="[%- aval | html -%]" selected="selected">[%- mv.labels.$aval | html -%]</option>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
index e82b5c9262..077967f587 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
@@ -151,9 +151,13 @@
                     <input type="hidden" id="[%- mv.id | html -%]" name="field_value" class="input_marceditor" size="50" maxlength="[%- mv.maxlength | html -%]" value="[%- mv.value | html -%]">
                 [% ELSIF ( mv.type == 'select' ) %]
                     [% IF ( mv.readonly ) %]
-                        <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor" readonly="readonly" disabled="disabled">
+                        <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor" readonly="readonly" disabled="disabled" >
                     [% ELSE %]
-                        <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor" data-category="[% mv.category | html %]">
+                        <select name="field_value" id="[%- mv.id | html -%]" size="1" class="input_marceditor" data-category="[% mv.category | html %]" data-not_av="[% mv.not_in_av %]">
+                    [% END %]
+
+                    [% IF mv.not_in_av %]
+                        <option value="[% mv.default %]">[% mv.default %]</option>
                     [% END %]
                     [% FOREACH aval IN mv.values %]
                         [% IF aval == mv.default %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js
index fd462b67ed..47d8548aa9 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/cataloging.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/cataloging.js
@@ -576,4 +576,13 @@ $(document).ready(function() {
         });
         return false;
     });
+
+    var tooltip_msg = _("The selected value is not in the list of authorised values!");
+    $("select[data-not_av='1']").each(function(){
+        var li = $(this).parent();
+        var tooltip = $('<i id="info_digests" data-toggle="tooltip" title="'+tooltip_msg+'" data-placement="right" class="fa fa-warning warn"></i>');
+        $(tooltip).tooltip();
+        $(li).append(tooltip);
+    });
+
 });
-- 
2.20.1