From 5ba5254da2a04e02266a7f4e91b904defd7539cc Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Thu, 1 Apr 2021 11:04:35 +0200
Subject: [PATCH] Bug 24564: Use the same tab as the other subfields within a
 field

For each subfield added, we check if other subfields exists in the same
field. If that's the case we use the same tab as the first subfield
found.

Test plan:
1. Find a biblio subfield in
   misc/migration_tools/ifla/data/biblio/default.yml that doesn't exist
   in your default biblio MARC framework (or delete one). The field
   should exist and have other subfields with a tab set.
2. Change the tab of all subfields within that field it's different from
   what's in the .yml file
3. Run misc/migration_tools/ifla/update.pl
4. Verify that the subfield has been added and have the same tab as
   others subfields
5. Do the same for authorities (files are in
   misc/migration_tools/ifla/data/auth/)

Signed-off-by: Koha team <koha@univ-lyon.fr>

Signed-off-by: sonia <sonia.bouis@univ-lyon3.fr>
---
 misc/migration_tools/ifla/update.pl | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/misc/migration_tools/ifla/update.pl b/misc/migration_tools/ifla/update.pl
index 3d4be0a624..a4e2d8d0fa 100755
--- a/misc/migration_tools/ifla/update.pl
+++ b/misc/migration_tools/ifla/update.pl
@@ -222,6 +222,14 @@ for my $tag (@tags) {
     $marc_tag_structure_rs->create($tag);
 }
 
+my @mss = $marc_subfield_structure_rs->search({ frameworkcode => '' });
+my %tab_for_field;
+foreach my $mss (@mss) {
+    next if $mss->tab < 0;
+    next if exists $tab_for_field{$mss->tagfield};
+    $tab_for_field{$mss->tagfield} = $mss->tab;
+}
+
 my $subfield_defaults = $defaults->{subfield};
 for my $subfield (@subfields) {
     foreach my $key (keys %$subfield_defaults) {
@@ -231,6 +239,11 @@ for my $subfield (@subfields) {
     }
     $subfield->{liblibrarian} = t($subfield->{liblibrarian});
 
+    # If other subfields exist in this field, use the same tab
+    if (exists $tab_for_field{$subfield->{tagfield}}) {
+        $subfield->{tab} = $tab_for_field{$subfield->{tagfield}};
+    }
+
     my $mss = $marc_subfield_structure_rs->find('', $subfield->{tagfield}, $subfield->{tagsubfield});
     if ($mss) {
         say sprintf('Subfield already exists: %s$%s', $subfield->{tagfield}, $subfield->{tagsubfield});
@@ -290,6 +303,18 @@ for my $authtag (@authtags) {
     $auth_tag_structure_rs->create($authtag);
 }
 
+my @ass = $auth_subfield_structure_rs->search();
+my %tab_for_authfield;
+foreach my $ass (@ass) {
+    my $authtypecode = $ass->get_column('authtypecode');
+    $tab_for_authfield{$authtypecode} //= {};
+
+    next if $ass->tab < 0;
+    next if exists $tab_for_authfield{$authtypecode}->{$ass->tagfield};
+
+    $tab_for_authfield{$authtypecode}->{$ass->tagfield} = $ass->tab;
+}
+
 my $authsubfield_defaults = $defaults->{authsubfield};
 for my $authsubfield (@authsubfields) {
     foreach my $key (keys %$authsubfield_defaults) {
@@ -299,6 +324,11 @@ for my $authsubfield (@authsubfields) {
     }
     $authsubfield->{liblibrarian} = t($authsubfield->{liblibrarian});
 
+    # If other subfields exist in this field, use the same tab
+    if (exists $tab_for_authfield{$authsubfield->{authtypecode}}->{$authsubfield->{tagfield}}) {
+        $authsubfield->{tab} = $tab_for_authfield{$authsubfield->{authtypecode}}->{$authsubfield->{tagfield}};
+    }
+
     my $ass = $auth_subfield_structure_rs->find($authsubfield->{authtypecode}, $authsubfield->{tagfield}, $authsubfield->{tagsubfield});
     if ($ass) {
         say sprintf('Auth subfield already exists: %s$%s (%s)', $authsubfield->{tagfield}, $authsubfield->{tagsubfield}, $authsubfield->{authtypecode});
-- 
2.11.0