bugfix - don't allow deletion of authority records from OPAC
[koha.git] / opac / opac-authorities-home.pl
1 #!/usr/bin/perl
2 # WARNING: 4-character tab stops here
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 require Exporter;
23 use CGI;
24 use C4::Auth;
25
26 use C4::Context;
27 use C4::Auth;
28 use C4::Output;
29 use C4::AuthoritiesMarc;
30 use C4::Koha;    # XXX subfield_is_koha_internal_p
31
32 my $query        = new CGI;
33 my $op           = $query->param('op');
34 my $authtypecode = $query->param('authtypecode');
35 my $dbh          = C4::Context->dbh;
36
37 my $startfrom = $query->param('startfrom');
38 my $authid    = $query->param('authid');
39 $startfrom = 0 if ( !defined $startfrom );
40 my ( $template, $loggedinuser, $cookie );
41 my $resultsperpage;
42
43 my $authtypes = getauthtypes;
44 my @authtypesloop;
45 foreach my $thisauthtype ( sort { $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'} }
46     keys %$authtypes )
47 {
48     my $selected = 1 if $thisauthtype eq $authtypecode;
49     my %row = (
50         value        => $thisauthtype,
51         selected     => $selected,
52         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
53     );
54     push @authtypesloop, \%row;
55 }
56
57 if ( $op eq "do_search" ) {
58     my @marclist = $query->param('marclist');
59     my @and_or = $query->param('and_or');
60     my @excluding = $query->param('excluding');
61     my @operator = $query->param('operator');
62     my $orderby = $query->param('orderby');
63     my @value = $query->param('value');
64
65     $resultsperpage = $query->param('resultsperpage');
66     $resultsperpage = 19 if ( !defined $resultsperpage );
67     my @tags;
68     my ( $results, $total, @fields ) =
69       SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator,
70         \@value, $startfrom * $resultsperpage,
71         $resultsperpage, $authtypecode, $orderby );
72     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
73         {
74             template_name   => "opac-authoritiessearchresultlist.tmpl",
75             query           => $query,
76             type            => 'opac',
77             authnotrequired => 1,
78             debug           => 1,
79         }
80     );
81
82     # multi page display gestion
83     my $displaynext = 0;
84     my $displayprev = $startfrom;
85     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
86         $displaynext = 1;
87     }
88
89     my @field_data = ();
90
91 # we must get parameters once again. Because if there is a mainentry, it has been replaced by something else during the search, thus the links next/previous would not work anymore
92     my @marclist_ini = $query->param('marclist');
93     for ( my $i = 0 ; $i <= $#marclist ; $i++ ) {
94         push @field_data, { term => "marclist",  val => $marclist_ini[$i] };
95         push @field_data, { term => "and_or",    val => $and_or[$i] };
96         push @field_data, { term => "excluding", val => $excluding[$i] };
97         push @field_data, { term => "operator",  val => $operator[$i] };
98         push @field_data, { term => "value",     val => $value[$i] };
99     }
100
101     my @numbers = ();
102
103     if ( $total > $resultsperpage ) {
104         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
105             if ( $i < 16 ) {
106                 my $highlight = 0;
107                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
108                 push @numbers,
109                   {
110                     number     => $i,
111                     highlight  => $highlight,
112                     searchdata => \@field_data,
113                     startfrom  => ( $i - 1 )
114                   };
115             }
116         }
117     }
118
119     my $from = $startfrom * $resultsperpage + 1;
120     my $to;
121
122     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
123         $to = $total;
124     }
125     else {
126         $to = ( ( $startfrom + 1 ) * $resultsperpage );
127     }
128     $template->param( result => $results ) if $results;
129     $template->param( FIELDS => \@fields );
130     $template->param( orderby => $orderby );
131     $template->param(
132         startfrom      => $startfrom,
133         displaynext    => $displaynext,
134         displayprev    => $displayprev,
135         resultsperpage => $resultsperpage,
136         startfromnext  => $startfrom + 1,
137         startfromprev  => $startfrom - 1,
138         searchdata     => \@field_data,
139         total          => $total,
140         from           => $from,
141         to             => $to,
142         numbers        => \@numbers,
143         authtypecode   => $authtypecode,
144         isEDITORS      => $authtypecode eq 'EDITORS',
145     );
146
147 }
148 elsif ( $op eq "AddStatement" ) {
149
150     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
151         {
152             template_name   => "authorities/authorities-home.tmpl",
153             query           => $query,
154             type            => 'intranet',
155             authnotrequired => 0,
156             flagsrequired   => { catalogue => 1 },
157             debug           => 1,
158         }
159     );
160
161     # Gets the entered information
162     my @marcfields = $query->param('marclist');
163     my @and_or     = $query->param('and_or');
164     my @excluding  = $query->param('excluding');
165     my @operator   = $query->param('operator');
166     my @value      = $query->param('value');
167
168     my @statements = ();
169
170     # List of the marc tags to display
171     my $marcarray = create_marclist();
172
173     my $nbstatements = $query->param('nbstatements');
174     $nbstatements = 1 if ( !defined $nbstatements );
175
176     for ( my $i = 0 ; $i < $nbstatements ; $i++ ) {
177         my %fields = ();
178
179         # Recreates the old scrolling lists with the previously selected values
180         my $marclist = create_scrolling_list(
181             {
182                 name     => "marclist",
183                 values   => $marcarray,
184                 size     => 1,
185                 default  => $marcfields[$i],
186                 onChange => "sql_update()"
187             }
188         );
189
190         $fields{'marclist'} = $marclist;
191         $fields{'first'}    = 1 if ( $i == 0 );
192
193 # Restores the and/or parameters (no need to test the 'and' for activation because it's the default value)
194         $fields{'or'} = 1 if ( $and_or[$i] eq "or" );
195
196         #Restores the "not" parameters
197         $fields{'not'} = 1 if ( $excluding[$i] );
198
199         #Restores the operators (most common operators first);
200         if    ( $operator[$i] eq "=" )        { $fields{'eq'}       = 1; }
201         elsif ( $operator[$i] eq "contains" ) { $fields{'contains'} = 1; }
202         elsif ( $operator[$i] eq "start" )    { $fields{'start'}    = 1; }
203         elsif ( $operator[$i] eq ">" )  { $fields{'gt'} = 1; } #greater than
204         elsif ( $operator[$i] eq ">=" ) { $fields{'ge'} = 1; } #greater or equal
205         elsif ( $operator[$i] eq "<" )  { $fields{'lt'} = 1; } #lower than
206         elsif ( $operator[$i] eq "<=" ) { $fields{'le'} = 1; } #lower or equal
207
208         #Restores the value
209         $fields{'value'} = $value[$i];
210
211         push @statements, \%fields;
212     }
213     $nbstatements++;
214
215     # The new scrolling list
216     my $marclist = create_scrolling_list(
217         {
218             name     => "marclist",
219             values   => $marcarray,
220             size     => 1,
221             onChange => "sql_update()"
222         }
223     );
224     push @statements, { "marclist" => $marclist };
225
226     $template->param(
227         "statements"   => \@statements,
228         "nbstatements" => $nbstatements
229     );
230
231 }
232 else {
233     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
234         {
235             template_name   => "opac-authorities-home.tmpl",
236             query           => $query,
237             type            => 'opac',
238             authnotrequired => 1,
239             debug           => 1,
240         }
241     );
242
243 }
244
245 $template->param( authtypesloop => \@authtypesloop );
246
247 # Print the page
248 output_html_with_http_headers $query, $cookie, $template->output;
249
250 # Local Variables:
251 # tab-width: 4
252 # End: