Bug 12467 [QA Followup] - Unit Tests
[koha.git] / admin / auth_subfields_structure.pl
blob96088bd40093a6fda0191d361cd3159a4410d173
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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.
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 use C4::Koha;
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);
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;
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";
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,
65 my $pagesize = 30;
66 $tagfield =~ s/\,//g;
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
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;
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';
101 # build thesaurus categories list
102 my @authtypes = (sort keys getauthtypes);
104 # build value_builder list
105 my @value_builder=('');
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: $!";
116 while (my $line = readdir(DIR)) {
117 if ($line =~ /\.pl$/) {
118 push (@value_builder,$line);
121 @value_builder= sort {$a cmp $b} @value_builder;
122 closedir DIR;
124 # build values list
125 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
126 $sth->execute($tagfield,$authtypecode);
127 my @loop_data = ();
128 my $i=0;
129 while ($data =$sth->fetchrow_hashref) {
131 my %row_data; # get a fresh hash for the row data
132 $row_data{defaultvalue} = $data->{defaultvalue};
133 $row_data{tab} = {
134 id => "tab$i",
135 default => $data->{'tab'},
137 $row_data{ohidden} = {
138 id => "ohidden$i",
139 default => $data->{'hidden'}
141 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
142 $row_data{tagsubfield} = $data->{'tagsubfield'};
143 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
144 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
145 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
146 $row_data{kohafield} = {
147 id => "kohafield$i",
148 values => \@kohafields,
149 default => "$data->{'kohafield'}",
151 $row_data{authorised_value} = {
152 id => "authorised_value$i",
153 values => $authorised_values,
154 default => $data->{'authorised_value'},
156 $row_data{frameworkcode} = {
157 id => "frameworkcode$i",
158 values => \@authtypes,
159 default => $data->{'frameworkcode'},
161 $row_data{value_builder} = {
162 id => "value_builder$i",
163 values => \@value_builder,
164 default => $data->{'value_builder'},
167 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
168 -checked => $data->{'repeatable'}?'checked':'',
169 -value => 1,
170 -label => '',
171 -id => "repeatable$i");
172 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
173 -checked => $data->{'mandatory'}?'checked':'',
174 -value => 1,
175 -label => '',
176 -id => "mandatory$i");
177 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
178 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
179 -id => "isurl$i",
180 -checked => $data->{'isurl'}?'checked':'',
181 -value => 1,
182 -label => '');
183 $row_data{row} = $i;
184 push(@loop_data, \%row_data);
185 $i++;
187 # add more_subfields empty lines for add if needed
188 for (my $i=1;$i<=$more_subfields;$i++) {
189 my %row_data; # get a fresh hash for the row data
190 $row_data{'new_subfield'} = 1;
191 $row_data{tab} = {
192 id => "tab$i",
193 default => $data->{'tab'},
195 $row_data{ohidden} = {
196 id => "ohidden$i",
197 default => $data->{'hidden'}
200 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
201 $row_data{tagsubfieldinput} =
202 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
203 . $data->{'tagsubfield'}
204 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
205 $row_data{tagsubfield} = $data->{'tagsubfield'};
206 $row_data{liblibrarian} = "";
207 $row_data{libopac} = "";
208 $row_data{seealso} = "";
209 $row_data{hidden} = "000";
210 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
211 -id => "repeatable$i",
212 -checked => '',
213 -value => 1,
214 -label => '');
215 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
216 -id => "mandatory$i",
217 -checked => '',
218 -value => 1,
219 -label => '');
220 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
221 -id => "isurl$i",
222 -checked => '',
223 -value => 1,
224 -label => '');
225 $row_data{kohafield} = {
226 id => "kohafield$i",
227 values => \@kohafields,
228 default => "",
230 $row_data{authorised_value} = {
231 id => "authorised_value",
232 values => $authorised_values,
233 default => "",
235 $row_data{frameworkcode} = {
236 id => "frameworkcode",
237 values => \@authtypes,
238 default => $data->{'frameworkcode'},
240 $row_data{value_builder} = {
241 id => "value_builder",
242 values => \@value_builder,
243 default => $data->{'value_builder'},
246 $row_data{row} = $i;
247 push(@loop_data, \%row_data);
249 $template->param('use_heading_flags_p' => 1);
250 $template->param('heading_edit_subfields_p' => 1);
251 $template->param(action => "Edit subfields",
252 tagfield => $tagfield,
253 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
254 loop => \@loop_data,
255 more_subfields => $more_subfields,
256 more_tag => $tagfield);
258 # END $OP eq ADD_FORM
259 ################## ADD_VALIDATE ##################################
260 # called by add_form, used to insert/modify data in DB
261 } elsif ($op eq 'add_validate') {
262 $template->param(tagfield => "$input->param('tagfield')");
263 # 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)
264 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
265 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)
266 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
267 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=?
268 where authtypecode=? and tagfield=? and tagsubfield=?");
269 my @tagsubfield = $input->param('tagsubfield');
270 my @liblibrarian = $input->param('liblibrarian');
271 my @libopac = $input->param('libopac');
272 my @kohafield = ''.$input->param('kohafield');
273 my @tab = $input->param('tab');
274 my @seealso = $input->param('seealso');
275 my @hidden;
276 my @ohidden = $input->param('ohidden');
277 #my @ihidden = $input->param('ihidden');
278 #my @ehidden = $input->param('ehidden');
279 my @authorised_values = $input->param('authorised_value');
280 my $authtypecode = $input->param('authtypecode');
281 my @frameworkcodes = $input->param('frameworkcode');
282 my @value_builder =$input->param('value_builder');
283 my @defaultvalue = $input->param('defaultvalue');
284 for (my $i=0; $i<= $#tagsubfield ; $i++) {
285 my $tagfield =$input->param('tagfield');
286 my $tagsubfield =$tagsubfield[$i];
287 $tagsubfield="@" unless $tagsubfield ne '';
288 my $liblibrarian =$liblibrarian[$i];
289 my $libopac =$libopac[$i];
290 my $repeatable =$input->param("repeatable$i")?1:0;
291 my $mandatory =$input->param("mandatory$i")?1:0;
292 my $kohafield =$kohafield[$i];
293 my $tab =$tab[$i];
294 my $seealso =$seealso[$i];
295 my $authorised_value =$authorised_values[$i];
296 my $frameworkcode =$frameworkcodes[$i];
297 my $value_builder=$value_builder[$i];
298 my $defaultvalue = $defaultvalue[$i];
299 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
300 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
301 my $isurl = $input->param("isurl$i")?1:0;
302 if ($liblibrarian) {
303 unless (C4::Context->config('demo') eq 1) {
304 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
305 $sth_update->execute(
306 $authtypecode,
307 $tagfield,
308 $tagsubfield,
309 $liblibrarian,
310 $libopac,
311 $repeatable,
312 $mandatory,
313 $kohafield,
314 $tab,
315 $seealso,
316 $authorised_value,
317 $frameworkcode,
318 $value_builder,
319 $hidden,
320 $isurl,
321 $defaultvalue,
323 $authtypecode,
324 $tagfield,
325 $tagsubfield
328 } else {
329 $sth_insert->execute(
330 $authtypecode,
331 $tagfield,
332 $tagsubfield,
333 $liblibrarian,
334 $libopac,
335 $repeatable,
336 $mandatory,
337 $kohafield,
338 $tab,
339 $seealso,
340 $authorised_value,
341 $frameworkcode,
342 $value_builder,
343 $hidden,
344 $isurl,
345 $defaultvalue,
351 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
352 exit;
354 # END $OP eq ADD_VALIDATE
355 ################## DELETE_CONFIRM ##################################
356 # called by default form, used to confirm deletion of data in DB
357 } elsif ($op eq 'delete_confirm') {
358 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
359 $sth->execute($tagfield,$tagsubfield,$authtypecode);
360 my $data=$sth->fetchrow_hashref;
361 $template->param(liblibrarian => $data->{'liblibrarian'},
362 tagsubfield => $data->{'tagsubfield'},
363 delete_link => $script_name,
364 tagfield =>$tagfield,
365 tagsubfield => $tagsubfield,
366 authtypecode => $authtypecode,
368 # END $OP eq DELETE_CONFIRM
369 ################## DELETE_CONFIRMED ##################################
370 # called by delete_confirm, used to effectively confirm deletion of data in DB
371 } elsif ($op eq 'delete_confirmed') {
372 unless (C4::Context->config('demo') eq 1) {
373 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
374 $sth->execute($tagfield,$tagsubfield,$authtypecode);
376 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=auth_subfields_structure.pl?tagfield=$tagfield&authtypecode=$authtypecode\"></html>";
377 exit;
378 $template->param(tagfield => $tagfield);
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;
404 push(@loop_data, \%row_data);
406 $template->param(loop => \@loop_data);
407 $template->param(edit_tagfield => $tagfield,
408 edit_authtypecode => $authtypecode);
410 if ($offset>0) {
411 my $prevpage = $offset-$pagesize;
412 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
414 if ($offset+$pagesize<$count) {
415 my $nextpage =$offset+$pagesize;
416 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
418 } #---- END $OP eq DEFAULT
419 output_html_with_http_headers $input, $cookie, $template->output;