Bug 30477: Add new UNIMARC installer translation files
[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 Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use URI::Escape qw( uri_escape_utf8 );
25 use C4::Auth qw( get_template_and_user );
26
27 use C4::Context;
28 use C4::Output qw( pagination_bar output_html_with_http_headers );
29 use C4::Koha;
30 use C4::Search::History;
31
32 use Koha::Authority::Types;
33 use Koha::SearchEngine::Search;
34 use Koha::SearchEngine::QueryBuilder;
35
36 my $query        = CGI->new;
37 my $op           = $query->param('op') || '';
38 my $authtypecode = $query->param('authtypecode') || '';
39 my $dbh          = C4::Context->dbh;
40
41 my $startfrom = $query->param('startfrom') || 1;
42 my $resultsperpage = $query->param('resultsperpage') || 20;
43 my $authid    = $query->param('authid');
44 my ( $template, $loggedinuser, $cookie );
45
46 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
47
48 if ( $op eq "do_search" ) {
49     my @marclist = $query->multi_param('marclist');
50     my @and_or = $query->multi_param('and_or');
51     my @excluding = $query->multi_param('excluding');
52     my @operator = $query->multi_param('operator');
53     my $orderby = $query->param('orderby');
54     my @value = $query->multi_param('value');
55     $value[0] ||= q||;
56
57     my $builder = Koha::SearchEngine::QueryBuilder->new(
58         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
59     my $searcher = Koha::SearchEngine::Search->new(
60         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
61     my $search_query = $builder->build_authorities_query_compat( \@marclist, \@and_or,
62         \@excluding, \@operator, \@value, $authtypecode, $orderby );
63     my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
64     my ( $results, $total ) =
65       $searcher->search_auth_compat( $search_query, $offset, $resultsperpage );
66     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
67         {
68             template_name   => "opac-authoritiessearchresultlist.tt",
69             query           => $query,
70             type            => 'opac',
71             authnotrequired => 1,
72         }
73     );
74     $template->param( search_query => $search_query ) if C4::Context->preference('DumpSearchQueryTemplate');
75
76     # multi page display gestion
77     my $value_url = uri_escape_utf8($value[0]);
78     my $base_url = "opac-authorities-home.pl?"
79       ."marclist=$marclist[0]"
80       ."&amp;and_or=$and_or[0]"
81       ."&amp;excluding=$excluding[0]"
82       ."&amp;operator=$operator[0]"
83       ."&amp;value=$value_url"
84       ."&amp;resultsperpage=$resultsperpage"
85       ."&amp;type=opac"
86       ."&amp;op=do_search"
87       ."&amp;authtypecode=$authtypecode"
88       ."&amp;orderby=$orderby";
89
90     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
91     my $to;
92     if ( !defined $total ) {
93         $total = 0;
94     }
95
96     if ( $total < $startfrom * $resultsperpage ) {
97         $to = $total;
98     }
99     else {
100         $to = $startfrom * $resultsperpage;
101     }
102
103     $template->param( result => $results ) if $results;
104
105     $template->param(
106         pagination_bar => pagination_bar(
107             $base_url,  int( $total / $resultsperpage ) + 1,
108             $startfrom, 'startfrom'
109         ),
110         total     => $total,
111         from      => $from,
112         to        => $to,
113     );
114
115     unless (C4::Context->preference('OPACShowUnusedAuthorities')) {
116 #        TODO implement usage counts
117 #        my @usedauths = grep { $_->{used} > 0 } @$results;
118 #        $results = \@usedauths;
119     }
120
121     # Opac search history
122     if (C4::Context->preference('EnableOpacSearchHistory')) {
123         if ( $startfrom == 1) {
124             my $path_info = $query->url(-path_info=>1);
125             my $query_cgi_history = $query->url(-query=>1);
126             $query_cgi_history =~ s/^$path_info\?//;
127             $query_cgi_history =~ s/;/&/g;
128
129             unless ( $loggedinuser ) {
130                 my $new_search = C4::Search::History::add_to_session({
131                         cgi => $query,
132                         query_desc => $value[0],
133                         query_cgi => $query_cgi_history,
134                         total => $total,
135                         type => "authority",
136                 });
137             } else {
138                 # To the session (the user is logged in)
139                 C4::Search::History::add({
140                     userid => $loggedinuser,
141                     sessionid => $query->cookie("CGISESSID"),
142                     query_desc => $value[0],
143                     query_cgi => $query_cgi_history,
144                     total => $total,
145                     type => "authority",
146                 });
147             }
148         }
149     }
150
151     $template->param( orderby => $orderby );
152     $template->param(
153         startfrom      => $startfrom,
154         resultsperpage => $resultsperpage,
155         countfuzzy     => !(C4::Context->preference('OPACShowUnusedAuthorities')),
156         resultcount    => scalar @$results,
157         authtypecode   => $authtypecode,
158         authtypetext   => $authority_types->find($authtypecode)->authtypetext,
159         isEDITORS      => $authtypecode eq 'EDITORS',
160     );
161
162 }
163 else {
164     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
165         {
166             template_name   => "opac-authorities-home.tt",
167             query           => $query,
168             type            => 'opac',
169             authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
170         }
171     );
172
173 }
174
175 $template->param(
176     authority_types => $authority_types,
177     authtypecode    => $authtypecode,
178 );
179
180 # Print the page
181 output_html_with_http_headers $query, $cookie, $template->output;
182
183 # Local Variables:
184 # tab-width: 4
185 # End: