a380261add8ffb1e241626de8b69a8aec9690c82
[koha.git] / Koha / SuggestionEngine / Plugin / ExplodedTerms.pm
1 package Koha::SuggestionEngine::Plugin::ExplodedTerms;
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 Koha::SuggestionEngine::Plugin::ExplodedTerms - suggest searches for broader/narrower/related subjects
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 Plugin to suggest expanding the search by adding broader/narrower/related
30 subjects to subject searches.
31
32 =cut
33
34 use strict;
35 use warnings;
36 use Carp;
37 use C4::Templates qw(gettemplate); # This is necessary for translatability
38
39 use base qw(Koha::SuggestionEngine::Base);
40 our $NAME    = 'ExplodedTerms';
41 our $VERSION = '1.0';
42
43 =head2 get_suggestions
44
45     my $suggestions = $plugin->get_suggestions(\%param);
46
47 Return suggestions for the specified search that add broader/narrower/related
48 terms to the search.
49
50 =cut
51
52 sub get_suggestions {
53     my $self  = shift;
54     my $param = shift;
55
56     my $search = $param->{'search'};
57
58     return if ( $search =~ m/^(ccl=|cql=|pqf=)/ );
59     $search =~ s/(su|su-br|su-na|su-rl)[:=](\w*)/OP!$2/g;
60     return if ( $search =~ m/\w+[:=]\w+/ );
61
62     my @indexes = (
63         'su-na',
64         'su-br',
65         'su-rl'
66     );
67     my $cgi = new CGI;
68     my $template = C4::Templates::gettemplate('text/explodedterms.tt', 'opac', $cgi);
69     my @results;
70     foreach my $index (@indexes) {
71         my $thissearch = $search;
72         $thissearch = "$index=$thissearch"
73           unless ( $thissearch =~ s/OP!/$index=/g );
74         $template->{VARS}->{index} = $index;
75         my $label = pack("U0a*", $template->output); #FIXME: C4::Templates is
76         # returning incorrectly-marked UTF-8. This fixes the problem, but is
77         # an annoying workaround.
78         push @results,
79         {
80             'search'  => $thissearch,
81             relevance => 100,
82                 # FIXME: it'd be nice to have some empirical measure of
83                 #        "relevance" in this case, but we don't.
84             label => $label
85         };
86     } return \@results;
87 }