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
22 use C4::Interface::CGI::Output;
29 my ( $env, $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 );
54 my $tagfield = $input->param('tagfield');
55 my $tagsubfield = $input->param('tagsubfield');
56 my $frameworkcode = $input->param('frameworkcode');
57 my $pkfield = "tagfield";
58 my $offset = $input->param('offset');
59 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
61 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
63 template_name => "admin/marc_subfields_structure.tmpl",
67 flagsrequired => { parameters => 1 },
72 my $op = $input->param('op');
77 script_name => $script_name,
78 tagfield => $tagfield,
79 frameworkcode => $frameworkcode,
81 ); # we show only the TMPL_VAR names $op
85 script_name => $script_name,
86 tagfield => $tagfield,
87 frameworkcode => $frameworkcode,
89 ); # we show only the TMPL_VAR names $op
92 ################## ADD_FORM ##################################
93 # called by default. Used to create form to add or modify a record
94 if ( $op eq 'add_form' ) {
96 my $dbh = C4::Context->dbh;
97 my $more_subfields = $input->param("more_subfields") + 1;
99 # builds kohafield tables
101 push @kohafields, "";
102 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
104 while ( ( my $field ) = $sth2->fetchrow_array ) {
105 push @kohafields, "biblio." . $field;
107 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
109 while ( ( my $field ) = $sth2->fetchrow_array ) {
110 if ( $field eq 'notes' ) { $field = 'bnotes'; }
111 push @kohafields, "biblioitems." . $field;
113 my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
115 while ( ( my $field ) = $sth2->fetchrow_array ) {
116 push @kohafields, "items." . $field;
119 # build authorised value list
121 $sth2 = $dbh->prepare("select distinct category from authorised_values");
123 my @authorised_values;
124 push @authorised_values, "";
125 while ( ( my $category ) = $sth2->fetchrow_array ) {
126 push @authorised_values, $category;
128 push( @authorised_values, "branches" );
129 push( @authorised_values, "itemtypes" );
131 # build thesaurus categories list
133 $sth2 = $dbh->prepare("select authtypecode from auth_types");
137 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
138 push @authtypes, $authtypecode;
141 # build value_builder list
142 my @value_builder = ('');
144 # read value_builder directory.
145 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
146 # on a standard install, /cgi-bin need to be added.
147 # test one, then the other
148 my $cgidir = C4::Context->intranetdir . "/cgi-bin";
149 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
150 $cgidir = C4::Context->intranetdir;
151 opendir( DIR, "$cgidir/cataloguing/value_builder" )
152 || die "can't opendir $cgidir/value_builder: $!";
154 while ( my $line = readdir(DIR) ) {
155 if ( $line =~ /\.pl$/ ) {
156 push( @value_builder, $line );
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{tab} = CGI::scrolling_list(
182 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
195 '10' => 'items (10)',
197 -default => $data->{'tab'},
202 $row_data{tagsubfield} =
203 $data->{'tagsubfield'}
204 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
205 . $data->{'tagsubfield'}
206 . "\" id=\"tagsubfield\">";
207 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
208 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
209 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
210 $row_data{kohafield} = CGI::scrolling_list(
211 -name => "kohafield",
212 -id => "kohafield$i",
213 -values => \@kohafields,
214 -default => "$data->{'kohafield'}",
219 $row_data{authorised_value} = CGI::scrolling_list(
220 -name => 'authorised_value',
221 -id => 'authorised_value',
222 -values => \@authorised_values,
223 -default => $data->{'authorised_value'},
228 $row_data{value_builder} = CGI::scrolling_list(
229 -name => 'value_builder',
230 -id => 'value_builder',
231 -values => \@value_builder,
232 -default => $data->{'value_builder'},
237 $row_data{authtypes} = CGI::scrolling_list(
238 -name => 'authtypecode',
239 -id => 'authtypecode',
240 -values => \@authtypes,
241 -default => $data->{'authtypecode'},
246 $row_data{repeatable} = CGI::checkbox(
247 -name => "repeatable$i",
248 -checked => $data->{'repeatable'} ? 'checked' : '',
252 -id => "repeatable$i"
254 $row_data{mandatory} = CGI::checkbox(
255 -name => "mandatory$i",
256 -checked => $data->{'mandatory'} ? 'checked' : '',
262 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
263 $row_data{isurl} = CGI::checkbox(
266 -checked => $data->{'isurl'} ? 'checked' : '',
272 $row_data{toggle} = $toggle;
273 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
274 push( @loop_data, \%row_data );
278 # add more_subfields empty lines for add if needed
279 for ( my $j = $i ; $j <= $more_subfields + $i ; $j++ ) {
280 my %row_data; # get a fresh hash for the row data
281 $row_data{tab} = CGI::scrolling_list(
285 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
298 '10' => 'items (10)',
305 $row_data{tagsubfield} =
306 "<input type=\"text\" name=\"tagsubfield\" value=\""
307 . $data->{'tagsubfield'}
308 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
309 $row_data{liblibrarian} = "";
310 $row_data{libopac} = "";
311 $row_data{seealso} = "";
312 $row_data{kohafield} = CGI::scrolling_list(
313 -name => 'kohafield',
314 -id => "kohafield$j",
315 -values => \@kohafields,
321 $row_data{hidden} = "";
322 $row_data{repeatable} = CGI::checkbox(
323 -name => "repeatable$j",
324 -id => "repeatable$j",
330 $row_data{mandatory} = CGI::checkbox(
331 -name => "mandatory$j",
332 -id => "mandatory$j",
338 $row_data{isurl} = CGI::checkbox(
346 $row_data{value_builder} = CGI::scrolling_list(
347 -name => 'value_builder',
348 -id => 'value_builder',
349 -values => \@value_builder,
350 -default => $data->{'value_builder'},
355 $row_data{authorised_value} = CGI::scrolling_list(
356 -name => 'authorised_value',
357 -id => 'authorised_value',
358 -values => \@authorised_values,
363 $row_data{authtypes} = CGI::scrolling_list(
364 -name => 'authtypecode',
365 -id => 'authtypecode',
366 -values => \@authtypes,
371 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
372 $row_data{toggle} = $toggle;
374 push( @loop_data, \%row_data );
376 $template->param( 'use-heading-flags-p' => 1 );
377 $template->param( 'heading-edit-subfields-p' => 1 );
379 action => "Edit subfields",
381 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
383 more_subfields => $more_subfields,
384 more_tag => $tagfield
387 # END $OP eq ADD_FORM
388 ################## ADD_VALIDATE ##################################
389 # called by add_form, used to insert/modify data in DB
391 elsif ( $op eq 'add_validate' ) {
392 my $dbh = C4::Context->dbh;
393 $template->param( tagfield => "$input->param('tagfield')" );
394 my $sth = $dbh->prepare(
395 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link)
396 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
398 my @tagsubfield = $input->param('tagsubfield');
399 my @liblibrarian = $input->param('liblibrarian');
400 my @libopac = $input->param('libopac');
401 my @kohafield = $input->param('kohafield');
402 my @tab = $input->param('tab');
403 my @seealso = $input->param('seealso');
404 my @hidden = $input->param('hidden');
405 my @authorised_values = $input->param('authorised_value');
406 my @authtypecodes = $input->param('authtypecode');
407 my @value_builder = $input->param('value_builder');
408 my @link = $input->param('link');
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 my $liblibrarian = $liblibrarian[$i];
415 my $libopac = $libopac[$i];
416 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
417 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
418 my $kohafield = $kohafield[$i];
420 my $seealso = $seealso[$i];
421 my $authorised_value = $authorised_values[$i];
422 my $authtypecode = $authtypecodes[$i];
423 my $value_builder = $value_builder[$i];
424 my $hidden = $hidden[$i]; #input->param("hidden$i");
425 my $isurl = $input->param("isurl$i") ? 1 : 0;
426 my $link = $link[$i];
429 unless ( C4::Context->config('demo') eq 1 ) {
454 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
457 # END $OP eq ADD_VALIDATE
458 ################## DELETE_CONFIRM ##################################
459 # called by default form, used to confirm deletion of data in DB
461 elsif ( $op eq 'delete_confirm' ) {
462 my $dbh = C4::Context->dbh;
465 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
468 #FIXME : called with 2 bind variables when 3 are needed
469 $sth->execute( $tagfield, $tagsubfield );
470 my $data = $sth->fetchrow_hashref;
473 liblibrarian => $data->{'liblibrarian'},
474 tagsubfield => $data->{'tagsubfield'},
475 delete_link => $script_name,
476 tagfield => $tagfield,
477 tagsubfield => $tagsubfield,
478 frameworkcode => $frameworkcode,
481 # END $OP eq DELETE_CONFIRM
482 ################## DELETE_CONFIRMED ##################################
483 # called by delete_confirm, used to effectively confirm deletion of data in DB
485 elsif ( $op eq 'delete_confirmed' ) {
486 my $dbh = C4::Context->dbh;
487 unless ( C4::Context->config('demo') eq 1 ) {
490 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
492 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
496 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
498 $template->param( tagfield => $tagfield );
500 # END $OP eq DELETE_CONFIRMED
501 ################## DEFAULT ##################################
505 my ( $count, $results ) = StringSearch( $env, $tagfield, $frameworkcode );
510 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
514 if ( $toggle eq 1 ) {
520 my %row_data; # get a fresh hash for the row data
521 $row_data{tagfield} = $results->[$i]{'tagfield'};
522 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
523 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
524 $row_data{kohafield} = $results->[$i]{'kohafield'};
525 $row_data{repeatable} = $results->[$i]{'repeatable'};
526 $row_data{mandatory} = $results->[$i]{'mandatory'};
527 $row_data{tab} = $results->[$i]{'tab'};
528 $row_data{seealso} = $results->[$i]{'seealso'};
529 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
530 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
531 $row_data{value_builder} = $results->[$i]{'value_builder'};
532 $row_data{hidden} = $results->[$i]{'hidden'};
533 $row_data{isurl} = $results->[$i]{'isurl'};
534 $row_data{link} = $results->[$i]{'link'};
536 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
537 . $results->[$i]{'tagsubfield'}
538 . "&frameworkcode=$frameworkcode";
539 $row_data{toggle} = $toggle;
541 if ( $row_data{tab} eq -1 ) {
542 $row_data{subfield_ignored} = 1;
545 push( @loop_data, \%row_data );
547 $template->param( loop => \@loop_data );
549 edit_tagfield => $tagfield,
550 edit_frameworkcode => $frameworkcode
554 my $prevpage = $offset - $pagesize;
556 prev => "<a href=\"$script_name?offset=$prevpage\">" );
558 if ( $offset + $pagesize < $count ) {
559 my $nextpage = $offset + $pagesize;
561 next => "<a href=\"$script_name?offset=$nextpage\">" );
563 } #---- END $OP eq DEFAULT
565 intranetcolorstylesheet =>
566 C4::Context->preference("intranetcolorstylesheet"),
567 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
568 IntranetNav => C4::Context->preference("IntranetNav"),
570 output_html_with_http_headers $input, $cookie, $template->output;