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{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 = $i ; $j <= $more_subfields + $i ; $j++ ) {
282 my %row_data; # get a fresh hash for the row data
283 $row_data{tab} = CGI::scrolling_list(
287 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
300 '10' => 'items (10)',
307 $row_data{tagsubfield} =
308 "<input type=\"text\" name=\"tagsubfield\" value=\""
309 . $data->{'tagsubfield'}
310 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
311 $row_data{liblibrarian} = "";
312 $row_data{libopac} = "";
313 $row_data{seealso} = "";
314 $row_data{kohafield} = CGI::scrolling_list(
315 -name => 'kohafield',
316 -id => "kohafield$j",
317 -values => \@kohafields,
323 $row_data{hidden} = "";
324 $row_data{repeatable} = CGI::checkbox(
325 -name => "repeatable$j",
326 -id => "repeatable$j",
332 $row_data{mandatory} = CGI::checkbox(
333 -name => "mandatory$j",
334 -id => "mandatory$j",
340 $row_data{isurl} = CGI::checkbox(
348 $row_data{value_builder} = CGI::scrolling_list(
349 -name => 'value_builder',
350 -id => 'value_builder',
351 -values => \@value_builder,
352 -default => $data->{'value_builder'},
357 $row_data{authorised_value} = CGI::scrolling_list(
358 -name => 'authorised_value',
359 -id => 'authorised_value',
360 -values => \@authorised_values,
365 $row_data{authtypes} = CGI::scrolling_list(
366 -name => 'authtypecode',
367 -id => 'authtypecode',
368 -values => \@authtypes,
373 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
374 $row_data{toggle} = $toggle;
376 push( @loop_data, \%row_data );
378 $template->param( 'use-heading-flags-p' => 1 );
379 $template->param( 'heading-edit-subfields-p' => 1 );
381 action => "Edit subfields",
383 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
385 more_subfields => $more_subfields,
386 more_tag => $tagfield
389 # END $OP eq ADD_FORM
390 ################## ADD_VALIDATE ##################################
391 # called by add_form, used to insert/modify data in DB
393 elsif ( $op eq 'add_validate' ) {
394 my $dbh = C4::Context->dbh;
395 $template->param( tagfield => "$input->param('tagfield')" );
396 my $sth = $dbh->prepare(
397 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
398 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
400 my @tagsubfield = $input->param('tagsubfield');
401 my @liblibrarian = $input->param('liblibrarian');
402 my @libopac = $input->param('libopac');
403 my @kohafield = $input->param('kohafield');
404 my @tab = $input->param('tab');
405 my @seealso = $input->param('seealso');
406 my @hidden = $input->param('hidden');
407 my @authorised_values = $input->param('authorised_value');
408 my @authtypecodes = $input->param('authtypecode');
409 my @value_builder = $input->param('value_builder');
410 my @link = $input->param('link');
411 my @defaultvalue = $input->param('defaultvalue');
413 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
414 my $tagfield = $input->param('tagfield');
415 my $tagsubfield = $tagsubfield[$i];
416 $tagsubfield = "@" unless $tagsubfield ne '';
417 my $liblibrarian = $liblibrarian[$i];
418 my $libopac = $libopac[$i];
419 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
420 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
421 my $kohafield = $kohafield[$i];
423 my $seealso = $seealso[$i];
424 my $authorised_value = $authorised_values[$i];
425 my $authtypecode = $authtypecodes[$i];
426 my $value_builder = $value_builder[$i];
427 my $hidden = $hidden[$i]; #input->param("hidden$i");
428 my $isurl = $input->param("isurl$i") ? 1 : 0;
429 my $link = $link[$i];
430 my $defaultvalue = $defaultvalue[$i];
433 unless ( C4::Context->config('demo') eq 1 ) {
458 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
461 # END $OP eq ADD_VALIDATE
462 ################## DELETE_CONFIRM ##################################
463 # called by default form, used to confirm deletion of data in DB
465 elsif ( $op eq 'delete_confirm' ) {
466 my $dbh = C4::Context->dbh;
469 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
472 #FIXME : called with 2 bind variables when 3 are needed
473 $sth->execute( $tagfield, $tagsubfield );
474 my $data = $sth->fetchrow_hashref;
477 liblibrarian => $data->{'liblibrarian'},
478 tagsubfield => $data->{'tagsubfield'},
479 delete_link => $script_name,
480 tagfield => $tagfield,
481 tagsubfield => $tagsubfield,
482 frameworkcode => $frameworkcode,
485 # END $OP eq DELETE_CONFIRM
486 ################## DELETE_CONFIRMED ##################################
487 # called by delete_confirm, used to effectively confirm deletion of data in DB
489 elsif ( $op eq 'delete_confirmed' ) {
490 my $dbh = C4::Context->dbh;
491 unless ( C4::Context->config('demo') eq 1 ) {
494 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
496 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
500 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
502 $template->param( tagfield => $tagfield );
504 # END $OP eq DELETE_CONFIRMED
505 ################## DEFAULT ##################################
509 my ( $count, $results ) = StringSearch( $env, $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;