Bug 108661: (follow-up) enshrine letting CardnumberLength specify just a maximum
[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
35 my $query = new CGI;
36 my $dbh   = C4::Context->dbh;
37 my $op           = $query->param('op')           || '';
38 my $authtypecode = $query->param('authtypecode') || '';
39 my $authid       = $query->param('authid')       || '';
40
41 my ( $template, $loggedinuser, $cookie );
42
43 my $authtypes = getauthtypes;
44 my @authtypesloop;
45 foreach my $thisauthtype (
46     sort {
47         $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
48     }
49     keys %$authtypes
50   )
51 {
52     my %row = (
53         value        => $thisauthtype,
54         selected     => $thisauthtype eq $authtypecode,
55         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
56     );
57     push @authtypesloop, \%row;
58 }
59
60 if ( $op eq "delete" ) {
61     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62         {
63             template_name   => "authorities/authorities-home.tmpl",
64             query           => $query,
65             type            => 'intranet',
66             authnotrequired => 0,
67             flagsrequired   => { catalogue => 1 },
68             debug           => 1,
69         }
70     );
71     &DelAuthority( $authid, 1 );
72
73     if ( $query->param('operator') ) {
74         # query contains search params so perform search
75         $op = "do_search";
76     }
77     else {
78         $op = '';
79     }
80 }
81 if ( $op eq "do_search" ) {
82     my $marclist  = $query->param('marclist')  || '';
83     my $and_or    = $query->param('and_or')    || '';
84     my $excluding = $query->param('excluding') || '';
85     my $operator  = $query->param('operator')  || '';
86     my $orderby   = $query->param('orderby')   || '';
87     my $value     = $query->param('value')     || '';
88
89     my $startfrom      = $query->param('startfrom')      || 1;
90     my $resultsperpage = $query->param('resultsperpage') || 20;
91
92     my ( $results, $total ) = SearchAuthorities(
93         [$marclist],  [$and_or],
94         [$excluding], [$operator],
95         [$value], ( $startfrom - 1 ) * $resultsperpage,
96         $resultsperpage, $authtypecode,
97         $orderby
98     );
99
100     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
101         {
102             template_name   => "authorities/searchresultlist.tmpl",
103             query           => $query,
104             type            => 'intranet',
105             authnotrequired => 0,
106             flagsrequired   => { catalogue => 1 },
107             debug           => 1,
108         }
109     );
110
111     $template->param(
112         marclist       => $marclist,
113         and_or         => $and_or,
114         excluding      => $excluding,
115         operator       => $operator,
116         orderby        => $orderby,
117         value          => $value,
118         authtypecode   => $authtypecode,
119         startfrom      => $startfrom,
120         resultsperpage => $resultsperpage,
121     );
122
123     # we must get parameters once again. Because if there is a mainentry, it
124     # has been replaced by something else during the search, thus the links
125     # next/previous would not work anymore
126
127     # construction of the url of each page
128     my $value_url = uri_escape($value);
129     my $base_url = "authorities-home.pl?"
130       ."marclist=$marclist"
131       ."&and_or=$and_or"
132       ."&excluding=$excluding"
133       ."&operator=$operator"
134       ."&value=$value_url"
135       ."&resultsperpage=$resultsperpage"
136       ."&type=intranet"
137       ."&op=do_search"
138       ."&authtypecode=$authtypecode"
139       ."&orderby=$orderby";
140
141     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
142     my $to;
143     if ( !defined $total ) {
144         $total = 0;
145     }
146
147     if ( $total < $startfrom * $resultsperpage ) {
148         $to = $total;
149     }
150     else {
151         $to = $startfrom * $resultsperpage;
152     }
153
154     $template->param( result => $results ) if $results;
155
156     $template->param(
157         pagination_bar => pagination_bar(
158             $base_url,  int( $total / $resultsperpage ) + 1,
159             $startfrom, 'startfrom'
160         ),
161         total     => $total,
162         from      => $from,
163         to        => $to,
164         isEDITORS => $authtypecode eq 'EDITORS',
165     );
166
167 }
168 if ( $op eq '' ) {
169     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
170         {
171             template_name   => "authorities/authorities-home.tmpl",
172             query           => $query,
173             type            => 'intranet',
174             authnotrequired => 0,
175             flagsrequired   => { catalogue => 1 },
176             debug           => 1,
177         }
178     );
179
180 }
181
182 $template->param(
183     authtypesloop => \@authtypesloop,
184     op            => $op,
185 );
186
187 $template->{VARS}->{marcflavour} = C4::Context->preference("marcflavour");
188
189 # Print the page
190 output_html_with_http_headers $query, $cookie, $template->output;