Bug 12478 - more authority searching
[koha.git] / opac / opac-authorities-home.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Koha;
32 use C4::Search::History;
33
34 use Koha::Authority::Types;
35 use Koha::SearchEngine::Search;
36 use Koha::SearchEngine::QueryBuilder;
37
38 my $query        = new CGI;
39 my $op           = $query->param('op') || '';
40 my $authtypecode = $query->param('authtypecode') || '';
41 my $dbh          = C4::Context->dbh;
42
43 my $startfrom = $query->param('startfrom');
44 my $authid    = $query->param('authid');
45 $startfrom = 0 if ( !defined $startfrom );
46 my ( $template, $loggedinuser, $cookie );
47 my $resultsperpage;
48
49 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
50
51 if ( $op eq "do_search" ) {
52     my @marclist = $query->multi_param('marclist');
53     my @and_or = $query->multi_param('and_or');
54     my @excluding = $query->multi_param('excluding');
55     my @operator = $query->multi_param('operator');
56     my $orderby = $query->param('orderby');
57     my @value = $query->multi_param('value');
58     $value[0] ||= q||;
59
60     $resultsperpage = $query->param('resultsperpage');
61     $resultsperpage = 20 if ( !defined $resultsperpage );
62     my @tags;
63     my $builder  = Koha::SearchEngine::QueryBuilder->new();
64     my $searcher = Koha::SearchEngine::Search->new({index => 'authorities'});
65     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
66         \@excluding, \@operator, \@value, $authtypecode, $orderby );
67 #    use Data::Dumper;
68 #    die Dumper(\@marclist, \@and_or,
69 #        \@excluding, \@operator, \@value, $authtypecode, $orderby, $query);
70     my ( $results, $total ) =
71       $searcher->search_marc( $search_query, $startfrom, $resultsperpage );
72
73     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
74         {
75             template_name   => "opac-authoritiessearchresultlist.tt",
76             query           => $query,
77             type            => 'opac',
78             authnotrequired => 1,
79             debug           => 1,
80         }
81     );
82
83     # multi page display gestion
84     my $displaynext = 0;
85     my $displayprev = $startfrom;
86     $total ||= 0;
87     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
88         $displaynext = 1;
89     }
90
91     my @field_data = (
92         { term => "marclist",  val => $marclist[0] },
93         { term => "and_or",    val => $and_or[0] },
94         { term => "excluding", val => $excluding[0] },
95         { term => "operator",  val => $operator[0] },
96         { term => "value",     val => $value[0] },
97     );
98
99     my @numbers = ();
100
101     if ( $total > $resultsperpage ) {
102         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
103             if ( $i < 16 ) {
104                 my $highlight = 0;
105                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
106                 push @numbers,
107                   {
108                     number     => $i,
109                     highlight  => $highlight,
110                     searchdata => \@field_data,
111                     startfrom  => ( $i - 1 )
112                   };
113             }
114         }
115     }
116
117     my $from = $startfrom * $resultsperpage + 1;
118     my $to;
119
120     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
121         $to = $total;
122     }
123     else {
124         $to = ( ( $startfrom + 1 ) * $resultsperpage );
125     }
126     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
127 #        TODO implement usage counts
128 #        my @usedauths = grep { $_->{used} > 0 } @$results;
129 #        $results = \@usedauths;
130     }
131
132     # Opac search history
133     if (C4::Context->preference('EnableOpacSearchHistory')) {
134         unless ( $startfrom ) {
135             my $path_info = $query->url(-path_info=>1);
136             my $query_cgi_history = $query->url(-query=>1);
137             $query_cgi_history =~ s/^$path_info\?//;
138             $query_cgi_history =~ s/;/&/g;
139
140             unless ( $loggedinuser ) {
141                 my $new_search = C4::Search::History::add_to_session({
142                         cgi => $query,
143                         query_desc => $value[0],
144                         query_cgi => $query_cgi_history,
145                         total => $total,
146                         type => "authority",
147                 });
148             } else {
149                 # To the session (the user is logged in)
150                 C4::Search::History::add({
151                     userid => $loggedinuser,
152                     sessionid => $query->cookie("CGISESSID"),
153                     query_desc => $value[0],
154                     query_cgi => $query_cgi_history,
155                     total => $total,
156                     type => "authority",
157                 });
158             }
159         }
160     }
161
162     $template->param( result => $results ) if $results;
163     $template->param( orderby => $orderby );
164     $template->param(
165         startfrom      => $startfrom,
166         displaynext    => $displaynext,
167         displayprev    => $displayprev,
168         resultsperpage => $resultsperpage,
169         startfromnext  => $startfrom + 1,
170         startfromprev  => $startfrom - 1,
171         searchdata     => \@field_data,
172         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
173         total          => $total,
174         from           => $from,
175         to             => $to,
176         resultcount    => scalar @$results,
177         numbers        => \@numbers,
178         authtypecode   => $authtypecode,
179         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
180         isEDITORS      => $authtypecode eq 'EDITORS',
181     );
182
183 }
184 else {
185     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
186         {
187             template_name   => "opac-authorities-home.tt",
188             query           => $query,
189             type            => 'opac',
190             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
191             debug           => 1,
192         }
193     );
194
195 }
196
197 $template->param(
198     authority_types => $authority_types,
199     authtypecode    => $authtypecode,
200 );
201
202 # Print the page
203 output_html_with_http_headers $query, $cookie, $template->output;
204
205 # Local Variables:
206 # tab-width: 4
207 # End: