removing useless code
[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         @value_builder= sort {$a cmp $b} @value_builder;
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 =>
155                                         [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
156                                         -labels => {
157                                             '-1' => 'ignore',
158                                             '0'  => '0',
159                                             '1'  => '1',
160                                             '2'  => '2',
161                                             '3'  => '3',
162                                             '4'  => '4',
163                                             '5'  => '5',
164                                             '6'  => '6',
165                                             '7'  => '7',
166                                             '8'  => '8',
167                                             '9'  => '9',
168                                         },
169                                         -default=>$data->{'tab'},
170                                         -size=>1,
171                                         -tabindex=>'',
172                                         -multiple=>0,
173                                         );
174                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
175                                         -id=>"ohidden$i",
176                                         -values=>['0','1','2'],
177                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
178                                                                         '2' =>'Hide',
179                                                                         },
180                                         -default=>substr($data->{'hidden'},0,1),
181                                         -size=>1,
182                                         -multiple=>0,
183                                         );
184                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
185                                         -id=>"ihidden$i",
186                                         -values=>['0','1','2'],
187                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
188                                                                         '2' =>'Hide',
189                                                                         },
190                                         -default=>substr($data->{'hidden'},1,1),
191                                         -size=>1,
192                                         -multiple=>0,
193                                         );
194                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
195                                         -id=>"ehidden$i",
196                                         -values=>['0','1','2'],
197                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
198                                                                         '2' =>'Hide',
199                                                                         },
200                                         -default=>substr($data->{'hidden'}."  ",2,1),
201                                         -size=>1,
202                                         -multiple=>0,
203                                         );
204                 $row_data{tagsubfield} =$data->{'tagsubfield'}."<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
205                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
206                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
207                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
208                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
209                                         -id=>"kohafield$i",
210                                         -values=> \@kohafields,
211                                         -default=> "$data->{'kohafield'}",
212                                         -size=>1,
213                                         -multiple=>0,
214                                         );
215                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
216                                         -id=>'authorised_value',
217                                         -values=> \@authorised_values,
218                                         -default=>$data->{'authorised_value'},
219                                         -size=>1,
220                                         -tabindex=>'',
221                                         -multiple=>0,
222                                         );
223                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
224                                         -id=>'frameworkcode',
225                                         -values=> \@authtypes,
226                                         -default=>$data->{'frameworkcode'},
227                                         -size=>1,
228                                         -tabindex=>'',
229                                         -multiple=>0,
230                                         );
231                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
232                                         -id=>'value_builder',
233                                         -values=> \@value_builder,
234                                         -default=>$data->{'value_builder'},
235                                         -size=>1,
236                                         -tabindex=>'',
237                                         -multiple=>0,
238                                         );
239                 
240                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
241         -checked => $data->{'repeatable'}?'checked':'',
242         -value => 1,
243         -label => '',
244         -id => "repeatable$i");
245                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
246         -checked => $data->{'mandatory'}?'checked':'',
247         -value => 1,
248         -label => '',
249         -id => "mandatory$i");
250                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
251                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
252                         -id => "isurl$i",
253                         -checked => $data->{'isurl'}?'checked':'',
254                         -value => 1,
255                         -label => '');
256                 $row_data{row} = $i;
257                 $row_data{toggle} = $toggle;
258                 push(@loop_data, \%row_data);
259                 $i++;
260         }
261         # add more_subfields empty lines for add if needed
262         for (my $i=1;$i<=$more_subfields;$i++) {
263                 my %row_data;  # get a fresh hash for the row data
264                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
265                                         -id => "tab$i",
266                                         -values =>
267                                         [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
268                                         -labels => {
269                                             '-1' => 'ignore',
270                                             '0'  => '0',
271                                             '1'  => '1',
272                                             '2'  => '2',
273                                             '3'  => '3',
274                                             '4'  => '4',
275                                             '5'  => '5',
276                                             '6'  => '6',
277                                             '7'  => '7',
278                                             '8'  => '8',
279                                             '9'  => '9',
280                                         },
281                                         -default=>"",
282                                         -size=>1,
283                                         -tabindex=>'',
284                                         -multiple=>0,
285                                         );
286                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
287                                         -id=>"ohidden$i",
288                                         -values=>['0','1','2'],
289                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
290                                                                         '2' =>'Hide',
291                                                                         },
292                                         -default=>"0",
293                                         -size=>1,
294                                         -multiple=>0,
295                                         );
296
297                 $row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
298                                         -id=>"ihidden$i",
299                                         -values=>['0','1','2'],
300                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
301                                                                         '2' =>'Hide',
302                                                                         },
303                                         -default=>"0",
304                                         -size=>1,
305                                         -multiple=>0,
306                                         );
307                 $row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
308                                         -id=>"ehidden$i",
309                                         -values=>['0','1','2'],
310                                         -labels => {'0'=>'Show','1'=>'Show Collapsed',
311                                                                         '2' =>'Hide',
312                                                                         },
313                                         -default=>"0",
314                                         -size=>1,
315                                         -multiple=>0,
316                                         );
317                 $row_data{tagsubfield} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
318                 $row_data{liblibrarian} = "";
319                 $row_data{libopac} = "";
320                 $row_data{seealso} = "";
321                 $row_data{hidden} = "000";
322                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
323                                 -id => "repeatable$i",
324                                 -checked => '',
325                                 -value => 1,
326                                 -label => '');
327                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
328                         -id => "mandatory$i",
329                         -checked => '',
330                         -value => 1,
331                         -label => '');
332                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
333                         -id => "isurl$i",
334                         -checked => '',
335                         -value => 1,
336                         -label => '');
337                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
338                                         -id => "kohafield$i",
339                                         -values=> \@kohafields,
340                                         -default=> "",
341                                         -size=>1,
342                                         -multiple=>0,
343                                         );
344                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
345                                         -id=>'frameworkcode',
346                                         -values=> \@authtypes,
347                                         -default=>$data->{'frameworkcode'},
348                                         -size=>1,
349                                         -tabindex=>'',
350                                         -multiple=>0,
351                                         );
352                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
353                                         -id => 'authorised_value',
354                                         -values=> \@authorised_values,
355                                         -size=>1,
356                                         -tabindex=>'',
357                                         -multiple=>0,
358                                         );
359                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
360                                         -id=>'value_builder',
361                                         -values=> \@value_builder,
362                                         -default=>$data->{'value_builder'},
363                                         -size=>1,
364                                         -tabindex=>'',
365                                         -multiple=>0,
366                                         );
367                 $row_data{toggle} = $toggle;
368                 $row_data{row} = $i;
369                 push(@loop_data, \%row_data);
370         }
371         $template->param('use-heading-flags-p' => 1);
372         $template->param('heading-edit-subfields-p' => 1);
373         $template->param(action => "Edit subfields",
374                                                         tagfield => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />$tagfield",
375                                                         loop => \@loop_data,
376                                                         more_subfields => $more_subfields,
377                                                         more_tag => $tagfield);
378
379                                                                                                 # END $OP eq ADD_FORM
380 ################## ADD_VALIDATE ##################################
381 # called by add_form, used to insert/modify data in DB
382 } elsif ($op eq 'add_validate') {
383         my $dbh = C4::Context->dbh;
384         $template->param(tagfield => "$input->param('tagfield')");
385         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)
386                                                                         values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
387         my @tagsubfield = $input->param('tagsubfield');
388         my @liblibrarian        = $input->param('liblibrarian');
389         my @libopac             = $input->param('libopac');
390         my @kohafield           = ''.$input->param('kohafield');
391         my @tab                         = $input->param('tab');
392         my @seealso             = $input->param('seealso');
393         my @hidden;
394         my @ohidden             = $input->param('ohidden');
395         my @ihidden             = $input->param('ihidden');
396         my @ehidden             = $input->param('ehidden');
397         my @authorised_values   = $input->param('authorised_value');
398         my $authtypecode        = $input->param('authtypecode');
399         my @frameworkcodes      = $input->param('frameworkcode');
400         my @value_builder       =$input->param('value_builder');
401         for (my $i=0; $i<= $#tagsubfield ; $i++) {
402                 my $tagfield                    =$input->param('tagfield');
403                 my $tagsubfield         =$tagsubfield[$i];
404                 $tagsubfield="@" unless $tagsubfield ne '';
405                 my $liblibrarian                =$liblibrarian[$i];
406                 my $libopac                     =$libopac[$i];
407                 my $repeatable          =$input->param("repeatable$i")?1:0;
408                 my $mandatory           =$input->param("mandatory$i")?1:0;
409                 my $kohafield           =$kohafield[$i];
410                 my $tab                         =$tab[$i];
411                 my $seealso                             =$seealso[$i];
412                 my $authorised_value            =$authorised_values[$i];
413                 my $frameworkcode               =$frameworkcodes[$i];
414                 my $value_builder=$value_builder[$i];
415                 my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
416                 my $isurl = $input->param("isurl$i")?1:0;
417                 if ($liblibrarian) {
418                         unless (C4::Context->config('demo') eq 1) {
419                                 $sth->execute($authtypecode,
420                               $tagfield,
421                               $tagsubfield,
422                               $liblibrarian,
423                               $libopac,
424                               $repeatable,
425                               $mandatory,
426                               $kohafield,
427                               $tab,
428                               $seealso,
429                               $authorised_value,
430                               $frameworkcode,
431                               $value_builder,
432                               $hidden,
433                               $isurl,
434                                               );
435                         }
436                 }
437         }
438         $sth->finish;
439         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
440         exit;
441
442                                                                                                         # END $OP eq ADD_VALIDATE
443 ################## DELETE_CONFIRM ##################################
444 # called by default form, used to confirm deletion of data in DB
445 } elsif ($op eq 'delete_confirm') {
446         my $dbh = C4::Context->dbh;
447         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
448         #FIXME : called with 2 bind variables when 3 are needed
449         $sth->execute($tagfield,$tagsubfield);
450         my $data=$sth->fetchrow_hashref;
451         $sth->finish;
452         $template->param(liblibrarian => $data->{'liblibrarian'},
453                                                         tagsubfield => $data->{'tagsubfield'},
454                                                         delete_link => $script_name,
455                                                         tagfield      =>$tagfield,
456                                                         tagsubfield => $tagsubfield,
457                                                         authtypecode => $authtypecode,
458                                                         );
459                                                                                                         # END $OP eq DELETE_CONFIRM
460 ################## DELETE_CONFIRMED ##################################
461 # called by delete_confirm, used to effectively confirm deletion of data in DB
462 } elsif ($op eq 'delete_confirmed') {
463         my $dbh = C4::Context->dbh;
464         unless (C4::Context->config('demo') eq 1) {
465                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
466                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
467                 $sth->finish;
468         }
469         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
470         exit;
471         $template->param(tagfield => $tagfield);
472                                                                                                         # END $OP eq DELETE_CONFIRMED
473 ################## DEFAULT ##################################
474 } else { # DEFAULT
475         my ($count,$results)=StringSearch($tagfield,$authtypecode);
476         my $toggle=1;
477         my @loop_data = ();
478         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
479                 if ($toggle eq 1){
480                         $toggle=0;
481                 } else {
482                         $toggle=1;
483                 }
484                 my %row_data;  # get a fresh hash for the row data
485                 $row_data{tagfield} = $results->[$i]{'tagfield'};
486                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
487                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
488                 $row_data{kohafield} = $results->[$i]{'kohafield'};
489                 $row_data{repeatable} = $results->[$i]{'repeatable'};
490                 $row_data{mandatory} = $results->[$i]{'mandatory'};
491                 $row_data{tab} = $results->[$i]{'tab'};
492                 $row_data{seealso} = $results->[$i]{'seealso'};
493                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
494                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
495                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
496                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
497                 $row_data{isurl}        = $results->[$i]{'isurl'};
498                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
499                 $row_data{toggle} = $toggle;
500                 if ($row_data{tab} eq -1) {
501                         $row_data{subfield_ignored} = 1;
502                 }
503
504                 push(@loop_data, \%row_data);
505         }
506         $template->param(loop => \@loop_data);
507         $template->param(edit_tagfield => $tagfield,
508                 edit_authtypecode => $authtypecode);
509         
510         if ($offset>0) {
511                 my $prevpage = $offset-$pagesize;
512                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
513         }
514         if ($offset+$pagesize<$count) {
515                 my $nextpage =$offset+$pagesize;
516                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
517         }
518 } #---- END $OP eq DEFAULT
519 output_html_with_http_headers $input, $cookie, $template->output;