Bug 10402 [Template follow-up] Add multiple contacts for vendors
[koha.git] / admin / marc_subfields_structure.pl
blob44a94946b5d3c229ad0080e9870d24fb7a8d6640
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
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 push( @value_builder, $line );
168 @value_builder= sort {$a cmp $b} @value_builder;
169 closedir DIR;
171 # build values list
172 my $sth =
173 $dbh->prepare(
174 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
175 ); # and tagsubfield='$tagsubfield'");
176 $sth->execute( $tagfield, $frameworkcode );
177 my @loop_data = ();
178 my $i = 0;
179 while ( $data = $sth->fetchrow_hashref ) {
180 my %row_data; # get a fresh hash for the row data
181 $row_data{defaultvalue} = $data->{defaultvalue};
182 $row_data{maxlength} = $data->{maxlength};
183 $row_data{tab} = {
184 id => "tab$i",
185 default => $data->{'tab'},
188 $row_data{tagsubfield} =
189 $data->{'tagsubfield'}
190 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
191 . $data->{'tagsubfield'}
192 . "\" id=\"tagsubfield\" />";
193 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
194 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
195 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
196 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
197 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
198 $row_data{kohafield} = {
199 id => "kohafield$i",
200 values => \@kohafields,
201 default => "$data->{'kohafield'}",
203 $row_data{authorised_value} = {
204 id => "authorised_value$i",
205 values => \@authorised_values,
206 default => $data->{'authorised_value'},
208 $row_data{value_builder} = {
209 id => "value_builder$i",
210 values => \@value_builder,
211 default => $data->{'value_builder'},
213 $row_data{authtypes} = {
214 id => "authtypecode$i",
215 values => \@authtypes,
216 default => $data->{'authtypecode'},
218 $row_data{repeatable} = CGI::checkbox(
219 -name => "repeatable$i",
220 -checked => $data->{'repeatable'} ? 'checked' : '',
221 -value => 1,
222 -label => '',
223 -id => "repeatable$i"
225 $row_data{mandatory} = CGI::checkbox(
226 -name => "mandatory$i",
227 -checked => $data->{'mandatory'} ? 'checked' : '',
228 -value => 1,
229 -label => '',
230 -id => "mandatory$i"
232 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
233 $row_data{isurl} = CGI::checkbox(
234 -name => "isurl$i",
235 -id => "isurl$i",
236 -checked => $data->{'isurl'} ? 'checked' : '',
237 -value => 1,
238 -label => ''
240 $row_data{row} = $i;
241 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
242 push( @loop_data, \%row_data );
243 $i++;
246 # add more_subfields empty lines for add if needed
247 my %row_data; # get a fresh hash for the row data
248 $row_data{'new_subfield'} = 1;
249 $row_data{'subfieldcode'} = '';
250 $row_data{'maxlength'} = 9999;
252 $row_data{tab} = {
253 id => "tab$i",
254 default => $data->{'tab'},
256 $row_data{tagsubfield} =
257 "<input type=\"text\" name=\"tagsubfield\" value=\""
258 . $data->{'tagsubfield'}
259 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
260 $row_data{liblibrarian} = "";
261 $row_data{libopac} = "";
262 $row_data{seealso} = "";
263 $row_data{kohafield} = {
264 id => "kohafield$i",
265 values => \@kohafields,
266 default => "$data->{'kohafield'}",
268 $row_data{hidden} = "";
269 $row_data{repeatable} = CGI::checkbox(
270 -name => "repeatable$i",
271 -id => "repeatable$i",
272 -checked => '',
273 -value => 1,
274 -label => ''
276 $row_data{mandatory} = CGI::checkbox(
277 -name => "mandatory$i",
278 -id => "mandatory$i",
279 -checked => '',
280 -value => 1,
281 -label => ''
283 $row_data{isurl} = CGI::checkbox(
284 -name => "isurl$i",
285 -id => "isurl$i",
286 -checked => '',
287 -value => 1,
288 -label => ''
290 $row_data{value_builder} = {
291 id => "value_builder$i",
292 values => \@value_builder,
293 default => $data->{'value_builder'},
295 $row_data{authorised_value} = {
296 id => "authorised_value$i",
297 values => \@authorised_values,
298 default => $data->{'authorised_value'},
300 $row_data{authtypes} = {
301 id => "authtypecode$i",
302 values => \@authtypes,
303 default => $data->{'authtypecode'},
305 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
306 $row_data{row} = $i;
307 push( @loop_data, \%row_data );
309 $template->param( 'use_heading_flags_p' => 1 );
310 $template->param( 'heading_edit_subfields_p' => 1 );
311 $template->param(
312 action => "Edit subfields",
313 tagfield => $tagfield,
314 loop => \@loop_data,
315 more_subfields => $more_subfields,
316 more_tag => $tagfield
319 # END $OP eq ADD_FORM
320 ################## ADD_VALIDATE ##################################
321 # called by add_form, used to insert/modify data in DB
323 elsif ( $op eq 'add_validate' ) {
324 my $dbh = C4::Context->dbh;
325 $template->param( tagfield => "$input->param('tagfield')" );
326 # my $sth = $dbh->prepare(
327 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
328 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
329 # );
330 my $sth_insert = $dbh->prepare(qq{
331 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)
332 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
334 my $sth_update = $dbh->prepare(qq{
335 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=?
336 where tagfield=? and tagsubfield=? and frameworkcode=?
338 my @tagsubfield = $input->param('tagsubfield');
339 my @liblibrarian = $input->param('liblibrarian');
340 my @libopac = $input->param('libopac');
341 my @kohafield = $input->param('kohafield');
342 my @tab = $input->param('tab');
343 my @seealso = $input->param('seealso');
344 my @hidden = $input->param('hidden');
345 my @authorised_values = $input->param('authorised_value');
346 my @authtypecodes = $input->param('authtypecode');
347 my @value_builder = $input->param('value_builder');
348 my @link = $input->param('link');
349 my @defaultvalue = $input->param('defaultvalue');
350 my @maxlength = $input->param('maxlength');
352 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
353 my $tagfield = $input->param('tagfield');
354 my $tagsubfield = $tagsubfield[$i];
355 $tagsubfield = "@" unless $tagsubfield ne '';
356 $tagsubfield = "@" if $tagsubfield eq '_';
357 my $liblibrarian = $liblibrarian[$i];
358 my $libopac = $libopac[$i];
359 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
360 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
361 my $kohafield = $kohafield[$i];
362 my $tab = $tab[$i];
363 my $seealso = $seealso[$i];
364 my $authorised_value = $authorised_values[$i];
365 my $authtypecode = $authtypecodes[$i];
366 my $value_builder = $value_builder[$i];
367 my $hidden = $hidden[$i]; #input->param("hidden$i");
368 my $isurl = $input->param("isurl$i") ? 1 : 0;
369 my $link = $link[$i];
370 my $defaultvalue = $defaultvalue[$i];
371 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
373 if (defined($liblibrarian) && $liblibrarian ne "") {
374 unless ( C4::Context->config('demo') eq 1 ) {
375 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
376 $sth_update->execute(
377 $tagfield,
378 $tagsubfield,
379 $liblibrarian,
380 $libopac,
381 $repeatable,
382 $mandatory,
383 $kohafield,
384 $tab,
385 $seealso,
386 $authorised_value,
387 $authtypecode,
388 $value_builder,
389 $hidden,
390 $isurl,
391 $frameworkcode,
392 $link,
393 $defaultvalue,
394 $maxlength,
396 $tagfield,
397 $tagsubfield,
398 $frameworkcode,
401 } else {
402 $sth_insert->execute(
403 $tagfield,
404 $tagsubfield,
405 $liblibrarian,
406 $libopac,
407 $repeatable,
408 $mandatory,
409 $kohafield,
410 $tab,
411 $seealso,
412 $authorised_value,
413 $authtypecode,
414 $value_builder,
415 $hidden,
416 $isurl,
417 $frameworkcode,
418 $link,
419 $defaultvalue,
420 $maxlength,
426 $sth_insert->finish;
427 $sth_update->finish;
428 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
429 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
431 print
432 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
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
475 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
476 exit;
477 $template->param( tagfield => $tagfield );
479 # END $OP eq DELETE_CONFIRMED
480 ################## DEFAULT ##################################
482 else { # DEFAULT
483 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
484 my @loop_data = ();
485 for ( my $i = 0; $i < $count; $i++ ) {
486 my %row_data; # get a fresh hash for the row data
487 $row_data{tagfield} = $results->[$i]{'tagfield'};
488 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
489 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
490 $row_data{kohafield} = $results->[$i]{'kohafield'};
491 $row_data{repeatable} = $results->[$i]{'repeatable'};
492 $row_data{mandatory} = $results->[$i]{'mandatory'};
493 $row_data{tab} = $results->[$i]{'tab'};
494 $row_data{seealso} = $results->[$i]{'seealso'};
495 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
496 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
497 $row_data{value_builder} = $results->[$i]{'value_builder'};
498 $row_data{hidden} = $results->[$i]{'hidden'};
499 $row_data{isurl} = $results->[$i]{'isurl'};
500 $row_data{link} = $results->[$i]{'link'};
502 if ( $row_data{tab} eq -1 ) {
503 $row_data{subfield_ignored} = 1;
506 push( @loop_data, \%row_data );
508 $template->param( loop => \@loop_data );
509 $template->param(
510 edit_tagfield => $tagfield,
511 edit_frameworkcode => $frameworkcode
514 } #---- END $OP eq DEFAULT
516 output_html_with_http_headers $input, $cookie, $template->output;