Revert "Bug 25762: Typo in linkitem.tt"
[koha.git] / authorities / authorities-home.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use URI::Escape;
24 use POSIX qw( ceil );
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::AuthoritiesMarc;
30 use C4::Acquisition;
31 use C4::Koha;
32 use C4::Biblio;
33 use C4::Search::History;
34
35 use Koha::Authority::Types;
36 use Koha::SearchEngine::Search;
37 use Koha::SearchEngine::QueryBuilder;
38 use Koha::Token;
39 use Koha::Z3950Servers;
40
41 my $query = new CGI;
42 my $dbh   = C4::Context->dbh;
43 my $op           = $query->param('op')           || '';
44 my $authtypecode = $query->param('authtypecode') || '';
45 my $authid       = $query->param('authid')       || '';
46
47 my ( $template, $loggedinuser, $cookie );
48
49 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
50
51 if ( $op eq "delete" ) {
52     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53         {
54             template_name   => "authorities/authorities-home.tt",
55             query           => $query,
56             type            => 'intranet',
57             flagsrequired   => { catalogue => 1 },
58             debug           => 1,
59         }
60     );
61
62     output_and_exit( $query, $cookie, $template, 'wrong_csrf_token' )
63         unless Koha::Token->new->check_csrf({
64             session_id => scalar $query->cookie('CGISESSID'),
65             token  => scalar $query->param('csrf_token'),
66         });
67
68     DelAuthority({ authid => $authid });
69
70     if ( $query->param('operator') ) {
71         # query contains search params so perform search
72         $op = "do_search";
73     }
74     else {
75         $op = '';
76     }
77 }
78 if ( $op eq "do_search" ) {
79     my $marclist  = $query->param('marclist')  || '';
80     my $and_or    = $query->param('and_or')    || '';
81     my $excluding = $query->param('excluding') || '';
82     my $operator  = $query->param('operator')  || '';
83     my $orderby   = $query->param('orderby')   || '';
84     my $value     = $query->param('value')     || '';
85
86     my $startfrom      = $query->param('startfrom')      || 1;
87     my $resultsperpage = $query->param('resultsperpage') || 20;
88     my $offset = ( $startfrom - 1 ) * $resultsperpage + 1;
89
90     my $builder = Koha::SearchEngine::QueryBuilder->new(
91         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
92     my $searcher = Koha::SearchEngine::Search->new(
93         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
94     my $search_query = $builder->build_authorities_query_compat(
95         [$marclist], [$and_or], [$excluding], [$operator],
96         [$value], $authtypecode, $orderby
97     );
98     my ( $results, $total ) = $searcher->search_auth_compat(
99         $search_query, $offset, $resultsperpage
100     );
101
102     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
103         {
104             template_name   => "authorities/searchresultlist.tt",
105             query           => $query,
106             type            => 'intranet',
107             flagsrequired   => { catalogue => 1 },
108             debug           => 1,
109         }
110     );
111
112     $template->param(
113         csrf_token => Koha::Token->new->generate_csrf({
114             session_id => scalar $query->cookie('CGISESSID'),
115         }),
116     );
117
118     # search history
119     if (C4::Context->preference('EnableSearchHistory')) {
120         if ( $startfrom == 1) {
121             my $path_info = $query->url(-path_info=>1);
122             my $query_cgi_history = $query->url(-query=>1);
123             $query_cgi_history =~ s/^$path_info\?//;
124             $query_cgi_history =~ s/;/&/g;
125
126             C4::Search::History::add({
127                 userid => $loggedinuser,
128                 sessionid => $query->cookie("CGISESSID"),
129                 query_desc => $value,
130                 query_cgi => $query_cgi_history,
131                 total => $total,
132                 type => "authority",
133             });
134         }
135     }
136
137     $template->param(
138         marclist       => $marclist,
139         and_or         => $and_or,
140         excluding      => $excluding,
141         operator       => $operator,
142         orderby        => $orderby,
143         value          => $value,
144         authtypecode   => $authtypecode,
145         startfrom      => $startfrom,
146         resultsperpage => $resultsperpage,
147     );
148
149     # we must get parameters once again. Because if there is a mainentry, it
150     # has been replaced by something else during the search, thus the links
151     # next/previous would not work anymore
152
153     # construction of the url of each page
154     my $value_url = uri_escape_utf8($value);
155     my $base_url = "authorities-home.pl?"
156       ."marclist=$marclist"
157       ."&amp;and_or=$and_or"
158       ."&amp;excluding=$excluding"
159       ."&amp;operator=$operator"
160       ."&amp;value=$value_url"
161       ."&amp;resultsperpage=$resultsperpage"
162       ."&amp;type=intranet"
163       ."&amp;op=do_search"
164       ."&amp;authtypecode=$authtypecode"
165       ."&amp;orderby=$orderby";
166
167     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
168     my $to;
169     if ( !defined $total ) {
170         $total = 0;
171     }
172
173     if ( $total < $startfrom * $resultsperpage ) {
174         $to = $total;
175     }
176     else {
177         $to = $startfrom * $resultsperpage;
178     }
179
180     $template->param( result => $results ) if $results;
181
182     my $max_result_window = $searcher->max_result_window;
183     my $hits_to_paginate = ($max_result_window && $max_result_window < $total) ? $max_result_window : $total;
184
185     $template->param(
186         pagination_bar => pagination_bar(
187             $base_url,  ceil( $hits_to_paginate / $resultsperpage ),
188             $startfrom, 'startfrom'
189         ),
190         total            => $total,
191         hits_to_paginate => $hits_to_paginate,
192         from             => $from,
193         to               => $to,
194         isEDITORS        => $authtypecode eq 'EDITORS',
195     );
196
197 }
198 if ( $op eq '' ) {
199     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
200         {
201             template_name   => "authorities/authorities-home.tt",
202             query           => $query,
203             type            => 'intranet',
204             flagsrequired   => { catalogue => 1 },
205             debug           => 1,
206         }
207     );
208
209 }
210
211 my $servers = Koha::Z3950Servers->search(
212     {
213         recordtype => 'authority',
214         servertype => ['zed', 'sru'],
215     },
216 );
217
218 $template->param(
219     servers => $servers,
220     authority_types => $authority_types,
221     op            => $op,
222 );
223
224 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
225
226 # Print the page
227 output_html_with_http_headers $query, $cookie, $template->output;