From 973141361c04e780420fc26a74da22afe5ee9645 Mon Sep 17 00:00:00 2001
From: Sam Lau <samalau@gmail.com>
Date: Mon, 1 Jul 2024 20:40:13 +0000
Subject: [PATCH] Bug 7223: Remove all instances of borrowerRelationship and
 replace with 'BORR_RELATIONSHIP'

---
 Koha/Patron/Relationship.pm                   |  4 +++-
 about.pl                                      |  4 +++-
 .../prog/en/includes/members-toolbar.inc      |  2 +-
 .../intranet-tmpl/prog/en/modules/about.tt    |  4 ++--
 .../prog/en/modules/members/memberentrygen.tt | 24 +++++++++----------
 .../prog/en/modules/members/moremember.tt     |  2 +-
 members/memberentry.pl                        |  5 +++-
 7 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/Koha/Patron/Relationship.pm b/Koha/Patron/Relationship.pm
index 544f4aaa0c..9b757d84a2 100644
--- a/Koha/Patron/Relationship.pm
+++ b/Koha/Patron/Relationship.pm
@@ -22,6 +22,7 @@ use Try::Tiny qw( catch try );
 
 use Koha::Database;
 use Koha::Exceptions::Patron::Relationship;
+use Koha::AuthorisedValues;
 
 use base qw(Koha::Object);
 
@@ -47,7 +48,8 @@ Overloaded method that makes some checks before storing on the DB
 sub store {
     my ( $self ) = @_;
 
-    my @valid_relationships = split /\|/, C4::Context->preference('borrowerRelationship'), -1;
+    my $auth_values = C4::Koha::GetAuthorisedValues('BOR_RELATIONSHIP');
+    my @valid_relationships = map { $_->{authorised_value} } @$auth_values;
     @valid_relationships = ('') unless @valid_relationships;
 
     Koha::Exceptions::Patron::Relationship::InvalidRelationship->throw(
diff --git a/about.pl b/about.pl
index 37e400fbb7..e24caecb6f 100755
--- a/about.pl
+++ b/about.pl
@@ -613,7 +613,9 @@ if($tab eq 'sysinfo') {
                 SELECT relationship FROM borrowers WHERE relationship IS NOT NULL) a
         });
 
-        my %valid_relationships = map { $_ => 1 } split( /,|\|/, C4::Context->preference('borrowerRelationship') );
+        my $borrower_relationships = C4::Koha::GetAuthorisedValues('BOR_RELATIONSHIP');
+        my @relationship_names = map { $_->{'authorised_value'} } @$borrower_relationships;
+        my %valid_relationships = map { $_ => 1 } @relationship_names;
         $valid_relationships{ _bad_data } = 1; # we handle this case in another way
 
         my $wrong_relationships = [ grep { !$valid_relationships{ $_->[0] } } @{$existing_relationships} ];
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc
index 08e5b0597d..51cbee3b47 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/members-toolbar.inc
@@ -11,7 +11,7 @@
     [% END %]
 
     [% IF CAN_user_borrowers_edit_borrowers %]
-        [% IF patron.is_adult AND Koha.Preference("borrowerRelationship") %]
+        [% IF patron.is_adult AND AuthorisedValues.Get( 'BOR_RELATIONSHIP' ) %]
             <a id="addchild" class="btn btn-default" href="/cgi-bin/koha/members/memberentry.pl?op=add_form&amp;guarantor_id=[% patron.borrowernumber | html %]"><i class="fa fa-plus"></i> Add guarantee</a>
         [% END %]
         <a id="changepassword" class="btn btn-default" href="/cgi-bin/koha/members/member-password.pl?member=[% patron.borrowernumber | html %]"><i class="fa fa-lock"></i> Change password</a>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
index cb5b62ce97..02ae81052a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/about.tt
@@ -398,13 +398,13 @@
                 [% END # /IF bad_relationships_count %]
 
                 [% IF wrong_relationships %]
-                    <p>The following values have been used for guarantee/guarantor relationships, but do not exist in the 'borrowerRelationship' system preference:</p>
+                    <p>The following values have been used for guarantee/guarantor relationships, but do not exist in the BOR_RELATIONSHIP authorized value category:</p>
                     <ul>
                         [% FOR rel IN wrong_relationships %]
                             <li>[% rel.0 | html %]</li>
                         [% END %]
                     </ul>
-                    <p>If the relationship is one you want, please add it to the 'borrowerRelationship' system preference, otherwise have your system's administrator correct the values in [% "borrowers.relationship" | $HtmlTags tag="strong" %] and/or [% "borrower_relationships.relationship" | $HtmlTags tag="strong" %] in the database.</p>
+                    <p>If the relationship is one you want, please add it to the BOR_RELATIONSHIP as an authorized value, otherwise have your system's administrator correct the values in [% "borrowers.relationship" | $HtmlTags tag="strong" %] and/or [% "borrower_relationships.relationship" | $HtmlTags tag="strong" %] in the database.</p>
                 [% END # /IF wrong_relationships %]
             [% END # /IF warnRelationships %]
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
index 6877ef453f..e3b6277c48 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt
@@ -4,6 +4,7 @@
 [% USE Koha %]
 [% USE KohaDates %]
 [% USE Branches %]
+[% USE AuthorisedValues %]
 [% PROCESS 'member-main-address-style.inc' %]
 [% PROCESS 'member-alt-address-style.inc' %]
 [% PROCESS 'member-alt-contact-style.inc' %]
@@ -485,9 +486,8 @@ legend.collapsed i.fa.fa-caret-down::before {
                                         </ol>
                                     </fieldset> <!-- /#memberentry_identity -->
                                 [% END # hide fieldset %]
-
+                                [% SET possible_relationships = AuthorisedValues.Get( 'BOR_RELATIONSHIP' ) %]
                                 [% IF show_guarantor || guarantor %]
-                                    [% SET possible_relationships = Koha.Preference('borrowerRelationship') %]
                                     <div id="memberentry_guarantor_anchor">
                                         <fieldset id="memberentry_guarantor" class="rows">
                                             <legend class="expanded" id="patron_guarantor_lgd">
@@ -545,9 +545,9 @@ legend.collapsed i.fa.fa-caret-down::before {
                                                                         <select class="new_guarantor_relationship" name="new_guarantor_relationship">
                                                                     [% END %]
                                                                         <option value=""></option>
-                                                                        [% FOREACH pr IN possible_relationships.split('\|') %]
-                                                                            [% IF pr != "" %]
-                                                                                <option value="[% pr | html %]">[% pr | html %]</option>
+                                                                        [% FOREACH pr IN possible_relationships %]
+                                                                            [% IF pr.authorised_value != "" %]
+                                                                                <option value="[% pr.authorised_value | html %]">[% pr.authorised_value | html %]</option>
                                                                             [% END %]
                                                                         [% END %]
                                                                     </select>
@@ -589,9 +589,9 @@ legend.collapsed i.fa.fa-caret-down::before {
                                                                 <select class="new_guarantor_relationship" name="new_guarantor_relationship">
                                                             [% END %]
                                                                 <option value="" selected></option>
-                                                                [% FOREACH pr IN possible_relationships.split('\|') %]
-                                                                    [% IF pr != "" %]
-                                                                        <option value="[% pr | html %]">[% pr | html %]</option>
+                                                                [% FOREACH pr IN possible_relationships %]
+                                                                    [% IF pr.authorised_value != "" %]
+                                                                        <option value="[% pr.authorised_value | html %]">[% pr.authorised_value | html %]</option>
                                                                     [% END %]
                                                                 [% END %]
                                                             </select>
@@ -704,11 +704,11 @@ legend.collapsed i.fa.fa-caret-down::before {
                                                             <select class="relationship" name="relationship">
                                                         [% END %]
                                                             <option value=""></option>
-                                                            [% FOREACH pr IN possible_relationships.split('\|') %]
-                                                                [% IF pr == borrower_data.relationship %]
-                                                                    <option value="[% pr | html %]" selected="selected">[% pr | html %]</option>
+                                                            [% FOREACH pr IN possible_relationships %]
+                                                                [% IF pr.authorised_value == borrower_data.relationship %]
+                                                                    <option value="[% pr.authorised_value | html %]" selected="selected">[% pr.authorised_value | html %]</option>
                                                                 [% ELSE %]
-                                                                    <option value="[% pr | html %]">[% pr | html %]</option>
+                                                                    <option value="[% pr.authorised_value | html %]">[% pr.authorised_value | html %]</option>
                                                                 [% END %]
                                                             [% END %]
                                                         </select>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index 4debd1abda..9c31cf1892 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -191,7 +191,7 @@
                                                 </li>
                                             [% END %]
                                         [% END %]
-                                        [% IF Koha.Preference("borrowerRelationship") %]
+                                        [% IF AuthorisedValues.Get( 'BOR_RELATIONSHIP' ) %]
                                              <li id="patron-privacyguarantor">
                                                 <span class="label">Show checkouts to guarantor:</span>
                                                 [% IF patron.privacy_guarantor_checkouts %]
diff --git a/members/memberentry.pl b/members/memberentry.pl
index f647234691..b30eeecc89 100755
--- a/members/memberentry.pl
+++ b/members/memberentry.pl
@@ -688,7 +688,10 @@ $template->param(
 my $default_borrowertitle = '';
 unless ( $op eq 'duplicate' ) { $default_borrowertitle=$data{'title'} }
 
-my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
+# Fetch the authorized values for the BOR_RELATIONSHIP category
+my $auth_values = C4::Koha::GetAuthorisedValues('BOR_RELATIONSHIP');
+my @relationships = map { $_->{authorised_value} } @$auth_values;
+
 my @relshipdata;
 while (@relationships) {
   my $relship = shift @relationships || '';
-- 
2.39.2