Bug 7843: Followup - fix broken url
[koha.git] / admin / marc_subfields_structure.pl
blobc8b7815fec6804f0727d3772b6493f4f80ac4aad
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;
28 sub string_search {
29 my ( $searchstring, $frameworkcode ) = @_;
30 my $dbh = C4::Context->dbh;
31 $searchstring =~ s/\'/\\\'/g;
32 my @data = split( ' ', $searchstring );
33 my $count = @data;
34 my $sth =
35 $dbh->prepare(
36 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
38 $sth->execute( "$searchstring%", $frameworkcode );
39 my @results;
40 my $cnt = 0;
41 my $u = 1;
43 while ( my $data = $sth->fetchrow_hashref ) {
44 push( @results, $data );
45 $cnt++;
46 $u++;
48 $sth->finish;
49 return ( $cnt, \@results );
52 sub marc_subfield_structure_exists {
53 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
54 my $dbh = C4::Context->dbh;
55 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
56 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
57 return @$rows > 0;
60 my $input = new CGI;
61 my $tagfield = $input->param('tagfield');
62 my $tagsubfield = $input->param('tagsubfield');
63 my $frameworkcode = $input->param('frameworkcode');
64 my $pkfield = "tagfield";
65 my $offset = $input->param('offset');
66 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
68 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70 template_name => "admin/marc_subfields_structure.tt",
71 query => $input,
72 type => "intranet",
73 authnotrequired => 0,
74 flagsrequired => { parameters => 'parameters_remaining_permissions' },
75 debug => 1,
78 my $cache = Koha::Cache->get_instance();
80 my $op = $input->param('op');
81 $tagfield =~ s/\,//g;
83 if ($op) {
84 $template->param(
85 script_name => $script_name,
86 tagfield => $tagfield,
87 frameworkcode => $frameworkcode,
88 $op => 1
89 ); # we show only the TMPL_VAR names $op
91 else {
92 $template->param(
93 script_name => $script_name,
94 tagfield => $tagfield,
95 frameworkcode => $frameworkcode,
96 else => 1
97 ); # we show only the TMPL_VAR names $op
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or modify a record
102 if ( $op eq 'add_form' ) {
103 my $data;
104 my $dbh = C4::Context->dbh;
105 my $more_subfields = $input->param("more_subfields") + 1;
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 $sth2->finish;
142 $sth2 = $dbh->prepare("select authtypecode from auth_types");
143 $sth2->execute;
144 my @authtypes;
145 push @authtypes, "";
146 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
147 push @authtypes, $authtypecode;
150 # build value_builder list
151 my @value_builder = ('');
153 # read value_builder directory.
154 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
155 # on a standard install, /cgi-bin need to be added.
156 # test one, then the other
157 my $cgidir = C4::Context->intranetdir . "/cgi-bin";
158 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
159 $cgidir = C4::Context->intranetdir;
160 opendir( DIR, "$cgidir/cataloguing/value_builder" )
161 || die "can't opendir $cgidir/value_builder: $!";
163 while ( my $line = readdir(DIR) ) {
164 if ( $line =~ /\.pl$/ &&
165 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
166 push( @value_builder, $line );
169 @value_builder= sort {$a cmp $b} @value_builder;
170 closedir DIR;
172 # build values list
173 my $sth =
174 $dbh->prepare(
175 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
176 ); # and tagsubfield='$tagsubfield'");
177 $sth->execute( $tagfield, $frameworkcode );
178 my @loop_data = ();
179 my $i = 0;
180 while ( $data = $sth->fetchrow_hashref ) {
181 my %row_data; # get a fresh hash for the row data
182 $row_data{defaultvalue} = $data->{defaultvalue};
183 $row_data{maxlength} = $data->{maxlength};
184 $row_data{tab} = {
185 id => "tab$i",
186 default => $data->{'tab'},
189 $row_data{tagsubfield} =
190 $data->{'tagsubfield'}
191 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
192 . $data->{'tagsubfield'}
193 . "\" id=\"tagsubfield\" />";
194 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
195 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
196 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
197 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
198 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
199 $row_data{kohafield} = {
200 id => "kohafield$i",
201 values => \@kohafields,
202 default => "$data->{'kohafield'}",
204 $row_data{authorised_value} = {
205 id => "authorised_value$i",
206 values => \@authorised_values,
207 default => $data->{'authorised_value'},
209 $row_data{value_builder} = {
210 id => "value_builder$i",
211 values => \@value_builder,
212 default => $data->{'value_builder'},
214 $row_data{authtypes} = {
215 id => "authtypecode$i",
216 values => \@authtypes,
217 default => $data->{'authtypecode'},
219 $row_data{repeatable} = CGI::checkbox(
220 -name => "repeatable$i",
221 -checked => $data->{'repeatable'} ? 'checked' : '',
222 -value => 1,
223 -label => '',
224 -id => "repeatable$i"
226 $row_data{mandatory} = CGI::checkbox(
227 -name => "mandatory$i",
228 -checked => $data->{'mandatory'} ? 'checked' : '',
229 -value => 1,
230 -label => '',
231 -id => "mandatory$i"
233 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
234 $row_data{isurl} = CGI::checkbox(
235 -name => "isurl$i",
236 -id => "isurl$i",
237 -checked => $data->{'isurl'} ? 'checked' : '',
238 -value => 1,
239 -label => ''
241 $row_data{row} = $i;
242 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
243 push( @loop_data, \%row_data );
244 $i++;
247 # add more_subfields empty lines for add if needed
248 my %row_data; # get a fresh hash for the row data
249 $row_data{'new_subfield'} = 1;
250 $row_data{'subfieldcode'} = '';
251 $row_data{'maxlength'} = 9999;
253 $row_data{tab} = {
254 id => "tab$i",
255 default => $data->{'tab'},
257 $row_data{tagsubfield} =
258 "<input type=\"text\" name=\"tagsubfield\" value=\""
259 . $data->{'tagsubfield'}
260 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
261 $row_data{liblibrarian} = "";
262 $row_data{libopac} = "";
263 $row_data{seealso} = "";
264 $row_data{kohafield} = {
265 id => "kohafield$i",
266 values => \@kohafields,
267 default => "$data->{'kohafield'}",
269 $row_data{hidden} = "";
270 $row_data{repeatable} = CGI::checkbox(
271 -name => "repeatable$i",
272 -id => "repeatable$i",
273 -checked => '',
274 -value => 1,
275 -label => ''
277 $row_data{mandatory} = CGI::checkbox(
278 -name => "mandatory$i",
279 -id => "mandatory$i",
280 -checked => '',
281 -value => 1,
282 -label => ''
284 $row_data{isurl} = CGI::checkbox(
285 -name => "isurl$i",
286 -id => "isurl$i",
287 -checked => '',
288 -value => 1,
289 -label => ''
291 $row_data{value_builder} = {
292 id => "value_builder$i",
293 values => \@value_builder,
294 default => $data->{'value_builder'},
296 $row_data{authorised_value} = {
297 id => "authorised_value$i",
298 values => \@authorised_values,
299 default => $data->{'authorised_value'},
301 $row_data{authtypes} = {
302 id => "authtypecode$i",
303 values => \@authtypes,
304 default => $data->{'authtypecode'},
306 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
307 $row_data{row} = $i;
308 push( @loop_data, \%row_data );
310 $template->param( 'use_heading_flags_p' => 1 );
311 $template->param( 'heading_edit_subfields_p' => 1 );
312 $template->param(
313 action => "Edit subfields",
314 tagfield => $tagfield,
315 loop => \@loop_data,
316 more_subfields => $more_subfields,
317 more_tag => $tagfield
320 # END $OP eq ADD_FORM
321 ################## ADD_VALIDATE ##################################
322 # called by add_form, used to insert/modify data in DB
324 elsif ( $op eq 'add_validate' ) {
325 my $dbh = C4::Context->dbh;
326 $template->param( tagfield => "$input->param('tagfield')" );
327 # my $sth = $dbh->prepare(
328 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
329 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
330 # );
331 my $sth_insert = $dbh->prepare(qq{
332 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)
333 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
335 my $sth_update = $dbh->prepare(qq{
336 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=?
337 where tagfield=? and tagsubfield=? and frameworkcode=?
339 my @tagsubfield = $input->param('tagsubfield');
340 my @liblibrarian = $input->param('liblibrarian');
341 my @libopac = $input->param('libopac');
342 my @kohafield = $input->param('kohafield');
343 my @tab = $input->param('tab');
344 my @seealso = $input->param('seealso');
345 my @hidden = $input->param('hidden');
346 my @authorised_values = $input->param('authorised_value');
347 my @authtypecodes = $input->param('authtypecode');
348 my @value_builder = $input->param('value_builder');
349 my @link = $input->param('link');
350 my @defaultvalue = $input->param('defaultvalue');
351 my @maxlength = $input->param('maxlength');
353 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
354 my $tagfield = $input->param('tagfield');
355 my $tagsubfield = $tagsubfield[$i];
356 $tagsubfield = "@" unless $tagsubfield ne '';
357 $tagsubfield = "@" if $tagsubfield eq '_';
358 my $liblibrarian = $liblibrarian[$i];
359 my $libopac = $libopac[$i];
360 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
361 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
362 my $kohafield = $kohafield[$i];
363 my $tab = $tab[$i];
364 my $seealso = $seealso[$i];
365 my $authorised_value = $authorised_values[$i];
366 my $authtypecode = $authtypecodes[$i];
367 my $value_builder = $value_builder[$i];
368 my $hidden = $hidden[$i]; #input->param("hidden$i");
369 my $isurl = $input->param("isurl$i") ? 1 : 0;
370 my $link = $link[$i];
371 my $defaultvalue = $defaultvalue[$i];
372 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
374 if (defined($liblibrarian) && $liblibrarian ne "") {
375 unless ( C4::Context->config('demo') eq 1 ) {
376 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
377 $sth_update->execute(
378 $tagfield,
379 $tagsubfield,
380 $liblibrarian,
381 $libopac,
382 $repeatable,
383 $mandatory,
384 $kohafield,
385 $tab,
386 $seealso,
387 $authorised_value,
388 $authtypecode,
389 $value_builder,
390 $hidden,
391 $isurl,
392 $frameworkcode,
393 $link,
394 $defaultvalue,
395 $maxlength,
397 $tagfield,
398 $tagsubfield,
399 $frameworkcode,
402 } else {
403 $sth_insert->execute(
404 $tagfield,
405 $tagsubfield,
406 $liblibrarian,
407 $libopac,
408 $repeatable,
409 $mandatory,
410 $kohafield,
411 $tab,
412 $seealso,
413 $authorised_value,
414 $authtypecode,
415 $value_builder,
416 $hidden,
417 $isurl,
418 $frameworkcode,
419 $link,
420 $defaultvalue,
421 $maxlength,
427 $sth_insert->finish;
428 $sth_update->finish;
429 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
430 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
432 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
433 exit;
435 # END $OP eq ADD_VALIDATE
436 ################## DELETE_CONFIRM ##################################
437 # called by default form, used to confirm deletion of data in DB
439 elsif ( $op eq 'delete_confirm' ) {
440 my $dbh = C4::Context->dbh;
441 my $sth =
442 $dbh->prepare(
443 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
446 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
447 my $data = $sth->fetchrow_hashref;
448 $sth->finish;
449 $template->param(
450 liblibrarian => $data->{'liblibrarian'},
451 tagsubfield => $data->{'tagsubfield'},
452 delete_link => $script_name,
453 tagfield => $tagfield,
454 tagsubfield => $tagsubfield,
455 frameworkcode => $frameworkcode,
458 # END $OP eq DELETE_CONFIRM
459 ################## DELETE_CONFIRMED ##################################
460 # called by delete_confirm, used to effectively confirm deletion of data in DB
462 elsif ( $op eq 'delete_confirmed' ) {
463 my $dbh = C4::Context->dbh;
464 unless ( C4::Context->config('demo') eq 1 ) {
465 my $sth =
466 $dbh->prepare(
467 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
469 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
470 $sth->finish;
472 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
473 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
474 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
475 exit;
477 # END $OP eq DELETE_CONFIRMED
478 ################## DEFAULT ##################################
480 else { # DEFAULT
481 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
482 my @loop_data = ();
483 for ( my $i = 0; $i < $count; $i++ ) {
484 my %row_data; # get a fresh hash for the row data
485 $row_data{tagfield} = $results->[$i]{'tagfield'};
486 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
487 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
488 $row_data{kohafield} = $results->[$i]{'kohafield'};
489 $row_data{repeatable} = $results->[$i]{'repeatable'};
490 $row_data{mandatory} = $results->[$i]{'mandatory'};
491 $row_data{tab} = $results->[$i]{'tab'};
492 $row_data{seealso} = $results->[$i]{'seealso'};
493 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
494 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
495 $row_data{value_builder} = $results->[$i]{'value_builder'};
496 $row_data{hidden} = $results->[$i]{'hidden'};
497 $row_data{isurl} = $results->[$i]{'isurl'};
498 $row_data{link} = $results->[$i]{'link'};
500 if ( $row_data{tab} eq -1 ) {
501 $row_data{subfield_ignored} = 1;
504 push( @loop_data, \%row_data );
506 $template->param( loop => \@loop_data );
507 $template->param(
508 edit_tagfield => $tagfield,
509 edit_frameworkcode => $frameworkcode
512 } #---- END $OP eq DEFAULT
514 output_html_with_http_headers $input, $cookie, $template->output;