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
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.
21 #use warnings; FIXME - Bug 2505
29 my ( $searchstring, $frameworkcode ) = @_;
30 my $dbh = C4::Context->dbh;
31 $searchstring =~ s/\'/\\\'/g;
32 my @data = split( ' ', $searchstring );
36 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
38 $sth->execute( "$searchstring%", $frameworkcode );
43 while ( my $data = $sth->fetchrow_hashref ) {
44 push( @results, $data );
50 return ( $cnt, \@results );
53 sub marc_subfield_structure_exists {
54 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
55 my $dbh = C4::Context->dbh;
56 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
57 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
62 my $tagfield = $input->param('tagfield');
63 my $tagsubfield = $input->param('tagsubfield');
64 my $frameworkcode = $input->param('frameworkcode');
65 my $pkfield = "tagfield";
66 my $offset = $input->param('offset');
67 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
69 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
71 template_name => "admin/marc_subfields_structure.tmpl",
75 flagsrequired => { parameters => 'parameters_remaining_permissions' },
80 my $op = $input->param('op');
85 script_name => $script_name,
86 tagfield => $tagfield,
87 frameworkcode => $frameworkcode,
89 ); # we show only the TMPL_VAR names $op
93 script_name => $script_name,
94 tagfield => $tagfield,
95 frameworkcode => $frameworkcode,
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' ) {
104 my $dbh = C4::Context->dbh;
105 my $more_subfields = $input->param("more_subfields") + 1;
107 # builds kohafield tables
109 push @kohafields, "";
110 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
112 while ( ( my $field ) = $sth2->fetchrow_array ) {
113 push @kohafields, "biblio." . $field;
115 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
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");
123 while ( ( my $field ) = $sth2->fetchrow_array ) {
124 push @kohafields, "items." . $field;
127 # build authorised value list
129 $sth2 = $dbh->prepare("select distinct category from authorised_values");
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
142 $sth2 = $dbh->prepare("select authtypecode from auth_types");
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;
174 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
175 ); # and tagsubfield='$tagsubfield'");
176 $sth->execute( $tagfield, $frameworkcode );
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} = CGI::scrolling_list(
187 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
200 '10' => 'items (10)',
202 -default => $data->{'tab'},
206 $row_data{tagsubfield} =
207 $data->{'tagsubfield'}
208 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
209 . $data->{'tagsubfield'}
210 . "\" id=\"tagsubfield\" />";
211 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
212 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
213 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
214 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
215 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
216 $row_data{kohafield} = CGI::scrolling_list(
217 -name => "kohafield",
218 -id => "kohafield$i",
219 -values => \@kohafields,
220 -default => "$data->{'kohafield'}",
224 $row_data{authorised_value} = CGI::scrolling_list(
225 -name => "authorised_value",
226 -id => "authorised_value$i",
227 -values => \@authorised_values,
228 -default => $data->{'authorised_value'},
232 $row_data{value_builder} = CGI::scrolling_list(
233 -name => "value_builder",
234 -id => "value_builder$i",
235 -values => \@value_builder,
236 -default => $data->{'value_builder'},
240 $row_data{authtypes} = CGI::scrolling_list(
241 -name => "authtypecode",
242 -id => "authtypecode$i",
243 -values => \@authtypes,
244 -default => $data->{'authtypecode'},
248 $row_data{repeatable} = CGI::checkbox(
249 -name => "repeatable$i",
250 -checked => $data->{'repeatable'} ? 'checked' : '',
253 -id => "repeatable$i"
255 $row_data{mandatory} = CGI::checkbox(
256 -name => "mandatory$i",
257 -checked => $data->{'mandatory'} ? 'checked' : '',
262 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
263 $row_data{isurl} = CGI::checkbox(
266 -checked => $data->{'isurl'} ? 'checked' : '',
271 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
272 push( @loop_data, \%row_data );
276 # add more_subfields empty lines for add if needed
277 my %row_data; # get a fresh hash for the row data
278 $row_data{'new_subfield'} = 1;
279 $row_data{'subfieldcode'} = '';
281 $row_data{tab} = CGI::scrolling_list(
285 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
298 '10' => 'items (10)',
304 $row_data{tagsubfield} =
305 "<input type=\"text\" name=\"tagsubfield\" value=\""
306 . $data->{'tagsubfield'}
307 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
308 $row_data{liblibrarian} = "";
309 $row_data{libopac} = "";
310 $row_data{seealso} = "";
311 $row_data{kohafield} = CGI::scrolling_list(
312 -name => 'kohafield',
313 -id => "kohafield$i",
314 -values => \@kohafields,
319 $row_data{hidden} = "";
320 $row_data{repeatable} = CGI::checkbox(
321 -name => "repeatable$i",
322 -id => "repeatable$i",
327 $row_data{mandatory} = CGI::checkbox(
328 -name => "mandatory$i",
329 -id => "mandatory$i",
334 $row_data{isurl} = CGI::checkbox(
341 $row_data{value_builder} = CGI::scrolling_list(
342 -name => "value_builder",
343 -id => "value_builder$i",
344 -values => \@value_builder,
345 -default => $data->{'value_builder'},
349 $row_data{authorised_value} = CGI::scrolling_list(
350 -name => "authorised_value",
351 -id => "authorised_value$i",
352 -values => \@authorised_values,
356 $row_data{authtypes} = CGI::scrolling_list(
357 -name => "authtypecode",
358 -id => "authtypecode$i",
359 -values => \@authtypes,
363 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
365 push( @loop_data, \%row_data );
367 $template->param( 'use_heading_flags_p' => 1 );
368 $template->param( 'heading_edit_subfields_p' => 1 );
370 action => "Edit subfields",
371 tagfield => $tagfield,
373 more_subfields => $more_subfields,
374 more_tag => $tagfield
377 # END $OP eq ADD_FORM
378 ################## ADD_VALIDATE ##################################
379 # called by add_form, used to insert/modify data in DB
381 elsif ( $op eq 'add_validate' ) {
382 my $dbh = C4::Context->dbh;
383 $template->param( tagfield => "$input->param('tagfield')" );
384 # my $sth = $dbh->prepare(
385 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
386 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
388 my $sth_insert = $dbh->prepare(qq{
389 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)
390 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
392 my $sth_update = $dbh->prepare(qq{
393 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=?
394 where tagfield=? and tagsubfield=? and frameworkcode=?
396 my @tagsubfield = $input->param('tagsubfield');
397 my @liblibrarian = $input->param('liblibrarian');
398 my @libopac = $input->param('libopac');
399 my @kohafield = $input->param('kohafield');
400 my @tab = $input->param('tab');
401 my @seealso = $input->param('seealso');
402 my @hidden = $input->param('hidden');
403 my @authorised_values = $input->param('authorised_value');
404 my @authtypecodes = $input->param('authtypecode');
405 my @value_builder = $input->param('value_builder');
406 my @link = $input->param('link');
407 my @defaultvalue = $input->param('defaultvalue');
408 my @maxlength = $input->param('maxlength');
410 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
411 my $tagfield = $input->param('tagfield');
412 my $tagsubfield = $tagsubfield[$i];
413 $tagsubfield = "@" unless $tagsubfield ne '';
414 $tagsubfield = "@" if $tagsubfield eq '_';
415 my $liblibrarian = $liblibrarian[$i];
416 my $libopac = $libopac[$i];
417 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
418 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
419 my $kohafield = $kohafield[$i];
421 my $seealso = $seealso[$i];
422 my $authorised_value = $authorised_values[$i];
423 my $authtypecode = $authtypecodes[$i];
424 my $value_builder = $value_builder[$i];
425 my $hidden = $hidden[$i]; #input->param("hidden$i");
426 my $isurl = $input->param("isurl$i") ? 1 : 0;
427 my $link = $link[$i];
428 my $defaultvalue = $defaultvalue[$i];
429 my $maxlength = $maxlength[$i];
431 if (defined($liblibrarian) && $liblibrarian ne "") {
432 unless ( C4::Context->config('demo') eq 1 ) {
433 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
434 $sth_update->execute(
460 $sth_insert->execute(
487 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
490 # END $OP eq ADD_VALIDATE
491 ################## DELETE_CONFIRM ##################################
492 # called by default form, used to confirm deletion of data in DB
494 elsif ( $op eq 'delete_confirm' ) {
495 my $dbh = C4::Context->dbh;
498 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
501 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
502 my $data = $sth->fetchrow_hashref;
505 liblibrarian => $data->{'liblibrarian'},
506 tagsubfield => $data->{'tagsubfield'},
507 delete_link => $script_name,
508 tagfield => $tagfield,
509 tagsubfield => $tagsubfield,
510 frameworkcode => $frameworkcode,
513 # END $OP eq DELETE_CONFIRM
514 ################## DELETE_CONFIRMED ##################################
515 # called by delete_confirm, used to effectively confirm deletion of data in DB
517 elsif ( $op eq 'delete_confirmed' ) {
518 my $dbh = C4::Context->dbh;
519 unless ( C4::Context->config('demo') eq 1 ) {
522 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
524 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
528 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
530 $template->param( tagfield => $tagfield );
532 # END $OP eq DELETE_CONFIRMED
533 ################## DEFAULT ##################################
536 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
538 for ( my $i = 0; $i < $count; $i++ ) {
539 my %row_data; # get a fresh hash for the row data
540 $row_data{tagfield} = $results->[$i]{'tagfield'};
541 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
542 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
543 $row_data{kohafield} = $results->[$i]{'kohafield'};
544 $row_data{repeatable} = $results->[$i]{'repeatable'};
545 $row_data{mandatory} = $results->[$i]{'mandatory'};
546 $row_data{tab} = $results->[$i]{'tab'};
547 $row_data{seealso} = $results->[$i]{'seealso'};
548 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
549 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
550 $row_data{value_builder} = $results->[$i]{'value_builder'};
551 $row_data{hidden} = $results->[$i]{'hidden'};
552 $row_data{isurl} = $results->[$i]{'isurl'};
553 $row_data{link} = $results->[$i]{'link'};
555 if ( $row_data{tab} eq -1 ) {
556 $row_data{subfield_ignored} = 1;
559 push( @loop_data, \%row_data );
561 $template->param( loop => \@loop_data );
563 edit_tagfield => $tagfield,
564 edit_frameworkcode => $frameworkcode
567 } #---- END $OP eq DEFAULT
569 output_html_with_http_headers $input, $cookie, $template->output;