Change syspref case to make tests pass
[koha.git] / admin / auth_subfields_structure.pl
blob321a33731e1eb7908a0e737659985b2a46e8c6f4
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Koha;
29 sub string_search {
30 my ($searchstring,$authtypecode)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
34 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
35 $sth->execute("$searchstring%",$authtypecode);
36 my $results = $sth->fetchall_arrayref({});
37 return (scalar(@$results), $results);
40 sub auth_subfield_structure_exists {
41 my ($authtypecode, $tagfield, $tagsubfield) = @_;
42 my $dbh = C4::Context->dbh;
43 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
44 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
45 return @$rows > 0;
48 my $input = new CGI;
49 my $tagfield = $input->param('tagfield');
50 my $tagsubfield = $input->param('tagsubfield');
51 my $authtypecode = $input->param('authtypecode');
52 my $offset = $input->param('offset');
53 my $op = $input->param('op') || '';
54 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
56 my ($template, $borrowernumber, $cookie) = get_template_and_user(
57 { template_name => "admin/auth_subfields_structure.tt",
58 query => $input,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => { parameters => 'parameters_remaining_permissions' },
62 debug => 1,
65 my $pagesize = 30;
66 $tagfield =~ s/\,//g;
68 if ($op) {
69 $template->param(script_name => $script_name,
70 tagfield =>$tagfield,
71 authtypecode => $authtypecode,
72 $op => 1); # we show only the TMPL_VAR names $op
73 } else {
74 $template->param(script_name => $script_name,
75 tagfield =>$tagfield,
76 authtypecode => $authtypecode,
77 else => 1); # we show only the TMPL_VAR names $op
80 my $dbh = C4::Context->dbh;
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or modify a record
83 if ($op eq 'add_form') {
84 my $data;
85 my $more_subfields = $input->param("more_subfields")+1;
86 # builds kohafield tables
87 my @kohafields;
88 push @kohafields, "";
89 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
90 $sth2->execute;
91 while ((my $field) = $sth2->fetchrow_array) {
92 push @kohafields, "auth_header.".$field;
95 # build authorised value list
96 my $authorised_values = C4::Koha::GetAuthorisedValueCategories;
97 unshift @$authorised_values, '';
98 push @$authorised_values, 'branches';
99 push @$authorised_values, 'itemtypes';
101 # build thesaurus categories list
102 my @authtypes = (sort keys %{C4::Koha::getauthtypes()});
104 # build value_builder list
105 my @value_builder=('');
107 # read value_builder directory.
108 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
109 # on a standard install, /cgi-bin need to be added.
110 # test one, then the other
111 my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
112 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
113 $cgidir = C4::Context->config('intranetdir');
114 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
116 while (my $line = readdir(DIR)) {
117 if ( $line =~ /\.pl$/ &&
118 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
119 push (@value_builder,$line);
122 @value_builder= sort {$a cmp $b} @value_builder;
123 closedir DIR;
125 # build values list
126 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
127 $sth->execute($tagfield,$authtypecode);
128 my @loop_data = ();
129 my $i=0;
130 while ($data =$sth->fetchrow_hashref) {
132 my %row_data; # get a fresh hash for the row data
133 $row_data{defaultvalue} = $data->{defaultvalue};
134 $row_data{tab} = {
135 id => "tab$i",
136 default => $data->{'tab'},
138 $row_data{ohidden} = {
139 id => "ohidden$i",
140 default => $data->{'hidden'}
142 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
143 $row_data{tagsubfield} = $data->{'tagsubfield'};
144 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
145 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
146 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
147 $row_data{kohafield} = {
148 id => "kohafield$i",
149 values => \@kohafields,
150 default => "$data->{'kohafield'}",
152 $row_data{authorised_value} = {
153 id => "authorised_value$i",
154 values => $authorised_values,
155 default => $data->{'authorised_value'},
157 $row_data{frameworkcode} = {
158 id => "frameworkcode$i",
159 values => \@authtypes,
160 default => $data->{'frameworkcode'},
162 $row_data{value_builder} = {
163 id => "value_builder$i",
164 values => \@value_builder,
165 default => $data->{'value_builder'},
168 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
169 -checked => $data->{'repeatable'}?'checked':'',
170 -value => 1,
171 -label => '',
172 -id => "repeatable$i");
173 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
174 -checked => $data->{'mandatory'}?'checked':'',
175 -value => 1,
176 -label => '',
177 -id => "mandatory$i");
178 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
179 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
180 -id => "isurl$i",
181 -checked => $data->{'isurl'}?'checked':'',
182 -value => 1,
183 -label => '');
184 $row_data{row} = $i;
185 push(@loop_data, \%row_data);
186 $i++;
188 # add more_subfields empty lines for add if needed
189 for (my $i=1;$i<=$more_subfields;$i++) {
190 my %row_data; # get a fresh hash for the row data
191 $row_data{'new_subfield'} = 1;
192 $row_data{tab} = {
193 id => "tab$i",
194 default => $data->{'tab'},
196 $row_data{ohidden} = {
197 id => "ohidden$i",
198 default => $data->{'hidden'}
201 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
202 $row_data{tagsubfieldinput} =
203 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
204 . $data->{'tagsubfield'}
205 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
206 $row_data{tagsubfield} = $data->{'tagsubfield'};
207 $row_data{liblibrarian} = "";
208 $row_data{libopac} = "";
209 $row_data{seealso} = "";
210 $row_data{hidden} = "000";
211 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
212 -id => "repeatable$i",
213 -checked => '',
214 -value => 1,
215 -label => '');
216 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
217 -id => "mandatory$i",
218 -checked => '',
219 -value => 1,
220 -label => '');
221 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
222 -id => "isurl$i",
223 -checked => '',
224 -value => 1,
225 -label => '');
226 $row_data{kohafield} = {
227 id => "kohafield$i",
228 values => \@kohafields,
229 default => "",
231 $row_data{authorised_value} = {
232 id => "authorised_value",
233 values => $authorised_values,
234 default => "",
236 $row_data{frameworkcode} = {
237 id => "frameworkcode",
238 values => \@authtypes,
239 default => $data->{'frameworkcode'},
241 $row_data{value_builder} = {
242 id => "value_builder",
243 values => \@value_builder,
244 default => $data->{'value_builder'},
247 $row_data{row} = $i;
248 push(@loop_data, \%row_data);
250 $template->param('use_heading_flags_p' => 1);
251 $template->param('heading_edit_subfields_p' => 1);
252 $template->param(action => "Edit subfields",
253 tagfield => $tagfield,
254 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
255 loop => \@loop_data,
256 more_subfields => $more_subfields,
257 more_tag => $tagfield);
259 # END $OP eq ADD_FORM
260 ################## ADD_VALIDATE ##################################
261 # called by add_form, used to insert/modify data in DB
262 } elsif ($op eq 'add_validate') {
263 $template->param(tagfield => "$input->param('tagfield')");
264 # my $sth=$dbh->prepare("replace auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl)
265 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
266 my $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl,defaultvalue)
267 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
268 my $sth_update = $dbh->prepare("update auth_subfield_structure set authtypecode=?, tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, frameworkcode=?, value_builder=?, hidden=?, isurl=?, defaultvalue=?
269 where authtypecode=? and tagfield=? and tagsubfield=?");
270 my @tagsubfield = $input->param('tagsubfield');
271 my @liblibrarian = $input->param('liblibrarian');
272 my @libopac = $input->param('libopac');
273 my @kohafield = ''.$input->param('kohafield');
274 my @tab = $input->param('tab');
275 my @seealso = $input->param('seealso');
276 my @ohidden = $input->param('ohidden');
277 #my @ihidden = $input->param('ihidden');
278 #my @ehidden = $input->param('ehidden');
279 my @authorised_values = $input->param('authorised_value');
280 my $authtypecode = $input->param('authtypecode');
281 my @frameworkcodes = $input->param('frameworkcode');
282 my @value_builder =$input->param('value_builder');
283 my @defaultvalue = $input->param('defaultvalue');
284 for (my $i=0; $i<= $#tagsubfield ; $i++) {
285 my $tagfield =$input->param('tagfield');
286 my $tagsubfield =$tagsubfield[$i];
287 $tagsubfield="@" unless $tagsubfield ne '';
288 my $liblibrarian =$liblibrarian[$i];
289 my $libopac =$libopac[$i];
290 my $repeatable =$input->param("repeatable$i")?1:0;
291 my $mandatory =$input->param("mandatory$i")?1:0;
292 my $kohafield =$kohafield[$i];
293 my $tab =$tab[$i];
294 my $seealso =$seealso[$i];
295 my $authorised_value =$authorised_values[$i];
296 my $frameworkcode =$frameworkcodes[$i];
297 my $value_builder=$value_builder[$i];
298 my $defaultvalue = $defaultvalue[$i];
299 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
300 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
301 my $isurl = $input->param("isurl$i")?1:0;
302 if ($liblibrarian) {
303 unless (C4::Context->config('demo') eq 1) {
304 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
305 $sth_update->execute(
306 $authtypecode,
307 $tagfield,
308 $tagsubfield,
309 $liblibrarian,
310 $libopac,
311 $repeatable,
312 $mandatory,
313 $kohafield,
314 $tab,
315 $seealso,
316 $authorised_value,
317 $frameworkcode,
318 $value_builder,
319 $hidden,
320 $isurl,
321 $defaultvalue,
323 $authtypecode,
324 $tagfield,
325 $tagsubfield
328 } else {
329 $sth_insert->execute(
330 $authtypecode,
331 $tagfield,
332 $tagsubfield,
333 $liblibrarian,
334 $libopac,
335 $repeatable,
336 $mandatory,
337 $kohafield,
338 $tab,
339 $seealso,
340 $authorised_value,
341 $frameworkcode,
342 $value_builder,
343 $hidden,
344 $isurl,
345 $defaultvalue,
351 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
352 exit;
354 # END $OP eq ADD_VALIDATE
355 ################## DELETE_CONFIRM ##################################
356 # called by default form, used to confirm deletion of data in DB
357 } elsif ($op eq 'delete_confirm') {
358 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
359 $sth->execute($tagfield,$tagsubfield,$authtypecode);
360 my $data=$sth->fetchrow_hashref;
361 $template->param(liblibrarian => $data->{'liblibrarian'},
362 tagsubfield => $data->{'tagsubfield'},
363 delete_link => $script_name,
364 tagfield =>$tagfield,
365 tagsubfield => $tagsubfield,
366 authtypecode => $authtypecode,
368 # END $OP eq DELETE_CONFIRM
369 ################## DELETE_CONFIRMED ##################################
370 # called by delete_confirm, used to effectively confirm deletion of data in DB
371 } elsif ($op eq 'delete_confirmed') {
372 unless (C4::Context->config('demo') eq 1) {
373 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
374 $sth->execute($tagfield,$tagsubfield,$authtypecode);
376 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
377 exit;
378 # END $OP eq DELETE_CONFIRMED
379 ################## DEFAULT ##################################
380 } else { # DEFAULT
381 my ($count,$results)=string_search($tagfield,$authtypecode);
382 my @loop_data = ();
383 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
384 my %row_data; # get a fresh hash for the row data
385 $row_data{tagfield} = $results->[$i]{'tagfield'};
386 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
387 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
388 $row_data{kohafield} = $results->[$i]{'kohafield'};
389 $row_data{repeatable} = $results->[$i]{'repeatable'};
390 $row_data{mandatory} = $results->[$i]{'mandatory'};
391 $row_data{tab} = $results->[$i]{'tab'};
392 $row_data{seealso} = $results->[$i]{'seealso'};
393 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
394 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
395 $row_data{value_builder} = $results->[$i]{'value_builder'};
396 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
397 $row_data{isurl} = $results->[$i]{'isurl'};
398 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
399 if ($row_data{tab} eq -1) {
400 $row_data{subfield_ignored} = 1;
403 push(@loop_data, \%row_data);
405 $template->param(loop => \@loop_data);
406 $template->param(edit_tagfield => $tagfield,
407 edit_authtypecode => $authtypecode);
409 if ($offset>0) {
410 my $prevpage = $offset-$pagesize;
411 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
413 if ($offset+$pagesize<$count) {
414 my $nextpage =$offset+$pagesize;
415 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
417 } #---- END $OP eq DEFAULT
418 output_html_with_http_headers $input, $cookie, $template->output;