Bug 15478 - Update the remaining schema files that do not match the db schema
[koha.git] / admin / auth_subfields_structure.pl
blobf06ee4fff27df2e213656296b636ea24600448bd
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
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.
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.
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>.
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;
28 use Koha::Authority::Types;
30 use List::MoreUtils qw( uniq );
32 sub string_search {
33 my ($searchstring,$authtypecode)=@_;
34 my $dbh = C4::Context->dbh;
35 $searchstring=~ s/\'/\\\'/g;
36 my @data=split(' ',$searchstring);
37 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
38 $sth->execute("$searchstring%",$authtypecode);
39 my $results = $sth->fetchall_arrayref({});
40 return (scalar(@$results), $results);
43 sub auth_subfield_structure_exists {
44 my ($authtypecode, $tagfield, $tagsubfield) = @_;
45 my $dbh = C4::Context->dbh;
46 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
47 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
48 return @$rows > 0;
51 my $input = new CGI;
52 my $tagfield = $input->param('tagfield');
53 my $tagsubfield = $input->param('tagsubfield');
54 my $authtypecode = $input->param('authtypecode');
55 my $offset = $input->param('offset');
56 my $op = $input->param('op') || '';
57 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
59 my ($template, $borrowernumber, $cookie) = get_template_and_user(
60 { template_name => "admin/auth_subfields_structure.tt",
61 query => $input,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => { parameters => 'parameters_remaining_permissions' },
65 debug => 1,
68 my $pagesize = 30;
69 $tagfield =~ s/\,//g;
71 if ($op) {
72 $template->param(script_name => $script_name,
73 tagfield =>$tagfield,
74 authtypecode => $authtypecode,
75 $op => 1); # we show only the TMPL_VAR names $op
76 } else {
77 $template->param(script_name => $script_name,
78 tagfield =>$tagfield,
79 authtypecode => $authtypecode,
80 else => 1); # we show only the TMPL_VAR names $op
83 my $dbh = C4::Context->dbh;
84 ################## ADD_FORM ##################################
85 # called by default. Used to create form to add or modify a record
86 if ($op eq 'add_form') {
87 my $data;
88 my $more_subfields = $input->param("more_subfields")+1;
89 # builds kohafield tables
90 my @kohafields;
91 push @kohafields, "";
92 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
93 $sth2->execute;
94 while ((my $field) = $sth2->fetchrow_array) {
95 push @kohafields, "auth_header.".$field;
98 # build authorised value list
99 my $authorised_values = C4::Koha::GetAuthorisedValueCategories;
100 unshift @$authorised_values, '';
101 push @$authorised_values, 'branches';
102 push @$authorised_values, 'itemtypes';
104 # build thesaurus categories list
105 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
107 # build value_builder list
108 my @value_builder=('');
110 # read value_builder directory.
111 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
112 # on a standard install, /cgi-bin need to be added.
113 # test one, then the other
114 my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
115 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
116 $cgidir = C4::Context->config('intranetdir');
117 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
119 while (my $line = readdir(DIR)) {
120 if ( $line =~ /\.pl$/ &&
121 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
122 push (@value_builder,$line);
125 @value_builder= sort {$a cmp $b} @value_builder;
126 closedir DIR;
128 # build values list
129 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
130 $sth->execute($tagfield,$authtypecode);
131 my @loop_data = ();
132 my $i=0;
133 while ($data =$sth->fetchrow_hashref) {
135 my %row_data; # get a fresh hash for the row data
136 $row_data{defaultvalue} = $data->{defaultvalue};
137 $row_data{tab} = {
138 id => "tab$i",
139 default => $data->{'tab'},
141 $row_data{ohidden} = {
142 id => "ohidden$i",
143 default => $data->{'hidden'}
145 $row_data{tagsubfieldinput} = "<input type=\"hidden\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" id=\"tagsubfield\" />";
146 $row_data{tagsubfield} = $data->{'tagsubfield'};
147 $row_data{liblibrarian} = CGI::escapeHTML($data->{'liblibrarian'});
148 $row_data{libopac} = CGI::escapeHTML($data->{'libopac'});
149 $row_data{seealso} = CGI::escapeHTML($data->{'seealso'});
150 $row_data{kohafield} = {
151 id => "kohafield$i",
152 values => \@kohafields,
153 default => "$data->{'kohafield'}",
155 $row_data{authorised_value} = {
156 id => "authorised_value$i",
157 values => $authorised_values,
158 default => $data->{'authorised_value'},
160 $row_data{frameworkcode} = {
161 id => "frameworkcode$i",
162 values => \@authtypes,
163 default => $data->{'frameworkcode'},
165 $row_data{value_builder} = {
166 id => "value_builder$i",
167 values => \@value_builder,
168 default => $data->{'value_builder'},
171 $row_data{repeatable} = CGI::checkbox(-name=>"repeatable$i",
172 -checked => $data->{'repeatable'}?'checked':'',
173 -value => 1,
174 -label => '',
175 -id => "repeatable$i");
176 $row_data{mandatory} = CGI::checkbox(-name => "mandatory$i",
177 -checked => $data->{'mandatory'}?'checked':'',
178 -value => 1,
179 -label => '',
180 -id => "mandatory$i");
181 $row_data{hidden} = CGI::escapeHTML($data->{hidden}) ;
182 $row_data{isurl} = CGI::checkbox( -name => "isurl$i",
183 -id => "isurl$i",
184 -checked => $data->{'isurl'}?'checked':'',
185 -value => 1,
186 -label => '');
187 $row_data{row} = $i;
188 push(@loop_data, \%row_data);
189 $i++;
191 # add more_subfields empty lines for add if needed
192 for (my $i=1;$i<=$more_subfields;$i++) {
193 my %row_data; # get a fresh hash for the row data
194 $row_data{'new_subfield'} = 1;
195 $row_data{tab} = {
196 id => "tab$i",
197 default => $data->{'tab'},
199 $row_data{ohidden} = {
200 id => "ohidden$i",
201 default => $data->{'hidden'}
204 $row_data{tagsubfieldinput} = "<input type=\"text\" name=\"tagsubfield\" value=\"".$data->{'tagsubfield'}."\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
205 $row_data{tagsubfieldinput} =
206 "<label><input type=\"text\" name=\"tagsubfield\" value=\""
207 . $data->{'tagsubfield'}
208 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" /></label>";
209 $row_data{tagsubfield} = $data->{'tagsubfield'};
210 $row_data{liblibrarian} = "";
211 $row_data{libopac} = "";
212 $row_data{seealso} = "";
213 $row_data{hidden} = "000";
214 $row_data{repeatable} = CGI::checkbox( -name=> 'repeatable',
215 -id => "repeatable$i",
216 -checked => '',
217 -value => 1,
218 -label => '');
219 $row_data{mandatory} = CGI::checkbox( -name=> 'mandatory',
220 -id => "mandatory$i",
221 -checked => '',
222 -value => 1,
223 -label => '');
224 $row_data{isurl} = CGI::checkbox(-name => 'isurl',
225 -id => "isurl$i",
226 -checked => '',
227 -value => 1,
228 -label => '');
229 $row_data{kohafield} = {
230 id => "kohafield$i",
231 values => \@kohafields,
232 default => "",
234 $row_data{authorised_value} = {
235 id => "authorised_value",
236 values => $authorised_values,
237 default => "",
239 $row_data{frameworkcode} = {
240 id => "frameworkcode",
241 values => \@authtypes,
242 default => $data->{'frameworkcode'},
244 $row_data{value_builder} = {
245 id => "value_builder",
246 values => \@value_builder,
247 default => $data->{'value_builder'},
250 $row_data{row} = $i;
251 push(@loop_data, \%row_data);
253 $template->param('use_heading_flags_p' => 1);
254 $template->param('heading_edit_subfields_p' => 1);
255 $template->param(action => "Edit subfields",
256 tagfield => $tagfield,
257 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
258 loop => \@loop_data,
259 more_subfields => $more_subfields,
260 more_tag => $tagfield);
262 # END $OP eq ADD_FORM
263 ################## ADD_VALIDATE ##################################
264 # called by add_form, used to insert/modify data in DB
265 } elsif ($op eq 'add_validate') {
266 $template->param(tagfield => "$input->param('tagfield')");
267 # 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)
268 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
269 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)
270 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
271 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=?
272 where authtypecode=? and tagfield=? and tagsubfield=?");
273 my @tagsubfield = $input->param('tagsubfield');
274 my @liblibrarian = $input->param('liblibrarian');
275 my @libopac = $input->param('libopac');
276 my @kohafield = ''.$input->param('kohafield');
277 my @tab = $input->param('tab');
278 my @seealso = $input->param('seealso');
279 my @ohidden = $input->param('ohidden');
280 #my @ihidden = $input->param('ihidden');
281 #my @ehidden = $input->param('ehidden');
282 my @authorised_values = $input->param('authorised_value');
283 my $authtypecode = $input->param('authtypecode');
284 my @frameworkcodes = $input->param('frameworkcode');
285 my @value_builder =$input->param('value_builder');
286 my @defaultvalue = $input->param('defaultvalue');
287 for (my $i=0; $i<= $#tagsubfield ; $i++) {
288 my $tagfield =$input->param('tagfield');
289 my $tagsubfield =$tagsubfield[$i];
290 $tagsubfield="@" unless $tagsubfield ne '';
291 my $liblibrarian =$liblibrarian[$i];
292 my $libopac =$libopac[$i];
293 my $repeatable =$input->param("repeatable$i")?1:0;
294 my $mandatory =$input->param("mandatory$i")?1:0;
295 my $kohafield =$kohafield[$i];
296 my $tab =$tab[$i];
297 my $seealso =$seealso[$i];
298 my $authorised_value =$authorised_values[$i];
299 my $frameworkcode =$frameworkcodes[$i];
300 my $value_builder=$value_builder[$i];
301 my $defaultvalue = $defaultvalue[$i];
302 #my $hidden = $ohidden[$i].$ihidden[$i].$ehidden[$i]; #collate from 3 hiddens;
303 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
304 my $isurl = $input->param("isurl$i")?1:0;
305 if ($liblibrarian) {
306 unless (C4::Context->config('demo') eq 1) {
307 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
308 $sth_update->execute(
309 $authtypecode,
310 $tagfield,
311 $tagsubfield,
312 $liblibrarian,
313 $libopac,
314 $repeatable,
315 $mandatory,
316 $kohafield,
317 $tab,
318 $seealso,
319 $authorised_value,
320 $frameworkcode,
321 $value_builder,
322 $hidden,
323 $isurl,
324 $defaultvalue,
326 $authtypecode,
327 $tagfield,
328 $tagsubfield
331 } else {
332 $sth_insert->execute(
333 $authtypecode,
334 $tagfield,
335 $tagsubfield,
336 $liblibrarian,
337 $libopac,
338 $repeatable,
339 $mandatory,
340 $kohafield,
341 $tab,
342 $seealso,
343 $authorised_value,
344 $frameworkcode,
345 $value_builder,
346 $hidden,
347 $isurl,
348 $defaultvalue,
354 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
355 exit;
357 # END $OP eq ADD_VALIDATE
358 ################## DELETE_CONFIRM ##################################
359 # called by default form, used to confirm deletion of data in DB
360 } elsif ($op eq 'delete_confirm') {
361 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
362 $sth->execute($tagfield,$tagsubfield,$authtypecode);
363 my $data=$sth->fetchrow_hashref;
364 $template->param(liblibrarian => $data->{'liblibrarian'},
365 tagsubfield => $data->{'tagsubfield'},
366 delete_link => $script_name,
367 tagfield =>$tagfield,
368 tagsubfield => $tagsubfield,
369 authtypecode => $authtypecode,
371 # END $OP eq DELETE_CONFIRM
372 ################## DELETE_CONFIRMED ##################################
373 # called by delete_confirm, used to effectively confirm deletion of data in DB
374 } elsif ($op eq 'delete_confirmed') {
375 unless (C4::Context->config('demo') eq 1) {
376 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
377 $sth->execute($tagfield,$tagsubfield,$authtypecode);
379 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
380 exit;
381 # END $OP eq DELETE_CONFIRMED
382 ################## DEFAULT ##################################
383 } else { # DEFAULT
384 my ($count,$results)=string_search($tagfield,$authtypecode);
385 my @loop_data = ();
386 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
387 my %row_data; # get a fresh hash for the row data
388 $row_data{tagfield} = $results->[$i]{'tagfield'};
389 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
390 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
391 $row_data{kohafield} = $results->[$i]{'kohafield'};
392 $row_data{repeatable} = $results->[$i]{'repeatable'};
393 $row_data{mandatory} = $results->[$i]{'mandatory'};
394 $row_data{tab} = $results->[$i]{'tab'};
395 $row_data{seealso} = $results->[$i]{'seealso'};
396 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
397 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
398 $row_data{value_builder} = $results->[$i]{'value_builder'};
399 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
400 $row_data{isurl} = $results->[$i]{'isurl'};
401 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
402 if ($row_data{tab} eq -1) {
403 $row_data{subfield_ignored} = 1;
406 push(@loop_data, \%row_data);
408 $template->param(loop => \@loop_data);
409 $template->param(edit_tagfield => $tagfield,
410 edit_authtypecode => $authtypecode);
412 if ($offset>0) {
413 my $prevpage = $offset-$pagesize;
414 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
416 if ($offset+$pagesize<$count) {
417 my $nextpage =$offset+$pagesize;
418 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
420 } #---- END $OP eq DEFAULT
421 output_html_with_http_headers $input, $cookie, $template->output;