Bug 11175: (QA follow-up) Take account of bug 15851
[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 qw( GetMarcBiblio );
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 = GetMarcBiblio( { biblionumber => $biblionumber } );
40
41     my $searchstr;
42     if ( C4::Context->preference('UseControlNumber') ) {
43         my $pf001 = $marc->field('001') || undef;
44
45         if ( defined($pf001) ) {
46             my $pf003 = $marc->field('003') || undef;
47
48             if ( !defined($pf003) ) {
49                 # search for 773$w='Host001'
50                 $searchstr = "rcn:" . $pf001->data();
51             }
52             else {
53                 $searchstr  = "(";
54                 # search for (773$w='Host001' and 003='Host003') or 773$w='Host003 Host001')
55                 $searchstr .= "(rcn:" . $pf001->data() . " AND cni:" . $pf003->data() . ")";
56                 $searchstr .= " OR rcn:" . $pf003->data() . " " . $pf001->data();
57                 $searchstr .= ")";
58             }
59
60             # limit to monograph and serial component part records
61             $searchstr .= " AND (bib-level:a OR bib-level:b)";
62         }
63     }
64     else {
65         my $cleaned_title = $marc->title;
66         $cleaned_title =~ tr|/||;
67         $searchstr = "Host-item:($cleaned_title)";
68     }
69
70     return $searchstr;
71 }
72
73 1;