installer (part 1): started major changes
[koha.git] / admin / auth_subfields_structure.pl
blobe59d6b7564af1d9a61bae7f89e982887834ce2e7
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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
21 use strict;
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
28 sub StringSearch {
29 my ($searchstring,$authtypecode)=@_;
30 my $dbh = C4::Context->dbh;
31 $searchstring=~ s/\'/\\\'/g;
32 my @data=split(' ',$searchstring);
33 my $count=@data;
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;
37 my $cnt=0;
38 my $u=1;
39 while (my $data=$sth->fetchrow_hashref){
40 push(@results,$data);
41 $cnt ++;
42 $u++;
44 $sth->finish;
45 $dbh->disconnect;
46 return ($cnt,\@results);
49 my $input = new CGI;
50 my $tagfield=$input->param('tagfield');
51 my $tagsubfield=$input->param('tagsubfield');
52 my $authtypecode=$input->param('authtypecode');
53 my $pkfield="tagfield";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {parameters => 1},
63 debug => 1,
64 });
65 my $pagesize=30;
66 my $op = $input->param('op');
67 $tagfield=~ s/\,//g;
69 if ($op) {
70 $template->param(script_name => $script_name,
71 tagfield =>$tagfield,
72 authtypecode => $authtypecode,
73 $op => 1); # we show only the TMPL_VAR names $op
74 } else {
75 $template->param(script_name => $script_name,
76 tagfield =>$tagfield,
77 authtypecode => $authtypecode,
78 else => 1); # we show only the TMPL_VAR names $op
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 $dbh = C4::Context->dbh;
86 my $more_subfields = $input->param("more_subfields")+1;
87 # builds kohafield tables
88 my @kohafields;
89 push @kohafields, "";
90 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
91 $sth2->execute;
92 while ((my $field) = $sth2->fetchrow_array) {
93 push @kohafields, "auth_header.".$field;
96 # build authorised value list
97 $sth2->finish;
98 $sth2 = $dbh->prepare("select distinct category from authorised_values");
99 $sth2->execute;
100 my @authorised_values;
101 push @authorised_values,"";
102 while ((my $category) = $sth2->fetchrow_array) {
103 push @authorised_values, $category;
105 push (@authorised_values,"branches");
106 push (@authorised_values,"itemtypes");
108 # build thesaurus categories list
109 $sth2->finish;
110 $sth2 = $dbh->prepare("select authtypecode from auth_types");
111 $sth2->execute;
112 my @authtypes;
113 push @authtypes, "";
114 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
115 push @authtypes, $authtypecode;
118 # build value_builder list
119 my @value_builder=('');
121 # read value_builder directory.
122 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
123 # on a standard install, /cgi-bin need to be added.
124 # test one, then the other
125 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
126 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
127 $cgidir = C4::Context->intranetdir;
128 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
130 while (my $line = readdir(DIR)) {
131 if ($line =~ /\.pl$/) {
132 push (@value_builder,$line);
135 @value_builder= sort {$a cmp $b} @value_builder;
136 closedir DIR;
138 # build values list
139 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
140 $sth->execute($tagfield,$authtypecode);
141 my @loop_data = ();
142 my $toggle=1;
143 my $i=0;
144 while ($data =$sth->fetchrow_hashref) {
146 my %row_data; # get a fresh hash for the row data
147 if ($toggle eq 1){
148 $toggle=0;
149 } else {
150 $toggle=1;
152 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
153 -id=>"tab$i",
154 -values =>
155 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
156 -labels => {
157 '-1' => 'ignore',
158 '0' => '0',
159 '1' => '1',
160 '2' => '2',
161 '3' => '3',
162 '4' => '4',
163 '5' => '5',
164 '6' => '6',
165 '7' => '7',
166 '8' => '8',
167 '9' => '9',
169 -default=>$data->{'tab'},
170 -size=>1,
171 -tabindex=>'',
172 -multiple=>0,
174 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
175 -id=>"ohidden$i",
176 -values=>['0','1','2'],
177 -labels => {'0'=>'Show','1'=>'Show Collapsed',
178 '2' =>'Hide',
180 -default=>substr($data->{'hidden'},0,1),
181 -size=>1,
182 -multiple=>0,
184 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
185 -id=>"ihidden$i",
186 -values=>['0','1','2'],
187 -labels => {'0'=>'Show','1'=>'Show Collapsed',
188 '2' =>'Hide',
190 -default=>substr($data->{'hidden'},1,1),
191 -size=>1,
192 -multiple=>0,
194 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
195 -id=>"ehidden$i",
196 -values=>['0','1','2'],
197 -labels => {'0'=>'Show','1'=>'Show Collapsed',
198 '2' =>'Hide',
200 -default=>substr($data->{'hidden'}." ",2,1),
201 -size=>1,
202 -multiple=>0,
204 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
205 $row_data{tagsubfield} = $data->{'tagsubfield'};
206 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
207 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
208 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
209 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
210 -id=>"kohafield$i",
211 -values=> \@kohafields,
212 -default=> "$data->{'kohafield'}",
213 -size=>1,
214 -multiple=>0,
216 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
217 -id=>'authorised_value',
218 -values=> \@authorised_values,
219 -default=>$data->{'authorised_value'},
220 -size=>1,
221 -tabindex=>'',
222 -multiple=>0,
224 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
225 -id=>'frameworkcode',
226 -values=> \@authtypes,
227 -default=>$data->{'frameworkcode'},
228 -size=>1,
229 -tabindex=>'',
230 -multiple=>0,
232 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
233 -id=>'value_builder',
234 -values=> \@value_builder,
235 -default=>$data->{'value_builder'},
236 -size=>1,
237 -tabindex=>'',
238 -multiple=>0,
241 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
242 -checked => $data->{'repeatable'}?'checked':'',
243 -value => 1,
244 -label => '',
245 -id => "repeatable$i");
246 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
247 -checked => $data->{'mandatory'}?'checked':'',
248 -value => 1,
249 -label => '',
250 -id => "mandatory$i");
251 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
252 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
253 -id => "isurl$i",
254 -checked => $data->{'isurl'}?'checked':'',
255 -value => 1,
256 -label => '');
257 $row_data{row} = $i;
258 $row_data{toggle} = $toggle;
259 push(@loop_data, \%row_data);
260 $i++;
262 # add more_subfields empty lines for add if needed
263 for (my $i=1;$i<=$more_subfields;$i++) {
264 my %row_data; # get a fresh hash for the row data
265 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
266 -id => "tab$i",
267 -values =>
268 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
269 -labels => {
270 '-1' => 'ignore',
271 '0' => '0',
272 '1' => '1',
273 '2' => '2',
274 '3' => '3',
275 '4' => '4',
276 '5' => '5',
277 '6' => '6',
278 '7' => '7',
279 '8' => '8',
280 '9' => '9',
282 -default=>"",
283 -size=>1,
284 -tabindex=>'',
285 -multiple=>0,
287 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
288 -id=>"ohidden$i",
289 -values=>['0','1','2'],
290 -labels => {'0'=>'Show','1'=>'Show Collapsed',
291 '2' =>'Hide',
293 -default=>"0",
294 -size=>1,
295 -multiple=>0,
298 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
299 -id=>"ihidden$i",
300 -values=>['0','1','2'],
301 -labels => {'0'=>'Show','1'=>'Show Collapsed',
302 '2' =>'Hide',
304 -default=>"0",
305 -size=>1,
306 -multiple=>0,
308 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
309 -id=>"ehidden$i",
310 -values=>['0','1','2'],
311 -labels => {'0'=>'Show','1'=>'Show Collapsed',
312 '2' =>'Hide',
314 -default=>"0",
315 -size=>1,
316 -multiple=>0,
318 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
319 $row_data{tagsubfield} = $data->{'tagsubfield'};
320 $row_data{liblibrarian} = "";
321 $row_data{libopac} = "";
322 $row_data{seealso} = "";
323 $row_data{hidden} = "000";
324 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
325 -id => "repeatable$i",
326 -checked => '',
327 -value => 1,
328 -label => '');
329 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
330 -id => "mandatory$i",
331 -checked => '',
332 -value => 1,
333 -label => '');
334 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
335 -id => "isurl$i",
336 -checked => '',
337 -value => 1,
338 -label => '');
339 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
340 -id => "kohafield$i",
341 -values=> \@kohafields,
342 -default=> "",
343 -size=>1,
344 -multiple=>0,
346 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
347 -id=>'frameworkcode',
348 -values=> \@authtypes,
349 -default=>$data->{'frameworkcode'},
350 -size=>1,
351 -tabindex=>'',
352 -multiple=>0,
354 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
355 -id => 'authorised_value',
356 -values=> \@authorised_values,
357 -size=>1,
358 -tabindex=>'',
359 -multiple=>0,
361 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
362 -id=>'value_builder',
363 -values=> \@value_builder,
364 -default=>$data->{'value_builder'},
365 -size=>1,
366 -tabindex=>'',
367 -multiple=>0,
369 $row_data{toggle} = $toggle;
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 my $dbh = C4::Context->dbh;
387 $template->param(tagfield => "$input->param('tagfield')");
388 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)
389 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
390 my @tagsubfield = $input->param('tagsubfield');
391 my @liblibrarian = $input->param('liblibrarian');
392 my @libopac = $input->param('libopac');
393 my @kohafield = ''.$input->param('kohafield');
394 my @tab = $input->param('tab');
395 my @seealso = $input->param('seealso');
396 my @hidden;
397 my @ohidden = $input->param('ohidden');
398 my @ihidden = $input->param('ihidden');
399 my @ehidden = $input->param('ehidden');
400 my @authorised_values = $input->param('authorised_value');
401 my $authtypecode = $input->param('authtypecode');
402 my @frameworkcodes = $input->param('frameworkcode');
403 my @value_builder =$input->param('value_builder');
404 for (my $i=0; $i<= $#tagsubfield ; $i++) {
405 my $tagfield =$input->param('tagfield');
406 my $tagsubfield =$tagsubfield[$i];
407 $tagsubfield="@" unless $tagsubfield ne '';
408 my $liblibrarian =$liblibrarian[$i];
409 my $libopac =$libopac[$i];
410 my $repeatable =$input->param("repeatable$i")?1:0;
411 my $mandatory =$input->param("mandatory$i")?1:0;
412 my $kohafield =$kohafield[$i];
413 my $tab =$tab[$i];
414 my $seealso =$seealso[$i];
415 my $authorised_value =$authorised_values[$i];
416 my $frameworkcode =$frameworkcodes[$i];
417 my $value_builder=$value_builder[$i];
418 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
419 my $isurl = $input->param("isurl$i")?1:0;
420 if ($liblibrarian) {
421 unless (C4::Context->config('demo') eq 1) {
422 $sth->execute($authtypecode,
423 $tagfield,
424 $tagsubfield,
425 $liblibrarian,
426 $libopac,
427 $repeatable,
428 $mandatory,
429 $kohafield,
430 $tab,
431 $seealso,
432 $authorised_value,
433 $frameworkcode,
434 $value_builder,
435 $hidden,
436 $isurl,
441 $sth->finish;
442 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
443 exit;
445 # END $OP eq ADD_VALIDATE
446 ################## DELETE_CONFIRM ##################################
447 # called by default form, used to confirm deletion of data in DB
448 } elsif ($op eq 'delete_confirm') {
449 my $dbh = C4::Context->dbh;
450 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
451 #FIXME : called with 2 bind variables when 3 are needed
452 $sth->execute($tagfield,$tagsubfield);
453 my $data=$sth->fetchrow_hashref;
454 $sth->finish;
455 $template->param(liblibrarian => $data->{'liblibrarian'},
456 tagsubfield => $data->{'tagsubfield'},
457 delete_link => $script_name,
458 tagfield =>$tagfield,
459 tagsubfield => $tagsubfield,
460 authtypecode => $authtypecode,
462 # END $OP eq DELETE_CONFIRM
463 ################## DELETE_CONFIRMED ##################################
464 # called by delete_confirm, used to effectively confirm deletion of data in DB
465 } elsif ($op eq 'delete_confirmed') {
466 my $dbh = C4::Context->dbh;
467 unless (C4::Context->config('demo') eq 1) {
468 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
469 $sth->execute($tagfield,$tagsubfield,$authtypecode);
470 $sth->finish;
472 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
473 exit;
474 $template->param(tagfield => $tagfield);
475 # END $OP eq DELETE_CONFIRMED
476 ################## DEFAULT ##################################
477 } else { # DEFAULT
478 my ($count,$results)=StringSearch($tagfield,$authtypecode);
479 my $toggle=1;
480 my @loop_data = ();
481 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
482 if ($toggle eq 1){
483 $toggle=0;
484 } else {
485 $toggle=1;
487 my %row_data; # get a fresh hash for the row data
488 $row_data{tagfield} = $results->[$i]{'tagfield'};
489 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
490 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
491 $row_data{kohafield} = $results->[$i]{'kohafield'};
492 $row_data{repeatable} = $results->[$i]{'repeatable'};
493 $row_data{mandatory} = $results->[$i]{'mandatory'};
494 $row_data{tab} = $results->[$i]{'tab'};
495 $row_data{seealso} = $results->[$i]{'seealso'};
496 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
497 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
498 $row_data{value_builder} = $results->[$i]{'value_builder'};
499 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
500 $row_data{isurl} = $results->[$i]{'isurl'};
501 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
502 $row_data{toggle} = $toggle;
503 if ($row_data{tab} eq -1) {
504 $row_data{subfield_ignored} = 1;
507 push(@loop_data, \%row_data);
509 $template->param(loop => \@loop_data);
510 $template->param(edit_tagfield => $tagfield,
511 edit_authtypecode => $authtypecode);
513 if ($offset>0) {
514 my $prevpage = $offset-$pagesize;
515 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
517 if ($offset+$pagesize<$count) {
518 my $nextpage =$offset+$pagesize;
519 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
521 } #---- END $OP eq DEFAULT
522 output_html_with_http_headers $input, $cookie, $template->output;