Clean up before final commits
[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
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         
90         # build authorised value list
91
92 my      $sth2 = $dbh->prepare("select distinct category from authorised_values");
93         $sth2->execute;
94         my @authorised_values;
95         push @authorised_values,"";
96         while ((my $category) = $sth2->fetchrow_array) {
97                 push @authorised_values, $category;
98         }
99         push (@authorised_values,"branches");
100         push (@authorised_values,"itemtypes");
101
102         # build value_builder list
103         my @value_builder=('');
104
105         # read value_builder directory.
106         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
107         # on a standard install, /cgi-bin need to be added. 
108         # test one, then the other
109         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
110         unless (opendir(DIR, "$cgidir/value_builder")) {
111                 $cgidir = C4::Context->intranetdir;
112                 opendir(DIR, "$cgidir/value_builder") || die "can't opendir $cgidir/value_builder: $!";
113         } 
114         while (my $line = readdir(DIR)) {
115                 if ($line =~ /\.pl$/) {
116                         push (@value_builder,$line);
117                 }
118         }
119         closedir DIR;
120
121         # build values list
122         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
123         $sth->execute($tagfield,$authtypecode);
124         my @loop_data = ();
125         my $toggle=1;
126         my $i=0;
127         while ($data =$sth->fetchrow_hashref) {
128
129                 my %row_data;  # get a fresh hash for the row data
130                 if ($toggle eq 1){
131                         $toggle=0;
132                 } else {
133                         $toggle=1;
134                 }
135                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
136                                         -id=>"tab$i",
137                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9'],
138                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1','2' =>'2','3'=>'3','4'=>'4',
139                                                                         '5' =>'5','6'=>'6','7'=>'7',
140                                                                         '8' =>'8','9'=>'9',},
141                                         -default=>$data->{'tab'},
142                                         -size=>1,
143                                         -multiple=>0,
144                                         );
145                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
146                                         -id=>"ohidden$i",
147                                         -values=>['0','2'],
148                                         -labels => {'0'=>'Show','2' =>'Hide',},
149                                         -default=>substr($data->{'hidden'},0,1),
150                                         -size=>1,
151                                         -multiple=>0,
152                                         );
153                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
154                                         -id=>"ihidden$i",
155                                         -values=>['0','2'],
156                                         -labels => {'0'=>'Show',
157                                                                         '2' =>'Hide',
158                                                                         },
159                                         -default=>substr($data->{'hidden'},1,1),
160                                         -size=>1,
161                                         -multiple=>0,
162                                         );
163                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
164                                         -id=>"ehidden$i",
165                                         -values=>['0','1','2'],
166                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
167                                                                         '2' =>'Hide',
168                                                                         },
169                                         -default=>substr($data->{'hidden'},2,1),
170                                         -size=>1,
171                                         -multiple=>0,
172                                         );
173                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\">";
174                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
175                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
176                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
177                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
178                                         -id=>'authorised_value',
179                                         -values=> \@authorised_values,
180                                         -default=>$data->{'authorised_value'},
181                                         -size=>1,
182                                         -multiple=>0,
183                                         );
184                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
185                                         -id=>'value_builder',
186                                         -values=> \@value_builder,
187                                         -default=>$data->{'value_builder'},
188                                         -size=>1,
189                                         -multiple=>0,
190                                         );
191                 
192                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
193         -checked => $data->{'repeatable'}?'checked':'',
194         -value => 1,
195         -label => '',
196         -id => "repeatable$i");
197                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
198         -checked => $data->{'mandatory'}?'checked':'',
199         -value => 1,
200         -label => '',
201         -id => "mandatory$i");
202                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
203                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
204                         -id => "isurl$i",
205                         -checked => $data->{'isurl'}?'checked':'',
206                         -value => 1,
207                         -label => '');
208                 $row_data{link} = CGI::checkbox( -name => "link$i",
209                         -id => "link$i",
210                         -checked => $data->{'link'}?'checked':'',
211                         -value => 1,
212                         -label => '');
213                 $row_data{row} = $i;
214                 $row_data{toggle} = $toggle;
215                 # $row_data{link} = CGI::escapeHTML($data->{'link'});
216                 push(@loop_data, \%row_data);
217                 $i++;
218         }
219         # add more_subfields empty lines for add if needed
220         for (my $i=1;$i<=$more_subfields;$i++) {
221                 my %row_data;  # get a fresh hash for the row data
222                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
223                                         -id=>"tab$i",
224                                         -values=>['-1','0','1','2','3','4','5','6','7','8','9'],
225                                         -labels => {'-1' =>'ignore','0'=>'0','1'=>'1','2' =>'2','3'=>'3','4'=>'4',
226                                                                         '5' =>'5','6'=>'6','7'=>'7',
227                                                                         '8' =>'8','9'=>'9',},
228                                         -default=>"",
229                                         -size=>1,
230                                         -multiple=>0,
231                                         );
232                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
233                                         -id=>"ohidden$i",
234                                         -values=>['0','2'],
235                                         -labels => {'0'=>'Show','2' =>'Hide',},
236                                         -default=>"0",
237                                         -size=>1,
238                                         -multiple=>0,
239                                         );
240
241                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
242                                         -id=>"ihidden$i",
243                                         -values=>['0','2'],
244                                         -labels => {'0'=>'Show','2' =>'Hide',},
245                                         -default=>"0",
246                                         -size=>1,
247                                         -multiple=>0,
248                                         );
249                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
250                                         -id=>"ehidden$i",
251                                         -values=>['0','1','2'],
252                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
253                                                                         '2' =>'Hide',
254                                                                         },
255                                         -default=>"0",
256                                         -size=>1,
257                                         -multiple=>0,
258                                         );
259                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
260                 $row_data{liblibrarian} = "";
261                 $row_data{libopac} = "";
262                 $row_data{seealso} = "";
263                 $row_data{hidden} = "000";
264                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
265                                 -id => "repeatable$i",
266                                 -checked => '',
267                                 -value => 1,
268                                 -label => '');
269                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
270                         -id => "mandatory$i",
271                         -checked => '',
272                         -value => 1,
273                         -label => '');
274                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
275                         -id => "isurl$i",
276                         -checked => '',
277                         -value => 1,
278                         -label => '');
279                 
280                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
281                                         -id => 'authorised_value',
282                                         -values=> \@authorised_values,
283                                         -size=>1,
284                                         -multiple=>0,
285                                         );
286                 $row_data{link} = CGI::checkbox( -name => "link",
287                         -id => "link$i",
288                         -checked => '',
289                         -value => 1,
290                         -label => '');
291                 # $row_data{link} = CGI::escapeHTML($data->{'link'});
292                 $row_data{toggle} = $toggle;
293                 $row_data{row} = $i;
294                 push(@loop_data, \%row_data);
295         }
296         $template->param('use-heading-flags-p' => 1);
297         $template->param('heading-edit-subfields-p' => 1);
298         $template->param(action => "Edit subfields",
299                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
300                                                         loop => \@loop_data,
301                                                         more_subfields => $more_subfields,
302                                                         more_tag => $tagfield);
303
304                                                                                                 # END $OP eq ADD_FORM
305 ################## ADD_VALIDATE ##################################
306 # called by add_form, used to insert/modify data in DB
307 } elsif ($op eq 'add_validate') {
308         my $dbh = C4::Context->dbh;
309         $template->param(tagfield => "$input->param('tagfield')");
310         my $sth=$dbh->prepare("replace auth_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl, link)
311                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
312         my @tagsubfield = $input->param('tagsubfield');
313         my @liblibrarian        = $input->param('liblibrarian');
314         my @libopac             = $input->param('libopac');
315         my @tab                         = $input->param('tab');
316         my @seealso             = $input->param('seealso');
317         #my @hidden             = $input->param('hidden');
318         my @hidden;
319         my @ohidden             = $input->param('ohidden');
320         my @ihidden             = $input->param('ihidden');
321         my @ehidden             = $input->param('ehidden');
322         my @authorised_values   = $input->param('authorised_value');
323 #       my $authtypecodes       = $input->param('authtypecode');
324         my @value_builder       =$input->param('value_builder');
325         my @link                =$input->param('link');
326         for (my $i=0; $i<= $#tagsubfield ; $i++) {
327                 my $tagfield                    =$input->param('tagfield');
328                 my $tagsubfield         =$tagsubfield[$i];
329                 $tagsubfield="@" unless $tagsubfield ne '';
330                 my $liblibrarian                =$liblibrarian[$i];
331                 my $libopac                     =$libopac[$i];
332                 my $repeatable          =$input->param("repeatable$i")?1:0;
333                 my $mandatory           =$input->param("mandatory$i")?1:0;
334
335                 my $tab                         =$tab[$i];
336                 my $seealso                             =$seealso[$i];
337                 my $authorised_value            =$authorised_values[$i];
338 #               my $authtypecode                =$authtypecodes;
339                 my $value_builder=$value_builder[$i];
340                 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
341                 my $isurl = $input->param("isurl$i")?1:0;
342                 my $link = $input->param("link$i")?1:0;
343                 if ($liblibrarian) {
344                         unless (C4::Context->config('demo') eq 1) {
345                                 $sth->execute ($tagfield,
346                                                                         $tagsubfield,
347                                                                         $liblibrarian,
348                                                                         $libopac,
349                                                                         $repeatable,
350                                                                         $mandatory,
351                                                                         $tab,
352                                                                         $seealso,
353                                                                         $authorised_value,
354                                                                         $authtypecode,
355                                                                         $value_builder,
356                                                                         $hidden,
357                                                                         $isurl,
358                                                                         
359
360          $link,
361                                               );
362                         }
363                 }
364         }
365         $sth->finish;
366         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
367         exit;
368
369                                                                                                         # END $OP eq ADD_VALIDATE
370 ################## DELETE_CONFIRM ##################################
371 # called by default form, used to confirm deletion of data in DB
372 } elsif ($op eq 'delete_confirm') {
373         my $dbh = C4::Context->dbh;
374         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
375         #FIXME : called with 2 bind variables when 3 are needed
376         $sth->execute($tagfield,$tagsubfield);
377         my $data=$sth->fetchrow_hashref;
378         $sth->finish;
379         $template->param(liblibrarian => $data->{'liblibrarian'},
380                                                         tagsubfield => $data->{'tagsubfield'},
381                                                         delete_link => $script_name,
382                                                         tagfield      =>$tagfield,
383                                                         tagsubfield => $tagsubfield,
384                                                         authtypecode => $authtypecode,
385                                                         );
386                                                                                                         # END $OP eq DELETE_CONFIRM
387 ################## DELETE_CONFIRMED ##################################
388 # called by delete_confirm, used to effectively confirm deletion of data in DB
389 } elsif ($op eq 'delete_confirmed') {
390         my $dbh = C4::Context->dbh;
391         unless (C4::Context->config('demo') eq 1) {
392                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
393                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
394                 $sth->finish;
395         }
396         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
397         exit;
398         $template->param(tagfield => $tagfield);
399                                                                                                         # END $OP eq DELETE_CONFIRMED
400 ################## DEFAULT ##################################
401 } else { # DEFAULT
402         my $env;
403         my ($count,$results)=StringSearch($env,$tagfield,$authtypecode);
404         my $toggle=1;
405         my @loop_data = ();
406         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
407                 if ($toggle eq 1){
408                         $toggle=0;
409                 } else {
410                         $toggle=1;
411                 }
412                 my %row_data;  # get a fresh hash for the row data
413                 $row_data{tagfield} = $results->[$i]{'tagfield'};
414                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
415                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
416                 $row_data{repeatable} = $results->[$i]{'repeatable'};
417                 $row_data{mandatory} = $results->[$i]{'mandatory'};
418                 $row_data{tab} = $results->[$i]{'tab'};
419                 $row_data{seealso} = $results->[$i]{'seealso'};
420                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
421                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
422                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
423                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
424                 $row_data{isurl}        = $results->[$i]{'isurl'};
425                 $row_data{link} = $results->[$i]{'link'};
426                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&authtypecode=$authtypecode";
427                 $row_data{toggle} = $toggle;
428                 if ($row_data{tab} eq -1) {
429                         $row_data{subfield_ignored} = 1;
430                 }
431
432                 push(@loop_data, \%row_data);
433         }
434         $template->param(loop => \@loop_data);
435         $template->param(edit_tagfield => $tagfield,
436                 edit_authtypecode => $authtypecode);
437         
438         if ($offset>0) {
439                 my $prevpage = $offset-$pagesize;
440                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
441         }
442         if ($offset+$pagesize<$count) {
443                 my $nextpage =$offset+$pagesize;
444                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
445         }
446 } #---- END $OP eq DEFAULT
447 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
448                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
449                 IntranetNav => C4::Context->preference("IntranetNav"),
450                 );
451 output_html_with_http_headers $input, $cookie, $template->output;