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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
28 my ( $searchstring, $frameworkcode ) = @_;
29 my $dbh = C4::Context->dbh;
30 $searchstring =~ s/\'/\\\'/g;
31 my @data = split( ' ', $searchstring );
35 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
37 $sth->execute( "$searchstring%", $frameworkcode );
42 while ( my $data = $sth->fetchrow_hashref ) {
43 push( @results, $data );
49 return ( $cnt, \@results );
53 my $tagfield = $input->param('tagfield');
54 my $tagsubfield = $input->param('tagsubfield');
55 my $frameworkcode = $input->param('frameworkcode');
56 my $pkfield = "tagfield";
57 my $offset = $input->param('offset');
58 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
60 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
62 template_name => "admin/marc_subfields_structure.tmpl",
66 flagsrequired => { parameters => 1 },
71 my $op = $input->param('op');
76 script_name => $script_name,
77 tagfield => $tagfield,
78 frameworkcode => $frameworkcode,
80 ); # we show only the TMPL_VAR names $op
84 script_name => $script_name,
85 tagfield => $tagfield,
86 frameworkcode => $frameworkcode,
88 ); # we show only the TMPL_VAR names $op
91 ################## ADD_FORM ##################################
92 # called by default. Used to create form to add or modify a record
93 if ( $op eq 'add_form' ) {
95 my $dbh = C4::Context->dbh;
96 my $more_subfields = $input->param("more_subfields") + 1;
98 # builds kohafield tables
100 push @kohafields, "";
101 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
103 while ( ( my $field ) = $sth2->fetchrow_array ) {
104 push @kohafields, "biblio." . $field;
106 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
108 while ( ( my $field ) = $sth2->fetchrow_array ) {
109 if ( $field eq 'notes' ) { $field = 'bnotes'; }
110 push @kohafields, "biblioitems." . $field;
112 my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
114 while ( ( my $field ) = $sth2->fetchrow_array ) {
115 push @kohafields, "items." . $field;
118 # build authorised value list
120 $sth2 = $dbh->prepare("select distinct category from authorised_values");
122 my @authorised_values;
123 push @authorised_values, "";
124 while ( ( my $category ) = $sth2->fetchrow_array ) {
125 push @authorised_values, $category;
127 push( @authorised_values, "branches" );
128 push( @authorised_values, "itemtypes" );
130 # build thesaurus categories list
132 $sth2 = $dbh->prepare("select authtypecode from auth_types");
136 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
137 push @authtypes, $authtypecode;
140 # build value_builder list
141 my @value_builder = ('');
143 # read value_builder directory.
144 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
145 # on a standard install, /cgi-bin need to be added.
146 # test one, then the other
147 my $cgidir = C4::Context->intranetdir . "/cgi-bin";
148 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
149 $cgidir = C4::Context->intranetdir;
150 opendir( DIR, "$cgidir/cataloguing/value_builder" )
151 || die "can't opendir $cgidir/value_builder: $!";
153 while ( my $line = readdir(DIR) ) {
154 if ( $line =~ /\.pl$/ ) {
155 push( @value_builder, $line );
158 @value_builder= sort {$a cmp $b} @value_builder;
164 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
165 ); # and tagsubfield='$tagsubfield'");
166 $sth->execute( $tagfield, $frameworkcode );
170 while ( $data = $sth->fetchrow_hashref ) {
171 my %row_data; # get a fresh hash for the row data
172 if ( $toggle eq 1 ) {
178 $row_data{defaultvalue} = $data->{defaultvalue};
179 $row_data{tab} = CGI::scrolling_list(
183 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
196 '10' => 'items (10)',
198 -default => $data->{'tab'},
203 $row_data{tagsubfield} =
204 $data->{'tagsubfield'}
205 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
206 . $data->{'tagsubfield'}
207 . "\" id=\"tagsubfield\" />";
208 $row_data{subfieldcode} = $data->{'tagsubfield'};
209 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
210 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
211 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
212 $row_data{kohafield} = CGI::scrolling_list(
213 -name => "kohafield",
214 -id => "kohafield$i",
215 -values => \@kohafields,
216 -default => "$data->{'kohafield'}",
221 $row_data{authorised_value} = CGI::scrolling_list(
222 -name => 'authorised_value',
223 -id => 'authorised_value',
224 -values => \@authorised_values,
225 -default => $data->{'authorised_value'},
230 $row_data{value_builder} = CGI::scrolling_list(
231 -name => 'value_builder',
232 -id => 'value_builder',
233 -values => \@value_builder,
234 -default => $data->{'value_builder'},
239 $row_data{authtypes} = CGI::scrolling_list(
240 -name => 'authtypecode',
241 -id => 'authtypecode',
242 -values => \@authtypes,
243 -default => $data->{'authtypecode'},
248 $row_data{repeatable} = CGI::checkbox(
249 -name => "repeatable$i",
250 -checked => $data->{'repeatable'} ? 'checked' : '',
254 -id => "repeatable$i"
256 $row_data{mandatory} = CGI::checkbox(
257 -name => "mandatory$i",
258 -checked => $data->{'mandatory'} ? 'checked' : '',
264 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
265 $row_data{isurl} = CGI::checkbox(
268 -checked => $data->{'isurl'} ? 'checked' : '',
274 $row_data{toggle} = $toggle;
275 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
276 push( @loop_data, \%row_data );
280 # add more_subfields empty lines for add if needed
281 for ( my $j = 1 ; $j <= 1 ; $j++ ) {
282 my %row_data; # get a fresh hash for the row data
283 $row_data{'subfieldcode'} = '';
285 $row_data{tab} = CGI::scrolling_list(
289 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
302 '10' => 'items (10)',
309 $row_data{tagsubfield} =
310 "<input type=\"text\" name=\"tagsubfield\" value=\""
311 . $data->{'tagsubfield'}
312 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
313 $row_data{liblibrarian} = "";
314 $row_data{libopac} = "";
315 $row_data{seealso} = "";
316 $row_data{kohafield} = CGI::scrolling_list(
317 -name => 'kohafield',
318 -id => "kohafield$j",
319 -values => \@kohafields,
325 $row_data{hidden} = "";
326 $row_data{repeatable} = CGI::checkbox(
327 -name => "repeatable$j",
328 -id => "repeatable$j",
334 $row_data{mandatory} = CGI::checkbox(
335 -name => "mandatory$j",
336 -id => "mandatory$j",
342 $row_data{isurl} = CGI::checkbox(
350 $row_data{value_builder} = CGI::scrolling_list(
351 -name => 'value_builder',
352 -id => 'value_builder',
353 -values => \@value_builder,
354 -default => $data->{'value_builder'},
359 $row_data{authorised_value} = CGI::scrolling_list(
360 -name => 'authorised_value',
361 -id => 'authorised_value',
362 -values => \@authorised_values,
367 $row_data{authtypes} = CGI::scrolling_list(
368 -name => 'authtypecode',
369 -id => 'authtypecode',
370 -values => \@authtypes,
375 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
376 $row_data{toggle} = $toggle;
378 push( @loop_data, \%row_data );
381 $template->param( 'use-heading-flags-p' => 1 );
382 $template->param( 'heading-edit-subfields-p' => 1 );
384 action => "Edit subfields",
385 tagfield => $tagfield,
387 more_subfields => $more_subfields,
388 more_tag => $tagfield
391 # END $OP eq ADD_FORM
392 ################## ADD_VALIDATE ##################################
393 # called by add_form, used to insert/modify data in DB
395 elsif ( $op eq 'add_validate' ) {
396 my $dbh = C4::Context->dbh;
397 $template->param( tagfield => "$input->param('tagfield')" );
398 my $sth = $dbh->prepare(
399 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
400 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
402 my @tagsubfield = $input->param('tagsubfield');
403 my @liblibrarian = $input->param('liblibrarian');
404 my @libopac = $input->param('libopac');
405 my @kohafield = $input->param('kohafield');
406 my @tab = $input->param('tab');
407 my @seealso = $input->param('seealso');
408 my @hidden = $input->param('hidden');
409 my @authorised_values = $input->param('authorised_value');
410 my @authtypecodes = $input->param('authtypecode');
411 my @value_builder = $input->param('value_builder');
412 my @link = $input->param('link');
413 my @defaultvalue = $input->param('defaultvalue');
415 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
416 my $tagfield = $input->param('tagfield');
417 my $tagsubfield = $tagsubfield[$i];
418 $tagsubfield = "@" unless $tagsubfield ne '';
419 my $liblibrarian = $liblibrarian[$i];
420 my $libopac = $libopac[$i];
421 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
422 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
423 my $kohafield = $kohafield[$i];
425 my $seealso = $seealso[$i];
426 my $authorised_value = $authorised_values[$i];
427 my $authtypecode = $authtypecodes[$i];
428 my $value_builder = $value_builder[$i];
429 my $hidden = $hidden[$i]; #input->param("hidden$i");
430 my $isurl = $input->param("isurl$i") ? 1 : 0;
431 my $link = $link[$i];
432 my $defaultvalue = $defaultvalue[$i];
435 unless ( C4::Context->config('demo') eq 1 ) {
460 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
463 # END $OP eq ADD_VALIDATE
464 ################## DELETE_CONFIRM ##################################
465 # called by default form, used to confirm deletion of data in DB
467 elsif ( $op eq 'delete_confirm' ) {
468 my $dbh = C4::Context->dbh;
471 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
474 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
475 my $data = $sth->fetchrow_hashref;
478 liblibrarian => $data->{'liblibrarian'},
479 tagsubfield => $data->{'tagsubfield'},
480 delete_link => $script_name,
481 tagfield => $tagfield,
482 tagsubfield => $tagsubfield,
483 frameworkcode => $frameworkcode,
486 # END $OP eq DELETE_CONFIRM
487 ################## DELETE_CONFIRMED ##################################
488 # called by delete_confirm, used to effectively confirm deletion of data in DB
490 elsif ( $op eq 'delete_confirmed' ) {
491 my $dbh = C4::Context->dbh;
492 unless ( C4::Context->config('demo') eq 1 ) {
495 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
497 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
501 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
503 $template->param( tagfield => $tagfield );
505 # END $OP eq DELETE_CONFIRMED
506 ################## DEFAULT ##################################
509 my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
514 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
518 if ( $toggle eq 1 ) {
524 my %row_data; # get a fresh hash for the row data
525 $row_data{tagfield} = $results->[$i]{'tagfield'};
526 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
527 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
528 $row_data{kohafield} = $results->[$i]{'kohafield'};
529 $row_data{repeatable} = $results->[$i]{'repeatable'};
530 $row_data{mandatory} = $results->[$i]{'mandatory'};
531 $row_data{tab} = $results->[$i]{'tab'};
532 $row_data{seealso} = $results->[$i]{'seealso'};
533 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
534 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
535 $row_data{value_builder} = $results->[$i]{'value_builder'};
536 $row_data{hidden} = $results->[$i]{'hidden'};
537 $row_data{isurl} = $results->[$i]{'isurl'};
538 $row_data{link} = $results->[$i]{'link'};
540 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
541 . $results->[$i]{'tagsubfield'}
542 . "&frameworkcode=$frameworkcode";
543 $row_data{toggle} = $toggle;
545 if ( $row_data{tab} eq -1 ) {
546 $row_data{subfield_ignored} = 1;
549 push( @loop_data, \%row_data );
551 $template->param( loop => \@loop_data );
553 edit_tagfield => $tagfield,
554 edit_frameworkcode => $frameworkcode
558 my $prevpage = $offset - $pagesize;
560 prev => "<a href=\"$script_name?offset=$prevpage\">" );
562 if ( $offset + $pagesize < $count ) {
563 my $nextpage = $offset + $pagesize;
565 next => "<a href=\"$script_name?offset=$nextpage\">" );
567 } #---- END $OP eq DEFAULT
569 output_html_with_http_headers $input, $cookie, $template->output;