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