Bug 16224: Fix t/db_dependent/Reports_Guided.t
[koha.git] / admin / auth_subfields_structure.pl
blob3b2aa0a5d578e397f0c6b1bea942cea0be038f3a
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;
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);
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;
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') || 0;
52 my $op = $input->param('op') || '';
53 my $script_name = "/cgi-bin/koha/admin/auth_subfields_structure.pl";
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,
64 my $pagesize = 30;
65 $tagfield =~ s/\,//g;
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
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 # builds kohafield tables
84 my @kohafields;
85 push @kohafields, "";
86 my $sth2=$dbh->prepare("SHOW COLUMNS from auth_header");
87 $sth2->execute;
88 while ((my $field) = $sth2->fetchrow_array) {
89 push @kohafields, "auth_header.".$field;
92 # build authorised value list
93 my $authorised_values = C4::Koha::GetAuthorisedValueCategories;
94 unshift @$authorised_values, '';
95 push @$authorised_values, 'branches';
96 push @$authorised_values, 'itemtypes';
98 # build thesaurus categories list
99 my @authtypes = (sort keys %{C4::Koha::getauthtypes()});
101 # build value_builder list
102 my @value_builder=('');
104 # read value_builder directory.
105 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
106 # on a standard install, /cgi-bin need to be added.
107 # test one, then the other
108 my $cgidir = C4::Context->config('intranetdir') ."/cgi-bin";
109 unless (opendir(DIR, "$cgidir/cataloguing/value_builder")) {
110 $cgidir = C4::Context->config('intranetdir');
111 opendir(DIR, "$cgidir/cataloguing/value_builder") || die "can't opendir $cgidir/value_builder: $!";
113 while (my $line = readdir(DIR)) {
114 if ( $line =~ /\.pl$/ &&
115 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
116 push (@value_builder,$line);
119 @value_builder= sort {$a cmp $b} @value_builder;
120 closedir DIR;
122 # build values list
123 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and authtypecode=?"); # and tagsubfield='$tagsubfield'");
124 $sth->execute($tagfield,$authtypecode);
125 my @loop_data = ();
126 my $i=0;
127 while ( my $data = $sth->fetchrow_hashref ) {
128 my %row_data; # get a fresh hash for the row data
129 $row_data{defaultvalue} = $data->{defaultvalue};
130 $row_data{tab} = $data->{tab};
131 $row_data{ohidden} = $data->{'hidden'};
132 $row_data{tagsubfield} = $data->{'tagsubfield'};
133 $row_data{liblibrarian} = $data->{'liblibrarian'};
134 $row_data{libopac} = $data->{'libopac'};
135 $row_data{seealso} = $data->{'seealso'};
136 $row_data{kohafields} = \@kohafields;
137 $row_data{kohafield} = $data->{'kohafield'};
138 $row_data{authorised_values} = $authorised_values;
139 $row_data{authorised_value} = $data->{'authorised_value'};
140 $row_data{frameworkcodes} = \@authtypes;
141 $row_data{frameworkcode} = $data->{'frameworkcode'};
142 $row_data{value_builders} = \@value_builder;
143 $row_data{value_builder} = $data->{'value_builder'};
144 $row_data{repeatable} = $data->{repeatable};
145 $row_data{mandatory} = $data->{mandatory};
146 $row_data{hidden} = $data->{hidden};
147 $row_data{isurl} = $data->{isurl};
148 $row_data{row} = $i;
149 push( @loop_data, \%row_data );
150 $i++;
153 # Add a new row for the "New" tab
154 my %row_data; # get a fresh hash for the row data
155 $row_data{'new_subfield'} = 1;
156 $row_data{tab} = -1; # ignore
157 $row_data{ohidden} = 0; # show all
158 $row_data{tagsubfield} = "";
159 $row_data{liblibrarian} = "";
160 $row_data{libopac} = "";
161 $row_data{seealso} = "";
162 $row_data{hidden} = "000";
163 $row_data{repeatable} = 0;
164 $row_data{mandatory} = 0;
165 $row_data{isurl} = 0;
166 $row_data{kohafields} = \@kohafields,
167 $row_data{authorised_values} = $authorised_values;
168 $row_data{frameworkcodes} = \@authtypes;
169 $row_data{value_builders} = \@value_builder;
170 $row_data{row} = $i;
171 push( @loop_data, \%row_data );
173 $template->param('use_heading_flags_p' => 1);
174 $template->param('heading_edit_subfields_p' => 1);
175 $template->param(action => "Edit subfields",
176 tagfield => $tagfield,
177 tagfieldinput => "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\" />",
178 loop => \@loop_data,
179 more_tag => $tagfield);
181 # END $OP eq ADD_FORM
182 ################## ADD_VALIDATE ##################################
183 # called by add_form, used to insert/modify data in DB
184 } elsif ($op eq 'add_validate') {
185 $template->param(tagfield => "$input->param('tagfield')");
186 # 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)
187 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
188 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)
189 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
190 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=?
191 where authtypecode=? and tagfield=? and tagsubfield=?");
192 my @tagsubfield = $input->param('tagsubfield');
193 my @liblibrarian = $input->param('liblibrarian');
194 my @libopac = $input->param('libopac');
195 my @kohafield = ''.$input->param('kohafield');
196 my @tab = $input->param('tab');
197 my @seealso = $input->param('seealso');
198 my @ohidden = $input->param('ohidden');
199 my @authorised_values = $input->param('authorised_value');
200 my $authtypecode = $input->param('authtypecode');
201 my @frameworkcodes = $input->param('frameworkcode');
202 my @value_builder =$input->param('value_builder');
203 my @defaultvalue = $input->param('defaultvalue');
204 for (my $i=0; $i<= $#tagsubfield ; $i++) {
205 my $tagfield =$input->param('tagfield');
206 my $tagsubfield =$tagsubfield[$i];
207 $tagsubfield="@" unless $tagsubfield ne '';
208 my $liblibrarian =$liblibrarian[$i];
209 my $libopac =$libopac[$i];
210 my $repeatable =$input->param("repeatable$i")?1:0;
211 my $mandatory =$input->param("mandatory$i")?1:0;
212 my $kohafield =$kohafield[$i];
213 my $tab =$tab[$i];
214 my $seealso =$seealso[$i];
215 my $authorised_value =$authorised_values[$i];
216 my $frameworkcode =$frameworkcodes[$i];
217 my $value_builder=$value_builder[$i];
218 my $defaultvalue = $defaultvalue[$i];
219 my $hidden = $ohidden[$i]; #collate from 3 hiddens;
220 my $isurl = $input->param("isurl$i")?1:0;
221 if ($liblibrarian) {
222 unless (C4::Context->config('demo') or C4::Context->config('demo') eq 1) {
223 if (auth_subfield_structure_exists($authtypecode, $tagfield, $tagsubfield)) {
224 $sth_update->execute(
225 $authtypecode,
226 $tagfield,
227 $tagsubfield,
228 $liblibrarian,
229 $libopac,
230 $repeatable,
231 $mandatory,
232 $kohafield,
233 $tab,
234 $seealso,
235 $authorised_value,
236 $frameworkcode,
237 $value_builder,
238 $hidden,
239 $isurl,
240 $defaultvalue,
242 $authtypecode,
243 $tagfield,
244 $tagsubfield
247 } else {
248 $sth_insert->execute(
249 $authtypecode,
250 $tagfield,
251 $tagsubfield,
252 $liblibrarian,
253 $libopac,
254 $repeatable,
255 $mandatory,
256 $kohafield,
257 $tab,
258 $seealso,
259 $authorised_value,
260 $frameworkcode,
261 $value_builder,
262 $hidden,
263 $isurl,
264 $defaultvalue,
270 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
271 exit;
273 # END $OP eq ADD_VALIDATE
274 ################## DELETE_CONFIRM ##################################
275 # called by default form, used to confirm deletion of data in DB
276 } elsif ($op eq 'delete_confirm') {
277 my $sth=$dbh->prepare("select * from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
278 $sth->execute($tagfield,$tagsubfield,$authtypecode);
279 my $data=$sth->fetchrow_hashref;
280 $template->param(liblibrarian => $data->{'liblibrarian'},
281 tagsubfield => $data->{'tagsubfield'},
282 delete_link => $script_name,
283 tagfield =>$tagfield,
284 tagsubfield => $tagsubfield,
285 authtypecode => $authtypecode,
287 # END $OP eq DELETE_CONFIRM
288 ################## DELETE_CONFIRMED ##################################
289 # called by delete_confirm, used to effectively confirm deletion of data in DB
290 } elsif ($op eq 'delete_confirmed') {
291 unless (C4::Context->config('demo') or C4::Context->config('demo') eq 1) {
292 my $sth=$dbh->prepare("delete from auth_subfield_structure where tagfield=? and tagsubfield=? and authtypecode=?");
293 $sth->execute($tagfield,$tagsubfield,$authtypecode);
295 print $input->redirect("/cgi-bin/koha/admin/auth_subfields_structure.pl?tagfield=$tagfield&amp;authtypecode=$authtypecode");
296 exit;
297 # END $OP eq DELETE_CONFIRMED
298 ################## DEFAULT ##################################
299 } else { # DEFAULT
300 my ($count,$results)=string_search($tagfield,$authtypecode);
301 my @loop_data = ();
302 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
303 my %row_data; # get a fresh hash for the row data
304 $row_data{tagfield} = $results->[$i]{'tagfield'};
305 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
306 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
307 $row_data{kohafield} = $results->[$i]{'kohafield'};
308 $row_data{repeatable} = $results->[$i]{'repeatable'};
309 $row_data{mandatory} = $results->[$i]{'mandatory'};
310 $row_data{tab} = $results->[$i]{'tab'};
311 $row_data{seealso} = $results->[$i]{'seealso'};
312 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
313 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
314 $row_data{value_builder} = $results->[$i]{'value_builder'};
315 $row_data{hidden} = $results->[$i]{'hidden'} if($results->[$i]{'hidden'} gt "000") ;
316 $row_data{isurl} = $results->[$i]{'isurl'};
317 $row_data{delete} = "$script_name?op=delete_confirm&amp;tagfield=$tagfield&amp;tagsubfield=".$results->[$i]{'tagsubfield'}."&amp;authtypecode=$authtypecode";
318 if ($row_data{tab} eq -1) {
319 $row_data{subfield_ignored} = 1;
322 push(@loop_data, \%row_data);
324 $template->param(loop => \@loop_data);
325 $template->param(edit_tagfield => $tagfield,
326 edit_authtypecode => $authtypecode);
328 if ($offset>0) {
329 my $prevpage = $offset-$pagesize;
330 $template->param(prev =>"<a href=\"$script_name?offset=$prevpage\">");
332 if ($offset+$pagesize<$count) {
333 my $nextpage =$offset+$pagesize;
334 $template->param(next => "<a href=\"$script_name?offset=$nextpage\">");
336 } #---- END $OP eq DEFAULT
337 output_html_with_http_headers $input, $cookie, $template->output;