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