Bug 15920: Clean up and fix errors in batch checkout template
[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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use URI::Escape;
25 use C4::Auth;
26
27 use C4::Context;
28 use C4::Auth;
29 use C4::Output;
30 use C4::AuthoritiesMarc;
31 use C4::Acquisition;
32 use C4::Koha;    # XXX subfield_is_koha_internal_p
33 use C4::Biblio;
34 use C4::Search::History;
35
36 use Koha::Authority::Types;
37
38 my $query = new CGI;
39 my $dbh   = C4::Context->dbh;
40 my $op           = $query->param('op')           || '';
41 my $authtypecode = $query->param('authtypecode') || '';
42 my $authid       = $query->param('authid')       || '';
43
44 my ( $template, $loggedinuser, $cookie );
45
46 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
47
48 if ( $op eq "delete" ) {
49     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50         {
51             template_name   => "authorities/authorities-home.tt",
52             query           => $query,
53             type            => 'intranet',
54             authnotrequired => 0,
55             flagsrequired   => { catalogue => 1 },
56             debug           => 1,
57         }
58     );
59     &DelAuthority( $authid, 1 );
60
61     if ( $query->param('operator') ) {
62         # query contains search params so perform search
63         $op = "do_search";
64     }
65     else {
66         $op = '';
67     }
68 }
69 if ( $op eq "do_search" ) {
70     my $marclist  = $query->param('marclist')  || '';
71     my $and_or    = $query->param('and_or')    || '';
72     my $excluding = $query->param('excluding') || '';
73     my $operator  = $query->param('operator')  || '';
74     my $orderby   = $query->param('orderby')   || '';
75     my $value     = $query->param('value')     || '';
76
77     my $startfrom      = $query->param('startfrom')      || 1;
78     my $resultsperpage = $query->param('resultsperpage') || 20;
79
80     my ( $results, $total ) = SearchAuthorities(
81         [$marclist],  [$and_or],
82         [$excluding], [$operator],
83         [$value], ( $startfrom - 1 ) * $resultsperpage,
84         $resultsperpage, $authtypecode,
85         $orderby
86     );
87
88
89     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
90         {
91             template_name   => "authorities/searchresultlist.tt",
92             query           => $query,
93             type            => 'intranet',
94             authnotrequired => 0,
95             flagsrequired   => { catalogue => 1 },
96             debug           => 1,
97         }
98     );
99
100     # search history
101     if (C4::Context->preference('EnableSearchHistory')) {
102         if ( $startfrom == 1) {
103             my $path_info = $query->url(-path_info=>1);
104             my $query_cgi_history = $query->url(-query=>1);
105             $query_cgi_history =~ s/^$path_info\?//;
106             $query_cgi_history =~ s/;/&/g;
107
108             C4::Search::History::add({
109                 userid => $loggedinuser,
110                 sessionid => $query->cookie("CGISESSID"),
111                 query_desc => $value,
112                 query_cgi => $query_cgi_history,
113                 total => $total,
114                 type => "authority",
115             });
116         }
117     }
118
119     $template->param(
120         marclist       => $marclist,
121         and_or         => $and_or,
122         excluding      => $excluding,
123         operator       => $operator,
124         orderby        => $orderby,
125         value          => $value,
126         authtypecode   => $authtypecode,
127         startfrom      => $startfrom,
128         resultsperpage => $resultsperpage,
129     );
130
131     # we must get parameters once again. Because if there is a mainentry, it
132     # has been replaced by something else during the search, thus the links
133     # next/previous would not work anymore
134
135     # construction of the url of each page
136     my $value_url = uri_escape_utf8($value);
137     my $base_url = "authorities-home.pl?"
138       ."marclist=$marclist"
139       ."&amp;and_or=$and_or"
140       ."&amp;excluding=$excluding"
141       ."&amp;operator=$operator"
142       ."&amp;value=$value_url"
143       ."&amp;resultsperpage=$resultsperpage"
144       ."&amp;type=intranet"
145       ."&amp;op=do_search"
146       ."&amp;authtypecode=$authtypecode"
147       ."&amp;orderby=$orderby";
148
149     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
150     my $to;
151     if ( !defined $total ) {
152         $total = 0;
153     }
154
155     if ( $total < $startfrom * $resultsperpage ) {
156         $to = $total;
157     }
158     else {
159         $to = $startfrom * $resultsperpage;
160     }
161
162     $template->param( result => $results ) if $results;
163
164     $template->param(
165         pagination_bar => pagination_bar(
166             $base_url,  int( $total / $resultsperpage ) + 1,
167             $startfrom, 'startfrom'
168         ),
169         total     => $total,
170         from      => $from,
171         to        => $to,
172         isEDITORS => $authtypecode eq 'EDITORS',
173     );
174
175 }
176 if ( $op eq '' ) {
177     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
178         {
179             template_name   => "authorities/authorities-home.tt",
180             query           => $query,
181             type            => 'intranet',
182             authnotrequired => 0,
183             flagsrequired   => { catalogue => 1 },
184             debug           => 1,
185         }
186     );
187
188 }
189
190 $template->param(
191     authority_types => $authority_types,
192     op            => $op,
193 );
194
195 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
196
197 # Print the page
198 output_html_with_http_headers $query, $cookie, $template->output;