adding 'gettext' as this is requred by the translation utility
[koha.git] / admin / auth_subfields_structure.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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
19
20 use strict;
21 use C4::Output;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25
26
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++;
42         }
43         $sth->finish;
44         return ($cnt,\@results);
45 }
46
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";
54
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;
66
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
77 }
78
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;
92         }
93         
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;
102         }
103         push (@authorised_values,"branches");
104         push (@authorised_values,"itemtypes");
105     
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;
114     }
115
116         # build value_builder list
117         my @value_builder=('');
118
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: $!";
127         } 
128         while (my $line = readdir(DIR)) {
129                 if ($line =~ /\.pl$/) {
130                         push (@value_builder,$line);
131                 }
132         }
133         @value_builder= sort {$a cmp $b} @value_builder;
134         closedir DIR;
135
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) {
143
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;
149                 }
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',
166                                         },
167                                         -default=>$data->{'tab'},
168                                         -size=>1,
169                                         -tabindex=>'',
170                                         -multiple=>0,
171                                         );
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',
177                                                                         },
178                                         -default=>substr($data->{'hidden'},0,1),
179                                         -size=>1,
180                                         -multiple=>0,
181                                         );
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',
187                                                                         },
188                                         -default=>substr($data->{'hidden'},1,1),
189                                         -size=>1,
190                                         -multiple=>0,
191                                         );
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',
197                                                                         },
198                                         -default=>substr($data->{'hidden'}."  ",2,1),
199                                         -size=>1,
200                                         -multiple=>0,
201                                         );
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,
213                                         );
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,
221                                         );
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,
229                                         );
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,
237                                         );
238                 
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++;
259         }
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',
280                                         },
281                                         -default=>"",
282                                         -size=>1,
283                                         -tabindex=>'',
284                                         -multiple=>0,
285                                         );
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',
291                                                                         },
292                                         -default=>"0",
293                                         -size=>1,
294                                         -multiple=>0,
295                                         );
296
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',
302                                                                         },
303                                         -default=>"0",
304                                         -size=>1,
305                                         -multiple=>0,
306                                         );
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',
312                                                                         },
313                                         -default=>"0",
314                                         -size=>1,
315                                         -multiple=>0,
316                                         );
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,
344                                         );
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,
352                                         );
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,
359                                         );
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,
367                                         );
368                 $row_data{toggle} = $toggle;
369                 $row_data{row} = $i;
370                 push(@loop_data, \%row_data);
371         }
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);
380
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,
436                                               );
437                         }
438                 }
439         }
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;
443
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         #FIXME : called with 2 bind variables when 3 are needed
451         $sth->execute($tagfield,$tagsubfield);
452         my $data=$sth->fetchrow_hashref;
453         $sth->finish;
454         $template->param(liblibrarian => $data->{'liblibrarian'},
455                                                         tagsubfield => $data->{'tagsubfield'},
456                                                         delete_link => $script_name,
457                                                         tagfield      =>$tagfield,
458                                                         tagsubfield => $tagsubfield,
459                                                         authtypecode => $authtypecode,
460                                                         );
461                                                                                                         # END $OP eq DELETE_CONFIRM
462 ################## DELETE_CONFIRMED ##################################
463 # called by delete_confirm, used to effectively confirm deletion of data in DB
464 } elsif ($op eq 'delete_confirmed') {
465         my $dbh = C4::Context->dbh;
466         unless (C4::Context->config('demo') eq 1) {
467                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
468                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
469                 $sth->finish;
470         }
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;
473         $template->param(tagfield => $tagfield);
474                                                                                                         # END $OP eq DELETE_CONFIRMED
475 ################## DEFAULT ##################################
476 } else { # DEFAULT
477         my ($count,$results)=StringSearch($tagfield,$authtypecode);
478         my $toggle=1;
479         my @loop_data = ();
480         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
481                 if ($toggle eq 1){
482                         $toggle=0;
483                 } else {
484                         $toggle=1;
485                 }
486                 my %row_data;  # get a fresh hash for the row data
487                 $row_data{tagfield} = $results->[$i]{'tagfield'};
488                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
489                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
490                 $row_data{kohafield} = $results->[$i]{'kohafield'};
491                 $row_data{repeatable} = $results->[$i]{'repeatable'};
492                 $row_data{mandatory} = $results->[$i]{'mandatory'};
493                 $row_data{tab} = $results->[$i]{'tab'};
494                 $row_data{seealso} = $results->[$i]{'seealso'};
495                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
496                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
497                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
498                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
499                 $row_data{isurl}        = $results->[$i]{'isurl'};
500                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
501                 $row_data{toggle} = $toggle;
502                 if ($row_data{tab} eq -1) {
503                         $row_data{subfield_ignored} = 1;
504                 }
505
506                 push(@loop_data, \%row_data);
507         }
508         $template->param(loop => \@loop_data);
509         $template->param(edit_tagfield => $tagfield,
510                 edit_authtypecode => $authtypecode);
511         
512         if ($offset>0) {
513                 my $prevpage = $offset-$pagesize;
514                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
515         }
516         if ($offset+$pagesize<$count) {
517                 my $nextpage =$offset+$pagesize;
518                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
519         }
520 } #---- END $OP eq DEFAULT
521 output_html_with_http_headers $input, $cookie, $template->output;