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