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