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 );
52 sub marc_subfield_structure_exists {
53 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
54 my $dbh = C4::Context->dbh;
55 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
56 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
61 my $tagfield = $input->param('tagfield');
62 my $tagsubfield = $input->param('tagsubfield');
63 my $frameworkcode = $input->param('frameworkcode');
64 my $pkfield = "tagfield";
65 my $offset = $input->param('offset');
66 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
68 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
70 template_name => "admin/marc_subfields_structure.tmpl",
74 flagsrequired => { parameters => 1 },
79 my $op = $input->param('op');
84 script_name => $script_name,
85 tagfield => $tagfield,
86 frameworkcode => $frameworkcode,
88 ); # we show only the TMPL_VAR names $op
92 script_name => $script_name,
93 tagfield => $tagfield,
94 frameworkcode => $frameworkcode,
96 ); # we show only the TMPL_VAR names $op
99 ################## ADD_FORM ##################################
100 # called by default. Used to create form to add or modify a record
101 if ( $op eq 'add_form' ) {
103 my $dbh = C4::Context->dbh;
104 my $more_subfields = $input->param("more_subfields") + 1;
106 # builds kohafield tables
108 push @kohafields, "";
109 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
111 while ( ( my $field ) = $sth2->fetchrow_array ) {
112 push @kohafields, "biblio." . $field;
114 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
116 while ( ( my $field ) = $sth2->fetchrow_array ) {
117 if ( $field eq 'notes' ) { $field = 'bnotes'; }
118 push @kohafields, "biblioitems." . $field;
120 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
122 while ( ( my $field ) = $sth2->fetchrow_array ) {
123 push @kohafields, "items." . $field;
126 # build authorised value list
128 $sth2 = $dbh->prepare("select distinct category from authorised_values");
130 my @authorised_values;
131 push @authorised_values, "";
132 while ( ( my $category ) = $sth2->fetchrow_array ) {
133 push @authorised_values, $category;
135 push( @authorised_values, "branches" );
136 push( @authorised_values, "itemtypes" );
137 push( @authorised_values, "cn_source" );
139 # build thesaurus categories list
141 $sth2 = $dbh->prepare("select authtypecode from auth_types");
145 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
146 push @authtypes, $authtypecode;
149 # build value_builder list
150 my @value_builder = ('');
152 # read value_builder directory.
153 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
154 # on a standard install, /cgi-bin need to be added.
155 # test one, then the other
156 my $cgidir = C4::Context->intranetdir . "/cgi-bin";
157 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
158 $cgidir = C4::Context->intranetdir;
159 opendir( DIR, "$cgidir/cataloguing/value_builder" )
160 || die "can't opendir $cgidir/value_builder: $!";
162 while ( my $line = readdir(DIR) ) {
163 if ( $line =~ /\.pl$/ ) {
164 push( @value_builder, $line );
167 @value_builder= sort {$a cmp $b} @value_builder;
173 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
174 ); # and tagsubfield='$tagsubfield'");
175 $sth->execute( $tagfield, $frameworkcode );
179 while ( $data = $sth->fetchrow_hashref ) {
180 my %row_data; # get a fresh hash for the row data
181 if ( $toggle eq 1 ) {
187 $row_data{defaultvalue} = $data->{defaultvalue};
188 $row_data{tab} = CGI::scrolling_list(
192 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
205 '10' => 'items (10)',
207 -default => $data->{'tab'},
211 $row_data{tagsubfield} =
212 $data->{'tagsubfield'}
213 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
214 . $data->{'tagsubfield'}
215 . "\" id=\"tagsubfield\" />";
216 $row_data{subfieldcode} = $data->{'tagsubfield'} eq '@'?'_':$data->{'tagsubfield'};
217 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
218 $row_data{liblibrarian} = CGI::escapeHTML( $data->{'liblibrarian'} );
219 $row_data{libopac} = CGI::escapeHTML( $data->{'libopac'} );
220 $row_data{seealso} = CGI::escapeHTML( $data->{'seealso'} );
221 $row_data{kohafield} = CGI::scrolling_list(
222 -name => "kohafield",
223 -id => "kohafield$i",
224 -values => \@kohafields,
225 -default => "$data->{'kohafield'}",
229 $row_data{authorised_value} = CGI::scrolling_list(
230 -name => "authorised_value",
231 -id => "authorised_value$i",
232 -values => \@authorised_values,
233 -default => $data->{'authorised_value'},
237 $row_data{value_builder} = CGI::scrolling_list(
238 -name => "value_builder",
239 -id => "value_builder$i",
240 -values => \@value_builder,
241 -default => $data->{'value_builder'},
245 $row_data{authtypes} = CGI::scrolling_list(
246 -name => "authtypecode",
247 -id => "authtypecode$i",
248 -values => \@authtypes,
249 -default => $data->{'authtypecode'},
253 $row_data{repeatable} = CGI::checkbox(
254 -name => "repeatable$i",
255 -checked => $data->{'repeatable'} ? 'checked' : '',
258 -id => "repeatable$i"
260 $row_data{mandatory} = CGI::checkbox(
261 -name => "mandatory$i",
262 -checked => $data->{'mandatory'} ? 'checked' : '',
267 $row_data{hidden} = CGI::escapeHTML( $data->{hidden} );
268 $row_data{isurl} = CGI::checkbox(
271 -checked => $data->{'isurl'} ? 'checked' : '',
276 $row_data{toggle} = $toggle;
277 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
278 push( @loop_data, \%row_data );
282 # add more_subfields empty lines for add if needed
283 for ( my $j = 1 ; $j <= 1 ; $j++ ) {
284 my %row_data; # get a fresh hash for the row data
285 $row_data{'new_subfield'} = 1;
286 $row_data{'subfieldcode'} = '';
288 $row_data{tab} = CGI::scrolling_list(
292 [ '-1', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ],
305 '10' => 'items (10)',
311 $row_data{tagsubfield} =
312 "<input type=\"text\" name=\"tagsubfield\" value=\""
313 . $data->{'tagsubfield'}
314 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
315 $row_data{liblibrarian} = "";
316 $row_data{libopac} = "";
317 $row_data{seealso} = "";
318 $row_data{kohafield} = CGI::scrolling_list(
319 -name => 'kohafield',
320 -id => "kohafield$j",
321 -values => \@kohafields,
326 $row_data{hidden} = "";
327 $row_data{repeatable} = CGI::checkbox(
328 -name => "repeatable$j",
329 -id => "repeatable$j",
334 $row_data{mandatory} = CGI::checkbox(
335 -name => "mandatory$j",
336 -id => "mandatory$j",
341 $row_data{isurl} = CGI::checkbox(
348 $row_data{value_builder} = CGI::scrolling_list(
349 -name => "value_builder",
350 -id => "value_builder$j",
351 -values => \@value_builder,
352 -default => $data->{'value_builder'},
356 $row_data{authorised_value} = CGI::scrolling_list(
357 -name => "authorised_value",
358 -id => "authorised_value$j",
359 -values => \@authorised_values,
363 $row_data{authtypes} = CGI::scrolling_list(
364 -name => "authtypecode",
365 -id => "authtypecode$j",
366 -values => \@authtypes,
370 $row_data{link} = CGI::escapeHTML( $data->{'link'} );
371 $row_data{toggle} = $toggle;
373 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",
380 tagfield => $tagfield,
382 more_subfields => $more_subfields,
383 more_tag => $tagfield
386 # END $OP eq ADD_FORM
387 ################## ADD_VALIDATE ##################################
388 # called by add_form, used to insert/modify data in DB
390 elsif ( $op eq 'add_validate' ) {
391 my $dbh = C4::Context->dbh;
392 $template->param( tagfield => "$input->param('tagfield')" );
393 # my $sth = $dbh->prepare(
394 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
395 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
397 my $sth_insert = $dbh->prepare(qq{
398 insert into marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
399 values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
401 my $sth_update = $dbh->prepare(qq{
402 update marc_subfield_structure set tagfield=?, tagsubfield=?, liblibrarian=?, libopac=?, repeatable=?, mandatory=?, kohafield=?, tab=?, seealso=?, authorised_value=?, authtypecode=?, value_builder=?, hidden=?, isurl=?, frameworkcode=?, link=?, defaultvalue=?
403 where tagfield=? and tagsubfield=? and frameworkcode=?
405 my @tagsubfield = $input->param('tagsubfield');
406 my @liblibrarian = $input->param('liblibrarian');
407 my @libopac = $input->param('libopac');
408 my @kohafield = $input->param('kohafield');
409 my @tab = $input->param('tab');
410 my @seealso = $input->param('seealso');
411 my @hidden = $input->param('hidden');
412 my @authorised_values = $input->param('authorised_value');
413 my @authtypecodes = $input->param('authtypecode');
414 my @value_builder = $input->param('value_builder');
415 my @link = $input->param('link');
416 my @defaultvalue = $input->param('defaultvalue');
418 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
419 my $tagfield = $input->param('tagfield');
420 my $tagsubfield = $tagsubfield[$i];
421 $tagsubfield = "@" unless $tagsubfield ne '';
422 $tagsubfield = "@" if $tagsubfield eq '_';
423 my $liblibrarian = $liblibrarian[$i];
424 my $libopac = $libopac[$i];
425 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
426 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
427 my $kohafield = $kohafield[$i];
429 my $seealso = $seealso[$i];
430 my $authorised_value = $authorised_values[$i];
431 my $authtypecode = $authtypecodes[$i];
432 my $value_builder = $value_builder[$i];
433 my $hidden = $hidden[$i]; #input->param("hidden$i");
434 my $isurl = $input->param("isurl$i") ? 1 : 0;
435 my $link = $link[$i];
436 my $defaultvalue = $defaultvalue[$i];
439 unless ( C4::Context->config('demo') eq 1 ) {
440 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
441 $sth_update->execute(
466 $sth_insert->execute(
492 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
495 # END $OP eq ADD_VALIDATE
496 ################## DELETE_CONFIRM ##################################
497 # called by default form, used to confirm deletion of data in DB
499 elsif ( $op eq 'delete_confirm' ) {
500 my $dbh = C4::Context->dbh;
503 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
506 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
507 my $data = $sth->fetchrow_hashref;
510 liblibrarian => $data->{'liblibrarian'},
511 tagsubfield => $data->{'tagsubfield'},
512 delete_link => $script_name,
513 tagfield => $tagfield,
514 tagsubfield => $tagsubfield,
515 frameworkcode => $frameworkcode,
518 # END $OP eq DELETE_CONFIRM
519 ################## DELETE_CONFIRMED ##################################
520 # called by delete_confirm, used to effectively confirm deletion of data in DB
522 elsif ( $op eq 'delete_confirmed' ) {
523 my $dbh = C4::Context->dbh;
524 unless ( C4::Context->config('demo') eq 1 ) {
527 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
529 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
533 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
535 $template->param( tagfield => $tagfield );
537 # END $OP eq DELETE_CONFIRMED
538 ################## DEFAULT ##################################
541 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
546 $i < ( $offset + $pagesize < $count ? $offset + $pagesize : $count ) ;
550 if ( $toggle eq 1 ) {
556 my %row_data; # get a fresh hash for the row data
557 $row_data{tagfield} = $results->[$i]{'tagfield'};
558 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
559 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
560 $row_data{kohafield} = $results->[$i]{'kohafield'};
561 $row_data{repeatable} = $results->[$i]{'repeatable'};
562 $row_data{mandatory} = $results->[$i]{'mandatory'};
563 $row_data{tab} = $results->[$i]{'tab'};
564 $row_data{seealso} = $results->[$i]{'seealso'};
565 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
566 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
567 $row_data{value_builder} = $results->[$i]{'value_builder'};
568 $row_data{hidden} = $results->[$i]{'hidden'};
569 $row_data{isurl} = $results->[$i]{'isurl'};
570 $row_data{link} = $results->[$i]{'link'};
572 "$script_name?op=delete_confirm&tagfield=$tagfield&tagsubfield="
573 . $results->[$i]{'tagsubfield'}
574 . "&frameworkcode=$frameworkcode";
575 $row_data{toggle} = $toggle;
577 if ( $row_data{tab} eq -1 ) {
578 $row_data{subfield_ignored} = 1;
581 push( @loop_data, \%row_data );
583 $template->param( loop => \@loop_data );
585 edit_tagfield => $tagfield,
586 edit_frameworkcode => $frameworkcode
590 my $prevpage = $offset - $pagesize;
592 prev => "<a href=\"$script_name?offset=$prevpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
594 if ( $offset + $pagesize < $count ) {
595 my $nextpage = $offset + $pagesize;
597 next => "<a href=\"$script_name?offset=$nextpage\&tagfield=$tagfield\&frameworkcode=$frameworkcode \">" );
599 } #---- END $OP eq DEFAULT
601 output_html_with_http_headers $input, $cookie, $template->output;