Bug 8567: Set output directory for fines.pl in cron config created by the packages
[koha.git] / admin / auth_subfields_structure.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26
27
28 sub string_search  {
29         my ($searchstring,$authtypecode)=@_;
30         my $dbh = C4::Context->dbh;
31         $searchstring=~ s/\'/\\\'/g;
32         my @data=split(' ',$searchstring);
33         my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
34         $sth->execute("$searchstring%",$authtypecode);
35         my $results = $sth->fetchall_arrayref({});
36         return (scalar(@$results), $results);
37 }
38
39 sub auth_subfield_structure_exists {
40         my ($authtypecode, $tagfield, $tagsubfield) = @_;
41         my $dbh  = C4::Context->dbh;
42         my $sql  = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
43         my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
44         return @$rows > 0;
45 }
46
47 my $input        = new CGI;
48 my $tagfield     = $input->param('tagfield');
49 my $tagsubfield  = $input->param('tagsubfield');
50 my $authtypecode = $input->param('authtypecode');
51 my $offset       = $input->param('offset');
52 my $op           = $input->param('op') || '';
53 my $script_name  = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
54
55 my ($template, $borrowernumber, $cookie) = get_template_and_user(
56     {   template_name   => "admin/auth_subfields_structure.tmpl",
57         query           => $input,
58         type            => "intranet",
59         authnotrequired => 0,
60         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
61         debug           => 1,
62     }
63 );
64 my $pagesize = 30;
65 $tagfield =~ s/\,//g;
66
67 if ($op) {
68 $template->param(script_name => $script_name,
69                                                 tagfield =>$tagfield,
70                                                 authtypecode => $authtypecode,
71                                                 $op              => 1); # we show only the TMPL_VAR names $op
72 } else {
73 $template->param(script_name => $script_name,
74                                                 tagfield =>$tagfield,
75                                                 authtypecode => $authtypecode,
76                                                 else              => 1); # we show only the TMPL_VAR names $op
77 }
78
79 my $dbh = C4::Context->dbh;
80 ################## ADD_FORM ##################################
81 # called by default. Used to create form to add or  modify a record
82 if ($op eq 'add_form') {
83         my $data;
84         my $more_subfields = $input->param("more_subfields")+1;
85         # builds kohafield tables
86         my @kohafields;
87         push @kohafields, "";
88         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
89         $sth2->execute;
90         while ((my $field) = $sth2->fetchrow_array) {
91                 push @kohafields, "auth_header.".$field;
92         }
93         
94         # build authorised value list
95         $sth2 = $dbh->prepare("select distinct category from authorised_values");
96         $sth2->execute;
97         my @authorised_values;
98         push @authorised_values,"";
99         while ((my $category) = $sth2->fetchrow_array) {
100                 push @authorised_values, $category;
101         }
102         push (@authorised_values,"branches");
103         push (@authorised_values,"itemtypes");
104     
105     # build thesaurus categories list
106     $sth2 = $dbh->prepare("select authtypecode from auth_types");
107     $sth2->execute;
108     my @authtypes;
109     push @authtypes, "";
110     while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
111         push @authtypes, $authtypecode;
112     }
113
114         # build value_builder list
115         my @value_builder=('');
116
117         # read value_builder directory.
118         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
119         # on a standard install, /cgi-bin need to be added. 
120         # test one, then the other
121         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
122         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
123                 $cgidir = C4::Context->intranetdir;
124                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
125         } 
126         while (my $line = readdir(DIR)) {
127                 if ($line =~ /\.pl$/) {
128                         push (@value_builder,$line);
129                 }
130         }
131         @value_builder= sort {$a cmp $b} @value_builder;
132         closedir DIR;
133
134         # build values list
135         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
136         $sth->execute($tagfield,$authtypecode);
137         my @loop_data = ();
138         my $i=0;
139         while ($data =$sth->fetchrow_hashref) {
140
141                 my %row_data;  # get a fresh hash for the row data
142         $row_data{defaultvalue} = $data->{defaultvalue};
143         $row_data{tab} = {
144                     id      => "tab$i",
145                     default => $data->{'tab'},
146                     };
147         $row_data{ohidden} = {
148                     id      => "ohidden$i",
149                     default => $data->{'hidden'}
150                     };
151                 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
152                 #                       -id=>"ihidden$i",
153                 #                       -values=>['0','1','2'],
154                 #                       -labels => {'0'=>'Show','1'=>'Show Collapsed',
155                 #                                                       '2' =>'Hide',
156                 #                                                       },
157                 #                       -default=>substr($data->{'hidden'},1,1),
158                 #                       -size=>1,
159                 #                       -multiple=>0,
160                 #                       );
161                 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
162                 #                       -id=>"ehidden$i",
163                 #                       -values=>['0','1','2'],
164                 #                       -labels => {'0'=>'Show','1'=>'Show Collapsed',
165                 #                                                       '2' =>'Hide',
166                 #                                                       },
167                 #                       -default=>substr($data->{'hidden'}."  ",2,1),
168                 #                       -size=>1,
169                 #                       -multiple=>0,
170                 #                       );
171                 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
172                 $row_data{tagsubfield} = $data->{'tagsubfield'};
173                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
174                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
175                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
176                 $row_data{kohafield}= CGI::scrolling_list( -name=>"kohafield",
177                                         -id=>"kohafield$i",
178                                         -values=> \@kohafields,
179                                         -default=> "$data->{'kohafield'}",
180                                         -size=>1,
181                                         -multiple=>0,
182                                         );
183                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
184                                         -id=>"authorised_value$i",
185                                         -values=> \@authorised_values,
186                                         -default=>$data->{'authorised_value'},
187                                         -size=>1,
188                                         -tabindex=>'',
189                                         -multiple=>0,
190                                         );
191                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
192                                         -id=>"frameworkcode$i",
193                                         -values=> \@authtypes,
194                                         -default=>$data->{'frameworkcode'},
195                                         -size=>1,
196                                         -tabindex=>'',
197                                         -multiple=>0,
198                                         );
199                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
200                                         -id=>"value_builder$i",
201                                         -values=> \@value_builder,
202                                         -default=>$data->{'value_builder'},
203                                         -size=>1,
204                                         -tabindex=>'',
205                                         -multiple=>0,
206                                         );
207                 
208                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
209         -checked => $data->{'repeatable'}?'checked':'',
210         -value => 1,
211         -label => '',
212         -id => "repeatable$i");
213                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
214         -checked => $data->{'mandatory'}?'checked':'',
215         -value => 1,
216         -label => '',
217         -id => "mandatory$i");
218                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
219                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
220                         -id => "isurl$i",
221                         -checked => $data->{'isurl'}?'checked':'',
222                         -value => 1,
223                         -label => '');
224                 $row_data{row} = $i;
225                 push(@loop_data, \%row_data);
226                 $i++;
227         }
228         # add more_subfields empty lines for add if needed
229         for (my $i=1;$i<=$more_subfields;$i++) {
230                 my %row_data;  # get a fresh hash for the row data
231         $row_data{'new_subfield'} = 1;
232                 $row_data{tab} = CGI::scrolling_list(-name=>'tab',
233                                         -id => "tab$i",
234                                         -values =>
235                                         [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
236                                         -labels => {
237                                             '-1' => 'ignore',
238                                             '0'  => '0',
239                                             '1'  => '1',
240                                             '2'  => '2',
241                                             '3'  => '3',
242                                             '4'  => '4',
243                                             '5'  => '5',
244                                             '6'  => '6',
245                                             '7'  => '7',
246                                             '8'  => '8',
247                                             '9'  => '9',
248                                         },
249                                         -default=>"",
250                                         -size=>1,
251                                         -tabindex=>'',
252                                         -multiple=>0,
253                                         );
254                 $row_data{ohidden} = CGI::scrolling_list(-name=>'ohidden',
255                                         -id=>"ohidden$i",
256                                         #-values=>['0','1','2'],
257                                         #-labels => {'0'=>'Show','1'=>'Show Collapsed',
258                                         #                               '2' =>'Hide',
259                                         #                               },
260                                         -values=>['0','-5'],
261                                         -labels => {'0'=>'Show All','-5'=>'Hide All',},
262                                         #-default=>"0",
263                                         -default=>$data->{'hidden'},
264                                         #-default=>"-5",
265                                         -size=>1,
266                                         -multiple=>0,
267                                         );
268
269                 #$row_data{ihidden} = CGI::scrolling_list(-name=>'ihidden',
270                 #                       -id=>"ihidden$i",
271                 #                       -values=>['0','1','2'],
272                 #                       -labels => {'0'=>'Show','1'=>'Show Collapsed',
273                 #                                                       '2' =>'Hide',
274                 #                                                       },
275                 #                       -default=>"0",
276                 #                       -size=>1,
277                 #                       -multiple=>0,
278                 #                       );
279                 #$row_data{ehidden} = CGI::scrolling_list(-name=>'ehidden',
280                 #                       -id=>"ehidden$i",
281                 #                       -values=>['0','1','2'],
282                 #                       -labels => {'0'=>'Show','1'=>'Show Collapsed',
283                 #                                                       '2' =>'Hide',
284                 #                                                       },
285                 #                       -default=>"0",
286                 #                       -size=>1,
287                 #                       -multiple=>0,
288                 #                       );
289                 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
290                 $row_data{tagsubfieldinput} = 
291                         "<label><input type=\"text\" name=\"tagsubfield\" value=\""
292                         . $data->{'tagsubfield'}
293                         . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
294                 $row_data{tagsubfield} = $data->{'tagsubfield'};
295                 $row_data{liblibrarian} = "";
296                 $row_data{libopac} = "";
297                 $row_data{seealso} = "";
298                 $row_data{hidden} = "000";
299                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
300                                 -id => "repeatable$i",
301                                 -checked => '',
302                                 -value => 1,
303                                 -label => '');
304                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
305                         -id => "mandatory$i",
306                         -checked => '',
307                         -value => 1,
308                         -label => '');
309                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
310                         -id => "isurl$i",
311                         -checked => '',
312                         -value => 1,
313                         -label => '');
314                 $row_data{kohafield}= CGI::scrolling_list( -name=>'kohafield',
315                                         -id => "kohafield$i",
316                                         -values=> \@kohafields,
317                                         -default=> "",
318                                         -size=>1,
319                                         -multiple=>0,
320                                         );
321                 $row_data{frameworkcode}  = CGI::scrolling_list(-name=>'frameworkcode',
322                                         -id=>'frameworkcode',
323                                         -values=> \@authtypes,
324                                         -default=>$data->{'frameworkcode'},
325                                         -size=>1,
326                                         -tabindex=>'',
327                                         -multiple=>0,
328                                         );
329                 $row_data{authorised_value}  = CGI::scrolling_list(-name=>'authorised_value',
330                                         -id => 'authorised_value',
331                                         -values=> \@authorised_values,
332                                         -size=>1,
333                                         -tabindex=>'',
334                                         -multiple=>0,
335                                         );
336                 $row_data{value_builder}  = CGI::scrolling_list(-name=>'value_builder',
337                                         -id=>'value_builder',
338                                         -values=> \@value_builder,
339                                         -default=>$data->{'value_builder'},
340                                         -size=>1,
341                                         -tabindex=>'',
342                                         -multiple=>0,
343                                         );
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 => $tagfield,
351                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
352                                                         loop => \@loop_data,
353                                                         more_subfields => $more_subfields,
354                                                         more_tag => $tagfield);
355
356                                                                                                 # END $OP eq ADD_FORM
357 ################## ADD_VALIDATE ##################################
358 # called by add_form, used to insert/modify data in DB
359 } elsif ($op eq 'add_validate') {
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 $sth_insert = $dbh->prepare("insert into auth_subfield_structure (authtypecode,tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,frameworkcode,value_builder,hidden,isurl,defaultvalue)
364                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
365     my $sth_update = $dbh->prepare("update auth_subfield_structure set authtypecode=?, tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, frameworkcode=?, value_builder=?, hidden=?, isurl=?, defaultvalue=?
366                                     where authtypecode=? and tagfield=? and tagsubfield=?");
367         my @tagsubfield = $input->param('tagsubfield');
368         my @liblibrarian        = $input->param('liblibrarian');
369         my @libopac             = $input->param('libopac');
370         my @kohafield           = ''.$input->param('kohafield');
371         my @tab                         = $input->param('tab');
372         my @seealso             = $input->param('seealso');
373         my @hidden;
374         my @ohidden             = $input->param('ohidden');
375         #my @ihidden            = $input->param('ihidden');
376         #my @ehidden            = $input->param('ehidden');
377         my @authorised_values   = $input->param('authorised_value');
378         my $authtypecode        = $input->param('authtypecode');
379         my @frameworkcodes      = $input->param('frameworkcode');
380         my @value_builder       =$input->param('value_builder');
381     my @defaultvalue = $input->param('defaultvalue');
382         for (my $i=0; $i<= $#tagsubfield ; $i++) {
383                 my $tagfield                    =$input->param('tagfield');
384                 my $tagsubfield         =$tagsubfield[$i];
385                 $tagsubfield="@" unless $tagsubfield ne '';
386                 my $liblibrarian                =$liblibrarian[$i];
387                 my $libopac                     =$libopac[$i];
388                 my $repeatable          =$input->param("repeatable$i")?1:0;
389                 my $mandatory           =$input->param("mandatory$i")?1:0;
390                 my $kohafield           =$kohafield[$i];
391                 my $tab                         =$tab[$i];
392                 my $seealso                             =$seealso[$i];
393                 my $authorised_value            =$authorised_values[$i];
394                 my $frameworkcode               =$frameworkcodes[$i];
395                 my $value_builder=$value_builder[$i];
396         my $defaultvalue = $defaultvalue[$i];
397                 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
398                 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
399                 my $isurl = $input->param("isurl$i")?1:0;
400                 if ($liblibrarian) {
401                         unless (C4::Context->config('demo') eq 1) {
402                                 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
403                                         $sth_update->execute(
404                                                 $authtypecode,
405                                                 $tagfield,
406                                                 $tagsubfield,
407                                                 $liblibrarian,
408                                                 $libopac,
409                                                 $repeatable,
410                                                 $mandatory,
411                                                 $kohafield,
412                                                 $tab,
413                                                 $seealso,
414                                                 $authorised_value,
415                                                 $frameworkcode,
416                                                 $value_builder,
417                                                 $hidden,
418                                                 $isurl,
419                         $defaultvalue,
420                                                 (
421                                                         $authtypecode,
422                                                         $tagfield,
423                                                         $tagsubfield
424                                                 ),
425                                         );
426                                 } else {
427                                         $sth_insert->execute(
428                                                 $authtypecode,
429                                                 $tagfield,
430                                                 $tagsubfield,
431                                                 $liblibrarian,
432                                                 $libopac,
433                                                 $repeatable,
434                                                 $mandatory,
435                                                 $kohafield,
436                                                 $tab,
437                                                 $seealso,
438                                                 $authorised_value,
439                                                 $frameworkcode,
440                                                 $value_builder,
441                                                 $hidden,
442                                                 $isurl,
443                         $defaultvalue,
444                                         );
445                                 }
446                         }
447                 }
448         }
449         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
450         exit;
451
452                                                                                                         # END $OP eq ADD_VALIDATE
453 ################## DELETE_CONFIRM ##################################
454 # called by default form, used to confirm deletion of data in DB
455 } elsif ($op eq 'delete_confirm') {
456         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
457         $sth->execute($tagfield,$tagsubfield,$authtypecode);
458         my $data=$sth->fetchrow_hashref;
459         $template->param(liblibrarian => $data->{'liblibrarian'},
460                                                         tagsubfield => $data->{'tagsubfield'},
461                                                         delete_link => $script_name,
462                                                         tagfield      =>$tagfield,
463                                                         tagsubfield => $tagsubfield,
464                                                         authtypecode => $authtypecode,
465                                                         );
466                                                                                                         # END $OP eq DELETE_CONFIRM
467 ################## DELETE_CONFIRMED ##################################
468 # called by delete_confirm, used to effectively confirm deletion of data in DB
469 } elsif ($op eq 'delete_confirmed') {
470         unless (C4::Context->config('demo') eq 1) {
471                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
472                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
473         }
474         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
475         exit;
476         $template->param(tagfield => $tagfield);
477                                                                                                         # END $OP eq DELETE_CONFIRMED
478 ################## DEFAULT ##################################
479 } else { # DEFAULT
480         my ($count,$results)=string_search($tagfield,$authtypecode);
481         my @loop_data = ();
482         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
483                 my %row_data;  # get a fresh hash for the row data
484                 $row_data{tagfield} = $results->[$i]{'tagfield'};
485                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
486                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
487                 $row_data{kohafield} = $results->[$i]{'kohafield'};
488                 $row_data{repeatable} = $results->[$i]{'repeatable'};
489                 $row_data{mandatory} = $results->[$i]{'mandatory'};
490                 $row_data{tab} = $results->[$i]{'tab'};
491                 $row_data{seealso} = $results->[$i]{'seealso'};
492                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
493                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
494                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
495                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
496                 $row_data{isurl}        = $results->[$i]{'isurl'};
497                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
498                 if ($row_data{tab} eq -1) {
499                         $row_data{subfield_ignored} = 1;
500                 }
501
502                 push(@loop_data, \%row_data);
503         }
504         $template->param(loop => \@loop_data);
505         $template->param(edit_tagfield => $tagfield,
506                 edit_authtypecode => $authtypecode);
507         
508         if ($offset>0) {
509                 my $prevpage = $offset-$pagesize;
510                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
511         }
512         if ($offset+$pagesize<$count) {
513                 my $nextpage =$offset+$pagesize;
514                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
515         }
516 } #---- END $OP eq DEFAULT
517 output_html_with_http_headers $input, $cookie, $template->output;