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 );
163 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
164 ); # and tagsubfield='$tagsubfield'");
165 $sth->execute( $tagfield, $frameworkcode );
169 while ( $data = $sth->fetchrow_hashref ) {
170 my %row_data; # get a fresh hash for the row data
171 if ( $toggle eq 1 ) {
177 $row_data{defaultvalue} = $data->{defaultvalue};
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{subfieldcode} = $data->{'tagsubfield'};
208 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
209 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
210 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
211 $row_data{kohafield} = CGI::scrolling_list(
212 -name => "kohafield",
213 -id => "kohafield$i",
214 -values => \@kohafields,
215 -default => "$data->{'kohafield'}",
220 $row_data{authorised_value} = CGI::scrolling_list(
221 -name => 'authorised_value',
222 -id => 'authorised_value',
223 -values => \@authorised_values,
224 -default => $data->{'authorised_value'},
229 $row_data{value_builder} = CGI::scrolling_list(
230 -name => 'value_builder',
231 -id => 'value_builder',
232 -values => \@value_builder,
233 -default => $data->{'value_builder'},
238 $row_data{authtypes} = CGI::scrolling_list(
239 -name => 'authtypecode',
240 -id => 'authtypecode',
241 -values => \@authtypes,
242 -default => $data->{'authtypecode'},
247 $row_data{repeatable} = CGI::checkbox(
248 -name => "repeatable$i",
249 -checked => $data->{'repeatable'} ? 'checked' : '',
253 -id => "repeatable$i"
255 $row_data{mandatory} = CGI::checkbox(
256 -name => "mandatory$i",
257 -checked => $data->{'mandatory'} ? 'checked' : '',
263 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
264 $row_data{isurl} = CGI::checkbox(
267 -checked => $data->{'isurl'} ? 'checked' : '',
273 $row_data{toggle} = $toggle;
274 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
275 push( @loop_data, \%row_data );
279 # add more_subfields empty lines for add if needed
280 for ( my $j = $i ; $j <= $more_subfields + $i ; $j++ ) {
281 my %row_data; # get a fresh hash for the row data
282 $row_data{tab} = CGI::scrolling_list(
286 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
299 '10' => 'items (10)',
306 $row_data{tagsubfield} =
307 "<input type=\"text\" name=\"tagsubfield\" value=\""
308 . $data->{'tagsubfield'}
309 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
310 $row_data{liblibrarian} = "";
311 $row_data{libopac} = "";
312 $row_data{seealso} = "";
313 $row_data{kohafield} = CGI::scrolling_list(
314 -name => 'kohafield',
315 -id => "kohafield$j",
316 -values => \@kohafields,
322 $row_data{hidden} = "";
323 $row_data{repeatable} = CGI::checkbox(
324 -name => "repeatable$j",
325 -id => "repeatable$j",
331 $row_data{mandatory} = CGI::checkbox(
332 -name => "mandatory$j",
333 -id => "mandatory$j",
339 $row_data{isurl} = CGI::checkbox(
347 $row_data{value_builder} = CGI::scrolling_list(
348 -name => 'value_builder',
349 -id => 'value_builder',
350 -values => \@value_builder,
351 -default => $data->{'value_builder'},
356 $row_data{authorised_value} = CGI::scrolling_list(
357 -name => 'authorised_value',
358 -id => 'authorised_value',
359 -values => \@authorised_values,
364 $row_data{authtypes} = CGI::scrolling_list(
365 -name => 'authtypecode',
366 -id => 'authtypecode',
367 -values => \@authtypes,
372 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
373 $row_data{toggle} = $toggle;
375 push( @loop_data, \%row_data );
377 $template->param( 'use-heading-flags-p' => 1 );
378 $template->param( 'heading-edit-subfields-p' => 1 );
380 action => "Edit subfields",
382 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
384 more_subfields => $more_subfields,
385 more_tag => $tagfield
388 # END $OP eq ADD_FORM
389 ################## ADD_VALIDATE ##################################
390 # called by add_form, used to insert/modify data in DB
392 elsif ( $op eq 'add_validate' ) {
393 my $dbh = C4::Context->dbh;
394 $template->param( tagfield => "$input->param('tagfield')" );
395 my $sth = $dbh->prepare(
396 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
397 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
399 my @tagsubfield = $input->param('tagsubfield');
400 my @liblibrarian = $input->param('liblibrarian');
401 my @libopac = $input->param('libopac');
402 my @kohafield = $input->param('kohafield');
403 my @tab = $input->param('tab');
404 my @seealso = $input->param('seealso');
405 my @hidden = $input->param('hidden');
406 my @authorised_values = $input->param('authorised_value');
407 my @authtypecodes = $input->param('authtypecode');
408 my @value_builder = $input->param('value_builder');
409 my @link = $input->param('link');
410 my @defaultvalue = $input->param('defaultvalue');
412 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
413 my $tagfield = $input->param('tagfield');
414 my $tagsubfield = $tagsubfield[$i];
415 $tagsubfield = "@" unless $tagsubfield ne '';
416 my $liblibrarian = $liblibrarian[$i];
417 my $libopac = $libopac[$i];
418 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
419 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
420 my $kohafield = $kohafield[$i];
422 my $seealso = $seealso[$i];
423 my $authorised_value = $authorised_values[$i];
424 my $authtypecode = $authtypecodes[$i];
425 my $value_builder = $value_builder[$i];
426 my $hidden = $hidden[$i]; #input->param("hidden$i");
427 my $isurl = $input->param("isurl$i") ? 1 : 0;
428 my $link = $link[$i];
429 my $defaultvalue = $defaultvalue[$i];
432 unless ( C4::Context->config('demo') eq 1 ) {
457 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
460 # END $OP eq ADD_VALIDATE
461 ################## DELETE_CONFIRM ##################################
462 # called by default form, used to confirm deletion of data in DB
464 elsif ( $op eq 'delete_confirm' ) {
465 my $dbh = C4::Context->dbh;
468 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
471 #FIXME : called with 2 bind variables when 3 are needed
472 $sth->execute( $tagfield, $tagsubfield );
473 my $data = $sth->fetchrow_hashref;
476 liblibrarian => $data->{'liblibrarian'},
477 tagsubfield => $data->{'tagsubfield'},
478 delete_link => $script_name,
479 tagfield => $tagfield,
480 tagsubfield => $tagsubfield,
481 frameworkcode => $frameworkcode,
484 # END $OP eq DELETE_CONFIRM
485 ################## DELETE_CONFIRMED ##################################
486 # called by delete_confirm, used to effectively confirm deletion of data in DB
488 elsif ( $op eq 'delete_confirmed' ) {
489 my $dbh = C4::Context->dbh;
490 unless ( C4::Context->config('demo') eq 1 ) {
493 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
495 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
499 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
501 $template->param( tagfield => $tagfield );
503 # END $OP eq DELETE_CONFIRMED
504 ################## DEFAULT ##################################
507 my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
512 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
516 if ( $toggle eq 1 ) {
522 my %row_data; # get a fresh hash for the row data
523 $row_data{tagfield} = $results->[$i]{'tagfield'};
524 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
525 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
526 $row_data{kohafield} = $results->[$i]{'kohafield'};
527 $row_data{repeatable} = $results->[$i]{'repeatable'};
528 $row_data{mandatory} = $results->[$i]{'mandatory'};
529 $row_data{tab} = $results->[$i]{'tab'};
530 $row_data{seealso} = $results->[$i]{'seealso'};
531 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
532 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
533 $row_data{value_builder} = $results->[$i]{'value_builder'};
534 $row_data{hidden} = $results->[$i]{'hidden'};
535 $row_data{isurl} = $results->[$i]{'isurl'};
536 $row_data{link} = $results->[$i]{'link'};
538 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
539 . $results->[$i]{'tagsubfield'}
540 . "&frameworkcode=$frameworkcode";
541 $row_data{toggle} = $toggle;
543 if ( $row_data{tab} eq -1 ) {
544 $row_data{subfield_ignored} = 1;
547 push( @loop_data, \%row_data );
549 $template->param( loop => \@loop_data );
551 edit_tagfield => $tagfield,
552 edit_frameworkcode => $frameworkcode
556 my $prevpage = $offset - $pagesize;
558 prev => "<a href=\"$script_name?offset=$prevpage\">" );
560 if ( $offset + $pagesize < $count ) {
561 my $nextpage = $offset + $pagesize;
563 next => "<a href=\"$script_name?offset=$nextpage\">" );
565 } #---- END $OP eq DEFAULT
567 output_html_with_http_headers $input, $cookie, $template->output;