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