Bug 9978: Replace license header with the correct license (GPLv3+)
[koha-equinox.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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Koha;
27
28
29 sub string_search  {
30         my ($searchstring,$authtypecode)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
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 = $sth->fetchall_arrayref({});
37         return (scalar(@$results), $results);
38 }
39
40 sub auth_subfield_structure_exists {
41         my ($authtypecode, $tagfield, $tagsubfield) = @_;
42         my $dbh  = C4::Context->dbh;
43         my $sql  = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
44         my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
45         return @$rows > 0;
46 }
47
48 my $input        = new CGI;
49 my $tagfield     = $input->param('tagfield');
50 my $tagsubfield  = $input->param('tagsubfield');
51 my $authtypecode = $input->param('authtypecode');
52 my $offset       = $input->param('offset');
53 my $op           = $input->param('op') || '';
54 my $script_name  = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
55
56 my ($template, $borrowernumber, $cookie) = get_template_and_user(
57     {   template_name   => "admin/auth_subfields_structure.tt",
58         query           => $input,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
62         debug           => 1,
63     }
64 );
65 my $pagesize = 30;
66 $tagfield =~ s/\,//g;
67
68 if ($op) {
69 $template->param(script_name => $script_name,
70                                                 tagfield =>$tagfield,
71                                                 authtypecode => $authtypecode,
72                                                 $op              => 1); # we show only the TMPL_VAR names $op
73 } else {
74 $template->param(script_name => $script_name,
75                                                 tagfield =>$tagfield,
76                                                 authtypecode => $authtypecode,
77                                                 else              => 1); # we show only the TMPL_VAR names $op
78 }
79
80 my $dbh = C4::Context->dbh;
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 $more_subfields = $input->param("more_subfields")+1;
86         # builds kohafield tables
87         my @kohafields;
88         push @kohafields, "";
89         my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
90         $sth2->execute;
91         while ((my $field) = $sth2->fetchrow_array) {
92                 push @kohafields, "auth_header.".$field;
93         }
94         
95         # build authorised value list
96         my $authorised_values = C4::Koha::GetAuthorisedValueCategories;
97         unshift @$authorised_values, '';
98         push @$authorised_values, 'branches';
99         push @$authorised_values, 'itemtypes';
100
101         # build thesaurus categories list
102         my @authtypes = (sort keys %{C4::Koha::getauthtypes()});
103
104         # build value_builder list
105         my @value_builder=('');
106
107         # read value_builder directory.
108         # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
109         # on a standard install, /cgi-bin need to be added. 
110         # test one, then the other
111         my $cgidir = C4::Context->intranetdir ."/cgi-bin";
112         unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
113                 $cgidir = C4::Context->intranetdir;
114                 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
115         } 
116         while (my $line = readdir(DIR)) {
117         if ( $line =~ /\.pl$/ &&
118              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
119             push (@value_builder,$line);
120                 }
121         }
122         @value_builder= sort {$a cmp $b} @value_builder;
123         closedir DIR;
124
125         # build values list
126         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
127         $sth->execute($tagfield,$authtypecode);
128         my @loop_data = ();
129         my $i=0;
130         while ($data =$sth->fetchrow_hashref) {
131
132                 my %row_data;  # get a fresh hash for the row data
133         $row_data{defaultvalue} = $data->{defaultvalue};
134         $row_data{tab} = {
135                     id      => "tab$i",
136                     default => $data->{'tab'},
137                     };
138         $row_data{ohidden} = {
139                     id      => "ohidden$i",
140                     default => $data->{'hidden'}
141                     };
142                 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
143                 $row_data{tagsubfield} = $data->{'tagsubfield'};
144                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
145                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
146                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
147         $row_data{kohafield} = {
148                     id      => "kohafield$i",
149                     values  => \@kohafields,
150                     default => "$data->{'kohafield'}",
151                     };
152         $row_data{authorised_value} = {
153                     id      => "authorised_value$i",
154                     values  => $authorised_values,
155                     default => $data->{'authorised_value'},
156         };
157         $row_data{frameworkcode} = {
158                     id      => "frameworkcode$i",
159                     values  => \@authtypes,
160                     default => $data->{'frameworkcode'},
161         };
162         $row_data{value_builder} = {
163                     id      => "value_builder$i",
164                     values  => \@value_builder,
165                     default => $data->{'value_builder'},
166         };
167                 
168                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
169         -checked => $data->{'repeatable'}?'checked':'',
170         -value => 1,
171         -label => '',
172         -id => "repeatable$i");
173                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
174         -checked => $data->{'mandatory'}?'checked':'',
175         -value => 1,
176         -label => '',
177         -id => "mandatory$i");
178                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
179                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
180                         -id => "isurl$i",
181                         -checked => $data->{'isurl'}?'checked':'',
182                         -value => 1,
183                         -label => '');
184                 $row_data{row} = $i;
185                 push(@loop_data, \%row_data);
186                 $i++;
187         }
188         # add more_subfields empty lines for add if needed
189         for (my $i=1;$i<=$more_subfields;$i++) {
190                 my %row_data;  # get a fresh hash for the row data
191         $row_data{'new_subfield'} = 1;
192         $row_data{tab} = {
193                     id      => "tab$i",
194                     default => $data->{'tab'},
195                     };
196         $row_data{ohidden} = {
197                     id      => "ohidden$i",
198                     default => $data->{'hidden'}
199                     };
200
201                 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
202                 $row_data{tagsubfieldinput} = 
203                         "<label><input type=\"text\" name=\"tagsubfield\" value=\""
204                         . $data->{'tagsubfield'}
205                         . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
206                 $row_data{tagsubfield} = $data->{'tagsubfield'};
207                 $row_data{liblibrarian} = "";
208                 $row_data{libopac} = "";
209                 $row_data{seealso} = "";
210                 $row_data{hidden} = "000";
211                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
212                                 -id => "repeatable$i",
213                                 -checked => '',
214                                 -value => 1,
215                                 -label => '');
216                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
217                         -id => "mandatory$i",
218                         -checked => '',
219                         -value => 1,
220                         -label => '');
221                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
222                         -id => "isurl$i",
223                         -checked => '',
224                         -value => 1,
225                         -label => '');
226         $row_data{kohafield} = {
227                     id      => "kohafield$i",
228                     values  => \@kohafields,
229                     default => "",
230                     };
231         $row_data{authorised_value} = {
232                     id      => "authorised_value",
233                     values  => $authorised_values,
234                     default => "",
235         };
236         $row_data{frameworkcode} = {
237                     id      => "frameworkcode",
238                     values  => \@authtypes,
239                     default => $data->{'frameworkcode'},
240         };
241         $row_data{value_builder} = {
242                     id      => "value_builder",
243                     values  => \@value_builder,
244                     default => $data->{'value_builder'},
245         };
246
247                 $row_data{row} = $i;
248                 push(@loop_data, \%row_data);
249         }
250         $template->param('use_heading_flags_p' => 1);
251         $template->param('heading_edit_subfields_p' => 1);
252         $template->param(action => "Edit subfields",
253                                                         tagfield => $tagfield,
254                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
255                                                         loop => \@loop_data,
256                                                         more_subfields => $more_subfields,
257                                                         more_tag => $tagfield);
258
259                                                                                                 # END $OP eq ADD_FORM
260 ################## ADD_VALIDATE ##################################
261 # called by add_form, used to insert/modify data in DB
262 } elsif ($op eq 'add_validate') {
263         $template->param(tagfield => "$input->param('tagfield')");
264 #       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)
265 #                                                                       values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
266     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)
267                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
268     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=?
269                                     where authtypecode=? and tagfield=? and tagsubfield=?");
270         my @tagsubfield = $input->param('tagsubfield');
271         my @liblibrarian        = $input->param('liblibrarian');
272         my @libopac             = $input->param('libopac');
273         my @kohafield           = ''.$input->param('kohafield');
274         my @tab                         = $input->param('tab');
275         my @seealso             = $input->param('seealso');
276         my @hidden;
277         my @ohidden             = $input->param('ohidden');
278         #my @ihidden            = $input->param('ihidden');
279         #my @ehidden            = $input->param('ehidden');
280         my @authorised_values   = $input->param('authorised_value');
281         my $authtypecode        = $input->param('authtypecode');
282         my @frameworkcodes      = $input->param('frameworkcode');
283         my @value_builder       =$input->param('value_builder');
284     my @defaultvalue = $input->param('defaultvalue');
285         for (my $i=0; $i<= $#tagsubfield ; $i++) {
286                 my $tagfield                    =$input->param('tagfield');
287                 my $tagsubfield         =$tagsubfield[$i];
288                 $tagsubfield="@" unless $tagsubfield ne '';
289                 my $liblibrarian                =$liblibrarian[$i];
290                 my $libopac                     =$libopac[$i];
291                 my $repeatable          =$input->param("repeatable$i")?1:0;
292                 my $mandatory           =$input->param("mandatory$i")?1:0;
293                 my $kohafield           =$kohafield[$i];
294                 my $tab                         =$tab[$i];
295                 my $seealso                             =$seealso[$i];
296                 my $authorised_value            =$authorised_values[$i];
297                 my $frameworkcode               =$frameworkcodes[$i];
298                 my $value_builder=$value_builder[$i];
299         my $defaultvalue = $defaultvalue[$i];
300                 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
301                 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
302                 my $isurl = $input->param("isurl$i")?1:0;
303                 if ($liblibrarian) {
304                         unless (C4::Context->config('demo') eq 1) {
305                                 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
306                                         $sth_update->execute(
307                                                 $authtypecode,
308                                                 $tagfield,
309                                                 $tagsubfield,
310                                                 $liblibrarian,
311                                                 $libopac,
312                                                 $repeatable,
313                                                 $mandatory,
314                                                 $kohafield,
315                                                 $tab,
316                                                 $seealso,
317                                                 $authorised_value,
318                                                 $frameworkcode,
319                                                 $value_builder,
320                                                 $hidden,
321                                                 $isurl,
322                         $defaultvalue,
323                                                 (
324                                                         $authtypecode,
325                                                         $tagfield,
326                                                         $tagsubfield
327                                                 ),
328                                         );
329                                 } else {
330                                         $sth_insert->execute(
331                                                 $authtypecode,
332                                                 $tagfield,
333                                                 $tagsubfield,
334                                                 $liblibrarian,
335                                                 $libopac,
336                                                 $repeatable,
337                                                 $mandatory,
338                                                 $kohafield,
339                                                 $tab,
340                                                 $seealso,
341                                                 $authorised_value,
342                                                 $frameworkcode,
343                                                 $value_builder,
344                                                 $hidden,
345                                                 $isurl,
346                         $defaultvalue,
347                                         );
348                                 }
349                         }
350                 }
351         }
352     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
353     exit;
354
355                                                                                                         # END $OP eq ADD_VALIDATE
356 ################## DELETE_CONFIRM ##################################
357 # called by default form, used to confirm deletion of data in DB
358 } elsif ($op eq 'delete_confirm') {
359         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
360         $sth->execute($tagfield,$tagsubfield,$authtypecode);
361         my $data=$sth->fetchrow_hashref;
362         $template->param(liblibrarian => $data->{'liblibrarian'},
363                                                         tagsubfield => $data->{'tagsubfield'},
364                                                         delete_link => $script_name,
365                                                         tagfield      =>$tagfield,
366                                                         tagsubfield => $tagsubfield,
367                                                         authtypecode => $authtypecode,
368                                                         );
369                                                                                                         # END $OP eq DELETE_CONFIRM
370 ################## DELETE_CONFIRMED ##################################
371 # called by delete_confirm, used to effectively confirm deletion of data in DB
372 } elsif ($op eq 'delete_confirmed') {
373         unless (C4::Context->config('demo') eq 1) {
374                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
375                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
376         }
377     print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
378     exit;
379                                                                                                         # END $OP eq DELETE_CONFIRMED
380 ################## DEFAULT ##################################
381 } else { # DEFAULT
382         my ($count,$results)=string_search($tagfield,$authtypecode);
383         my @loop_data = ();
384         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
385                 my %row_data;  # get a fresh hash for the row data
386                 $row_data{tagfield} = $results->[$i]{'tagfield'};
387                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
388                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
389                 $row_data{kohafield} = $results->[$i]{'kohafield'};
390                 $row_data{repeatable} = $results->[$i]{'repeatable'};
391                 $row_data{mandatory} = $results->[$i]{'mandatory'};
392                 $row_data{tab} = $results->[$i]{'tab'};
393                 $row_data{seealso} = $results->[$i]{'seealso'};
394                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
395                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
396                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
397                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
398                 $row_data{isurl}        = $results->[$i]{'isurl'};
399                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
400                 if ($row_data{tab} eq -1) {
401                         $row_data{subfield_ignored} = 1;
402                 }
403
404                 push(@loop_data, \%row_data);
405         }
406         $template->param(loop => \@loop_data);
407         $template->param(edit_tagfield => $tagfield,
408                 edit_authtypecode => $authtypecode);
409         
410         if ($offset>0) {
411                 my $prevpage = $offset-$pagesize;
412                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
413         }
414         if ($offset+$pagesize<$count) {
415                 my $nextpage =$offset+$pagesize;
416                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
417         }
418 } #---- END $OP eq DEFAULT
419 output_html_with_http_headers $input, $cookie, $template->output;