bug 1891 followup - remove dangling /TMPL_IF
[koha.git] / admin / auth_subfields_structure.pl
bloba1c636eb0feed6e18a562ee2b0f1ce8c34f190ef
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use C4::Output;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
27 sub StringSearch {
28 my ($searchstring,$authtypecode)=@_;
29 my $dbh = C4::Context->dbh;
30 $searchstring=~ s/\'/\\\'/g;
31 my @data=split(' ',$searchstring);
32 my $count=@data;
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;
36 my $cnt=0;
37 my $u=1;
38 while (my $data=$sth->fetchrow_hashref){
39 push(@results,$data);
40 $cnt ++;
41 $u++;
43 $sth->finish;
44 return ($cnt,\@results);
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 $pkfield="tagfield";
52 my $offset=$input->param('offset');
53 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {parameters => 1},
61 debug => 1,
62 });
63 my $pagesize=30;
64 my $op = $input->param('op');
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 ################## ADD_FORM ##################################
80 # called by default. Used to create form to add or modify a record
81 if ($op eq 'add_form') {
82 my $data;
83 my $dbh = C4::Context->dbh;
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->finish;
96 $sth2 = $dbh->prepare("select distinct category from authorised_values");
97 $sth2->execute;
98 my @authorised_values;
99 push @authorised_values,"";
100 while ((my $category) = $sth2->fetchrow_array) {
101 push @authorised_values, $category;
103 push (@authorised_values,"branches");
104 push (@authorised_values,"itemtypes");
106 # build thesaurus categories list
107 $sth2->finish;
108 $sth2 = $dbh->prepare("select authtypecode from auth_types");
109 $sth2->execute;
110 my @authtypes;
111 push @authtypes, "";
112 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
113 push @authtypes, $authtypecode;
116 # build value_builder list
117 my @value_builder=('');
119 # read value_builder directory.
120 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
121 # on a standard install, /cgi-bin need to be added.
122 # test one, then the other
123 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
124 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
125 $cgidir = C4::Context->intranetdir;
126 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
128 while (my $line = readdir(DIR)) {
129 if ($line =~ /\.pl$/) {
130 push (@value_builder,$line);
133 @value_builder= sort {$a cmp $b} @value_builder;
134 closedir DIR;
136 # build values list
137 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
138 $sth->execute($tagfield,$authtypecode);
139 my @loop_data = ();
140 my $toggle=1;
141 my $i=0;
142 while ($data =$sth->fetchrow_hashref) {
144 my %row_data; # get a fresh hash for the row data
145 if ($toggle eq 1){
146 $toggle=0;
147 } else {
148 $toggle=1;
150 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
151 -id=>"tab$i",
152 -values =>
153 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
154 -labels => {
155 '-1' => 'ignore',
156 '0' => '0',
157 '1' => '1',
158 '2' => '2',
159 '3' => '3',
160 '4' => '4',
161 '5' => '5',
162 '6' => '6',
163 '7' => '7',
164 '8' => '8',
165 '9' => '9',
167 -default=>$data->{'tab'},
168 -size=>1,
169 -tabindex=>'',
170 -multiple=>0,
172 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
173 -id=>"ohidden$i",
174 -values=>['0','1','2'],
175 -labels => {'0'=>'Show','1'=>'Show Collapsed',
176 '2' =>'Hide',
178 -default=>substr($data->{'hidden'},0,1),
179 -size=>1,
180 -multiple=>0,
182 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
183 -id=>"ihidden$i",
184 -values=>['0','1','2'],
185 -labels => {'0'=>'Show','1'=>'Show Collapsed',
186 '2' =>'Hide',
188 -default=>substr($data->{'hidden'},1,1),
189 -size=>1,
190 -multiple=>0,
192 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
193 -id=>"ehidden$i",
194 -values=>['0','1','2'],
195 -labels => {'0'=>'Show','1'=>'Show Collapsed',
196 '2' =>'Hide',
198 -default=>substr($data->{'hidden'}." ",2,1),
199 -size=>1,
200 -multiple=>0,
202 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
203 $row_data{tagsubfield} = $data->{'tagsubfield'};
204 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
205 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
206 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
207 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
208 -id=>"kohafield$i",
209 -values=> \@kohafields,
210 -default=> "$data->{'kohafield'}",
211 -size=>1,
212 -multiple=>0,
214 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
215 -id=>"authorised_value$i",
216 -values=> \@authorised_values,
217 -default=>$data->{'authorised_value'},
218 -size=>1,
219 -tabindex=>'',
220 -multiple=>0,
222 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
223 -id=>"frameworkcode$i",
224 -values=> \@authtypes,
225 -default=>$data->{'frameworkcode'},
226 -size=>1,
227 -tabindex=>'',
228 -multiple=>0,
230 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
231 -id=>"value_builder$i",
232 -values=> \@value_builder,
233 -default=>$data->{'value_builder'},
234 -size=>1,
235 -tabindex=>'',
236 -multiple=>0,
239 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
240 -checked => $data->{'repeatable'}?'checked':'',
241 -value => 1,
242 -label => '',
243 -id => "repeatable$i");
244 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
245 -checked => $data->{'mandatory'}?'checked':'',
246 -value => 1,
247 -label => '',
248 -id => "mandatory$i");
249 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
250 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
251 -id => "isurl$i",
252 -checked => $data->{'isurl'}?'checked':'',
253 -value => 1,
254 -label => '');
255 $row_data{row} = $i;
256 $row_data{toggle} = $toggle;
257 push(@loop_data, \%row_data);
258 $i++;
260 # add more_subfields empty lines for add if needed
261 for (my $i=1;$i<=$more_subfields;$i++) {
262 my %row_data; # get a fresh hash for the row data
263 $row_data{'new_subfield'} = 1;
264 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
265 -id => "tab$i",
266 -values =>
267 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
268 -labels => {
269 '-1' => 'ignore',
270 '0' => '0',
271 '1' => '1',
272 '2' => '2',
273 '3' => '3',
274 '4' => '4',
275 '5' => '5',
276 '6' => '6',
277 '7' => '7',
278 '8' => '8',
279 '9' => '9',
281 -default=>"",
282 -size=>1,
283 -tabindex=>'',
284 -multiple=>0,
286 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
287 -id=>"ohidden$i",
288 -values=>['0','1','2'],
289 -labels => {'0'=>'Show','1'=>'Show Collapsed',
290 '2' =>'Hide',
292 -default=>"0",
293 -size=>1,
294 -multiple=>0,
297 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
298 -id=>"ihidden$i",
299 -values=>['0','1','2'],
300 -labels => {'0'=>'Show','1'=>'Show Collapsed',
301 '2' =>'Hide',
303 -default=>"0",
304 -size=>1,
305 -multiple=>0,
307 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
308 -id=>"ehidden$i",
309 -values=>['0','1','2'],
310 -labels => {'0'=>'Show','1'=>'Show Collapsed',
311 '2' =>'Hide',
313 -default=>"0",
314 -size=>1,
315 -multiple=>0,
317 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
318 $row_data{tagsubfield} = $data->{'tagsubfield'};
319 $row_data{liblibrarian} = "";
320 $row_data{libopac} = "";
321 $row_data{seealso} = "";
322 $row_data{hidden} = "000";
323 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
324 -id => "repeatable$i",
325 -checked => '',
326 -value => 1,
327 -label => '');
328 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
329 -id => "mandatory$i",
330 -checked => '',
331 -value => 1,
332 -label => '');
333 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
334 -id => "isurl$i",
335 -checked => '',
336 -value => 1,
337 -label => '');
338 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
339 -id => "kohafield$i",
340 -values=> \@kohafields,
341 -default=> "",
342 -size=>1,
343 -multiple=>0,
345 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
346 -id=>'frameworkcode',
347 -values=> \@authtypes,
348 -default=>$data->{'frameworkcode'},
349 -size=>1,
350 -tabindex=>'',
351 -multiple=>0,
353 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
354 -id => 'authorised_value',
355 -values=> \@authorised_values,
356 -size=>1,
357 -tabindex=>'',
358 -multiple=>0,
360 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
361 -id=>'value_builder',
362 -values=> \@value_builder,
363 -default=>$data->{'value_builder'},
364 -size=>1,
365 -tabindex=>'',
366 -multiple=>0,
368 $row_data{toggle} = $toggle;
369 $row_data{row} = $i;
370 push(@loop_data, \%row_data);
372 $template->param('use-heading-flags-p' => 1);
373 $template->param('heading-edit-subfields-p' => 1);
374 $template->param(action => "Edit subfields",
375 tagfield => $tagfield,
376 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
377 loop => \@loop_data,
378 more_subfields => $more_subfields,
379 more_tag => $tagfield);
381 # END $OP eq ADD_FORM
382 ################## ADD_VALIDATE ##################################
383 # called by add_form, used to insert/modify data in DB
384 } elsif ($op eq 'add_validate') {
385 my $dbh = C4::Context->dbh;
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 @tagsubfield = $input->param('tagsubfield');
390 my @liblibrarian = $input->param('liblibrarian');
391 my @libopac = $input->param('libopac');
392 my @kohafield = ''.$input->param('kohafield');
393 my @tab = $input->param('tab');
394 my @seealso = $input->param('seealso');
395 my @hidden;
396 my @ohidden = $input->param('ohidden');
397 my @ihidden = $input->param('ihidden');
398 my @ehidden = $input->param('ehidden');
399 my @authorised_values = $input->param('authorised_value');
400 my $authtypecode = $input->param('authtypecode');
401 my @frameworkcodes = $input->param('frameworkcode');
402 my @value_builder =$input->param('value_builder');
403 for (my $i=0; $i<= $#tagsubfield ; $i++) {
404 my $tagfield =$input->param('tagfield');
405 my $tagsubfield =$tagsubfield[$i];
406 $tagsubfield="@" unless $tagsubfield ne '';
407 my $liblibrarian =$liblibrarian[$i];
408 my $libopac =$libopac[$i];
409 my $repeatable =$input->param("repeatable$i")?1:0;
410 my $mandatory =$input->param("mandatory$i")?1:0;
411 my $kohafield =$kohafield[$i];
412 my $tab =$tab[$i];
413 my $seealso =$seealso[$i];
414 my $authorised_value =$authorised_values[$i];
415 my $frameworkcode =$frameworkcodes[$i];
416 my $value_builder=$value_builder[$i];
417 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
418 my $isurl = $input->param("isurl$i")?1:0;
419 if ($liblibrarian) {
420 unless (C4::Context->config('demo') eq 1) {
421 $sth->execute($authtypecode,
422 $tagfield,
423 $tagsubfield,
424 $liblibrarian,
425 $libopac,
426 $repeatable,
427 $mandatory,
428 $kohafield,
429 $tab,
430 $seealso,
431 $authorised_value,
432 $frameworkcode,
433 $value_builder,
434 $hidden,
435 $isurl,
440 $sth->finish;
441 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
442 exit;
444 # END $OP eq ADD_VALIDATE
445 ################## DELETE_CONFIRM ##################################
446 # called by default form, used to confirm deletion of data in DB
447 } elsif ($op eq 'delete_confirm') {
448 my $dbh = C4::Context->dbh;
449 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
450 $sth->execute($tagfield,$tagsubfield,$authtypecode);
451 my $data=$sth->fetchrow_hashref;
452 $sth->finish;
453 $template->param(liblibrarian => $data->{'liblibrarian'},
454 tagsubfield => $data->{'tagsubfield'},
455 delete_link => $script_name,
456 tagfield =>$tagfield,
457 tagsubfield => $tagsubfield,
458 authtypecode => $authtypecode,
460 # END $OP eq DELETE_CONFIRM
461 ################## DELETE_CONFIRMED ##################################
462 # called by delete_confirm, used to effectively confirm deletion of data in DB
463 } elsif ($op eq 'delete_confirmed') {
464 my $dbh = C4::Context->dbh;
465 unless (C4::Context->config('demo') eq 1) {
466 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
467 $sth->execute($tagfield,$tagsubfield,$authtypecode);
468 $sth->finish;
470 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
471 exit;
472 $template->param(tagfield => $tagfield);
473 # END $OP eq DELETE_CONFIRMED
474 ################## DEFAULT ##################################
475 } else { # DEFAULT
476 my ($count,$results)=StringSearch($tagfield,$authtypecode);
477 my $toggle=1;
478 my @loop_data = ();
479 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
480 if ($toggle eq 1){
481 $toggle=0;
482 } else {
483 $toggle=1;
485 my %row_data; # get a fresh hash for the row data
486 $row_data{tagfield} = $results->[$i]{'tagfield'};
487 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
488 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
489 $row_data{kohafield} = $results->[$i]{'kohafield'};
490 $row_data{repeatable} = $results->[$i]{'repeatable'};
491 $row_data{mandatory} = $results->[$i]{'mandatory'};
492 $row_data{tab} = $results->[$i]{'tab'};
493 $row_data{seealso} = $results->[$i]{'seealso'};
494 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
495 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
496 $row_data{value_builder} = $results->[$i]{'value_builder'};
497 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
498 $row_data{isurl} = $results->[$i]{'isurl'};
499 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
500 $row_data{toggle} = $toggle;
501 if ($row_data{tab} eq -1) {
502 $row_data{subfield_ignored} = 1;
505 push(@loop_data, \%row_data);
507 $template->param(loop => \@loop_data);
508 $template->param(edit_tagfield => $tagfield,
509 edit_authtypecode => $authtypecode);
511 if ($offset>0) {
512 my $prevpage = $offset-$pagesize;
513 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
515 if ($offset+$pagesize<$count) {
516 my $nextpage =$offset+$pagesize;
517 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
519 } #---- END $OP eq DEFAULT
520 output_html_with_http_headers $input, $cookie, $template->output;