From bb98bd0d024a8494a5283b3925910dfebdd2c6c9 Mon Sep 17 00:00:00 2001
From: Fridolin Somers <fridolin.somers@biblibre.com>
Date: Mon, 4 Dec 2023 21:30:24 -1000
Subject: [PATCH] Bug 35486: When editing an authority show all subfields of
 the heading field

Currently when creating/editing and authority, only mandatory fields are shown.
This patch changes to show all subfields for the heading tag.
For example in UNIMARC authorities NP show subfields of 200 : $a surname, $b firstname ...
For example in MARC21 authorities Personal Name show subfields of 100

This will make cataloguing easier since heading subfields are usually filled.

Test plan :
1) Use MARC21 database
2) Create a new authority type 'Personal Name'
3) In section 0 you see for example field 40 showing only mandatory
   subfield $a
4) Go to section 1
5) You see all subfields of field 100 because it is the heading
---
 authorities/authorities.pl | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/authorities/authorities.pl b/authorities/authorities.pl
index bd20fd6374..67ee1704f4 100755
--- a/authorities/authorities.pl
+++ b/authorities/authorities.pl
@@ -100,7 +100,7 @@ builds the <input ...> entry for a subfield.
 =cut
 
 sub create_input {
-    my ( $tag, $subfield, $value, $index_tag, $rec, $authorised_values_sth, $cgi ) = @_;
+    my ( $tag, $subfield, $value, $index_tag, $rec, $authorised_values_sth, $cgi, $heading_tag ) = @_;
 
     my $index_subfield = CreateKey(); # create a specific key for each subfield
 
@@ -162,7 +162,7 @@ sub create_input {
     
     $subfield_data{visibility} = "display:none;"
         if( $tagslib->{$tag}->{$subfield}->{hidden} and $value ne ''
-            or ($value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory})
+            or ( $value eq '' and !$tagslib->{$tag}->{$subfield}->{mandatory} and $tag ne $heading_tag )
         );
     
     # it's an authorised field
@@ -322,7 +322,7 @@ sub GetMandatoryFieldZ3950 {
 }
 
 sub build_tabs {
-    my ( $template, $record, $dbh, $input ) = @_;
+    my ( $template, $record, $dbh, $input, $heading_tag ) = @_;
 
     # fill arrays
     my @loop_data = ();
@@ -386,7 +386,7 @@ sub build_tabs {
                             @subfields_data,
                             &create_input(
                                 $tag, $subfield, $value, $index_tag, $record,
-                                $authorised_values_sth,$input
+                                $authorised_values_sth, $input, $heading_tag
                             )
                         );
                     }
@@ -402,7 +402,7 @@ sub build_tabs {
                                 @subfields_data,
                                 &create_input(
                                     $tag, $subfield, $value, $index_tag,
-                                    $record, $authorised_values_sth,$input
+                                    $record, $authorised_values_sth, $input, $heading_tag
                                 )
                             );
                         }
@@ -420,7 +420,7 @@ sub build_tabs {
                             @subfields_data,
                             &create_input(
                                 $tag, $subfield, '', $index_tag, $record,
-                                $authorised_values_sth,$input
+                                $authorised_values_sth, $input, $heading_tag
                             )
                         );
                     }
@@ -462,7 +462,7 @@ sub build_tabs {
                         @subfields_data,
                         &create_input(
                             $tag, $subfield->{subfield}, '', $index_tag, $record,
-                            $authorised_values_sth,$input
+                            $authorised_values_sth, $input, $heading_tag
                         )
                     );
                 }
@@ -554,6 +554,11 @@ if(!$authtypecode) {
 my $authobj = Koha::Authorities->find($authid);
 my $count = $authobj ? $authobj->get_usage_count : 0;
 
+my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
+my $authtype = $authority_types->find($authtypecode);
+my $authtypetext = $authtype ? $authtype->authtypetext : q{};
+my $heading_tag = $authtype ? $authtype->auth_tag_to_report : q{};
+
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "authorities/authorities.tt",
                             query => $input,
@@ -614,7 +619,7 @@ if ($op eq "add") {
         exit;
     } else {
     # it may be a duplicate, warn the user and do nothing
-        build_tabs($template, $record, $dbh, $input);
+        build_tabs($template, $record, $dbh, $input, $heading_tag);
         build_hidden_data;
         $template->param(authid =>$authid,
                         duplicateauthid     => $duplicateauthid,
@@ -639,7 +644,7 @@ if ($op eq "add") {
         $record = TransformHtmlToMarc( $input, 0 );
     }
 
-    build_tabs( $template, $record, $dbh, $input );
+    build_tabs( $template, $record, $dbh, $input, $heading_tag );
     build_hidden_data;
     $template->param(
         oldauthtypetagfield    => $oldauthtypetagfield,
@@ -651,15 +656,12 @@ if ($op eq "add") {
     );
 }
 
-my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
-
-my $type = $authority_types->find($authtypecode);
 $template->param(
     authority_types => $authority_types,
     authtypecode    => $authtypecode,
     authid          => $authid,
     linkid          => $linkid,
-    authtypetext    => $type ? $type->authtypetext : "",
+    authtypetext    => $authtypetext,
     hide_marc       => C4::Context->preference('hide_marc'),
 );
 output_html_with_http_headers $input, $cookie, $template->output;
-- 
2.43.0