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