From 2f2c3a54a223bb23589885b745f32889b4680284 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 12 Aug 2021 11:02:39 +0000 Subject: [PATCH] Bug 28847: Move SearchLimitLibrary code to QueryBuilder Modules This patch removes the code from the search scripts into QueryBuilder modules. To test: 1 - Have a library group defined as a search group for both staff and opac 2 - Search on staff client and opac with that group limit and a single branch limit 3 - Note your results/counts 4 - Note the visuals of the search description 5 - Apply patch 6 - Repeat searches 7 - All should work as before Signed-off-by: Andrew Fuerste-Henry --- C4/Search.pm | 30 ++++++++++++++ .../Elasticsearch/QueryBuilder.pm | 26 +++++++++++++ catalogue/search.pl | 39 ------------------- opac/opac-search.pl | 39 ------------------- 4 files changed, 56 insertions(+), 78 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 1ca987add2..52f1541f1b 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1470,6 +1470,36 @@ sub buildQuery { $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit); $limit_desc .= " $this_limit"; } + elsif ( $this_limit =~ 'multibranchlimit|^branch' ) { + $limit_cgi .= "&limit=" . uri_escape_utf8($this_limit); + $limit .= " and " if $limit || $query; + my $branchfield = C4::Context->preference('SearchLimitLibrary'); + my @branchcodes; + if( $this_limit =~ 'multibranchlimit' ){ + my ($group_id) = ( $this_limit =~ /^multibranchlimit:(.*)$/ ); + my $search_group = Koha::Library::Groups->find( $group_id ); + @branchcodes = map { $_->branchcode } $search_group->all_libraries; + @branchcodes = sort { $a cmp $b } @branchcodes; + } else { + @branchcodes = ( $this_limit =~ /^branch:(.*)$/ ); + } + + if (@branchcodes) { + if ( $branchfield eq "homebranch" ) { + $this_limit = sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes; + } + elsif ( $branchfield eq "holdingbranch" ) { + $this_limit = sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes; + } + else { + $this_limit = sprintf "(%s or %s)", + join( " or ", map { 'homebranch: ' . $_ } @branchcodes ), + join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes ); + } + } + $limit .= "$this_limit"; + $limit_desc .= " $this_limit"; + } # Regular old limits else { diff --git a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm index 2a0775d386..533b79677e 100644 --- a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm +++ b/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm @@ -1010,6 +1010,32 @@ sub _fix_limit_special_cases { $date = $self->_modify_string_by_type(type => 'st-year', operand => $date); push @new_lim, "date-of-publication:$date"; } + elsif ( $l =~ 'multibranchlimit|^branch' ) { + my $branchfield = C4::Context->preference('SearchLimitLibrary'); + my @branchcodes; + if( $l =~ 'multibranchlimit' ) { + my ($group_id) = ( $l =~ /^multibranchlimit:(.*)$/ ); + my $search_group = Koha::Library::Groups->find( $group_id ); + @branchcodes = map { $_->branchcode } $search_group->all_libraries; + @branchcodes = sort { $a cmp $b } @branchcodes; + } else { + @branchcodes = ( $l =~ /^branch:(.*)$/ ); + } + + if (@branchcodes) { + if ( $branchfield eq "homebranch" ) { + push @new_lim, sprintf "(%s)", join " OR ", map { 'homebranch: ' . $_ } @branchcodes; + } + elsif ( $branchfield eq "holdingbranch" ) { + push @new_lim, sprintf "(%s)", join " OR ", map { 'holdingbranch: ' . $_ } @branchcodes; + } + else { + push @new_lim, sprintf "(%s OR %s)", + join( " OR ", map { 'homebranch: ' . $_ } @branchcodes ), + join( " OR ", map { 'holdingbranch: ' . $_ } @branchcodes ); + } + } + } elsif ( $l =~ /^available$/ ) { push @new_lim, 'onloan:false'; } diff --git a/catalogue/search.pl b/catalogue/search.pl index 8ed6ff3181..9c99bbe4f3 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -378,45 +378,6 @@ my @nolimits = map uri_unescape($_), $cgi->multi_param('nolimit'); my %is_nolimit = map { $_ => 1 } @nolimits; @limits = grep { not $is_nolimit{$_} } @limits; -if ( $params->{'multibranchlimit'} ) { - my $branchfield = C4::Context->preference('SearchLimitLibrary'); - my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} ); - my @branchcodes = map { $_->branchcode } $search_group->all_libraries; - - if (@branchcodes) { - if ( $branchfield eq "homebranch" ) { - push @limits, sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes; - } - elsif ( $branchfield eq "holdingbranch" ) { - push @limits, sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes; - } - else { - push @limits, sprintf "(%s or %s)", - join( " or ", map { 'homebranch: ' . $_ } @branchcodes ), - join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes ); - } - } -} - -for ( my $i=0; $i<@limits; $i++ ) { - if ( $limits[$i] =~ /^branch:/ ) { - my $branchfield = C4::Context->preference('SearchLimitLibrary'); - if ( $branchfield eq "homebranch" ) { - $limits[$i] =~ s/branch/homebranch/; - } - elsif ( $branchfield eq "holdingbranch" ) { - $limits[$i] =~ s/branch/holdingbranch/; - } - else { - my $homebranchlimit = $limits[$i]; - my $holdingbranchlimit = $limits[$i]; - $homebranchlimit =~ s/branch/homebranch/; - $holdingbranchlimit =~ s/branch/holdingbranch/; - $limits[$i] = "($homebranchlimit or $holdingbranchlimit)"; - } - } -} - my $available; foreach my $limit(@limits) { if ($limit =~/available/) { diff --git a/opac/opac-search.pl b/opac/opac-search.pl index 67c11c182f..fc13dffe64 100755 --- a/opac/opac-search.pl +++ b/opac/opac-search.pl @@ -484,45 +484,6 @@ if (@searchCategories > 0) { @limits = map { uri_unescape($_) } @limits; -if ( $params->{'multibranchlimit'} || ( $branch_group_limit && $branch_group_limit =~ /^multibranchlimit-/ ) ) { - my $branchfield = C4::Context->preference('SearchLimitLibrary'); - my $search_group = Koha::Library::Groups->find( $params->{multibranchlimit} ); - - my @branchcodes = map { $_->branchcode } $search_group->all_libraries; - - if (@branchcodes) { - if ( $branchfield eq "homebranch" ) { - push @limits, sprintf "(%s)", join " or ", map { 'homebranch: ' . $_ } @branchcodes; - } - elsif ( $branchfield eq "holdingbranch" ) { - push @limits, sprintf "(%s)", join " or ", map { 'holdingbranch: ' . $_ } @branchcodes; - } - else { - push @limits, sprintf "(%s or %s)", - join( " or ", map { 'homebranch: ' . $_ } @branchcodes ), - join( " or ", map { 'holdingbranch: ' . $_ } @branchcodes ); - } - } -} - -for ( my $i=0; $i<@limits; $i++ ) { - if ( $limits[$i] =~ /^branch:/ ) { - my $branchfield = C4::Context->preference('SearchLimitLibrary'); - if ( $branchfield eq "homebranch" ) { - $limits[$i] =~ s/branch/homebranch/; - } - elsif ( $branchfield eq "holdingbranch" ) { - $limits[$i] =~ s/branch/holdingbranch/; - } - else { - my $homebranchlimit = $limits[$i]; - my $holdingbranchlimit = $limits[$i]; - $homebranchlimit =~ s/branch/homebranch/; - $holdingbranchlimit =~ s/branch/holdingbranch/; - $limits[$i] = "($homebranchlimit or $holdingbranchlimit)"; - } - } -} my $available; foreach my $limit(@limits) { -- 2.20.1