Bug 14048: Add syspref and atomic update
[koha.git] / admin / marc_subfields_structure.pl
blobbfaa3dc68140b639e705a42df11c3f330f16d527
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;
26 use Koha::Authority::Types;
28 use List::MoreUtils qw( uniq );
30 sub string_search {
31 my ( $searchstring, $frameworkcode ) = @_;
32 my $dbh = C4::Context->dbh;
33 $searchstring =~ s/\'/\\\'/g;
34 my @data = split( ' ', $searchstring );
35 my $count = @data;
36 my $sth =
37 $dbh->prepare(
38 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
40 $sth->execute( "$searchstring%", $frameworkcode );
41 my @results;
42 my $cnt = 0;
43 my $u = 1;
45 while ( my $data = $sth->fetchrow_hashref ) {
46 push( @results, $data );
47 $cnt++;
48 $u++;
50 $sth->finish;
51 return ( $cnt, \@results );
54 sub marc_subfield_structure_exists {
55 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
56 my $dbh = C4::Context->dbh;
57 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
58 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
59 return @$rows > 0;
62 my $input = new CGI;
63 my $tagfield = $input->param('tagfield');
64 my $tagsubfield = $input->param('tagsubfield');
65 my $frameworkcode = $input->param('frameworkcode');
66 my $pkfield = "tagfield";
67 my $offset = $input->param('offset') || 0;
68 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
70 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
72 template_name => "admin/marc_subfields_structure.tt",
73 query => $input,
74 type => "intranet",
75 authnotrequired => 0,
76 flagsrequired => { parameters => 'parameters_remaining_permissions' },
77 debug => 1,
80 my $cache = Koha::Cache->get_instance();
82 my $op = $input->param('op') || "";
83 $tagfield =~ s/\,//g;
85 if ($op) {
86 $template->param(
87 script_name => $script_name,
88 tagfield => $tagfield,
89 frameworkcode => $frameworkcode,
90 $op => 1
91 ); # we show only the TMPL_VAR names $op
93 else {
94 $template->param(
95 script_name => $script_name,
96 tagfield => $tagfield,
97 frameworkcode => $frameworkcode,
98 else => 1
99 ); # we show only the TMPL_VAR names $op
102 ################## ADD_FORM ##################################
103 # called by default. Used to create form to add or modify a record
104 if ( $op eq 'add_form' ) {
105 my $dbh = C4::Context->dbh;
107 # builds kohafield tables
108 my @kohafields;
109 push @kohafields, "";
110 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
111 $sth2->execute;
112 while ( ( my $field ) = $sth2->fetchrow_array ) {
113 push @kohafields, "biblio." . $field;
115 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
116 $sth2->execute;
117 while ( ( my $field ) = $sth2->fetchrow_array ) {
118 if ( $field eq 'notes' ) { $field = 'bnotes'; }
119 push @kohafields, "biblioitems." . $field;
121 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
122 $sth2->execute;
123 while ( ( my $field ) = $sth2->fetchrow_array ) {
124 push @kohafields, "items." . $field;
127 # build authorised value list
128 $sth2->finish;
129 $sth2 = $dbh->prepare("select distinct category from authorised_values");
130 $sth2->execute;
131 my @authorised_values;
132 push @authorised_values, "";
133 while ( ( my $category ) = $sth2->fetchrow_array ) {
134 push @authorised_values, $category;
136 push( @authorised_values, "branches" );
137 push( @authorised_values, "itemtypes" );
138 push( @authorised_values, "cn_source" );
140 # build thesaurus categories list
141 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
143 # build value_builder list
144 my @value_builder = ('');
146 # read value_builder directory.
147 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
148 # on a standard install, /cgi-bin need to be added.
149 # test one, then the other
150 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
151 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
152 $cgidir = C4::Context->config('intranetdir');
153 opendir( DIR, "$cgidir/cataloguing/value_builder" )
154 || die "can't opendir $cgidir/value_builder: $!";
156 while ( my $line = readdir(DIR) ) {
157 if ( $line =~ /\.pl$/ &&
158 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
159 push( @value_builder, $line );
162 @value_builder= sort {$a cmp $b} @value_builder;
163 closedir DIR;
165 # build values list
166 my $sth =
167 $dbh->prepare(
168 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
169 ); # and tagsubfield='$tagsubfield'");
170 $sth->execute( $tagfield, $frameworkcode );
171 my @loop_data = ();
172 my $i = 0;
173 while ( my $data = $sth->fetchrow_hashref ) {
174 my %row_data; # get a fresh hash for the row data
175 $row_data{defaultvalue} = $data->{defaultvalue};
176 $row_data{maxlength} = $data->{maxlength};
177 $row_data{tab} = $data->{tab};
178 $row_data{tagsubfield} = $data->{tagsubfield};
179 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@' ? '_' : $data->{'tagsubfield'};
180 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
181 $row_data{liblibrarian} = $data->{'liblibrarian'};
182 $row_data{libopac} = $data->{'libopac'};
183 $row_data{seealso} = $data->{'seealso'};
184 $row_data{kohafields} = \@kohafields;
185 $row_data{kohafield} = $data->{kohafield};
186 $row_data{authorised_values} = \@authorised_values;
187 $row_data{authorised_value} = $data->{authorised_value};
188 $row_data{value_builders} = \@value_builder;
189 $row_data{value_builder} = $data->{'value_builder'};
190 $row_data{authtypes} = \@authtypes;
191 $row_data{authtypecode} = $data->{'authtypecode'};
192 $row_data{repeatable} = $data->{repeatable};
193 $row_data{mandatory} = $data->{mandatory};
194 $row_data{hidden} = $data->{hidden};
195 $row_data{isurl} = $data->{isurl};
196 $row_data{row} = $i;
197 $row_data{link} = $data->{'link'};
198 push( @loop_data, \%row_data );
199 $i++;
202 # Add a new row for the "New" tab
203 my %row_data; # get a fresh hash for the row data
204 $row_data{'new_subfield'} = 1;
205 $row_data{'subfieldcode'} = '';
206 $row_data{'maxlength'} = 9999;
207 $row_data{tab} = -1; #ignore
208 $row_data{tagsubfield} = "";
209 $row_data{liblibrarian} = "";
210 $row_data{libopac} = "";
211 $row_data{seealso} = "";
212 $row_data{hidden} = "";
213 $row_data{repeatable} = 0;
214 $row_data{mandatory} = 0;
215 $row_data{isurl} = 0;
216 $row_data{kohafields} = \@kohafields;
217 $row_data{authorised_values} = \@authorised_values;
218 $row_data{value_builders} = \@value_builder;
219 $row_data{authtypes} = \@authtypes;
220 $row_data{link} = "";
221 $row_data{row} = $i;
222 push( @loop_data, \%row_data );
224 $template->param( 'use_heading_flags_p' => 1 );
225 $template->param( 'heading_edit_subfields_p' => 1 );
226 $template->param(
227 action => "Edit subfields",
228 tagfield => $tagfield,
229 loop => \@loop_data,
230 more_tag => $tagfield
233 # END $OP eq ADD_FORM
234 ################## ADD_VALIDATE ##################################
235 # called by add_form, used to insert/modify data in DB
237 elsif ( $op eq 'add_validate' ) {
238 my $dbh = C4::Context->dbh;
239 $template->param( tagfield => "$input->param('tagfield')" );
240 # my $sth = $dbh->prepare(
241 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
242 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
243 # );
244 my $sth_insert = $dbh->prepare(qq{
245 insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue,maxlength)
246 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
248 my $sth_update = $dbh->prepare(qq{
249 update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?, link=?, defaultvalue=?, maxlength=?
250 where tagfield=? and tagsubfield=? and frameworkcode=?
252 my @tagsubfield = $input->multi_param('tagsubfield');
253 my @liblibrarian = $input->multi_param('liblibrarian');
254 my @libopac = $input->multi_param('libopac');
255 my @kohafield = $input->multi_param('kohafield');
256 my @tab = $input->multi_param('tab');
257 my @seealso = $input->multi_param('seealso');
258 my @hidden = $input->multi_param('hidden');
259 my @authorised_values = $input->multi_param('authorised_value');
260 my @authtypecodes = $input->multi_param('authtypecode');
261 my @value_builder = $input->multi_param('value_builder');
262 my @link = $input->multi_param('link');
263 my @defaultvalue = $input->multi_param('defaultvalue');
264 my @maxlength = $input->multi_param('maxlength');
266 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
267 my $tagfield = $input->param('tagfield');
268 my $tagsubfield = $tagsubfield[$i];
269 $tagsubfield = "@" unless $tagsubfield ne '';
270 $tagsubfield = "@" if $tagsubfield eq '_';
271 my $liblibrarian = $liblibrarian[$i];
272 my $libopac = $libopac[$i];
273 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
274 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
275 my $kohafield = $kohafield[$i];
276 my $tab = $tab[$i];
277 my $seealso = $seealso[$i];
278 my $authorised_value = $authorised_values[$i];
279 my $authtypecode = $authtypecodes[$i];
280 my $value_builder = $value_builder[$i];
281 my $hidden = $hidden[$i]; #input->param("hidden$i");
282 my $isurl = $input->param("isurl$i") ? 1 : 0;
283 my $link = $link[$i];
284 my $defaultvalue = $defaultvalue[$i];
285 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
287 if (defined($liblibrarian) && $liblibrarian ne "") {
288 unless ( C4::Context->config('demo') or C4::Context->config('demo') eq 1 ) {
289 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
290 $sth_update->execute(
291 $tagfield,
292 $tagsubfield,
293 $liblibrarian,
294 $libopac,
295 $repeatable,
296 $mandatory,
297 $kohafield,
298 $tab,
299 $seealso,
300 $authorised_value,
301 $authtypecode,
302 $value_builder,
303 $hidden,
304 $isurl,
305 $frameworkcode,
306 $link,
307 $defaultvalue,
308 $maxlength,
310 $tagfield,
311 $tagsubfield,
312 $frameworkcode,
315 } else {
316 $sth_insert->execute(
317 $tagfield,
318 $tagsubfield,
319 $liblibrarian,
320 $libopac,
321 $repeatable,
322 $mandatory,
323 $kohafield,
324 $tab,
325 $seealso,
326 $authorised_value,
327 $authtypecode,
328 $value_builder,
329 $hidden,
330 $isurl,
331 $frameworkcode,
332 $link,
333 $defaultvalue,
334 $maxlength,
340 $sth_insert->finish;
341 $sth_update->finish;
342 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
343 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
344 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
346 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
347 exit;
349 # END $OP eq ADD_VALIDATE
350 ################## DELETE_CONFIRM ##################################
351 # called by default form, used to confirm deletion of data in DB
353 elsif ( $op eq 'delete_confirm' ) {
354 my $dbh = C4::Context->dbh;
355 my $sth =
356 $dbh->prepare(
357 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
360 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
361 my $data = $sth->fetchrow_hashref;
362 $sth->finish;
363 $template->param(
364 liblibrarian => $data->{'liblibrarian'},
365 tagsubfield => $data->{'tagsubfield'},
366 delete_link => $script_name,
367 tagfield => $tagfield,
368 tagsubfield => $tagsubfield,
369 frameworkcode => $frameworkcode,
372 # END $OP eq DELETE_CONFIRM
373 ################## DELETE_CONFIRMED ##################################
374 # called by delete_confirm, used to effectively confirm deletion of data in DB
376 elsif ( $op eq 'delete_confirmed' ) {
377 my $dbh = C4::Context->dbh;
378 unless ( C4::Context->config('demo') or C4::Context->config('demo') eq 1 ) {
379 my $sth =
380 $dbh->prepare(
381 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
383 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
384 $sth->finish;
386 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
387 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
388 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
389 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
390 exit;
392 # END $OP eq DELETE_CONFIRMED
393 ################## DEFAULT ##################################
395 else { # DEFAULT
396 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
397 my @loop_data = ();
398 for ( my $i = 0; $i < $count; $i++ ) {
399 my %row_data; # get a fresh hash for the row data
400 $row_data{tagfield} = $results->[$i]{'tagfield'};
401 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
402 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
403 $row_data{kohafield} = $results->[$i]{'kohafield'};
404 $row_data{repeatable} = $results->[$i]{'repeatable'};
405 $row_data{mandatory} = $results->[$i]{'mandatory'};
406 $row_data{tab} = $results->[$i]{'tab'};
407 $row_data{seealso} = $results->[$i]{'seealso'};
408 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
409 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
410 $row_data{value_builder} = $results->[$i]{'value_builder'};
411 $row_data{hidden} = $results->[$i]{'hidden'};
412 $row_data{isurl} = $results->[$i]{'isurl'};
413 $row_data{link} = $results->[$i]{'link'};
415 if ( $row_data{tab} eq -1 ) {
416 $row_data{subfield_ignored} = 1;
419 push( @loop_data, \%row_data );
421 $template->param( loop => \@loop_data );
422 $template->param(
423 edit_tagfield => $tagfield,
424 edit_frameworkcode => $frameworkcode
427 } #---- END $OP eq DEFAULT
429 output_html_with_http_headers $input, $cookie, $template->output;