Bug 28959: Add virtualshelves.public as a boolean
[koha.git] / Koha / Authorities.pm
1 package Koha::Authorities;
2
3 # Copyright 2015 Koha Development Team
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
23 use Koha::Database;
24
25 use Koha::Authority;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::Authorities - Koha Authority object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =head3 get_usage_count
38
39     $count = Koha::Authorities->get_usage_count({ authid => $i });
40
41     Returns the number of linked biblio records.
42
43     Note: Code originates from C4::AuthoritiesMarc::CountUsage.
44
45     This is a class method, since the authid may refer to a deleted record.
46
47 =cut
48
49 sub get_usage_count {
50     my ( $class, $params ) = @_;
51     my $authid = $params->{authid} || return;
52
53     my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
54     my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, 0, 0 );
55     if( $err ) {
56         warn "Error: $err from search for " . $authid;
57         return;
58     }
59     return $count;
60 }
61
62 =head3 linked_biblionumbers
63
64     my @biblios = Koha::Authorities->linked_biblionumbers({
65         authid => $id, [ max_results => $max ], [ offset => $offset ],
66     });
67
68     Returns array of biblionumbers, as extracted from the result records of
69     the search engine.
70
71     This is a class method, since the authid may refer to a deleted record.
72
73 =cut
74
75 sub linked_biblionumbers {
76     my ( $self, $params ) = @_;
77     my $authid = $params->{authid} || return;
78
79     my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
80     # if max_results is undefined, we will get all results
81     my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, $params->{offset} // 0, $params->{max_results} );
82
83     if( $err ) {
84         warn "Error: $err from search for " . $authid;
85         return;
86     }
87
88     my @biblionumbers;
89     foreach my $res ( @$result ) {
90         my $bibno = $searcher->extract_biblionumber( $res );
91         push @biblionumbers, $bibno if $bibno;
92     }
93     return @biblionumbers;
94 }
95
96 =head3 type
97
98 =cut
99
100 sub _type {
101     return 'AuthHeader';
102 }
103
104 =head3 object_class
105
106 =cut
107
108 sub object_class {
109     return 'Koha::Authority';
110 }
111
112 1;