Bug 15478 - Update the remaining schema files that do not match the db schema
[koha.git] / admin / marc_subfields_structure.pl
blobe6d2231aca2a4613182efcc5a3bf750bf8df9c15
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;
27 use Koha::Authority::Types;
29 use List::MoreUtils qw( uniq );
31 sub string_search {
32 my ( $searchstring, $frameworkcode ) = @_;
33 my $dbh = C4::Context->dbh;
34 $searchstring =~ s/\'/\\\'/g;
35 my @data = split( ' ', $searchstring );
36 my $count = @data;
37 my $sth =
38 $dbh->prepare(
39 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
41 $sth->execute( "$searchstring%", $frameworkcode );
42 my @results;
43 my $cnt = 0;
44 my $u = 1;
46 while ( my $data = $sth->fetchrow_hashref ) {
47 push( @results, $data );
48 $cnt++;
49 $u++;
51 $sth->finish;
52 return ( $cnt, \@results );
55 sub marc_subfield_structure_exists {
56 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
57 my $dbh = C4::Context->dbh;
58 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
59 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
60 return @$rows > 0;
63 my $input = new CGI;
64 my $tagfield = $input->param('tagfield');
65 my $tagsubfield = $input->param('tagsubfield');
66 my $frameworkcode = $input->param('frameworkcode');
67 my $pkfield = "tagfield";
68 my $offset = $input->param('offset');
69 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
71 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
73 template_name => "admin/marc_subfields_structure.tt",
74 query => $input,
75 type => "intranet",
76 authnotrequired => 0,
77 flagsrequired => { parameters => 'parameters_remaining_permissions' },
78 debug => 1,
81 my $cache = Koha::Cache->get_instance();
83 my $op = $input->param('op');
84 $tagfield =~ s/\,//g;
86 if ($op) {
87 $template->param(
88 script_name => $script_name,
89 tagfield => $tagfield,
90 frameworkcode => $frameworkcode,
91 $op => 1
92 ); # we show only the TMPL_VAR names $op
94 else {
95 $template->param(
96 script_name => $script_name,
97 tagfield => $tagfield,
98 frameworkcode => $frameworkcode,
99 else => 1
100 ); # we show only the TMPL_VAR names $op
103 ################## ADD_FORM ##################################
104 # called by default. Used to create form to add or modify a record
105 if ( $op eq 'add_form' ) {
106 my $data;
107 my $dbh = C4::Context->dbh;
108 my $more_subfields = $input->param("more_subfields") + 1;
110 # builds kohafield tables
111 my @kohafields;
112 push @kohafields, "";
113 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
114 $sth2->execute;
115 while ( ( my $field ) = $sth2->fetchrow_array ) {
116 push @kohafields, "biblio." . $field;
118 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
119 $sth2->execute;
120 while ( ( my $field ) = $sth2->fetchrow_array ) {
121 if ( $field eq 'notes' ) { $field = 'bnotes'; }
122 push @kohafields, "biblioitems." . $field;
124 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
125 $sth2->execute;
126 while ( ( my $field ) = $sth2->fetchrow_array ) {
127 push @kohafields, "items." . $field;
130 # build authorised value list
131 $sth2->finish;
132 $sth2 = $dbh->prepare("select distinct category from authorised_values");
133 $sth2->execute;
134 my @authorised_values;
135 push @authorised_values, "";
136 while ( ( my $category ) = $sth2->fetchrow_array ) {
137 push @authorised_values, $category;
139 push( @authorised_values, "branches" );
140 push( @authorised_values, "itemtypes" );
141 push( @authorised_values, "cn_source" );
143 # build thesaurus categories list
144 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
146 # build value_builder list
147 my @value_builder = ('');
149 # read value_builder directory.
150 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
151 # on a standard install, /cgi-bin need to be added.
152 # test one, then the other
153 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
154 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
155 $cgidir = C4::Context->config('intranetdir');
156 opendir( DIR, "$cgidir/cataloguing/value_builder" )
157 || die "can't opendir $cgidir/value_builder: $!";
159 while ( my $line = readdir(DIR) ) {
160 if ( $line =~ /\.pl$/ &&
161 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
162 push( @value_builder, $line );
165 @value_builder= sort {$a cmp $b} @value_builder;
166 closedir DIR;
168 # build values list
169 my $sth =
170 $dbh->prepare(
171 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
172 ); # and tagsubfield='$tagsubfield'");
173 $sth->execute( $tagfield, $frameworkcode );
174 my @loop_data = ();
175 my $i = 0;
176 while ( $data = $sth->fetchrow_hashref ) {
177 my %row_data; # get a fresh hash for the row data
178 $row_data{defaultvalue} = $data->{defaultvalue};
179 $row_data{maxlength} = $data->{maxlength};
180 $row_data{tab} = {
181 id => "tab$i",
182 default => $data->{'tab'},
185 $row_data{tagsubfield} =
186 $data->{'tagsubfield'}
187 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
188 . $data->{'tagsubfield'}
189 . "\" id=\"tagsubfield\" />";
190 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
191 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
192 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
193 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
194 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
195 $row_data{kohafield} = {
196 id => "kohafield$i",
197 values => \@kohafields,
198 default => "$data->{'kohafield'}",
200 $row_data{authorised_value} = {
201 id => "authorised_value$i",
202 values => \@authorised_values,
203 default => $data->{'authorised_value'},
205 $row_data{value_builder} = {
206 id => "value_builder$i",
207 values => \@value_builder,
208 default => $data->{'value_builder'},
210 $row_data{authtypes} = {
211 id => "authtypecode$i",
212 values => \@authtypes,
213 default => $data->{'authtypecode'},
215 $row_data{repeatable} = CGI::checkbox(
216 -name => "repeatable$i",
217 -checked => $data->{'repeatable'} ? 'checked' : '',
218 -value => 1,
219 -label => '',
220 -id => "repeatable$i"
222 $row_data{mandatory} = CGI::checkbox(
223 -name => "mandatory$i",
224 -checked => $data->{'mandatory'} ? 'checked' : '',
225 -value => 1,
226 -label => '',
227 -id => "mandatory$i"
229 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
230 $row_data{isurl} = CGI::checkbox(
231 -name => "isurl$i",
232 -id => "isurl$i",
233 -checked => $data->{'isurl'} ? 'checked' : '',
234 -value => 1,
235 -label => ''
237 $row_data{row} = $i;
238 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
239 push( @loop_data, \%row_data );
240 $i++;
243 # add more_subfields empty lines for add if needed
244 my %row_data; # get a fresh hash for the row data
245 $row_data{'new_subfield'} = 1;
246 $row_data{'subfieldcode'} = '';
247 $row_data{'maxlength'} = 9999;
249 $row_data{tab} = {
250 id => "tab$i",
251 default => $data->{'tab'},
253 $row_data{tagsubfield} =
254 "<input type=\"text\" name=\"tagsubfield\" value=\""
255 . $data->{'tagsubfield'}
256 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
257 $row_data{liblibrarian} = "";
258 $row_data{libopac} = "";
259 $row_data{seealso} = "";
260 $row_data{kohafield} = {
261 id => "kohafield$i",
262 values => \@kohafields,
263 default => "$data->{'kohafield'}",
265 $row_data{hidden} = "";
266 $row_data{repeatable} = CGI::checkbox(
267 -name => "repeatable$i",
268 -id => "repeatable$i",
269 -checked => '',
270 -value => 1,
271 -label => ''
273 $row_data{mandatory} = CGI::checkbox(
274 -name => "mandatory$i",
275 -id => "mandatory$i",
276 -checked => '',
277 -value => 1,
278 -label => ''
280 $row_data{isurl} = CGI::checkbox(
281 -name => "isurl$i",
282 -id => "isurl$i",
283 -checked => '',
284 -value => 1,
285 -label => ''
287 $row_data{value_builder} = {
288 id => "value_builder$i",
289 values => \@value_builder,
290 default => $data->{'value_builder'},
292 $row_data{authorised_value} = {
293 id => "authorised_value$i",
294 values => \@authorised_values,
295 default => $data->{'authorised_value'},
297 $row_data{authtypes} = {
298 id => "authtypecode$i",
299 values => \@authtypes,
300 default => $data->{'authtypecode'},
302 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
303 $row_data{row} = $i;
304 push( @loop_data, \%row_data );
306 $template->param( 'use_heading_flags_p' => 1 );
307 $template->param( 'heading_edit_subfields_p' => 1 );
308 $template->param(
309 action => "Edit subfields",
310 tagfield => $tagfield,
311 loop => \@loop_data,
312 more_subfields => $more_subfields,
313 more_tag => $tagfield
316 # END $OP eq ADD_FORM
317 ################## ADD_VALIDATE ##################################
318 # called by add_form, used to insert/modify data in DB
320 elsif ( $op eq 'add_validate' ) {
321 my $dbh = C4::Context->dbh;
322 $template->param( tagfield => "$input->param('tagfield')" );
323 # my $sth = $dbh->prepare(
324 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
325 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
326 # );
327 my $sth_insert = $dbh->prepare(qq{
328 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)
329 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
331 my $sth_update = $dbh->prepare(qq{
332 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=?
333 where tagfield=? and tagsubfield=? and frameworkcode=?
335 my @tagsubfield = $input->param('tagsubfield');
336 my @liblibrarian = $input->param('liblibrarian');
337 my @libopac = $input->param('libopac');
338 my @kohafield = $input->param('kohafield');
339 my @tab = $input->param('tab');
340 my @seealso = $input->param('seealso');
341 my @hidden = $input->param('hidden');
342 my @authorised_values = $input->param('authorised_value');
343 my @authtypecodes = $input->param('authtypecode');
344 my @value_builder = $input->param('value_builder');
345 my @link = $input->param('link');
346 my @defaultvalue = $input->param('defaultvalue');
347 my @maxlength = $input->param('maxlength');
349 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
350 my $tagfield = $input->param('tagfield');
351 my $tagsubfield = $tagsubfield[$i];
352 $tagsubfield = "@" unless $tagsubfield ne '';
353 $tagsubfield = "@" if $tagsubfield eq '_';
354 my $liblibrarian = $liblibrarian[$i];
355 my $libopac = $libopac[$i];
356 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
357 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
358 my $kohafield = $kohafield[$i];
359 my $tab = $tab[$i];
360 my $seealso = $seealso[$i];
361 my $authorised_value = $authorised_values[$i];
362 my $authtypecode = $authtypecodes[$i];
363 my $value_builder = $value_builder[$i];
364 my $hidden = $hidden[$i]; #input->param("hidden$i");
365 my $isurl = $input->param("isurl$i") ? 1 : 0;
366 my $link = $link[$i];
367 my $defaultvalue = $defaultvalue[$i];
368 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
370 if (defined($liblibrarian) && $liblibrarian ne "") {
371 unless ( C4::Context->config('demo') eq 1 ) {
372 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
373 $sth_update->execute(
374 $tagfield,
375 $tagsubfield,
376 $liblibrarian,
377 $libopac,
378 $repeatable,
379 $mandatory,
380 $kohafield,
381 $tab,
382 $seealso,
383 $authorised_value,
384 $authtypecode,
385 $value_builder,
386 $hidden,
387 $isurl,
388 $frameworkcode,
389 $link,
390 $defaultvalue,
391 $maxlength,
393 $tagfield,
394 $tagsubfield,
395 $frameworkcode,
398 } else {
399 $sth_insert->execute(
400 $tagfield,
401 $tagsubfield,
402 $liblibrarian,
403 $libopac,
404 $repeatable,
405 $mandatory,
406 $kohafield,
407 $tab,
408 $seealso,
409 $authorised_value,
410 $authtypecode,
411 $value_builder,
412 $hidden,
413 $isurl,
414 $frameworkcode,
415 $link,
416 $defaultvalue,
417 $maxlength,
423 $sth_insert->finish;
424 $sth_update->finish;
425 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
426 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
428 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
429 exit;
431 # END $OP eq ADD_VALIDATE
432 ################## DELETE_CONFIRM ##################################
433 # called by default form, used to confirm deletion of data in DB
435 elsif ( $op eq 'delete_confirm' ) {
436 my $dbh = C4::Context->dbh;
437 my $sth =
438 $dbh->prepare(
439 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
442 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
443 my $data = $sth->fetchrow_hashref;
444 $sth->finish;
445 $template->param(
446 liblibrarian => $data->{'liblibrarian'},
447 tagsubfield => $data->{'tagsubfield'},
448 delete_link => $script_name,
449 tagfield => $tagfield,
450 tagsubfield => $tagsubfield,
451 frameworkcode => $frameworkcode,
454 # END $OP eq DELETE_CONFIRM
455 ################## DELETE_CONFIRMED ##################################
456 # called by delete_confirm, used to effectively confirm deletion of data in DB
458 elsif ( $op eq 'delete_confirmed' ) {
459 my $dbh = C4::Context->dbh;
460 unless ( C4::Context->config('demo') eq 1 ) {
461 my $sth =
462 $dbh->prepare(
463 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
465 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
466 $sth->finish;
468 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
469 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
470 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
471 exit;
473 # END $OP eq DELETE_CONFIRMED
474 ################## DEFAULT ##################################
476 else { # DEFAULT
477 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
478 my @loop_data = ();
479 for ( my $i = 0; $i < $count; $i++ ) {
480 my %row_data; # get a fresh hash for the row data
481 $row_data{tagfield} = $results->[$i]{'tagfield'};
482 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
483 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
484 $row_data{kohafield} = $results->[$i]{'kohafield'};
485 $row_data{repeatable} = $results->[$i]{'repeatable'};
486 $row_data{mandatory} = $results->[$i]{'mandatory'};
487 $row_data{tab} = $results->[$i]{'tab'};
488 $row_data{seealso} = $results->[$i]{'seealso'};
489 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
490 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
491 $row_data{value_builder} = $results->[$i]{'value_builder'};
492 $row_data{hidden} = $results->[$i]{'hidden'};
493 $row_data{isurl} = $results->[$i]{'isurl'};
494 $row_data{link} = $results->[$i]{'link'};
496 if ( $row_data{tab} eq -1 ) {
497 $row_data{subfield_ignored} = 1;
500 push( @loop_data, \%row_data );
502 $template->param( loop => \@loop_data );
503 $template->param(
504 edit_tagfield => $tagfield,
505 edit_frameworkcode => $frameworkcode
508 } #---- END $OP eq DEFAULT
510 output_html_with_http_headers $input, $cookie, $template->output;