From b4c762c7e375602da21aa11bfeceb9a71fe6650b Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Mon, 3 Mar 2025 16:54:53 +0100 Subject: [PATCH] Bug 39229: Search unique extended attributes on patron quicksearch To test: 1) Apply patch 2) Create a unique extended patron attribute, set it to "Searchable" and and "Searched by default" 3) Edit a patron and set a value for the created attribute. 4) Enable UniqueExtendedAttributesQuickSearch syspref 5) Edit memebers/member.pl, after the if (@attributes_conditinos) block, around line 93, write "exit;" and save the file. 6) Perforn a search on the patron attribute value 7) A redirect to the patron page should occur 8) Search for something else not matching the attribute 9) You should now recieve a 500 error (since the script exits prematurely, and the normal search is never run) Sponsored-by: Gothenburg University Library --- .../atomicupdate/bug_39229_add_syspref.pl | 19 ++++++++ installer/data/mysql/mandatory/sysprefs.sql | 1 + .../en/modules/admin/preferences/patrons.pref | 7 +++ members/member.pl | 44 +++++++++++++++---- 4 files changed, 63 insertions(+), 8 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_39229_add_syspref.pl diff --git a/installer/data/mysql/atomicupdate/bug_39229_add_syspref.pl b/installer/data/mysql/atomicupdate/bug_39229_add_syspref.pl new file mode 100755 index 00000000000..8bfde1624b3 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_39229_add_syspref.pl @@ -0,0 +1,19 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_success say_info); + +return { + bug_number => "39229", + description => "Search unique extended attributes on patron quicksearch", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Do you stuffs here + $dbh->do( + q{ INSERT IGNORE INTO `systempreferences`(`variable`, `value`, `options`, `explanation`, `type`) VALUES ('UniqueExtendedAttributesQuickSearch', '0', NULL, 'Enable first running a search against all unique attributes when using the patron search bar, if a patron is found redirect to that patron without performing a full search. This can significantly improve performance if unique attributes are commonly searached for and the number of patrons is sufficiently large.', 'YesNo') } + ); + + # sysprefs + say $out "Added new system preference 'UniqueExtendedAttributesQuickSearch'"; + }, +}; diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index c4bb3ca9a7d..ff0b5ac14f6 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -818,6 +818,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('UNIMARCAuthorityField100','afrey50 ba0',NULL,'Define the contents of UNIMARC authority control field 100 position 08-35','Textarea'), ('UNIMARCAuthorsFacetsSeparator',', ',NULL,'UNIMARC authors facets separator','short'), ('UNIMARCField100Language','fre',NULL,'UNIMARC field 100 default language','short'), +('UniqueExtendedAttributesQuickSearch', '0', NULL, 'Enable first running a search against all unique attributes when using the patron search bar, if a patron is found redirect to that patron without performing a full search. This can significantly improve performance if unique attributes are commonly searached for and the number of patrons is sufficiently large.', 'YesNo'), ('UniqueItemFields','barcode','','Pipe-separated list of fields that should be unique (used in acquisition module for item creation). Fields must be valid SQL column names of items table','Free'), ('UnseenRenewals','0','','Allow renewals to be recorded as "unseen" by the library, and count against the patrons unseen renewals limit.','YesNo'), ('UnsubscribeReflectionDelay','',NULL,'Delay for locking unsubscribers', 'Integer'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 671b4f4d416..73008d0fce3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -86,6 +86,13 @@ Patrons: 1: Do 0: "Don't" - enable the ability to upload and attach arbitrary files to a borrower record. + - + - pref: UniqueExtendedAttributesQuickSearch + choices: + 1: Enable + 0: "Don't enable" + - to first run a search against all unique attributes when using the patron search bar, if a patron is found redirect to that patron without performing a full search. + - This can significantly improve performance if unique attributes are commonly searached for and the number of patrons is sufficiently large. - - pref: useDischarge choices: diff --git a/members/member.pl b/members/member.pl index 366c49ebfba..4cc58340505 100755 --- a/members/member.pl +++ b/members/member.pl @@ -52,15 +52,43 @@ if ( $quicksearch and $searchmember && !$circsearch ) { my $userenv = C4::Context->userenv; $branchcode = $userenv->{'branch'}; } + my $maybe_redirect = sub { + my ($patron) = @_; + if ( + $patron + and ( ( $branchcode and $patron->branchcode eq $branchcode ) + or ( not $branchcode ) ) + ) + { + print $input->redirect( "/cgi-bin/koha/members/moremember.pl?borrowernumber=" . $patron->borrowernumber ); + exit; + } + }; + my $patron = Koha::Patrons->find( { cardnumber => $searchmember } ); - if ( - $patron - and ( ( $branchcode and $patron->branchcode eq $branchcode ) - or ( not $branchcode ) ) - ) - { - print $input->redirect( "/cgi-bin/koha/members/moremember.pl?borrowernumber=" . $patron->borrowernumber ); - exit; + $maybe_redirect->($patron) if ($patron); + + + if ( C4::Context->preference('UniqueExtendedAttributesQuickSearch') ) { + # Search all unique patron attributes + my @unique_types = Koha::Patron::Attribute::Types->search( { 'unique_id' => 1, 'staff_searchable' => 1, 'searched_by_default' } )->as_list; + my @attribute_conditions; + for my $type (@unique_types) { + push @attribute_conditions, [ + { + "extended_attributes.code" => $type->code, + "extended_attributes.attribute" => $searchmember + } + ]; + } + + if (@attribute_conditions) { + my @patrons = Koha::Patrons->search( \@attribute_conditions, { prefetch => ['extended_attributes'] } )->as_list; + + # The only case where could be more than one is when multiple unique attribute has the same value for different patrons + # which should be unlikely. If that is the case we should perform a full search. + $maybe_redirect->( $patrons[0] ) if ( @patrons == 1 ); + } } } -- 2.48.1