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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 #use warnings; FIXME - Bug 2505
27 use Koha
::Authority
::Types
;
29 use List
::MoreUtils
qw( uniq );
32 my ( $searchstring, $frameworkcode ) = @_;
33 my $dbh = C4
::Context
->dbh;
34 $searchstring =~ s/\'/\\\'/g;
35 my @data = split( ' ', $searchstring );
39 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
41 $sth->execute( "$searchstring%", $frameworkcode );
46 while ( my $data = $sth->fetchrow_hashref ) {
47 push( @results, $data );
52 return ( $cnt, \
@results );
55 sub marc_subfield_structure_exists
{
56 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
57 my $dbh = C4
::Context
->dbh;
58 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
59 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
64 my $tagfield = $input->param('tagfield');
65 my $tagsubfield = $input->param('tagsubfield');
66 my $frameworkcode = $input->param('frameworkcode');
67 my $pkfield = "tagfield";
68 my $offset = $input->param('offset');
69 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
71 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
73 template_name
=> "admin/marc_subfields_structure.tt",
77 flagsrequired
=> { parameters
=> 'parameters_remaining_permissions' },
81 my $cache = Koha
::Cache
->get_instance();
83 my $op = $input->param('op');
88 script_name
=> $script_name,
89 tagfield
=> $tagfield,
90 frameworkcode
=> $frameworkcode,
92 ); # we show only the TMPL_VAR names $op
96 script_name
=> $script_name,
97 tagfield
=> $tagfield,
98 frameworkcode
=> $frameworkcode,
100 ); # we show only the TMPL_VAR names $op
103 ################## ADD_FORM ##################################
104 # called by default. Used to create form to add or modify a record
105 if ( $op eq 'add_form' ) {
107 my $dbh = C4
::Context
->dbh;
108 my $more_subfields = $input->param("more_subfields") + 1;
110 # builds kohafield tables
112 push @kohafields, "";
113 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
115 while ( ( my $field ) = $sth2->fetchrow_array ) {
116 push @kohafields, "biblio." . $field;
118 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
120 while ( ( my $field ) = $sth2->fetchrow_array ) {
121 if ( $field eq 'notes' ) { $field = 'bnotes'; }
122 push @kohafields, "biblioitems." . $field;
124 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
126 while ( ( my $field ) = $sth2->fetchrow_array ) {
127 push @kohafields, "items." . $field;
130 # build authorised value list
132 $sth2 = $dbh->prepare("select distinct category from authorised_values");
134 my @authorised_values;
135 push @authorised_values, "";
136 while ( ( my $category ) = $sth2->fetchrow_array ) {
137 push @authorised_values, $category;
139 push( @authorised_values, "branches" );
140 push( @authorised_values, "itemtypes" );
141 push( @authorised_values, "cn_source" );
143 # build thesaurus categories list
144 my @authtypes = uniq
( "", map { $_->authtypecode } Koha
::Authority
::Types
->search );
146 # build value_builder list
147 my @value_builder = ('');
149 # read value_builder directory.
150 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
151 # on a standard install, /cgi-bin need to be added.
152 # test one, then the other
153 my $cgidir = C4
::Context
->config('intranetdir') . "/cgi-bin";
154 unless ( opendir( DIR
, "$cgidir/cataloguing/value_builder" ) ) {
155 $cgidir = C4
::Context
->config('intranetdir');
156 opendir( DIR
, "$cgidir/cataloguing/value_builder" )
157 || die "can't opendir $cgidir/value_builder: $!";
159 while ( my $line = readdir(DIR
) ) {
160 if ( $line =~ /\.pl$/ &&
161 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
162 push( @value_builder, $line );
165 @value_builder= sort {$a cmp $b} @value_builder;
171 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
172 ); # and tagsubfield='$tagsubfield'");
173 $sth->execute( $tagfield, $frameworkcode );
176 while ( $data = $sth->fetchrow_hashref ) {
177 my %row_data; # get a fresh hash for the row data
178 $row_data{defaultvalue
} = $data->{defaultvalue
};
179 $row_data{maxlength
} = $data->{maxlength
};
182 default => $data->{'tab'},
185 $row_data{tagsubfield
} =
186 $data->{'tagsubfield'}
187 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
188 . $data->{'tagsubfield'}
189 . "\" id=\"tagsubfield\" />";
190 $row_data{subfieldcode
} = $data->{'tagsubfield'} eq '@'?
'_':$data->{'tagsubfield'};
191 $row_data{urisubfieldcode
} = $row_data{subfieldcode
} eq '%' ?
'pct' : $row_data{subfieldcode
};
192 $row_data{liblibrarian
} = CGI
::escapeHTML
( $data->{'liblibrarian'} );
193 $row_data{libopac
} = CGI
::escapeHTML
( $data->{'libopac'} );
194 $row_data{seealso
} = CGI
::escapeHTML
( $data->{'seealso'} );
195 $row_data{kohafield
} = {
197 values => \
@kohafields,
198 default => "$data->{'kohafield'}",
200 $row_data{authorised_value
} = {
201 id
=> "authorised_value$i",
202 values => \
@authorised_values,
203 default => $data->{'authorised_value'},
205 $row_data{value_builder
} = {
206 id
=> "value_builder$i",
207 values => \
@value_builder,
208 default => $data->{'value_builder'},
210 $row_data{authtypes
} = {
211 id
=> "authtypecode$i",
212 values => \
@authtypes,
213 default => $data->{'authtypecode'},
215 $row_data{repeatable
} = CGI
::checkbox
(
216 -name
=> "repeatable$i",
217 -checked
=> $data->{'repeatable'} ?
'checked' : '',
220 -id
=> "repeatable$i"
222 $row_data{mandatory
} = CGI
::checkbox
(
223 -name
=> "mandatory$i",
224 -checked
=> $data->{'mandatory'} ?
'checked' : '',
229 $row_data{hidden
} = CGI
::escapeHTML
( $data->{hidden
} );
230 $row_data{isurl
} = CGI
::checkbox
(
233 -checked
=> $data->{'isurl'} ?
'checked' : '',
238 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
239 push( @loop_data, \
%row_data );
243 # add more_subfields empty lines for add if needed
244 my %row_data; # get a fresh hash for the row data
245 $row_data{'new_subfield'} = 1;
246 $row_data{'subfieldcode'} = '';
247 $row_data{'maxlength'} = 9999;
251 default => $data->{'tab'},
253 $row_data{tagsubfield
} =
254 "<input type=\"text\" name=\"tagsubfield\" value=\""
255 . $data->{'tagsubfield'}
256 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
257 $row_data{liblibrarian
} = "";
258 $row_data{libopac
} = "";
259 $row_data{seealso
} = "";
260 $row_data{kohafield
} = {
262 values => \
@kohafields,
263 default => "$data->{'kohafield'}",
265 $row_data{hidden
} = "";
266 $row_data{repeatable
} = CGI
::checkbox
(
267 -name
=> "repeatable$i",
268 -id
=> "repeatable$i",
273 $row_data{mandatory
} = CGI
::checkbox
(
274 -name
=> "mandatory$i",
275 -id
=> "mandatory$i",
280 $row_data{isurl
} = CGI
::checkbox
(
287 $row_data{value_builder
} = {
288 id
=> "value_builder$i",
289 values => \
@value_builder,
290 default => $data->{'value_builder'},
292 $row_data{authorised_value
} = {
293 id
=> "authorised_value$i",
294 values => \
@authorised_values,
295 default => $data->{'authorised_value'},
297 $row_data{authtypes
} = {
298 id
=> "authtypecode$i",
299 values => \
@authtypes,
300 default => $data->{'authtypecode'},
302 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
304 push( @loop_data, \
%row_data );
306 $template->param( 'use_heading_flags_p' => 1 );
307 $template->param( 'heading_edit_subfields_p' => 1 );
309 action
=> "Edit subfields",
310 tagfield
=> $tagfield,
312 more_subfields
=> $more_subfields,
313 more_tag
=> $tagfield
316 # END $OP eq ADD_FORM
317 ################## ADD_VALIDATE ##################################
318 # called by add_form, used to insert/modify data in DB
320 elsif ( $op eq 'add_validate' ) {
321 my $dbh = C4
::Context
->dbh;
322 $template->param( tagfield
=> "$input->param('tagfield')" );
323 # my $sth = $dbh->prepare(
324 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
325 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
327 my $sth_insert = $dbh->prepare(qq{
328 insert into marc_subfield_structure
(tagfield
,tagsubfield
,liblibrarian
,libopac
,repeatable
,mandatory
,kohafield
,tab
,seealso
,authorised_value
,authtypecode
,value_builder
,hidden
,isurl
,frameworkcode
, link,defaultvalue
,maxlength
)
329 values (?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
331 my $sth_update = $dbh->prepare(qq{
332 update marc_subfield_structure set tagfield
=?
, tagsubfield
=?
, liblibrarian
=?
, libopac
=?
, repeatable
=?
, mandatory
=?
, kohafield
=?
, tab
=?
, seealso
=?
, authorised_value
=?
, authtypecode
=?
, value_builder
=?
, hidden
=?
, isurl
=?
, frameworkcode
=?
, link=?
, defaultvalue
=?
, maxlength
=?
333 where tagfield
=?
and tagsubfield
=?
and frameworkcode
=?
335 my @tagsubfield = $input->param('tagsubfield');
336 my @liblibrarian = $input->param('liblibrarian');
337 my @libopac = $input->param('libopac');
338 my @kohafield = $input->param('kohafield');
339 my @tab = $input->param('tab');
340 my @seealso = $input->param('seealso');
341 my @hidden = $input->param('hidden');
342 my @authorised_values = $input->param('authorised_value');
343 my @authtypecodes = $input->param('authtypecode');
344 my @value_builder = $input->param('value_builder');
345 my @link = $input->param('link');
346 my @defaultvalue = $input->param('defaultvalue');
347 my @maxlength = $input->param('maxlength');
349 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
350 my $tagfield = $input->param('tagfield');
351 my $tagsubfield = $tagsubfield[$i];
352 $tagsubfield = "@" unless $tagsubfield ne '';
353 $tagsubfield = "@" if $tagsubfield eq '_';
354 my $liblibrarian = $liblibrarian[$i];
355 my $libopac = $libopac[$i];
356 my $repeatable = $input->param("repeatable$i") ?
1 : 0;
357 my $mandatory = $input->param("mandatory$i") ?
1 : 0;
358 my $kohafield = $kohafield[$i];
360 my $seealso = $seealso[$i];
361 my $authorised_value = $authorised_values[$i];
362 my $authtypecode = $authtypecodes[$i];
363 my $value_builder = $value_builder[$i];
364 my $hidden = $hidden[$i]; #input->param("hidden$i");
365 my $isurl = $input->param("isurl$i") ?
1 : 0;
366 my $link = $link[$i];
367 my $defaultvalue = $defaultvalue[$i];
368 my $maxlength = $maxlength[$i] ?
$maxlength[$i] : 9999;
370 if (defined($liblibrarian) && $liblibrarian ne "") {
371 unless ( C4
::Context
->config('demo') eq 1 ) {
372 if (marc_subfield_structure_exists
($tagfield, $tagsubfield, $frameworkcode)) {
373 $sth_update->execute(
399 $sth_insert->execute(
425 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
426 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
428 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
431 # END $OP eq ADD_VALIDATE
432 ################## DELETE_CONFIRM ##################################
433 # called by default form, used to confirm deletion of data in DB
435 elsif ( $op eq 'delete_confirm' ) {
436 my $dbh = C4
::Context
->dbh;
439 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
442 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
443 my $data = $sth->fetchrow_hashref;
446 liblibrarian
=> $data->{'liblibrarian'},
447 tagsubfield
=> $data->{'tagsubfield'},
448 delete_link
=> $script_name,
449 tagfield
=> $tagfield,
450 tagsubfield
=> $tagsubfield,
451 frameworkcode
=> $frameworkcode,
454 # END $OP eq DELETE_CONFIRM
455 ################## DELETE_CONFIRMED ##################################
456 # called by delete_confirm, used to effectively confirm deletion of data in DB
458 elsif ( $op eq 'delete_confirmed' ) {
459 my $dbh = C4
::Context
->dbh;
460 unless ( C4
::Context
->config('demo') eq 1 ) {
463 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
465 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
468 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
469 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
470 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
473 # END $OP eq DELETE_CONFIRMED
474 ################## DEFAULT ##################################
477 my ( $count, $results ) = string_search
( $tagfield, $frameworkcode );
479 for ( my $i = 0; $i < $count; $i++ ) {
480 my %row_data; # get a fresh hash for the row data
481 $row_data{tagfield
} = $results->[$i]{'tagfield'};
482 $row_data{tagsubfield
} = $results->[$i]{'tagsubfield'};
483 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
484 $row_data{kohafield
} = $results->[$i]{'kohafield'};
485 $row_data{repeatable
} = $results->[$i]{'repeatable'};
486 $row_data{mandatory
} = $results->[$i]{'mandatory'};
487 $row_data{tab
} = $results->[$i]{'tab'};
488 $row_data{seealso
} = $results->[$i]{'seealso'};
489 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
490 $row_data{authtypecode
} = $results->[$i]{'authtypecode'};
491 $row_data{value_builder
} = $results->[$i]{'value_builder'};
492 $row_data{hidden
} = $results->[$i]{'hidden'};
493 $row_data{isurl
} = $results->[$i]{'isurl'};
494 $row_data{link} = $results->[$i]{'link'};
496 if ( $row_data{tab
} eq -1 ) {
497 $row_data{subfield_ignored
} = 1;
500 push( @loop_data, \
%row_data );
502 $template->param( loop => \
@loop_data );
504 edit_tagfield
=> $tagfield,
505 edit_frameworkcode
=> $frameworkcode
508 } #---- END $OP eq DEFAULT
510 output_html_with_http_headers
$input, $cookie, $template->output;