Merge remote-tracking branch 'kc/new/bug_6205' into kcmaster
[koha.git] / admin / auth_subfields_structure.pl
blob432d49667fa4bec4fb046fd5adea3d686e554b9f
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 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.
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.
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.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
28 sub string_search {
29 my ($searchstring,$authtypecode)=@_;
30 my $dbh = C4::Context->dbh;
31 $searchstring=~ s/\'/\\\'/g;
32 my @data=split(' ',$searchstring);
33 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
34 $sth->execute("$searchstring%",$authtypecode);
35 my $results = $sth->fetchall_arrayref({});
36 return (scalar(@$results), $results);
39 sub auth_subfield_structure_exists {
40 my ($authtypecode, $tagfield, $tagsubfield) = @_;
41 my $dbh = C4::Context->dbh;
42 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
43 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
44 return @$rows > 0;
47 my $input = new CGI;
48 my $tagfield = $input->param('tagfield');
49 my $tagsubfield = $input->param('tagsubfield');
50 my $authtypecode = $input->param('authtypecode');
51 my $offset = $input->param('offset');
52 my $op = $input->param('op') || '';
53 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
55 my ($template, $borrowernumber, $cookie) = get_template_and_user(
56 { template_name => "admin/auth_subfields_structure.tmpl",
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => { parameters => 1 },
61 debug => 1,
64 my $pagesize = 30;
65 $tagfield =~ s/\,//g;
67 if ($op) {
68 $template->param(script_name => $script_name,
69 tagfield =>$tagfield,
70 authtypecode => $authtypecode,
71 $op => 1); # we show only the TMPL_VAR names $op
72 } else {
73 $template->param(script_name => $script_name,
74 tagfield =>$tagfield,
75 authtypecode => $authtypecode,
76 else => 1); # we show only the TMPL_VAR names $op
79 my $dbh = C4::Context->dbh;
80 ################## ADD_FORM ##################################
81 # called by default. Used to create form to add or modify a record
82 if ($op eq 'add_form') {
83 my $data;
84 my $more_subfields = $input->param("more_subfields")+1;
85 # builds kohafield tables
86 my @kohafields;
87 push @kohafields, "";
88 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
89 $sth2->execute;
90 while ((my $field) = $sth2->fetchrow_array) {
91 push @kohafields, "auth_header.".$field;
94 # build authorised value list
95 $sth2 = $dbh->prepare("select distinct category from authorised_values");
96 $sth2->execute;
97 my @authorised_values;
98 push @authorised_values,"";
99 while ((my $category) = $sth2->fetchrow_array) {
100 push @authorised_values, $category;
102 push (@authorised_values,"branches");
103 push (@authorised_values,"itemtypes");
105 # build thesaurus categories list
106 $sth2 = $dbh->prepare("select authtypecode from auth_types");
107 $sth2->execute;
108 my @authtypes;
109 push @authtypes, "";
110 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
111 push @authtypes, $authtypecode;
114 # build value_builder list
115 my @value_builder=('');
117 # read value_builder directory.
118 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
119 # on a standard install, /cgi-bin need to be added.
120 # test one, then the other
121 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
122 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
123 $cgidir = C4::Context->intranetdir;
124 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
126 while (my $line = readdir(DIR)) {
127 if ($line =~ /\.pl$/) {
128 push (@value_builder,$line);
131 @value_builder= sort {$a cmp $b} @value_builder;
132 closedir DIR;
134 # build values list
135 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
136 $sth->execute($tagfield,$authtypecode);
137 my @loop_data = ();
138 my $i=0;
139 while ($data =$sth->fetchrow_hashref) {
141 my %row_data; # get a fresh hash for the row data
142 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
143 -id=>"tab$i",
144 -values =>
145 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
146 -labels => {
147 '-1' => 'ignore',
148 '0' => '0',
149 '1' => '1',
150 '2' => '2',
151 '3' => '3',
152 '4' => '4',
153 '5' => '5',
154 '6' => '6',
155 '7' => '7',
156 '8' => '8',
157 '9' => '9',
159 -default=>$data->{'tab'},
160 -size=>1,
161 -tabindex=>'',
162 -multiple=>0,
164 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
165 -id=>"ohidden$i",
166 #-values=>['0','1','2'],
167 #-labels => {'0'=>'Show','1'=>'Show Collapsed',
168 # '2' =>'Hide',
169 # },
170 -values=>['0','-5'],
171 -labels => {'0'=>'Show All','-5'=>'Hide All',},
172 -default=>$data->{'hidden'},
173 #-default=>"-5",
174 -size=>1,
175 -multiple=>0,
177 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
178 # -id=>"ihidden$i",
179 # -values=>['0','1','2'],
180 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
181 # '2' =>'Hide',
182 # },
183 # -default=>substr($data->{'hidden'},1,1),
184 # -size=>1,
185 # -multiple=>0,
186 # );
187 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
188 # -id=>"ehidden$i",
189 # -values=>['0','1','2'],
190 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
191 # '2' =>'Hide',
192 # },
193 # -default=>substr($data->{'hidden'}." ",2,1),
194 # -size=>1,
195 # -multiple=>0,
196 # );
197 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
198 $row_data{tagsubfield} = $data->{'tagsubfield'};
199 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
200 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
201 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
202 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
203 -id=>"kohafield$i",
204 -values=> \@kohafields,
205 -default=> "$data->{'kohafield'}",
206 -size=>1,
207 -multiple=>0,
209 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
210 -id=>"authorised_value$i",
211 -values=> \@authorised_values,
212 -default=>$data->{'authorised_value'},
213 -size=>1,
214 -tabindex=>'',
215 -multiple=>0,
217 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
218 -id=>"frameworkcode$i",
219 -values=> \@authtypes,
220 -default=>$data->{'frameworkcode'},
221 -size=>1,
222 -tabindex=>'',
223 -multiple=>0,
225 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
226 -id=>"value_builder$i",
227 -values=> \@value_builder,
228 -default=>$data->{'value_builder'},
229 -size=>1,
230 -tabindex=>'',
231 -multiple=>0,
234 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
235 -checked => $data->{'repeatable'}?'checked':'',
236 -value => 1,
237 -label => '',
238 -id => "repeatable$i");
239 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
240 -checked => $data->{'mandatory'}?'checked':'',
241 -value => 1,
242 -label => '',
243 -id => "mandatory$i");
244 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
245 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
246 -id => "isurl$i",
247 -checked => $data->{'isurl'}?'checked':'',
248 -value => 1,
249 -label => '');
250 $row_data{row} = $i;
251 push(@loop_data, \%row_data);
252 $i++;
254 # add more_subfields empty lines for add if needed
255 for (my $i=1;$i<=$more_subfields;$i++) {
256 my %row_data; # get a fresh hash for the row data
257 $row_data{'new_subfield'} = 1;
258 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
259 -id => "tab$i",
260 -values =>
261 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
262 -labels => {
263 '-1' => 'ignore',
264 '0' => '0',
265 '1' => '1',
266 '2' => '2',
267 '3' => '3',
268 '4' => '4',
269 '5' => '5',
270 '6' => '6',
271 '7' => '7',
272 '8' => '8',
273 '9' => '9',
275 -default=>"",
276 -size=>1,
277 -tabindex=>'',
278 -multiple=>0,
280 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
281 -id=>"ohidden$i",
282 #-values=>['0','1','2'],
283 #-labels => {'0'=>'Show','1'=>'Show Collapsed',
284 # '2' =>'Hide',
285 # },
286 -values=>['0','-5'],
287 -labels => {'0'=>'Show All','-5'=>'Hide All',},
288 #-default=>"0",
289 -default=>$data->{'hidden'},
290 #-default=>"-5",
291 -size=>1,
292 -multiple=>0,
295 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
296 # -id=>"ihidden$i",
297 # -values=>['0','1','2'],
298 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
299 # '2' =>'Hide',
300 # },
301 # -default=>"0",
302 # -size=>1,
303 # -multiple=>0,
304 # );
305 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
306 # -id=>"ehidden$i",
307 # -values=>['0','1','2'],
308 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
309 # '2' =>'Hide',
310 # },
311 # -default=>"0",
312 # -size=>1,
313 # -multiple=>0,
314 # );
315 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
316 $row_data{tagsubfieldinput} =
317 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
318 . $data->{'tagsubfield'}
319 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
320 $row_data{tagsubfield} = $data->{'tagsubfield'};
321 $row_data{liblibrarian} = "";
322 $row_data{libopac} = "";
323 $row_data{seealso} = "";
324 $row_data{hidden} = "000";
325 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
326 -id => "repeatable$i",
327 -checked => '',
328 -value => 1,
329 -label => '');
330 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
331 -id => "mandatory$i",
332 -checked => '',
333 -value => 1,
334 -label => '');
335 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
336 -id => "isurl$i",
337 -checked => '',
338 -value => 1,
339 -label => '');
340 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
341 -id => "kohafield$i",
342 -values=> \@kohafields,
343 -default=> "",
344 -size=>1,
345 -multiple=>0,
347 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
348 -id=>'frameworkcode',
349 -values=> \@authtypes,
350 -default=>$data->{'frameworkcode'},
351 -size=>1,
352 -tabindex=>'',
353 -multiple=>0,
355 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
356 -id => 'authorised_value',
357 -values=> \@authorised_values,
358 -size=>1,
359 -tabindex=>'',
360 -multiple=>0,
362 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
363 -id=>'value_builder',
364 -values=> \@value_builder,
365 -default=>$data->{'value_builder'},
366 -size=>1,
367 -tabindex=>'',
368 -multiple=>0,
370 $row_data{row} = $i;
371 push(@loop_data, \%row_data);
373 $template->param('use_heading_flags_p' => 1);
374 $template->param('heading_edit_subfields_p' => 1);
375 $template->param(action => "Edit subfields",
376 tagfield => $tagfield,
377 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
378 loop => \@loop_data,
379 more_subfields => $more_subfields,
380 more_tag => $tagfield);
382 # END $OP eq ADD_FORM
383 ################## ADD_VALIDATE ##################################
384 # called by add_form, used to insert/modify data in DB
385 } elsif ($op eq 'add_validate') {
386 $template->param(tagfield => "$input->param('tagfield')");
387 # 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)
388 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
389 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)
390 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
391 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=?
392 where authtypecode=? and tagfield=? and tagsubfield=?");
393 my @tagsubfield = $input->param('tagsubfield');
394 my @liblibrarian = $input->param('liblibrarian');
395 my @libopac = $input->param('libopac');
396 my @kohafield = ''.$input->param('kohafield');
397 my @tab = $input->param('tab');
398 my @seealso = $input->param('seealso');
399 my @hidden;
400 my @ohidden = $input->param('ohidden');
401 #my @ihidden = $input->param('ihidden');
402 #my @ehidden = $input->param('ehidden');
403 my @authorised_values = $input->param('authorised_value');
404 my $authtypecode = $input->param('authtypecode');
405 my @frameworkcodes = $input->param('frameworkcode');
406 my @value_builder =$input->param('value_builder');
407 for (my $i=0; $i<= $#tagsubfield ; $i++) {
408 my $tagfield =$input->param('tagfield');
409 my $tagsubfield =$tagsubfield[$i];
410 $tagsubfield="@" unless $tagsubfield ne '';
411 my $liblibrarian =$liblibrarian[$i];
412 my $libopac =$libopac[$i];
413 my $repeatable =$input->param("repeatable$i")?1:0;
414 my $mandatory =$input->param("mandatory$i")?1:0;
415 my $kohafield =$kohafield[$i];
416 my $tab =$tab[$i];
417 my $seealso =$seealso[$i];
418 my $authorised_value =$authorised_values[$i];
419 my $frameworkcode =$frameworkcodes[$i];
420 my $value_builder=$value_builder[$i];
421 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
422 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
423 my $isurl = $input->param("isurl$i")?1:0;
424 if ($liblibrarian) {
425 unless (C4::Context->config('demo') eq 1) {
426 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
427 $sth_update->execute(
428 $authtypecode,
429 $tagfield,
430 $tagsubfield,
431 $liblibrarian,
432 $libopac,
433 $repeatable,
434 $mandatory,
435 $kohafield,
436 $tab,
437 $seealso,
438 $authorised_value,
439 $frameworkcode,
440 $value_builder,
441 $hidden,
442 $isurl,
444 $authtypecode,
445 $tagfield,
446 $tagsubfield
449 } else {
450 $sth_insert->execute(
451 $authtypecode,
452 $tagfield,
453 $tagsubfield,
454 $liblibrarian,
455 $libopac,
456 $repeatable,
457 $mandatory,
458 $kohafield,
459 $tab,
460 $seealso,
461 $authorised_value,
462 $frameworkcode,
463 $value_builder,
464 $hidden,
465 $isurl,
471 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
472 exit;
474 # END $OP eq ADD_VALIDATE
475 ################## DELETE_CONFIRM ##################################
476 # called by default form, used to confirm deletion of data in DB
477 } elsif ($op eq 'delete_confirm') {
478 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
479 $sth->execute($tagfield,$tagsubfield,$authtypecode);
480 my $data=$sth->fetchrow_hashref;
481 $template->param(liblibrarian => $data->{'liblibrarian'},
482 tagsubfield => $data->{'tagsubfield'},
483 delete_link => $script_name,
484 tagfield =>$tagfield,
485 tagsubfield => $tagsubfield,
486 authtypecode => $authtypecode,
488 # END $OP eq DELETE_CONFIRM
489 ################## DELETE_CONFIRMED ##################################
490 # called by delete_confirm, used to effectively confirm deletion of data in DB
491 } elsif ($op eq 'delete_confirmed') {
492 unless (C4::Context->config('demo') eq 1) {
493 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
494 $sth->execute($tagfield,$tagsubfield,$authtypecode);
496 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
497 exit;
498 $template->param(tagfield => $tagfield);
499 # END $OP eq DELETE_CONFIRMED
500 ################## DEFAULT ##################################
501 } else { # DEFAULT
502 my ($count,$results)=string_search($tagfield,$authtypecode);
503 my @loop_data = ();
504 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
505 my %row_data; # get a fresh hash for the row data
506 $row_data{tagfield} = $results->[$i]{'tagfield'};
507 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
508 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
509 $row_data{kohafield} = $results->[$i]{'kohafield'};
510 $row_data{repeatable} = $results->[$i]{'repeatable'};
511 $row_data{mandatory} = $results->[$i]{'mandatory'};
512 $row_data{tab} = $results->[$i]{'tab'};
513 $row_data{seealso} = $results->[$i]{'seealso'};
514 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
515 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
516 $row_data{value_builder} = $results->[$i]{'value_builder'};
517 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
518 $row_data{isurl} = $results->[$i]{'isurl'};
519 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
520 if ($row_data{tab} eq -1) {
521 $row_data{subfield_ignored} = 1;
524 push(@loop_data, \%row_data);
526 $template->param(loop => \@loop_data);
527 $template->param(edit_tagfield => $tagfield,
528 edit_authtypecode => $authtypecode);
530 if ($offset>0) {
531 my $prevpage = $offset-$pagesize;
532 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
534 if ($offset+$pagesize<$count) {
535 my $nextpage =$offset+$pagesize;
536 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
538 } #---- END $OP eq DEFAULT
539 output_html_with_http_headers $input, $cookie, $template->output;