From df944f97ebded5db023acb1c8c0532f41528c12d Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 28 Apr 2023 12:39:58 +0000
Subject: [PATCH] Bug 33445: Pass through form info if present and use record
 values otherwise
Content-Type: text/plain; charset=utf-8

This patch updates the form to pass through values formthe form when searching, and
to use record values if nothing is passed in.

TO test:
 1 - Without the patch, edit a personal name authority record
 2 - Replace 100 subfield a (200 subfield a for UNIMARC) with your own name
 3 - Click "Replace record via Z39.50/SRU search"
 4 - Note that prefilled search field contains the original value, not your name.
 5 - Replace the Author (personal) field with your name in the search form
 6 - choose a search target, and click Search.
 7 - Note that the results of the search are for the original value, not your name
 8 - From the details page of a record, click 'Edit -> Replace record via Z3950'
 9 - Note form is prefilled
10 - Change value as in 5 and search
11 - Note original term was searched
12 - Apply patch, restart all
13 - Repeat searches as before
14 - Confirm that form is prefilled with record or the updated value
15 - Confirm your entered search terms are always searched

Signed-off-by: Phil Ringnalda <phil@chetcolibrary.org>
Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 cataloguing/z3950_auth_search.pl | 50 +++++++++++++++-----------------
 1 file changed, 24 insertions(+), 26 deletions(-)

diff --git a/cataloguing/z3950_auth_search.pl b/cataloguing/z3950_auth_search.pl
index 5b413201fa..ce6a3bd324 100755
--- a/cataloguing/z3950_auth_search.pl
+++ b/cataloguing/z3950_auth_search.pl
@@ -33,6 +33,7 @@ my $input        = CGI->new;
 my $dbh          = C4::Context->dbh;
 my $error         = $input->param('error');
 my $authid  = $input->param('authid') || 0;
+my $op            = $input->param('op')||'';
 
 my $record         = GetAuthority($authid);
 my $marc_flavour = C4::Context->preference('marcflavour');
@@ -44,34 +45,31 @@ my $authfields_mapping = {
     'subject'          => $marc_flavour eq 'MARC21' ? '150' : '250',
 };
 
-my $nameany     = $input->param('nameany');
-my $authorany     = $input->param('authorany');
-my $authorcorp =
-    $record
-  ? $record->subfield( $authfields_mapping->{'authorcorp'}, 'a' )
-  : $input->param('authorcorp');
-my $authorpersonal =
-    $record
-  ? $record->subfield( $authfields_mapping->{'authorpersonal'}, 'a' )
-  : $input->param('authorpersonal');
-my $authormeetingcon =
-    $record
-  ? $record->subfield( $authfields_mapping->{'authormeetingcon'}, 'a' )
-  : $input->param('authormeetingcon');
+my $nameany          = $input->param('nameany');
+my $authorany        = $input->param('authorany');
+my $title            = $input->param('title');
+my $authorpersonal   = $input->param('authorpersonal');
+my $authormeetingcon = $input->param('authormeetingcon');
+my $uniformtitle     = $input->param('uniformtitle');
+my $subject          = $input->param('subject');
+my $authorcorp       = $input->param('authorcorp');
+my $subjectsubdiv    = $input->param('subjectsubdiv');
+my $srchany          = $input->param('srchany');
 
-my $title         = $input->param('title');
-my $uniformtitle =
-    $record
-  ? $record->subfield( $authfields_mapping->{'uniformtitle'}, 'a' )
-  : $input->param('uniformtitle');
-my $subject =
-    $record
-  ? $record->subfield( $authfields_mapping->{'subject'}, 'a' )
-  : $input->param('subject');
+# If replacing an existing record we want to initially populate the form with record info,
+# however, we want to use entered inputs when searching
+if ( $record && $op ne 'do_search' ) {
+    $authorcorp ||=
+      $record->subfield( $authfields_mapping->{'authorcorp'}, 'a' );
+    $authorpersonal ||=
+      $record->subfield( $authfields_mapping->{'authorpersonal'}, 'a' );
+    $authormeetingcon ||=
+      $record->subfield( $authfields_mapping->{'authormeetingcon'}, 'a' );
+    $uniformtitle ||=
+      $record->subfield( $authfields_mapping->{'uniformtitle'}, 'a' );
+    $subject ||= $record->subfield( $authfields_mapping->{'subject'}, 'a' );
+}
 
-my $subjectsubdiv       = $input->param('subjectsubdiv');
-my $srchany       = $input->param('srchany');
-my $op            = $input->param('op')||'';
 my $page            = $input->param('current_page') || 1;
 my $index =$input->param('index');
 $page = $input->param('goto_page') if $input->param('changepage_goto');
-- 
2.30.2