Bug 11349: Change .tmpl -> .tt in scripts using templates
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
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 my $query = new CGI;
37 my $dbh   = C4::Context->dbh;
38 my $op           = $query->param('op')           || '';
39 my $authtypecode = $query->param('authtypecode') || '';
40 my $authid       = $query->param('authid')       || '';
41
42 my ( $template, $loggedinuser, $cookie );
43
44 my $authtypes = getauthtypes;
45 my @authtypesloop;
46 foreach my $thisauthtype (
47     sort {
48         $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
49     }
50     keys %$authtypes
51   )
52 {
53     my %row = (
54         value        => $thisauthtype,
55         selected     => $thisauthtype eq $authtypecode,
56         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
57     );
58     push @authtypesloop, \%row;
59 }
60
61 if ( $op eq "delete" ) {
62     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63         {
64             template_name   => "authorities/authorities-home.tt",
65             query           => $query,
66             type            => 'intranet',
67             authnotrequired => 0,
68             flagsrequired   => { catalogue => 1 },
69             debug           => 1,
70         }
71     );
72     &DelAuthority( $authid, 1 );
73
74     if ( $query->param('operator') ) {
75         # query contains search params so perform search
76         $op = "do_search";
77     }
78     else {
79         $op = '';
80     }
81 }
82 if ( $op eq "do_search" ) {
83     my $marclist  = $query->param('marclist')  || '';
84     my $and_or    = $query->param('and_or')    || '';
85     my $excluding = $query->param('excluding') || '';
86     my $operator  = $query->param('operator')  || '';
87     my $orderby   = $query->param('orderby')   || '';
88     my $value     = $query->param('value')     || '';
89
90     my $startfrom      = $query->param('startfrom')      || 1;
91     my $resultsperpage = $query->param('resultsperpage') || 20;
92
93     my ( $results, $total ) = SearchAuthorities(
94         [$marclist],  [$and_or],
95         [$excluding], [$operator],
96         [$value], ( $startfrom - 1 ) * $resultsperpage,
97         $resultsperpage, $authtypecode,
98         $orderby
99     );
100
101
102     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
103         {
104             template_name   => "authorities/searchresultlist.tt",
105             query           => $query,
106             type            => 'intranet',
107             authnotrequired => 0,
108             flagsrequired   => { catalogue => 1 },
109             debug           => 1,
110         }
111     );
112
113     # search history
114     if (C4::Context->preference('EnableSearchHistory')) {
115         if ( $startfrom == 1) {
116             my $path_info = $query->url(-path_info=>1);
117             my $query_cgi_history = $query->url(-query=>1);
118             $query_cgi_history =~ s/^$path_info\?//;
119             $query_cgi_history =~ s/;/&/g;
120
121             C4::Search::History::add({
122                 userid => $loggedinuser,
123                 sessionid => $query->cookie("CGISESSID"),
124                 query_desc => $value,
125                 query_cgi => $query_cgi_history,
126                 total => $total,
127                 type => "authority",
128             });
129         }
130     }
131
132     $template->param(
133         marclist       => $marclist,
134         and_or         => $and_or,
135         excluding      => $excluding,
136         operator       => $operator,
137         orderby        => $orderby,
138         value          => $value,
139         authtypecode   => $authtypecode,
140         startfrom      => $startfrom,
141         resultsperpage => $resultsperpage,
142     );
143
144     # we must get parameters once again. Because if there is a mainentry, it
145     # has been replaced by something else during the search, thus the links
146     # next/previous would not work anymore
147
148     # construction of the url of each page
149     my $value_url = uri_escape($value);
150     my $base_url = "authorities-home.pl?"
151       ."marclist=$marclist"
152       ."&and_or=$and_or"
153       ."&excluding=$excluding"
154       ."&operator=$operator"
155       ."&value=$value_url"
156       ."&resultsperpage=$resultsperpage"
157       ."&type=intranet"
158       ."&op=do_search"
159       ."&authtypecode=$authtypecode"
160       ."&orderby=$orderby";
161
162     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
163     my $to;
164     if ( !defined $total ) {
165         $total = 0;
166     }
167
168     if ( $total < $startfrom * $resultsperpage ) {
169         $to = $total;
170     }
171     else {
172         $to = $startfrom * $resultsperpage;
173     }
174
175     $template->param( result => $results ) if $results;
176
177     $template->param(
178         pagination_bar => pagination_bar(
179             $base_url,  int( $total / $resultsperpage ) + 1,
180             $startfrom, 'startfrom'
181         ),
182         total     => $total,
183         from      => $from,
184         to        => $to,
185         isEDITORS => $authtypecode eq 'EDITORS',
186     );
187
188 }
189 if ( $op eq '' ) {
190     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
191         {
192             template_name   => "authorities/authorities-home.tt",
193             query           => $query,
194             type            => 'intranet',
195             authnotrequired => 0,
196             flagsrequired   => { catalogue => 1 },
197             debug           => 1,
198         }
199     );
200
201 }
202
203 $template->param(
204     authtypesloop => \@authtypesloop,
205     op            => $op,
206 );
207
208 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
209
210 # Print the page
211 output_html_with_http_headers $query, $cookie, $template->output;