Bug 11772: (follow-up) make warning about no active currency a link to currency setup...
[koha.git] / opac / search.pl
blob1a8aa415e5ac09e745c8ba8226e8d67361717125
1 #!/usr/bin/perl
3 # Copyright 2012 BibLibre
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
22 use C4::Context;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Output;
27 use Koha::SearchEngine::Search;
28 use Koha::SearchEngine::QueryBuilder;
29 use Koha::SearchEngine::FacetsBuilder;
31 my $cgi = new CGI;
33 my $template_name;
34 my $template_type = "basic";
35 if ( $cgi->param("idx") or $cgi->param("q") ) {
36 $template_name = 'search/results.tt';
37 } else {
38 $template_name = 'search/advsearch.tt';
39 $template_type = 'advsearch';
42 # load the template
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 { template_name => $template_name,
45 query => $cgi,
46 type => "opac",
47 authnotrequired => 1,
51 my $format = $cgi->param("format") || 'html';
56 # load the Type stuff
57 my $itemtypes = GetItemTypes;
59 my $page = $cgi->param("page") || 1;
60 my $count = $cgi->param('count') || C4::Context->preference('OPACnumSearchResults') || 20;
61 $count = 5;
62 my $q = $cgi->param("q");
63 my $builder = Koha::SearchEngine::QueryBuilder->new;
64 $q = $builder->build_query( $q );
65 my $search_service = Koha::SearchEngine::Search->new;
67 # load the sorting stuff
68 my $sort_by = $cgi->param('sort_by')
69 || C4::Context->preference('OPACdefaultSortField') . ' ' . C4::Context->preference('OPACdefaultSortOrder');
71 my $search_engine_config = Koha::SearchEngine->new->config;
72 my $sortable_indexes = $search_engine_config->sortable_indexes;
73 my ( $sort_indexname, $sort_order );
74 ( $sort_indexname, $sort_order ) = ($1, $2) if ( $sort_by =~ m/^(.*) (asc|desc)$/ );
75 my $sort_by_indexname = eval {
77 map {
78 $_->{code} eq $sort_indexname
79 ? 'srt_' . $_->{type} . '_' . $_->{code} . ' ' . $sort_order
80 : ()
81 } @$sortable_indexes
82 ]->[0]
85 # This array is used to build facets GUI
86 my %filters;
87 my @tplfilters;
88 for my $filter ( $cgi->param('filters') ) {
89 next if not $filter;
90 my ($k, @v) = $filter =~ /(?: \\. | [^:] )+/xg;
91 my $v = join ':', @v;
92 push @{$filters{$k}}, $v;
93 $v =~ s/^"(.*)"$/$1/; # Remove quotes around
94 push @tplfilters, {
95 'var' => $k,
96 'val' => $v,
99 push @{$filters{recordtype}}, 'biblio';
101 my $results = $search_service->search(
103 \%filters,
105 page => $page,
106 count => $count,
107 sort => $sort_by_indexname,
108 facets => 1,
109 fl => ["ste_title", "str_author", 'int_biblionumber'],
113 if ($results->{error}){
114 $template->param(query_error => $results->{error});
115 output_with_http_headers $cgi, $cookie, $template->output, 'html';
116 exit;
120 # populate results with records
121 my @r;
122 for my $searchresult ( @{ $results->items } ) {
123 my $biblionumber = $searchresult->{values}->{recordid};
125 my $nr;
126 while ( my ($k, $v) = each %{$searchresult->{values}} ) {
127 my $nk = $k;
128 $nk =~ s/^[^_]*_(.*)$/$1/;
129 $nr->{$nk} = ref $v ? shift @{$v} : $v;
131 push( @r, $nr );
134 # build facets
135 my $facets_builder = Koha::SearchEngine::FacetsBuilder->new;
136 my @facets_loop = $facets_builder->build_facets( $results, $search_engine_config->facetable_indexes, \%filters );
138 my $total = $results->{pager}->{total_entries};
139 my $pager = Data::Pagination->new(
140 $total,
141 $count,
143 $page,
146 # params we want to pass for all actions require another query (pagination, sort, facets)
147 my @follower_params = map { {
148 var => 'filters',
149 val => $_->{var}.':"'.$_->{val}.'"'
150 } } @tplfilters;
151 push @follower_params, { var => 'q', val => $q};
152 push @follower_params, { var => 'sort_by', val => $sort_by};
154 # Pager template params
155 $template->param(
156 previous_page => $pager->{'prev_page'},
157 next_page => $pager->{'next_page'},
158 PAGE_NUMBERS => [ map { { page => $_, current => $_ == $page } } @{ $pager->{'numbers_of_set'} } ],
159 current_page => $page,
160 follower_params => \@follower_params,
161 total => $total,
162 SEARCH_RESULTS => \@r,
163 query => $q,
164 count => $count,
165 sort_by => $sort_by,
166 sortable_indexes => $sortable_indexes,
167 facets_loop => \@facets_loop,
168 filters => \@tplfilters,
171 my $content_type = ( $format eq 'rss' or $format eq 'atom' ) ? $format : 'html';
172 output_with_http_headers $cgi, $cookie, $template->output, $content_type;