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 = 1 ; $j <= 1 ; $j++ ) {
281 my %row_data; # get a fresh hash for the row data
282 $row_data{'subfieldcode'} = '';
284 $row_data{tab} = CGI::scrolling_list(
288 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
301 '10' => 'items (10)',
308 $row_data{tagsubfield} =
309 "<input type=\"text\" name=\"tagsubfield\" value=\""
310 . $data->{'tagsubfield'}
311 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\">";
312 $row_data{liblibrarian} = "";
313 $row_data{libopac} = "";
314 $row_data{seealso} = "";
315 $row_data{kohafield} = CGI::scrolling_list(
316 -name => 'kohafield',
317 -id => "kohafield$j",
318 -values => \@kohafields,
324 $row_data{hidden} = "";
325 $row_data{repeatable} = CGI::checkbox(
326 -name => "repeatable$j",
327 -id => "repeatable$j",
333 $row_data{mandatory} = CGI::checkbox(
334 -name => "mandatory$j",
335 -id => "mandatory$j",
341 $row_data{isurl} = CGI::checkbox(
349 $row_data{value_builder} = CGI::scrolling_list(
350 -name => 'value_builder',
351 -id => 'value_builder',
352 -values => \@value_builder,
353 -default => $data->{'value_builder'},
358 $row_data{authorised_value} = CGI::scrolling_list(
359 -name => 'authorised_value',
360 -id => 'authorised_value',
361 -values => \@authorised_values,
366 $row_data{authtypes} = CGI::scrolling_list(
367 -name => 'authtypecode',
368 -id => 'authtypecode',
369 -values => \@authtypes,
374 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
375 $row_data{toggle} = $toggle;
377 push( @loop_data, \%row_data );
379 warn "push : ".Dumper(%row_data);
381 $template->param( 'use-heading-flags-p' => 1 );
382 $template->param( 'heading-edit-subfields-p' => 1 );
384 action => "Edit subfields",
386 "<input type=\"hidden\" name=\"tagfield\" value=\"$tagfield\">$tagfield",
388 more_subfields => $more_subfields,
389 more_tag => $tagfield
392 # END $OP eq ADD_FORM
393 ################## ADD_VALIDATE ##################################
394 # called by add_form, used to insert/modify data in DB
396 elsif ( $op eq 'add_validate' ) {
397 my $dbh = C4::Context->dbh;
398 $template->param( tagfield => "$input->param('tagfield')" );
399 my $sth = $dbh->prepare(
400 "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
401 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
403 my @tagsubfield = $input->param('tagsubfield');
404 my @liblibrarian = $input->param('liblibrarian');
405 my @libopac = $input->param('libopac');
406 my @kohafield = $input->param('kohafield');
407 my @tab = $input->param('tab');
408 my @seealso = $input->param('seealso');
409 my @hidden = $input->param('hidden');
410 my @authorised_values = $input->param('authorised_value');
411 my @authtypecodes = $input->param('authtypecode');
412 my @value_builder = $input->param('value_builder');
413 my @link = $input->param('link');
414 my @defaultvalue = $input->param('defaultvalue');
416 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
417 my $tagfield = $input->param('tagfield');
418 my $tagsubfield = $tagsubfield[$i];
419 $tagsubfield = "@" unless $tagsubfield ne '';
420 my $liblibrarian = $liblibrarian[$i];
421 my $libopac = $libopac[$i];
422 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
423 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
424 my $kohafield = $kohafield[$i];
426 my $seealso = $seealso[$i];
427 my $authorised_value = $authorised_values[$i];
428 my $authtypecode = $authtypecodes[$i];
429 my $value_builder = $value_builder[$i];
430 my $hidden = $hidden[$i]; #input->param("hidden$i");
431 my $isurl = $input->param("isurl$i") ? 1 : 0;
432 my $link = $link[$i];
433 my $defaultvalue = $defaultvalue[$i];
436 unless ( C4::Context->config('demo') eq 1 ) {
461 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
464 # END $OP eq ADD_VALIDATE
465 ################## DELETE_CONFIRM ##################################
466 # called by default form, used to confirm deletion of data in DB
468 elsif ( $op eq 'delete_confirm' ) {
469 my $dbh = C4::Context->dbh;
472 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
475 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
476 my $data = $sth->fetchrow_hashref;
479 liblibrarian => $data->{'liblibrarian'},
480 tagsubfield => $data->{'tagsubfield'},
481 delete_link => $script_name,
482 tagfield => $tagfield,
483 tagsubfield => $tagsubfield,
484 frameworkcode => $frameworkcode,
487 # END $OP eq DELETE_CONFIRM
488 ################## DELETE_CONFIRMED ##################################
489 # called by delete_confirm, used to effectively confirm deletion of data in DB
491 elsif ( $op eq 'delete_confirmed' ) {
492 my $dbh = C4::Context->dbh;
493 unless ( C4::Context->config('demo') eq 1 ) {
496 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
498 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
502 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
504 $template->param( tagfield => $tagfield );
506 # END $OP eq DELETE_CONFIRMED
507 ################## DEFAULT ##################################
510 my ( $count, $results ) = StringSearch( $tagfield, $frameworkcode );
515 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
519 if ( $toggle eq 1 ) {
525 my %row_data; # get a fresh hash for the row data
526 $row_data{tagfield} = $results->[$i]{'tagfield'};
527 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
528 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
529 $row_data{kohafield} = $results->[$i]{'kohafield'};
530 $row_data{repeatable} = $results->[$i]{'repeatable'};
531 $row_data{mandatory} = $results->[$i]{'mandatory'};
532 $row_data{tab} = $results->[$i]{'tab'};
533 $row_data{seealso} = $results->[$i]{'seealso'};
534 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
535 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
536 $row_data{value_builder} = $results->[$i]{'value_builder'};
537 $row_data{hidden} = $results->[$i]{'hidden'};
538 $row_data{isurl} = $results->[$i]{'isurl'};
539 $row_data{link} = $results->[$i]{'link'};
541 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
542 . $results->[$i]{'tagsubfield'}
543 . "&frameworkcode=$frameworkcode";
544 $row_data{toggle} = $toggle;
546 if ( $row_data{tab} eq -1 ) {
547 $row_data{subfield_ignored} = 1;
550 push( @loop_data, \%row_data );
552 $template->param( loop => \@loop_data );
554 edit_tagfield => $tagfield,
555 edit_frameworkcode => $frameworkcode
559 my $prevpage = $offset - $pagesize;
561 prev => "<a href=\"$script_name?offset=$prevpage\">" );
563 if ( $offset + $pagesize < $count ) {
564 my $nextpage = $offset + $pagesize;
566 next => "<a href=\"$script_name?offset=$nextpage\">" );
568 } #---- END $OP eq DEFAULT
570 output_html_with_http_headers $input, $cookie, $template->output;