Bug 7677: UT: Remove $dbh->rollback
[koha.git] / admin / auth_subfields_structure.pl
blobffa74f2746645d3ac2ddc05173c17ae072fbaa2b
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 Modern::Perl;
21 use C4::Output;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Koha;
27 use Koha::Authority::Types;
29 use List::MoreUtils qw( uniq );
31 sub string_search {
32 my ($searchstring,$authtypecode)=@_;
33 my $dbh = C4::Context->dbh;
34 $searchstring=~ s/\'/\\\'/g;
35 my @data=split(' ',$searchstring);
36 my $sth=$dbh->prepare("Select * from auth_subfield_structure where (tagfield like ? and authtypecode=?) order by tagfield");
37 $sth->execute("$searchstring%",$authtypecode);
38 my $results = $sth->fetchall_arrayref({});
39 return (scalar(@$results), $results);
42 sub auth_subfield_structure_exists {
43 my ($authtypecode, $tagfield, $tagsubfield) = @_;
44 my $dbh = C4::Context->dbh;
45 my $sql = "select tagfield from auth_subfield_structure where authtypecode = ? and tagfield = ? and tagsubfield = ?";
46 my $rows = $dbh->selectall_arrayref($sql, {}, $authtypecode, $tagfield, $tagsubfield);
47 return @$rows > 0;
50 my $input = new CGI;
51 my $tagfield = $input->param('tagfield');
52 my $tagsubfield = $input->param('tagsubfield');
53 my $authtypecode = $input->param('authtypecode');
54 my $offset = $input->param('offset') || 0;
55 my $op = $input->param('op') || '';
56 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
58 my ($template, $borrowernumber, $cookie) = get_template_and_user(
59 { template_name => "admin/auth_subfields_structure.tt",
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => { parameters => 'parameters_remaining_permissions' },
64 debug => 1,
67 my $pagesize = 30;
68 $tagfield =~ s/\,//g;
70 if ($op) {
71 $template->param(script_name => $script_name,
72 tagfield =>$tagfield,
73 authtypecode => $authtypecode,
74 $op => 1); # we show only the TMPL_VAR names $op
75 } else {
76 $template->param(script_name => $script_name,
77 tagfield =>$tagfield,
78 authtypecode => $authtypecode,
79 else => 1); # we show only the TMPL_VAR names $op
82 my $dbh = C4::Context->dbh;
83 ################## ADD_FORM ##################################
84 # called by default. Used to create form to add or modify a record
85 if ($op eq 'add_form') {
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 = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
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->config('intranetdir') ."/cgi-bin";
112 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
113 $cgidir = C4::Context->config('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 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
119 push (@value_builder,$line);
122 @value_builder= sort {$a cmp $b} @value_builder;
123 closedir DIR;
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 ( my $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} = $data->{tab};
134 $row_data{ohidden} = $data->{'hidden'};
135 $row_data{tagsubfield} = $data->{'tagsubfield'};
136 $row_data{liblibrarian} = $data->{'liblibrarian'};
137 $row_data{libopac} = $data->{'libopac'};
138 $row_data{seealso} = $data->{'seealso'};
139 $row_data{kohafields} = \@kohafields;
140 $row_data{kohafield} = $data->{'kohafield'};
141 $row_data{authorised_values} = $authorised_values;
142 $row_data{authorised_value} = $data->{'authorised_value'};
143 $row_data{frameworkcodes} = \@authtypes;
144 $row_data{frameworkcode} = $data->{'frameworkcode'};
145 $row_data{value_builders} = \@value_builder;
146 $row_data{value_builder} = $data->{'value_builder'};
147 $row_data{repeatable} = $data->{repeatable};
148 $row_data{mandatory} = $data->{mandatory};
149 $row_data{hidden} = $data->{hidden};
150 $row_data{isurl} = $data->{isurl};
151 $row_data{row} = $i;
152 push( @loop_data, \%row_data );
153 $i++;
156 # Add a new row for the "New" tab
157 my %row_data; # get a fresh hash for the row data
158 $row_data{'new_subfield'} = 1;
159 $row_data{tab} = -1; # ignore
160 $row_data{ohidden} = 0; # show all
161 $row_data{tagsubfield} = "";
162 $row_data{liblibrarian} = "";
163 $row_data{libopac} = "";
164 $row_data{seealso} = "";
165 $row_data{hidden} = "000";
166 $row_data{repeatable} = 0;
167 $row_data{mandatory} = 0;
168 $row_data{isurl} = 0;
169 $row_data{kohafields} = \@kohafields,
170 $row_data{authorised_values} = $authorised_values;
171 $row_data{frameworkcodes} = \@authtypes;
172 $row_data{value_builders} = \@value_builder;
173 $row_data{row} = $i;
174 push( @loop_data, \%row_data );
176 $template->param('use_heading_flags_p' => 1);
177 $template->param('heading_edit_subfields_p' => 1);
178 $template->param(action => "Edit subfields",
179 tagfield => $tagfield,
180 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
181 loop => \@loop_data,
182 more_tag => $tagfield);
184 # END $OP eq ADD_FORM
185 ################## ADD_VALIDATE ##################################
186 # called by add_form, used to insert/modify data in DB
187 } elsif ($op eq 'add_validate') {
188 $template->param(tagfield => "$input->param('tagfield')");
189 # 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)
190 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
191 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)
192 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
193 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=?
194 where authtypecode=? and tagfield=? and tagsubfield=?");
195 my @tagsubfield = $input->multi_param('tagsubfield');
196 my @liblibrarian = $input->multi_param('liblibrarian');
197 my @libopac = $input->multi_param('libopac');
198 my @kohafield = ''.$input->param('kohafield');
199 my @tab = $input->multi_param('tab');
200 my @seealso = $input->multi_param('seealso');
201 my @ohidden = $input->multi_param('ohidden');
202 my @authorised_values = $input->multi_param('authorised_value');
203 my $authtypecode = $input->param('authtypecode');
204 my @frameworkcodes = $input->multi_param('frameworkcode');
205 my @value_builder =$input->multi_param('value_builder');
206 my @defaultvalue = $input->multi_param('defaultvalue');
207 for (my $i=0; $i<= $#tagsubfield ; $i++) {
208 my $tagfield =$input->param('tagfield');
209 my $tagsubfield =$tagsubfield[$i];
210 $tagsubfield="@" unless $tagsubfield ne '';
211 my $liblibrarian =$liblibrarian[$i];
212 my $libopac =$libopac[$i];
213 my $repeatable =$input->param("repeatable$i")?1:0;
214 my $mandatory =$input->param("mandatory$i")?1:0;
215 my $kohafield =$kohafield[$i];
216 my $tab =$tab[$i];
217 my $seealso =$seealso[$i];
218 my $authorised_value =$authorised_values[$i];
219 my $frameworkcode =$frameworkcodes[$i];
220 my $value_builder=$value_builder[$i];
221 my $defaultvalue = $defaultvalue[$i];
222 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
223 my $isurl = $input->param("isurl$i")?1:0;
224 if ($liblibrarian) {
225 unless (C4::Context->config('demo') or C4::Context->config('demo') eq 1) {
226 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
227 $sth_update->execute(
228 $authtypecode,
229 $tagfield,
230 $tagsubfield,
231 $liblibrarian,
232 $libopac,
233 $repeatable,
234 $mandatory,
235 $kohafield,
236 $tab,
237 $seealso,
238 $authorised_value,
239 $frameworkcode,
240 $value_builder,
241 $hidden,
242 $isurl,
243 $defaultvalue,
245 $authtypecode,
246 $tagfield,
247 $tagsubfield
250 } else {
251 $sth_insert->execute(
252 $authtypecode,
253 $tagfield,
254 $tagsubfield,
255 $liblibrarian,
256 $libopac,
257 $repeatable,
258 $mandatory,
259 $kohafield,
260 $tab,
261 $seealso,
262 $authorised_value,
263 $frameworkcode,
264 $value_builder,
265 $hidden,
266 $isurl,
267 $defaultvalue,
273 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
274 exit;
276 # END $OP eq ADD_VALIDATE
277 ################## DELETE_CONFIRM ##################################
278 # called by default form, used to confirm deletion of data in DB
279 } elsif ($op eq 'delete_confirm') {
280 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
281 $sth->execute($tagfield,$tagsubfield,$authtypecode);
282 my $data=$sth->fetchrow_hashref;
283 $template->param(liblibrarian => $data->{'liblibrarian'},
284 tagsubfield => $data->{'tagsubfield'},
285 delete_link => $script_name,
286 tagfield =>$tagfield,
287 tagsubfield => $tagsubfield,
288 authtypecode => $authtypecode,
290 # END $OP eq DELETE_CONFIRM
291 ################## DELETE_CONFIRMED ##################################
292 # called by delete_confirm, used to effectively confirm deletion of data in DB
293 } elsif ($op eq 'delete_confirmed') {
294 unless (C4::Context->config('demo') or C4::Context->config('demo') eq 1) {
295 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
296 $sth->execute($tagfield,$tagsubfield,$authtypecode);
298 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
299 exit;
300 # END $OP eq DELETE_CONFIRMED
301 ################## DEFAULT ##################################
302 } else { # DEFAULT
303 my ($count,$results)=string_search($tagfield,$authtypecode);
304 my @loop_data = ();
305 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
306 my %row_data; # get a fresh hash for the row data
307 $row_data{tagfield} = $results->[$i]{'tagfield'};
308 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
309 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
310 $row_data{kohafield} = $results->[$i]{'kohafield'};
311 $row_data{repeatable} = $results->[$i]{'repeatable'};
312 $row_data{mandatory} = $results->[$i]{'mandatory'};
313 $row_data{tab} = $results->[$i]{'tab'};
314 $row_data{seealso} = $results->[$i]{'seealso'};
315 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
316 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
317 $row_data{value_builder} = $results->[$i]{'value_builder'};
318 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
319 $row_data{isurl} = $results->[$i]{'isurl'};
320 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
321 if ($row_data{tab} eq -1) {
322 $row_data{subfield_ignored} = 1;
325 push(@loop_data, \%row_data);
327 $template->param(loop => \@loop_data);
328 $template->param(edit_tagfield => $tagfield,
329 edit_authtypecode => $authtypecode);
331 if ($offset>0) {
332 my $prevpage = $offset-$pagesize;
333 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
335 if ($offset+$pagesize<$count) {
336 my $nextpage =$offset+$pagesize;
337 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
339 } #---- END $OP eq DEFAULT
340 output_html_with_http_headers $input, $cookie, $template->output;