Bug 16534: Block AddIssue from issuing if the return is not possible
[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         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
65     my $searcher = Koha::SearchEngine::Search->new(
66         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
67     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
68         \@excluding, \@operator, \@value, $authtypecode, $orderby );
69 #    use Data::Dumper;
70 #    die Dumper(\@marclist, \@and_or,
71 #        \@excluding, \@operator, \@value, $authtypecode, $orderby, $query);
72     # The searchengine API expects pages to start at page 1
73     $startfrom = $startfrom // 0;
74     my ( $results, $total ) =
75       $searcher->search_auth_compat( $search_query, $startfrom+1, $resultsperpage );
76     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
77         {
78             template_name   => "opac-authoritiessearchresultlist.tt",
79             query           => $query,
80             type            => 'opac',
81             authnotrequired => 1,
82             debug           => 1,
83         }
84     );
85
86     # multi page display gestion
87     my $displaynext = 0;
88     my $displayprev = $startfrom;
89     $total ||= 0;
90     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
91         $displaynext = 1;
92     }
93
94     my @field_data = (
95         { term => "marclist",  val => $marclist[0] },
96         { term => "and_or",    val => $and_or[0] },
97         { term => "excluding", val => $excluding[0] },
98         { term => "operator",  val => $operator[0] },
99         { term => "value",     val => $value[0] },
100     );
101
102     my @numbers = ();
103
104     if ( $total > $resultsperpage ) {
105         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
106             if ( $i < 16 ) {
107                 my $highlight = 0;
108                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
109                 push @numbers,
110                   {
111                     number     => $i,
112                     highlight  => $highlight,
113                     searchdata => \@field_data,
114                     startfrom  => ( $i - 1 )
115                   };
116             }
117         }
118     }
119
120     my $from = $startfrom * $resultsperpage + 1;
121     my $to;
122
123     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
124         $to = $total;
125     }
126     else {
127         $to = ( ( $startfrom + 1 ) * $resultsperpage );
128     }
129     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
130 #        TODO implement usage counts
131 #        my @usedauths = grep { $_->{used} > 0 } @$results;
132 #        $results = \@usedauths;
133     }
134
135     # Opac search history
136     if (C4::Context->preference('EnableOpacSearchHistory')) {
137         unless ( $startfrom ) {
138             my $path_info = $query->url(-path_info=>1);
139             my $query_cgi_history = $query->url(-query=>1);
140             $query_cgi_history =~ s/^$path_info\?//;
141             $query_cgi_history =~ s/;/&/g;
142
143             unless ( $loggedinuser ) {
144                 my $new_search = C4::Search::History::add_to_session({
145                         cgi => $query,
146                         query_desc => $value[0],
147                         query_cgi => $query_cgi_history,
148                         total => $total,
149                         type => "authority",
150                 });
151             } else {
152                 # To the session (the user is logged in)
153                 C4::Search::History::add({
154                     userid => $loggedinuser,
155                     sessionid => $query->cookie("CGISESSID"),
156                     query_desc => $value[0],
157                     query_cgi => $query_cgi_history,
158                     total => $total,
159                     type => "authority",
160                 });
161             }
162         }
163     }
164
165     $template->param( result => $results ) if $results;
166     $template->param( orderby => $orderby );
167     $template->param(
168         startfrom      => $startfrom,
169         displaynext    => $displaynext,
170         displayprev    => $displayprev,
171         resultsperpage => $resultsperpage,
172         startfromnext  => $startfrom + 1,
173         startfromprev  => $startfrom - 1,
174         searchdata     => \@field_data,
175         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
176         total          => $total,
177         from           => $from,
178         to             => $to,
179         resultcount    => scalar @$results,
180         numbers        => \@numbers,
181         authtypecode   => $authtypecode,
182         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
183         isEDITORS      => $authtypecode eq 'EDITORS',
184     );
185
186 }
187 else {
188     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
189         {
190             template_name   => "opac-authorities-home.tt",
191             query           => $query,
192             type            => 'opac',
193             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
194             debug           => 1,
195         }
196     );
197
198 }
199
200 $template->param(
201     authority_types => $authority_types,
202     authtypecode    => $authtypecode,
203 );
204
205 # Print the page
206 output_html_with_http_headers $query, $cookie, $template->output;
207
208 # Local Variables:
209 # tab-width: 4
210 # End: