Bug 15524: (QA follow-up) Change Can[Book|Item]BeReserved to return hashref, pass...
[koha.git] / admin / auth_subfields_structure.pl
blob14408594f31e99f66aeda51c56400f484e9d1fb2
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;
28 use Koha::AuthorisedValues;
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 $offset = 0 if not defined $offset or $offset < 0;
57 my $op = $input->param('op') || '';
58 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
60 my ($template, $borrowernumber, $cookie) = get_template_and_user(
61 { template_name => "admin/auth_subfields_structure.tt",
62 query => $input,
63 type => "intranet",
64 authnotrequired => 0,
65 flagsrequired => { parameters => 'parameters_remaining_permissions' },
66 debug => 1,
69 my $pagesize = 30;
70 $tagfield =~ s/\,//g;
72 if ($op) {
73 $template->param(script_name => $script_name,
74 tagfield =>$tagfield,
75 authtypecode => $authtypecode,
76 $op => 1); # we show only the TMPL_VAR names $op
77 } else {
78 $template->param(script_name => $script_name,
79 tagfield =>$tagfield,
80 authtypecode => $authtypecode,
81 else => 1); # we show only the TMPL_VAR names $op
84 my $dbh = C4::Context->dbh;
85 ################## ADD_FORM ##################################
86 # called by default. Used to create form to add or modify a record
87 if ($op eq 'add_form') {
88 # builds kohafield tables
89 my @kohafields;
90 push @kohafields, "";
91 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
92 $sth2->execute;
93 while ((my $field) = $sth2->fetchrow_array) {
94 push @kohafields, "auth_header.".$field;
97 # build authorised value category list
98 my @authorised_value_categories = Koha::AuthorisedValues->new->categories;
99 unshift @authorised_value_categories, '';
100 push @authorised_value_categories, 'branches';
101 push @authorised_value_categories, 'itemtypes';
103 # build thesaurus categories list
104 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
106 # build value_builder list
107 my @value_builder=('');
109 # read value_builder directory.
110 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
111 # on a standard install, /cgi-bin need to be added.
112 # test one, then the other
113 my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
114 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
115 $cgidir = C4::Context->config('intranetdir');
116 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
118 while (my $line = readdir(DIR)) {
119 if ( $line =~ /\.pl$/ &&
120 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
121 push (@value_builder,$line);
124 @value_builder= sort {$a cmp $b} @value_builder;
125 closedir DIR;
127 # build values list
128 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
129 $sth->execute($tagfield,$authtypecode);
130 my @loop_data = ();
131 my $i=0;
132 while ( my $data = $sth->fetchrow_hashref ) {
133 my %row_data; # get a fresh hash for the row data
134 $row_data{defaultvalue} = $data->{defaultvalue};
135 $row_data{tab} = $data->{tab};
136 $row_data{ohidden} = $data->{'hidden'};
137 $row_data{tagsubfield} = $data->{'tagsubfield'};
138 $row_data{liblibrarian} = $data->{'liblibrarian'};
139 $row_data{libopac} = $data->{'libopac'};
140 $row_data{seealso} = $data->{'seealso'};
141 $row_data{kohafields} = \@kohafields;
142 $row_data{kohafield} = $data->{'kohafield'};
143 $row_data{authorised_values} = \@authorised_value_categories;
144 $row_data{authorised_value} = $data->{'authorised_value'};
145 $row_data{frameworkcodes} = \@authtypes;
146 $row_data{frameworkcode} = $data->{'frameworkcode'};
147 $row_data{value_builders} = \@value_builder;
148 $row_data{value_builder} = $data->{'value_builder'};
149 $row_data{repeatable} = $data->{repeatable};
150 $row_data{mandatory} = $data->{mandatory};
151 $row_data{hidden} = $data->{hidden};
152 $row_data{isurl} = $data->{isurl};
153 $row_data{row} = $i;
154 push( @loop_data, \%row_data );
155 $i++;
158 # Add a new row for the "New" tab
159 my %row_data; # get a fresh hash for the row data
160 $row_data{'new_subfield'} = 1;
161 $row_data{tab} = -1; # ignore
162 $row_data{ohidden} = 0; # show all
163 $row_data{tagsubfield} = "";
164 $row_data{liblibrarian} = "";
165 $row_data{libopac} = "";
166 $row_data{seealso} = "";
167 $row_data{hidden} = "000";
168 $row_data{repeatable} = 0;
169 $row_data{mandatory} = 0;
170 $row_data{isurl} = 0;
171 $row_data{kohafields} = \@kohafields,
172 $row_data{authorised_values} = \@authorised_value_categories;
173 $row_data{frameworkcodes} = \@authtypes;
174 $row_data{value_builders} = \@value_builder;
175 $row_data{row} = $i;
176 push( @loop_data, \%row_data );
178 $template->param('use_heading_flags_p' => 1);
179 $template->param('heading_edit_subfields_p' => 1);
180 $template->param(action => "Edit subfields",
181 tagfield => $tagfield,
182 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
183 loop => \@loop_data,
184 more_tag => $tagfield);
186 # END $OP eq ADD_FORM
187 ################## ADD_VALIDATE ##################################
188 # called by add_form, used to insert/modify data in DB
189 } elsif ($op eq 'add_validate') {
190 $template->param(tagfield => "$input->param('tagfield')");
191 # 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)
192 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
193 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)
194 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
195 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=?
196 where authtypecode=? and tagfield=? and tagsubfield=?");
197 my @tagsubfield = $input->multi_param('tagsubfield');
198 my @liblibrarian = $input->multi_param('liblibrarian');
199 my @libopac = $input->multi_param('libopac');
200 my @kohafield = ''.$input->param('kohafield');
201 my @tab = $input->multi_param('tab');
202 my @seealso = $input->multi_param('seealso');
203 my @ohidden = $input->multi_param('ohidden');
204 my @authorised_value_categories = $input->multi_param('authorised_value');
205 my $authtypecode = $input->param('authtypecode');
206 my @frameworkcodes = $input->multi_param('frameworkcode');
207 my @value_builder =$input->multi_param('value_builder');
208 my @defaultvalue = $input->multi_param('defaultvalue');
209 for (my $i=0; $i<= $#tagsubfield ; $i++) {
210 my $tagfield =$input->param('tagfield');
211 my $tagsubfield =$tagsubfield[$i];
212 $tagsubfield="@" unless $tagsubfield ne '';
213 my $liblibrarian =$liblibrarian[$i];
214 my $libopac =$libopac[$i];
215 my $repeatable =$input->param("repeatable$i")?1:0;
216 my $mandatory =$input->param("mandatory$i")?1:0;
217 my $kohafield =$kohafield[$i];
218 my $tab =$tab[$i];
219 my $seealso =$seealso[$i];
220 my $authorised_value = $authorised_value_categories[$i];
221 my $frameworkcode =$frameworkcodes[$i];
222 my $value_builder=$value_builder[$i];
223 my $defaultvalue = $defaultvalue[$i];
224 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
225 my $isurl = $input->param("isurl$i")?1:0;
226 if ($liblibrarian) {
227 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
228 $sth_update->execute(
229 $authtypecode,
230 $tagfield,
231 $tagsubfield,
232 $liblibrarian,
233 $libopac,
234 $repeatable,
235 $mandatory,
236 $kohafield,
237 $tab,
238 $seealso,
239 $authorised_value,
240 $frameworkcode,
241 $value_builder,
242 $hidden,
243 $isurl,
244 $defaultvalue,
246 $authtypecode,
247 $tagfield,
248 $tagsubfield
251 } else {
252 $sth_insert->execute(
253 $authtypecode,
254 $tagfield,
255 $tagsubfield,
256 $liblibrarian,
257 $libopac,
258 $repeatable,
259 $mandatory,
260 $kohafield,
261 $tab,
262 $seealso,
263 $authorised_value,
264 $frameworkcode,
265 $value_builder,
266 $hidden,
267 $isurl,
268 $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 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
295 $sth->execute($tagfield,$tagsubfield,$authtypecode);
296 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
297 exit;
298 # END $OP eq DELETE_CONFIRMED
299 ################## DEFAULT ##################################
300 } else { # DEFAULT
301 my ($count,$results)=string_search($tagfield,$authtypecode);
302 my @loop_data = ();
303 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
304 my %row_data; # get a fresh hash for the row data
305 $row_data{tagfield} = $results->[$i]{'tagfield'};
306 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
307 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
308 $row_data{kohafield} = $results->[$i]{'kohafield'};
309 $row_data{repeatable} = $results->[$i]{'repeatable'};
310 $row_data{mandatory} = $results->[$i]{'mandatory'};
311 $row_data{tab} = $results->[$i]{'tab'};
312 $row_data{seealso} = $results->[$i]{'seealso'};
313 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
314 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
315 $row_data{value_builder} = $results->[$i]{'value_builder'};
316 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
317 $row_data{isurl} = $results->[$i]{'isurl'};
318 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
319 if ($row_data{tab} eq -1) {
320 $row_data{subfield_ignored} = 1;
323 push(@loop_data, \%row_data);
325 $template->param(loop => \@loop_data);
326 $template->param(edit_tagfield => $tagfield,
327 edit_authtypecode => $authtypecode);
329 if ($offset>0) {
330 my $prevpage = $offset-$pagesize;
331 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
333 if ($offset+$pagesize<$count) {
334 my $nextpage =$offset+$pagesize;
335 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
337 } #---- END $OP eq DEFAULT
338 output_html_with_http_headers $input, $cookie, $template->output;