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
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.
27 use Koha
::SearchEngine
::Search
;
28 use Koha
::SearchEngine
::QueryBuilder
;
29 use Koha
::SearchEngine
::FacetsBuilder
;
34 my $template_type = "basic";
35 if ( $cgi->param("idx") or $cgi->param("q") ) {
36 $template_name = 'search/results.tt';
38 $template_name = 'search/advsearch.tt';
39 $template_type = 'advsearch';
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
44 { template_name
=> $template_name,
51 my $format = $cgi->param("format") || 'html';
57 my $itemtypes = GetItemTypes
;
59 my $page = $cgi->param("page") || 1;
60 my $count = $cgi->param('count') || C4
::Context
->preference('OPACnumSearchResults') || 20;
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 {
78 $_->{code
} eq $sort_indexname
79 ?
'srt_' . $_->{type
} . '_' . $_->{code
} . ' ' . $sort_order
85 # This array is used to build facets GUI
88 for my $filter ( $cgi->param('filters') ) {
90 my ($k, @v) = $filter =~ /(?: \\. | [^:] )+/xg;
92 push @
{$filters{$k}}, $v;
93 $v =~ s/^"(.*)"$/$1/; # Remove quotes around
99 push @
{$filters{recordtype
}}, 'biblio';
101 my $results = $search_service->search(
107 sort => $sort_by_indexname,
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';
120 # populate results with records
122 for my $searchresult ( @
{ $results->items } ) {
123 my $biblionumber = $searchresult->{values}->{recordid
};
126 while ( my ($k, $v) = each %{$searchresult->{values}} ) {
128 $nk =~ s/^[^_]*_(.*)$/$1/;
129 $nr->{$nk} = ref $v ?
shift @
{$v} : $v;
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(
146 # params we want to pass for all actions require another query (pagination, sort, facets)
147 my @follower_params = map { {
149 val
=> $_->{var
}.':"'.$_->{val
}.'"'
151 push @follower_params, { var
=> 'q', val
=> $q};
152 push @follower_params, { var
=> 'sort_by', val
=> $sort_by};
154 # Pager template params
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,
162 SEARCH_RESULTS
=> \
@r,
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;