Bug 11175: Limit the amount of component parts returned
[koha.git] / Koha / Util / Search.pm
1 package Koha::Util::Search;
2
3 # Copyright 2020 University of Helsinki
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use C4::Biblio;
23
24 =head1 NAME
25
26 Koha::Util::Search - functions to build complex search queries
27
28 =head1 FUNCTIONS
29
30 =head2 get_component_part_query
31
32 Returns a query which can be used to search for all component parts of MARC21 biblios
33
34 =cut
35
36 sub get_component_part_query {
37     my ($biblionumber) = @_;
38
39     my $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
40     my $pf001 = $marc->field('001') || undef;
41
42     if (defined($pf001)) {
43         my $pf003 = $marc->field('003') || undef;
44         my $searchstr;
45
46         if (!defined($pf003)) {
47             # search for 773$w='Host001'
48             $searchstr = "rcn='".$pf001->data()."'";
49         } else {
50             # search for (773$w='Host001' and 003='Host003') or 773$w='Host003 Host001')
51             $searchstr = "(rcn='".$pf001->data()."' AND cni='".$pf003->data()."')";
52             $searchstr .= " OR rcn='".$pf003->data()." ".$pf001->data()."'";
53         }
54     }
55 }
56
57 1;