Bug 8148: (regression tests) C4::Auth_with_ldap needs unit tests
[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.tt",
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{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
152                 $row_data{tagsubfield} = $data->{'tagsubfield'};
153                 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
154                 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
155                 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
156         $row_data{kohafield} = {
157                     id      => "kohafield$i",
158                     values  => \@kohafields,
159                     default => "$data->{'kohafield'}",
160                     };
161         $row_data{authorised_value} = {
162                     id      => "authorised_value$i",
163                     values  => \@authorised_values,
164                     default => $data->{'authorised_value'},
165         };
166         $row_data{frameworkcode} = {
167                     id      => "frameworkcode$i",
168                     values  => \@authtypes,
169                     default => $data->{'frameworkcode'},
170         };
171         $row_data{value_builder} = {
172                     id      => "value_builder$i",
173                     values  => \@value_builder,
174                     default => $data->{'value_builder'},
175         };
176                 
177                 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
178         -checked => $data->{'repeatable'}?'checked':'',
179         -value => 1,
180         -label => '',
181         -id => "repeatable$i");
182                 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
183         -checked => $data->{'mandatory'}?'checked':'',
184         -value => 1,
185         -label => '',
186         -id => "mandatory$i");
187                 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
188                 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
189                         -id => "isurl$i",
190                         -checked => $data->{'isurl'}?'checked':'',
191                         -value => 1,
192                         -label => '');
193                 $row_data{row} = $i;
194                 push(@loop_data, \%row_data);
195                 $i++;
196         }
197         # add more_subfields empty lines for add if needed
198         for (my $i=1;$i<=$more_subfields;$i++) {
199                 my %row_data;  # get a fresh hash for the row data
200         $row_data{'new_subfield'} = 1;
201         $row_data{tab} = {
202                     id      => "tab$i",
203                     default => $data->{'tab'},
204                     };
205         $row_data{ohidden} = {
206                     id      => "ohidden$i",
207                     default => $data->{'hidden'}
208                     };
209
210                 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
211                 $row_data{tagsubfieldinput} = 
212                         "<label><input type=\"text\" name=\"tagsubfield\" value=\""
213                         . $data->{'tagsubfield'}
214                         . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
215                 $row_data{tagsubfield} = $data->{'tagsubfield'};
216                 $row_data{liblibrarian} = "";
217                 $row_data{libopac} = "";
218                 $row_data{seealso} = "";
219                 $row_data{hidden} = "000";
220                 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
221                                 -id => "repeatable$i",
222                                 -checked => '',
223                                 -value => 1,
224                                 -label => '');
225                 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
226                         -id => "mandatory$i",
227                         -checked => '',
228                         -value => 1,
229                         -label => '');
230                 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
231                         -id => "isurl$i",
232                         -checked => '',
233                         -value => 1,
234                         -label => '');
235         $row_data{kohafield} = {
236                     id      => "kohafield$i",
237                     values  => \@kohafields,
238                     default => "",
239                     };
240         $row_data{authorised_value} = {
241                     id      => "authorised_value",
242                     values  => \@authorised_values,
243                     default => "",
244         };
245         $row_data{frameworkcode} = {
246                     id      => "frameworkcode",
247                     values  => \@authtypes,
248                     default => $data->{'frameworkcode'},
249         };
250         $row_data{value_builder} = {
251                     id      => "value_builder",
252                     values  => \@value_builder,
253                     default => $data->{'value_builder'},
254         };
255
256                 $row_data{row} = $i;
257                 push(@loop_data, \%row_data);
258         }
259         $template->param('use_heading_flags_p' => 1);
260         $template->param('heading_edit_subfields_p' => 1);
261         $template->param(action => "Edit subfields",
262                                                         tagfield => $tagfield,
263                                                         tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
264                                                         loop => \@loop_data,
265                                                         more_subfields => $more_subfields,
266                                                         more_tag => $tagfield);
267
268                                                                                                 # END $OP eq ADD_FORM
269 ################## ADD_VALIDATE ##################################
270 # called by add_form, used to insert/modify data in DB
271 } elsif ($op eq 'add_validate') {
272         $template->param(tagfield => "$input->param('tagfield')");
273 #       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)
274 #                                                                       values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
275     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)
276                                     values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
277     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=?
278                                     where authtypecode=? and tagfield=? and tagsubfield=?");
279         my @tagsubfield = $input->param('tagsubfield');
280         my @liblibrarian        = $input->param('liblibrarian');
281         my @libopac             = $input->param('libopac');
282         my @kohafield           = ''.$input->param('kohafield');
283         my @tab                         = $input->param('tab');
284         my @seealso             = $input->param('seealso');
285         my @hidden;
286         my @ohidden             = $input->param('ohidden');
287         #my @ihidden            = $input->param('ihidden');
288         #my @ehidden            = $input->param('ehidden');
289         my @authorised_values   = $input->param('authorised_value');
290         my $authtypecode        = $input->param('authtypecode');
291         my @frameworkcodes      = $input->param('frameworkcode');
292         my @value_builder       =$input->param('value_builder');
293     my @defaultvalue = $input->param('defaultvalue');
294         for (my $i=0; $i<= $#tagsubfield ; $i++) {
295                 my $tagfield                    =$input->param('tagfield');
296                 my $tagsubfield         =$tagsubfield[$i];
297                 $tagsubfield="@" unless $tagsubfield ne '';
298                 my $liblibrarian                =$liblibrarian[$i];
299                 my $libopac                     =$libopac[$i];
300                 my $repeatable          =$input->param("repeatable$i")?1:0;
301                 my $mandatory           =$input->param("mandatory$i")?1:0;
302                 my $kohafield           =$kohafield[$i];
303                 my $tab                         =$tab[$i];
304                 my $seealso                             =$seealso[$i];
305                 my $authorised_value            =$authorised_values[$i];
306                 my $frameworkcode               =$frameworkcodes[$i];
307                 my $value_builder=$value_builder[$i];
308         my $defaultvalue = $defaultvalue[$i];
309                 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
310                 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
311                 my $isurl = $input->param("isurl$i")?1:0;
312                 if ($liblibrarian) {
313                         unless (C4::Context->config('demo') eq 1) {
314                                 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
315                                         $sth_update->execute(
316                                                 $authtypecode,
317                                                 $tagfield,
318                                                 $tagsubfield,
319                                                 $liblibrarian,
320                                                 $libopac,
321                                                 $repeatable,
322                                                 $mandatory,
323                                                 $kohafield,
324                                                 $tab,
325                                                 $seealso,
326                                                 $authorised_value,
327                                                 $frameworkcode,
328                                                 $value_builder,
329                                                 $hidden,
330                                                 $isurl,
331                         $defaultvalue,
332                                                 (
333                                                         $authtypecode,
334                                                         $tagfield,
335                                                         $tagsubfield
336                                                 ),
337                                         );
338                                 } else {
339                                         $sth_insert->execute(
340                                                 $authtypecode,
341                                                 $tagfield,
342                                                 $tagsubfield,
343                                                 $liblibrarian,
344                                                 $libopac,
345                                                 $repeatable,
346                                                 $mandatory,
347                                                 $kohafield,
348                                                 $tab,
349                                                 $seealso,
350                                                 $authorised_value,
351                                                 $frameworkcode,
352                                                 $value_builder,
353                                                 $hidden,
354                                                 $isurl,
355                         $defaultvalue,
356                                         );
357                                 }
358                         }
359                 }
360         }
361         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
362         exit;
363
364                                                                                                         # END $OP eq ADD_VALIDATE
365 ################## DELETE_CONFIRM ##################################
366 # called by default form, used to confirm deletion of data in DB
367 } elsif ($op eq 'delete_confirm') {
368         my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
369         $sth->execute($tagfield,$tagsubfield,$authtypecode);
370         my $data=$sth->fetchrow_hashref;
371         $template->param(liblibrarian => $data->{'liblibrarian'},
372                                                         tagsubfield => $data->{'tagsubfield'},
373                                                         delete_link => $script_name,
374                                                         tagfield      =>$tagfield,
375                                                         tagsubfield => $tagsubfield,
376                                                         authtypecode => $authtypecode,
377                                                         );
378                                                                                                         # END $OP eq DELETE_CONFIRM
379 ################## DELETE_CONFIRMED ##################################
380 # called by delete_confirm, used to effectively confirm deletion of data in DB
381 } elsif ($op eq 'delete_confirmed') {
382         unless (C4::Context->config('demo') eq 1) {
383                 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
384                 $sth->execute($tagfield,$tagsubfield,$authtypecode);
385         }
386         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
387         exit;
388         $template->param(tagfield => $tagfield);
389                                                                                                         # END $OP eq DELETE_CONFIRMED
390 ################## DEFAULT ##################################
391 } else { # DEFAULT
392         my ($count,$results)=string_search($tagfield,$authtypecode);
393         my @loop_data = ();
394         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
395                 my %row_data;  # get a fresh hash for the row data
396                 $row_data{tagfield} = $results->[$i]{'tagfield'};
397                 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
398                 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
399                 $row_data{kohafield} = $results->[$i]{'kohafield'};
400                 $row_data{repeatable} = $results->[$i]{'repeatable'};
401                 $row_data{mandatory} = $results->[$i]{'mandatory'};
402                 $row_data{tab} = $results->[$i]{'tab'};
403                 $row_data{seealso} = $results->[$i]{'seealso'};
404                 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
405                 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
406                 $row_data{value_builder}        = $results->[$i]{'value_builder'};
407                 $row_data{hidden}       = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
408                 $row_data{isurl}        = $results->[$i]{'isurl'};
409                 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
410                 if ($row_data{tab} eq -1) {
411                         $row_data{subfield_ignored} = 1;
412                 }
413
414                 push(@loop_data, \%row_data);
415         }
416         $template->param(loop => \@loop_data);
417         $template->param(edit_tagfield => $tagfield,
418                 edit_authtypecode => $authtypecode);
419         
420         if ($offset>0) {
421                 my $prevpage = $offset-$pagesize;
422                 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
423         }
424         if ($offset+$pagesize<$count) {
425                 my $nextpage =$offset+$pagesize;
426                 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
427         }
428 } #---- END $OP eq DEFAULT
429 output_html_with_http_headers $input, $cookie, $template->output;