cfb34da7aa57b92c1661767ff1e6d278413feb5d
[srvgit] / Koha / SuggestionEngine / Plugin / AuthorityFile.pm
1 package Koha::SuggestionEngine::Plugin::AuthorityFile;
2
3 # Copyright 2012 C & P Bibliography Services
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 =head1 NAME
21
22 Koha::SuggestionEngine::Plugin::AuthorityFile - get suggestions from the authority file
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 Plugin to get suggestions from Koha's authority file
30
31 =cut
32
33 use Modern::Perl;
34 use Carp;
35
36 use base qw(Koha::SuggestionEngine::Base);
37
38 =head2 NAME
39     my $name = $plugin->NAME;
40
41 =cut
42
43 sub NAME {
44     return 'AuthorityFile';
45 }
46
47 =head2 VERSION
48     my $version = $plugin->VERSION;
49
50 =cut
51
52 sub VERSION {
53     return '1.1';
54 }
55
56 =head2 get_suggestions
57
58     my $suggestions = $plugin->get_suggestions(\%param);
59
60 Return suggestions for the specified search by searching for the
61 search terms in the authority file and returning the results.
62
63 =cut
64
65 sub get_suggestions {
66     my $self  = shift;
67     my $param = shift;
68
69     my $search = $param->{'search'};
70
71     # Remove any CCL. This does not handle CQL or PQF, which is unfortunate,
72     # but what can you do? At some point the search will have to be passed
73     # not as a string but as some sort of data structure, at which point it
74     # will be possible to support multiple search syntaxes.
75     $search =~ s/ccl=//;
76     $search =~ s/\w*[:=](\w*)/$1/g;
77
78     my @marclist  = ['mainentry'];
79     my @and_or    = ['and'];
80     my @excluding = [];
81     my @operator  = ['any'];
82     my @value     = ["$search"];
83
84     # FIXME: calling into C4
85     require C4::AuthoritiesMarc;
86     my ( $searchresults, $count ) = C4::AuthoritiesMarc::SearchAuthorities(
87         @marclist,  @and_or, @excluding,       @operator,
88         @value,      0,        $param->{'count'}, '',
89         'Relevance', 0
90     );
91
92     my @results;
93     foreach my $auth (@$searchresults) {
94         push @results,
95           {
96             'search'  => "an:$auth->{'authid'}",
97             relevance => $count--,
98             label     => $auth->{summary}->{authorized}->[0]->{heading}
99           };
100     }
101     return \@results;
102 }
103
104 1;