Bug 11003: fix JS error on the staff cart page
[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
41 =head2 NAME
42     my $name = $plugin->NAME;
43
44 =cut
45
46 sub NAME {
47     return 'ExplodedTerms';
48 }
49
50 =head2 VERSION
51     my $version = $plugin->VERSION;
52
53 =cut
54
55 sub VERSION {
56     return '1.0';
57 }
58
59 =head2 get_suggestions
60
61     my $suggestions = $plugin->get_suggestions(\%param);
62
63 Return suggestions for the specified search that add broader/narrower/related
64 terms to the search.
65
66 =cut
67
68 sub get_suggestions {
69     my $self  = shift;
70     my $param = shift;
71
72     my $search = $param->{'search'};
73
74     return if ( $search =~ m/^(ccl=|cql=|pqf=)/ );
75     $search =~ s/(su|su-br|su-na|su-rl)[:=](\w*)/OP!$2/g;
76     return if ( $search =~ m/\w+[:=]\w+/ );
77
78     my @indexes = (
79         'su-na',
80         'su-br',
81         'su-rl'
82     );
83     my $cgi = new CGI;
84     my $template = C4::Templates::gettemplate('text/explodedterms.tt', 'opac', $cgi);
85     my @results;
86     foreach my $index (@indexes) {
87         my $thissearch = $search;
88         $thissearch = "$index=$thissearch"
89           unless ( $thissearch =~ s/OP!/$index=/g );
90         $template->{VARS}->{index} = $index;
91         my $label = pack("U0a*", $template->output); #FIXME: C4::Templates is
92         # returning incorrectly-marked UTF-8. This fixes the problem, but is
93         # an annoying workaround.
94         push @results,
95         {
96             'search'  => $thissearch,
97             relevance => 100,
98                 # FIXME: it'd be nice to have some empirical measure of
99                 #        "relevance" in this case, but we don't.
100             label => $label
101         };
102     } return \@results;
103 }
104
105 1;