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 $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 $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" );
129 push( @authorised_values, "cn_source" );
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 );
159 @value_builder= sort {$a cmp $b} @value_builder;
165 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
166 ); # and tagsubfield='$tagsubfield'");
167 $sth->execute( $tagfield, $frameworkcode );
171 while ( $data = $sth->fetchrow_hashref ) {
172 my %row_data; # get a fresh hash for the row data
173 if ( $toggle eq 1 ) {
179 $row_data{defaultvalue} = $data->{defaultvalue};
180 $row_data{tab} = CGI::scrolling_list(
184 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
197 '10' => 'items (10)',
199 -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'} eq '@'?'_':$data->{'tagsubfield'};
209 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
210 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
211 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
212 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
213 $row_data{kohafield} = CGI::scrolling_list(
214 -name => "kohafield",
215 -id => "kohafield$i",
216 -values => \@kohafields,
217 -default => "$data->{'kohafield'}",
221 $row_data{authorised_value} = CGI::scrolling_list(
222 -name => "authorised_value",
223 -id => "authorised_value$i",
224 -values => \@authorised_values,
225 -default => $data->{'authorised_value'},
229 $row_data{value_builder} = CGI::scrolling_list(
230 -name => "value_builder",
231 -id => "value_builder$i",
232 -values => \@value_builder,
233 -default => $data->{'value_builder'},
237 $row_data{authtypes} = CGI::scrolling_list(
238 -name => "authtypecode",
239 -id => "authtypecode$i",
240 -values => \@authtypes,
241 -default => $data->{'authtypecode'},
245 $row_data{repeatable} = CGI::checkbox(
246 -name => "repeatable$i",
247 -checked => $data->{'repeatable'} ? 'checked' : '',
250 -id => "repeatable$i"
252 $row_data{mandatory} = CGI::checkbox(
253 -name => "mandatory$i",
254 -checked => $data->{'mandatory'} ? 'checked' : '',
259 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
260 $row_data{isurl} = CGI::checkbox(
263 -checked => $data->{'isurl'} ? 'checked' : '',
268 $row_data{toggle} = $toggle;
269 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
270 push( @loop_data, \%row_data );
274 # add more_subfields empty lines for add if needed
275 for ( my $j = 1 ; $j <= 1 ; $j++ ) {
276 my %row_data; # get a fresh hash for the row data
277 $row_data{'new_subfield'} = 1;
278 $row_data{'subfieldcode'} = '';
280 $row_data{tab} = CGI::scrolling_list(
284 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
297 '10' => 'items (10)',
303 $row_data{tagsubfield} =
304 "<input type=\"text\" name=\"tagsubfield\" value=\""
305 . $data->{'tagsubfield'}
306 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
307 $row_data{liblibrarian} = "";
308 $row_data{libopac} = "";
309 $row_data{seealso} = "";
310 $row_data{kohafield} = CGI::scrolling_list(
311 -name => 'kohafield',
312 -id => "kohafield$j",
313 -values => \@kohafields,
318 $row_data{hidden} = "";
319 $row_data{repeatable} = CGI::checkbox(
320 -name => "repeatable$j",
321 -id => "repeatable$j",
326 $row_data{mandatory} = CGI::checkbox(
327 -name => "mandatory$j",
328 -id => "mandatory$j",
333 $row_data{isurl} = CGI::checkbox(
340 $row_data{value_builder} = CGI::scrolling_list(
341 -name => "value_builder",
342 -id => "value_builder$j",
343 -values => \@value_builder,
344 -default => $data->{'value_builder'},
348 $row_data{authorised_value} = CGI::scrolling_list(
349 -name => "authorised_value",
350 -id => "authorised_value$j",
351 -values => \@authorised_values,
355 $row_data{authtypes} = CGI::scrolling_list(
356 -name => "authtypecode",
357 -id => "authtypecode$j",
358 -values => \@authtypes,
362 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
363 $row_data{toggle} = $toggle;
365 push( @loop_data, \%row_data );
368 $template->param( 'use-heading-flags-p' => 1 );
369 $template->param( 'heading-edit-subfields-p' => 1 );
371 action => "Edit subfields",
372 tagfield => $tagfield,
374 more_subfields => $more_subfields,
375 more_tag => $tagfield
378 # END $OP eq ADD_FORM
379 ################## ADD_VALIDATE ##################################
380 # called by add_form, used to insert/modify data in DB
382 elsif ( $op eq 'add_validate' ) {
383 my $dbh = C4::Context->dbh;
384 $template->param( tagfield => "$input->param('tagfield')" );
385 my $sth = $dbh->prepare(
386 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
387 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
389 my @tagsubfield = $input->param('tagsubfield');
390 my @liblibrarian = $input->param('liblibrarian');
391 my @libopac = $input->param('libopac');
392 my @kohafield = $input->param('kohafield');
393 my @tab = $input->param('tab');
394 my @seealso = $input->param('seealso');
395 my @hidden = $input->param('hidden');
396 my @authorised_values = $input->param('authorised_value');
397 my @authtypecodes = $input->param('authtypecode');
398 my @value_builder = $input->param('value_builder');
399 my @link = $input->param('link');
400 my @defaultvalue = $input->param('defaultvalue');
402 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
403 my $tagfield = $input->param('tagfield');
404 my $tagsubfield = $tagsubfield[$i];
405 $tagsubfield = "@" unless $tagsubfield ne '';
406 $tagsubfield = "@" if $tagsubfield eq '_';
407 my $liblibrarian = $liblibrarian[$i];
408 my $libopac = $libopac[$i];
409 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
410 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
411 my $kohafield = $kohafield[$i];
413 my $seealso = $seealso[$i];
414 my $authorised_value = $authorised_values[$i];
415 my $authtypecode = $authtypecodes[$i];
416 my $value_builder = $value_builder[$i];
417 my $hidden = $hidden[$i]; #input->param("hidden$i");
418 my $isurl = $input->param("isurl$i") ? 1 : 0;
419 my $link = $link[$i];
420 my $defaultvalue = $defaultvalue[$i];
423 unless ( C4::Context->config('demo') eq 1 ) {
448 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
451 # END $OP eq ADD_VALIDATE
452 ################## DELETE_CONFIRM ##################################
453 # called by default form, used to confirm deletion of data in DB
455 elsif ( $op eq 'delete_confirm' ) {
456 my $dbh = C4::Context->dbh;
459 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
462 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
463 my $data = $sth->fetchrow_hashref;
466 liblibrarian => $data->{'liblibrarian'},
467 tagsubfield => $data->{'tagsubfield'},
468 delete_link => $script_name,
469 tagfield => $tagfield,
470 tagsubfield => $tagsubfield,
471 frameworkcode => $frameworkcode,
474 # END $OP eq DELETE_CONFIRM
475 ################## DELETE_CONFIRMED ##################################
476 # called by delete_confirm, used to effectively confirm deletion of data in DB
478 elsif ( $op eq 'delete_confirmed' ) {
479 my $dbh = C4::Context->dbh;
480 unless ( C4::Context->config('demo') eq 1 ) {
483 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
485 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
489 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
491 $template->param( tagfield => $tagfield );
493 # END $OP eq DELETE_CONFIRMED
494 ################## DEFAULT ##################################
497 my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
502 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
506 if ( $toggle eq 1 ) {
512 my %row_data; # get a fresh hash for the row data
513 $row_data{tagfield} = $results->[$i]{'tagfield'};
514 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
515 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
516 $row_data{kohafield} = $results->[$i]{'kohafield'};
517 $row_data{repeatable} = $results->[$i]{'repeatable'};
518 $row_data{mandatory} = $results->[$i]{'mandatory'};
519 $row_data{tab} = $results->[$i]{'tab'};
520 $row_data{seealso} = $results->[$i]{'seealso'};
521 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
522 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
523 $row_data{value_builder} = $results->[$i]{'value_builder'};
524 $row_data{hidden} = $results->[$i]{'hidden'};
525 $row_data{isurl} = $results->[$i]{'isurl'};
526 $row_data{link} = $results->[$i]{'link'};
528 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
529 . $results->[$i]{'tagsubfield'}
530 . "&frameworkcode=$frameworkcode";
531 $row_data{toggle} = $toggle;
533 if ( $row_data{tab} eq -1 ) {
534 $row_data{subfield_ignored} = 1;
537 push( @loop_data, \%row_data );
539 $template->param( loop => \@loop_data );
541 edit_tagfield => $tagfield,
542 edit_frameworkcode => $frameworkcode
546 my $prevpage = $offset - $pagesize;
548 prev => "<a href=\"$script_name?offset=$prevpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
550 if ( $offset + $pagesize < $count ) {
551 my $nextpage = $offset + $pagesize;
553 next => "<a href=\"$script_name?offset=$nextpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
555 } #---- END $OP eq DEFAULT
557 output_html_with_http_headers $input, $cookie, $template->output;