bug-2149, added new block to C4::Letters::SendAlerts() to email 'account creation...
[koha.git] / admin / auth_subfields_structure.pl
blob1b2643e8b053f0342e8e9891f0871150f113d928
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 return ($cnt,\@results);
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 $pkfield="tagfield";
53 my $offset=$input->param('offset');
54 my $script_name="/cgi-bin/koha/admin/auth_subfields_structure.pl";
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => "admin/auth_subfields_structure.tmpl",
58 query => $input,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => {parameters => 1},
62 debug => 1,
63 });
64 my $pagesize=30;
65 my $op = $input->param('op');
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 ################## 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 $dbh = C4::Context->dbh;
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 $sth2->finish;
97 $sth2 = $dbh->prepare("select distinct category from authorised_values");
98 $sth2->execute;
99 my @authorised_values;
100 push @authorised_values,"";
101 while ((my $category) = $sth2->fetchrow_array) {
102 push @authorised_values, $category;
104 push (@authorised_values,"branches");
105 push (@authorised_values,"itemtypes");
107 # build thesaurus categories list
108 $sth2->finish;
109 $sth2 = $dbh->prepare("select authtypecode from auth_types");
110 $sth2->execute;
111 my @authtypes;
112 push @authtypes, "";
113 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
114 push @authtypes, $authtypecode;
117 # build value_builder list
118 my @value_builder=('');
120 # read value_builder directory.
121 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
122 # on a standard install, /cgi-bin need to be added.
123 # test one, then the other
124 my $cgidir = C4::Context->intranetdir ."/cgi-bin";
125 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
126 $cgidir = C4::Context->intranetdir;
127 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
129 while (my $line = readdir(DIR)) {
130 if ($line =~ /\.pl$/) {
131 push (@value_builder,$line);
134 @value_builder= sort {$a cmp $b} @value_builder;
135 closedir DIR;
137 # build values list
138 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
139 $sth->execute($tagfield,$authtypecode);
140 my @loop_data = ();
141 my $toggle=1;
142 my $i=0;
143 while ($data =$sth->fetchrow_hashref) {
145 my %row_data; # get a fresh hash for the row data
146 if ($toggle eq 1){
147 $toggle=0;
148 } else {
149 $toggle=1;
151 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
152 -id=>"tab$i",
153 -values =>
154 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
155 -labels => {
156 '-1' => 'ignore',
157 '0' => '0',
158 '1' => '1',
159 '2' => '2',
160 '3' => '3',
161 '4' => '4',
162 '5' => '5',
163 '6' => '6',
164 '7' => '7',
165 '8' => '8',
166 '9' => '9',
168 -default=>$data->{'tab'},
169 -size=>1,
170 -tabindex=>'',
171 -multiple=>0,
173 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
174 -id=>"ohidden$i",
175 -values=>['0','1','2'],
176 -labels => {'0'=>'Show','1'=>'Show Collapsed',
177 '2' =>'Hide',
179 -default=>substr($data->{'hidden'},0,1),
180 -size=>1,
181 -multiple=>0,
183 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
184 -id=>"ihidden$i",
185 -values=>['0','1','2'],
186 -labels => {'0'=>'Show','1'=>'Show Collapsed',
187 '2' =>'Hide',
189 -default=>substr($data->{'hidden'},1,1),
190 -size=>1,
191 -multiple=>0,
193 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
194 -id=>"ehidden$i",
195 -values=>['0','1','2'],
196 -labels => {'0'=>'Show','1'=>'Show Collapsed',
197 '2' =>'Hide',
199 -default=>substr($data->{'hidden'}." ",2,1),
200 -size=>1,
201 -multiple=>0,
203 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
204 $row_data{tagsubfield} = $data->{'tagsubfield'};
205 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
206 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
207 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
208 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
209 -id=>"kohafield$i",
210 -values=> \@kohafields,
211 -default=> "$data->{'kohafield'}",
212 -size=>1,
213 -multiple=>0,
215 $row_data{authorised_value} = CGI::scrolling_list(-name=>'authorised_value',
216 -id=>"authorised_value$i",
217 -values=> \@authorised_values,
218 -default=>$data->{'authorised_value'},
219 -size=>1,
220 -tabindex=>'',
221 -multiple=>0,
223 $row_data{frameworkcode} = CGI::scrolling_list(-name=>'frameworkcode',
224 -id=>"frameworkcode$i",
225 -values=> \@authtypes,
226 -default=>$data->{'frameworkcode'},
227 -size=>1,
228 -tabindex=>'',
229 -multiple=>0,
231 $row_data{value_builder} = CGI::scrolling_list(-name=>'value_builder',
232 -id=>"value_builder$i",
233 -values=> \@value_builder,
234 -default=>$data->{'value_builder'},
235 -size=>1,
236 -tabindex=>'',
237 -multiple=>0,
240 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
241 -checked => $data->{'repeatable'}?'checked':'',
242 -value => 1,
243 -label => '',
244 -id => "repeatable$i");
245 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
246 -checked => $data->{'mandatory'}?'checked':'',
247 -value => 1,
248 -label => '',
249 -id => "mandatory$i");
250 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
251 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
252 -id => "isurl$i",
253 -checked => $data->{'isurl'}?'checked':'',
254 -value => 1,
255 -label => '');
256 $row_data{row} = $i;
257 $row_data{toggle} = $toggle;
258 push(@loop_data, \%row_data);
259 $i++;
261 # add more_subfields empty lines for add if needed
262 for (my $i=1;$i<=$more_subfields;$i++) {
263 my %row_data; # get a fresh hash for the row data
264 $row_data{'new_subfield'} = 1;
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;