Merge commit 'koha-biblibre/master' into bl-acq
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21
22 use CGI;
23 use C4::Auth;
24
25 use C4::Context;
26 use C4::Auth;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Acquisition;
30 use C4::Koha;    # XXX subfield_is_koha_internal_p
31 use C4::Biblio;
32
33 my $query        = new CGI;
34 my $op           = $query->param('op');
35 my $authtypecode = $query->param('authtypecode');
36 my $dbh          = C4::Context->dbh;
37
38 my $authid = $query->param('authid');
39 my ( $template, $loggedinuser, $cookie );
40
41 my $authtypes = getauthtypes;
42 my @authtypesloop;
43 foreach my $thisauthtype ( sort { $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'} }
44     keys %$authtypes )
45 {
46     my $selected = 1 if $thisauthtype eq $authtypecode;
47     my %row = (
48         value        => $thisauthtype,
49         selected     => $selected,
50         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
51     );
52     push @authtypesloop, \%row;
53 }
54
55 if ( $op eq "do_search" ) {
56     my @marclist  = $query->param('marclist');
57     my @and_or    = $query->param('and_or');
58     my @excluding = $query->param('excluding');
59     my @operator  = $query->param('operator');
60     my $orderby   = $query->param('orderby');
61     my @value     = $query->param('value');
62
63     my $startfrom      = $query->param('startfrom')      || 1;
64     my $resultsperpage = $query->param('resultsperpage') || 20;
65
66     my ( $results, $total ) =
67       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value,
68         ( $startfrom - 1 ) * $resultsperpage,
69         $resultsperpage, $authtypecode, $orderby );
70 #     use Data::Dumper; warn Data::Dumper::Dumper(@$results);
71     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
72         {
73             template_name   => "authorities/searchresultlist.tmpl",
74             query           => $query,
75             type            => 'intranet',
76             authnotrequired => 0,
77             flagsrequired   => { catalogue => 1 },
78             debug           => 1,
79         }
80     );
81
82     my @field_data = ();
83
84     # we must get parameters once again. Because if there is a mainentry, it
85     # has been replaced by something else during the search, thus the links
86     # next/previous would not work anymore
87     my @marclist_ini = $query->param('marclist');
88     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
89         if ($value[$i]){   
90           push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
91           push @field_data, { term => "and_or",    val => $and_or[$i] };
92           push @field_data, { term => "excluding", val => $excluding[$i] };
93           push @field_data, { term => "operator",  val => $operator[$i] };
94           push @field_data, { term => "value",     val => $value[$i] };
95         }    
96     }
97
98     # construction of the url of each page
99     my $base_url =
100         'authorities-home.pl?'
101       . join( '&amp;', map { $_->{term} . '=' . $_->{val} } @field_data )
102       . '&amp;'
103       . join(
104         '&amp;',
105         map { $_->{term} . '=' . $_->{val} } (
106             { term => 'resultsperpage', val => $resultsperpage },
107             { term => 'type',           val => 'intranet' },
108             { term => 'op',             val => 'do_search' },
109             { term => 'authtypecode',   val => $authtypecode },
110             { term => 'orderby',        val => $orderby },
111         )
112       );
113
114     my $from = ( $startfrom - 1 ) * $resultsperpage + 1;
115     my $to;
116
117     if ( $total < $startfrom * $resultsperpage ) {
118         $to = $total;
119     }
120     else {
121         $to = $startfrom * $resultsperpage;
122     }
123
124     $template->param( result => $results ) if $results;
125
126     $template->param(
127         pagination_bar => pagination_bar(
128             $base_url,  int( $total / $resultsperpage ) + 1,
129             $startfrom, 'startfrom'
130         ),
131         total     => $total,
132         from      => $from,
133         to        => $to,
134         isEDITORS => $authtypecode eq 'EDITORS',
135     );
136
137 }
138 elsif ( $op eq "delete" ) {
139
140     &DelAuthority( $authid, 1 );
141
142     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
143         {
144             template_name   => "authorities/authorities-home.tmpl",
145             query           => $query,
146             type            => 'intranet',
147             authnotrequired => 0,
148             flagsrequired   => { catalogue => 1 },
149             debug           => 1,
150         }
151     );
152
153     #   $template->param("statements" => \@statements,
154     #                                           "nbstatements" => $nbstatements);
155 }
156 else {
157     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
158         {
159             template_name   => "authorities/authorities-home.tmpl",
160             query           => $query,
161             type            => 'intranet',
162             authnotrequired => 0,
163             flagsrequired   => { catalogue => 1 },
164             debug           => 1,
165         }
166     );
167
168 }
169
170 $template->param(
171     authtypesloop => \@authtypesloop,
172 );
173
174 # Print the page
175 output_html_with_http_headers $query, $cookie, $template->output;