Bug 10691: use authority ID in see-also links in authority search results (staff)
[koha.git] / admin / auth_subfields_structure.pl
blobfb0e59c7c0ff57921f6624f0996009441ba3d9c7
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 => 'parameters_remaining_permissions' },
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{defaultvalue} = $data->{defaultvalue};
143 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
144 -id=>"tab$i",
145 -values =>
146 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
147 -labels => {
148 '-1' => 'ignore',
149 '0' => '0',
150 '1' => '1',
151 '2' => '2',
152 '3' => '3',
153 '4' => '4',
154 '5' => '5',
155 '6' => '6',
156 '7' => '7',
157 '8' => '8',
158 '9' => '9',
160 -default=>$data->{'tab'},
161 -size=>1,
162 -tabindex=>'',
163 -multiple=>0,
165 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
166 -id=>"ohidden$i",
167 #-values=>['0','1','2'],
168 #-labels => {'0'=>'Show','1'=>'Show Collapsed',
169 # '2' =>'Hide',
170 # },
171 -values=>['0','-5'],
172 -labels => {'0'=>'Show All','-5'=>'Hide All',},
173 -default=>$data->{'hidden'},
174 #-default=>"-5",
175 -size=>1,
176 -multiple=>0,
178 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
179 # -id=>"ihidden$i",
180 # -values=>['0','1','2'],
181 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
182 # '2' =>'Hide',
183 # },
184 # -default=>substr($data->{'hidden'},1,1),
185 # -size=>1,
186 # -multiple=>0,
187 # );
188 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
189 # -id=>"ehidden$i",
190 # -values=>['0','1','2'],
191 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
192 # '2' =>'Hide',
193 # },
194 # -default=>substr($data->{'hidden'}." ",2,1),
195 # -size=>1,
196 # -multiple=>0,
197 # );
198 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
199 $row_data{tagsubfield} = $data->{'tagsubfield'};
200 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
201 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
202 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
203 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
204 -id=>"kohafield$i",
205 -values=> \@kohafields,
206 -default=> "$data->{'kohafield'}",
207 -size=>1,
208 -multiple=>0,
210 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
211 -id=>"authorised_value$i",
212 -values=> \@authorised_values,
213 -default=>$data->{'authorised_value'},
214 -size=>1,
215 -tabindex=>'',
216 -multiple=>0,
218 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
219 -id=>"frameworkcode$i",
220 -values=> \@authtypes,
221 -default=>$data->{'frameworkcode'},
222 -size=>1,
223 -tabindex=>'',
224 -multiple=>0,
226 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
227 -id=>"value_builder$i",
228 -values=> \@value_builder,
229 -default=>$data->{'value_builder'},
230 -size=>1,
231 -tabindex=>'',
232 -multiple=>0,
235 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
236 -checked => $data->{'repeatable'}?'checked':'',
237 -value => 1,
238 -label => '',
239 -id => "repeatable$i");
240 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
241 -checked => $data->{'mandatory'}?'checked':'',
242 -value => 1,
243 -label => '',
244 -id => "mandatory$i");
245 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
246 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
247 -id => "isurl$i",
248 -checked => $data->{'isurl'}?'checked':'',
249 -value => 1,
250 -label => '');
251 $row_data{row} = $i;
252 push(@loop_data, \%row_data);
253 $i++;
255 # add more_subfields empty lines for add if needed
256 for (my $i=1;$i<=$more_subfields;$i++) {
257 my %row_data; # get a fresh hash for the row data
258 $row_data{'new_subfield'} = 1;
259 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
260 -id => "tab$i",
261 -values =>
262 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
263 -labels => {
264 '-1' => 'ignore',
265 '0' => '0',
266 '1' => '1',
267 '2' => '2',
268 '3' => '3',
269 '4' => '4',
270 '5' => '5',
271 '6' => '6',
272 '7' => '7',
273 '8' => '8',
274 '9' => '9',
276 -default=>"",
277 -size=>1,
278 -tabindex=>'',
279 -multiple=>0,
281 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
282 -id=>"ohidden$i",
283 #-values=>['0','1','2'],
284 #-labels => {'0'=>'Show','1'=>'Show Collapsed',
285 # '2' =>'Hide',
286 # },
287 -values=>['0','-5'],
288 -labels => {'0'=>'Show All','-5'=>'Hide All',},
289 #-default=>"0",
290 -default=>$data->{'hidden'},
291 #-default=>"-5",
292 -size=>1,
293 -multiple=>0,
296 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
297 # -id=>"ihidden$i",
298 # -values=>['0','1','2'],
299 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
300 # '2' =>'Hide',
301 # },
302 # -default=>"0",
303 # -size=>1,
304 # -multiple=>0,
305 # );
306 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
307 # -id=>"ehidden$i",
308 # -values=>['0','1','2'],
309 # -labels => {'0'=>'Show','1'=>'Show Collapsed',
310 # '2' =>'Hide',
311 # },
312 # -default=>"0",
313 # -size=>1,
314 # -multiple=>0,
315 # );
316 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
317 $row_data{tagsubfieldinput} =
318 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
319 . $data->{'tagsubfield'}
320 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
321 $row_data{tagsubfield} = $data->{'tagsubfield'};
322 $row_data{liblibrarian} = "";
323 $row_data{libopac} = "";
324 $row_data{seealso} = "";
325 $row_data{hidden} = "000";
326 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
327 -id => "repeatable$i",
328 -checked => '',
329 -value => 1,
330 -label => '');
331 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
332 -id => "mandatory$i",
333 -checked => '',
334 -value => 1,
335 -label => '');
336 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
337 -id => "isurl$i",
338 -checked => '',
339 -value => 1,
340 -label => '');
341 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
342 -id => "kohafield$i",
343 -values=> \@kohafields,
344 -default=> "",
345 -size=>1,
346 -multiple=>0,
348 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
349 -id=>'frameworkcode',
350 -values=> \@authtypes,
351 -default=>$data->{'frameworkcode'},
352 -size=>1,
353 -tabindex=>'',
354 -multiple=>0,
356 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
357 -id => 'authorised_value',
358 -values=> \@authorised_values,
359 -size=>1,
360 -tabindex=>'',
361 -multiple=>0,
363 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
364 -id=>'value_builder',
365 -values=> \@value_builder,
366 -default=>$data->{'value_builder'},
367 -size=>1,
368 -tabindex=>'',
369 -multiple=>0,
371 $row_data{row} = $i;
372 push(@loop_data, \%row_data);
374 $template->param('use_heading_flags_p' => 1);
375 $template->param('heading_edit_subfields_p' => 1);
376 $template->param(action => "Edit subfields",
377 tagfield => $tagfield,
378 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
379 loop => \@loop_data,
380 more_subfields => $more_subfields,
381 more_tag => $tagfield);
383 # END $OP eq ADD_FORM
384 ################## ADD_VALIDATE ##################################
385 # called by add_form, used to insert/modify data in DB
386 } elsif ($op eq 'add_validate') {
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 $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)
391 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
392 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=?
393 where authtypecode=? and tagfield=? and tagsubfield=?");
394 my @tagsubfield = $input->param('tagsubfield');
395 my @liblibrarian = $input->param('liblibrarian');
396 my @libopac = $input->param('libopac');
397 my @kohafield = ''.$input->param('kohafield');
398 my @tab = $input->param('tab');
399 my @seealso = $input->param('seealso');
400 my @hidden;
401 my @ohidden = $input->param('ohidden');
402 #my @ihidden = $input->param('ihidden');
403 #my @ehidden = $input->param('ehidden');
404 my @authorised_values = $input->param('authorised_value');
405 my $authtypecode = $input->param('authtypecode');
406 my @frameworkcodes = $input->param('frameworkcode');
407 my @value_builder =$input->param('value_builder');
408 my @defaultvalue = $input->param('defaultvalue');
409 for (my $i=0; $i<= $#tagsubfield ; $i++) {
410 my $tagfield =$input->param('tagfield');
411 my $tagsubfield =$tagsubfield[$i];
412 $tagsubfield="@" unless $tagsubfield ne '';
413 my $liblibrarian =$liblibrarian[$i];
414 my $libopac =$libopac[$i];
415 my $repeatable =$input->param("repeatable$i")?1:0;
416 my $mandatory =$input->param("mandatory$i")?1:0;
417 my $kohafield =$kohafield[$i];
418 my $tab =$tab[$i];
419 my $seealso =$seealso[$i];
420 my $authorised_value =$authorised_values[$i];
421 my $frameworkcode =$frameworkcodes[$i];
422 my $value_builder=$value_builder[$i];
423 my $defaultvalue = $defaultvalue[$i];
424 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
425 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
426 my $isurl = $input->param("isurl$i")?1:0;
427 if ($liblibrarian) {
428 unless (C4::Context->config('demo') eq 1) {
429 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
430 $sth_update->execute(
431 $authtypecode,
432 $tagfield,
433 $tagsubfield,
434 $liblibrarian,
435 $libopac,
436 $repeatable,
437 $mandatory,
438 $kohafield,
439 $tab,
440 $seealso,
441 $authorised_value,
442 $frameworkcode,
443 $value_builder,
444 $hidden,
445 $isurl,
446 $defaultvalue,
448 $authtypecode,
449 $tagfield,
450 $tagsubfield
453 } else {
454 $sth_insert->execute(
455 $authtypecode,
456 $tagfield,
457 $tagsubfield,
458 $liblibrarian,
459 $libopac,
460 $repeatable,
461 $mandatory,
462 $kohafield,
463 $tab,
464 $seealso,
465 $authorised_value,
466 $frameworkcode,
467 $value_builder,
468 $hidden,
469 $isurl,
470 $defaultvalue,
476 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
477 exit;
479 # END $OP eq ADD_VALIDATE
480 ################## DELETE_CONFIRM ##################################
481 # called by default form, used to confirm deletion of data in DB
482 } elsif ($op eq 'delete_confirm') {
483 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
484 $sth->execute($tagfield,$tagsubfield,$authtypecode);
485 my $data=$sth->fetchrow_hashref;
486 $template->param(liblibrarian => $data->{'liblibrarian'},
487 tagsubfield => $data->{'tagsubfield'},
488 delete_link => $script_name,
489 tagfield =>$tagfield,
490 tagsubfield => $tagsubfield,
491 authtypecode => $authtypecode,
493 # END $OP eq DELETE_CONFIRM
494 ################## DELETE_CONFIRMED ##################################
495 # called by delete_confirm, used to effectively confirm deletion of data in DB
496 } elsif ($op eq 'delete_confirmed') {
497 unless (C4::Context->config('demo') eq 1) {
498 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
499 $sth->execute($tagfield,$tagsubfield,$authtypecode);
501 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
502 exit;
503 $template->param(tagfield => $tagfield);
504 # END $OP eq DELETE_CONFIRMED
505 ################## DEFAULT ##################################
506 } else { # DEFAULT
507 my ($count,$results)=string_search($tagfield,$authtypecode);
508 my @loop_data = ();
509 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
510 my %row_data; # get a fresh hash for the row data
511 $row_data{tagfield} = $results->[$i]{'tagfield'};
512 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
513 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
514 $row_data{kohafield} = $results->[$i]{'kohafield'};
515 $row_data{repeatable} = $results->[$i]{'repeatable'};
516 $row_data{mandatory} = $results->[$i]{'mandatory'};
517 $row_data{tab} = $results->[$i]{'tab'};
518 $row_data{seealso} = $results->[$i]{'seealso'};
519 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
520 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
521 $row_data{value_builder} = $results->[$i]{'value_builder'};
522 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
523 $row_data{isurl} = $results->[$i]{'isurl'};
524 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
525 if ($row_data{tab} eq -1) {
526 $row_data{subfield_ignored} = 1;
529 push(@loop_data, \%row_data);
531 $template->param(loop => \@loop_data);
532 $template->param(edit_tagfield => $tagfield,
533 edit_authtypecode => $authtypecode);
535 if ($offset>0) {
536 my $prevpage = $offset-$pagesize;
537 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
539 if ($offset+$pagesize<$count) {
540 my $nextpage =$offset+$pagesize;
541 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
543 } #---- END $OP eq DEFAULT
544 output_html_with_http_headers $input, $cookie, $template->output;