Bug 24584: Rewrite optional/sample_creator_data to YAML
[koha.git] / opac / svc / suggestion
blobf4fb1d87222cb9737a4b1d4c4bfa763817768bbd
1 #!/usr/bin/perl
3 # Copyright 2012 C & P Bibliography Services
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 NAME
22 opac-suggestion.pl : script to render suggestions for the OPAC
24 =head1 SYNOPSIS
26 =cut
28 =head1 DESCRIPTION
30 This script produces suggestions for the OPAC given a search string.
32 It takes the following parameters:
34 =over 8
36 =item I<q>
38 Required. Query string.
40 =item I<render>
42 If set to 'stub' render a stub HTML page suitable for inclusion into a
43 div via AJAX. If set to 'standalone', return a full page instead of the stub.
44 If not set, return JSON.
46 =item I<count>
48 Number of suggestions to display. Defaults to 4 in stub mode, 20 otherwise.
50 =back
52 =cut
54 use Modern::Perl;
56 use C4::Auth;
57 use C4::Context;
58 use C4::Output;
59 use CGI qw ( -utf8 );
60 use JSON;
61 use Koha::SuggestionEngine;
63 my $query = new CGI;
65 my $dbh = C4::Context->dbh;
67 my $search = $query->param('q') || '';
68 my $render = $query->param('render') || '';
69 my $count = $query->param('count') || ( $render eq 'stub' ? 4 : 20 );
71 # open template
72 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74 template_name => "svc/suggestion.tt",
75 query => $query,
76 type => "opac",
77 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
78 debug => 1,
82 my @plugins = ();
84 my $pluginsconfig = from_json(C4::Context->preference('OPACdidyoumean') || '[]');
86 foreach my $plugin (@$pluginsconfig) {
87 push @plugins, $plugin->{name} if ($plugin->{enabled});
90 unless ( @plugins ) {
91 print $query->header;
92 exit;
95 my $suggestor = Koha::SuggestionEngine->new( { plugins => \@plugins } );
97 my $suggestions =
98 $suggestor->get_suggestions( { search => $search, count => $count } );
100 if ($render) {
101 $template->{VARS}->{render} = $render;
102 $template->{VARS}->{suggestions} = $suggestions if $suggestions;
103 output_html_with_http_headers $query, $cookie, $template->output;
105 else {
106 print $query->header;
107 print to_json($suggestions);