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
29 my ( $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 );
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.tt",
74 flagsrequired
=> { parameters
=> 'parameters_remaining_permissions' },
78 my $cache = Koha
::Cache
->get_instance();
80 my $op = $input->param('op');
85 script_name
=> $script_name,
86 tagfield
=> $tagfield,
87 frameworkcode
=> $frameworkcode,
89 ); # we show only the TMPL_VAR names $op
93 script_name
=> $script_name,
94 tagfield
=> $tagfield,
95 frameworkcode
=> $frameworkcode,
97 ); # we show only the TMPL_VAR names $op
100 ################## ADD_FORM ##################################
101 # called by default. Used to create form to add or modify a record
102 if ( $op eq 'add_form' ) {
104 my $dbh = C4
::Context
->dbh;
105 my $more_subfields = $input->param("more_subfields") + 1;
107 # builds kohafield tables
109 push @kohafields, "";
110 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
112 while ( ( my $field ) = $sth2->fetchrow_array ) {
113 push @kohafields, "biblio." . $field;
115 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
117 while ( ( my $field ) = $sth2->fetchrow_array ) {
118 if ( $field eq 'notes' ) { $field = 'bnotes'; }
119 push @kohafields, "biblioitems." . $field;
121 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
123 while ( ( my $field ) = $sth2->fetchrow_array ) {
124 push @kohafields, "items." . $field;
127 # build authorised value list
129 $sth2 = $dbh->prepare("select distinct category from authorised_values");
131 my @authorised_values;
132 push @authorised_values, "";
133 while ( ( my $category ) = $sth2->fetchrow_array ) {
134 push @authorised_values, $category;
136 push( @authorised_values, "branches" );
137 push( @authorised_values, "itemtypes" );
138 push( @authorised_values, "cn_source" );
140 # build thesaurus categories list
142 $sth2 = $dbh->prepare("select authtypecode from auth_types");
146 while ( ( my $authtypecode ) = $sth2->fetchrow_array ) {
147 push @authtypes, $authtypecode;
150 # build value_builder list
151 my @value_builder = ('');
153 # read value_builder directory.
154 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
155 # on a standard install, /cgi-bin need to be added.
156 # test one, then the other
157 my $cgidir = C4
::Context
->config('intranetdir') . "/cgi-bin";
158 unless ( opendir( DIR
, "$cgidir/cataloguing/value_builder" ) ) {
159 $cgidir = C4
::Context
->config('intranetdir');
160 opendir( DIR
, "$cgidir/cataloguing/value_builder" )
161 || die "can't opendir $cgidir/value_builder: $!";
163 while ( my $line = readdir(DIR
) ) {
164 if ( $line =~ /\.pl$/ &&
165 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
166 push( @value_builder, $line );
169 @value_builder= sort {$a cmp $b} @value_builder;
175 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
176 ); # and tagsubfield='$tagsubfield'");
177 $sth->execute( $tagfield, $frameworkcode );
180 while ( $data = $sth->fetchrow_hashref ) {
181 my %row_data; # get a fresh hash for the row data
182 $row_data{defaultvalue
} = $data->{defaultvalue
};
183 $row_data{maxlength
} = $data->{maxlength
};
186 default => $data->{'tab'},
189 $row_data{tagsubfield
} =
190 $data->{'tagsubfield'}
191 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
192 . $data->{'tagsubfield'}
193 . "\" id=\"tagsubfield\" />";
194 $row_data{subfieldcode
} = $data->{'tagsubfield'} eq '@'?
'_':$data->{'tagsubfield'};
195 $row_data{urisubfieldcode
} = $row_data{subfieldcode
} eq '%' ?
'pct' : $row_data{subfieldcode
};
196 $row_data{liblibrarian
} = CGI
::escapeHTML
( $data->{'liblibrarian'} );
197 $row_data{libopac
} = CGI
::escapeHTML
( $data->{'libopac'} );
198 $row_data{seealso
} = CGI
::escapeHTML
( $data->{'seealso'} );
199 $row_data{kohafield
} = {
201 values => \
@kohafields,
202 default => "$data->{'kohafield'}",
204 $row_data{authorised_value
} = {
205 id
=> "authorised_value$i",
206 values => \
@authorised_values,
207 default => $data->{'authorised_value'},
209 $row_data{value_builder
} = {
210 id
=> "value_builder$i",
211 values => \
@value_builder,
212 default => $data->{'value_builder'},
214 $row_data{authtypes
} = {
215 id
=> "authtypecode$i",
216 values => \
@authtypes,
217 default => $data->{'authtypecode'},
219 $row_data{repeatable
} = CGI
::checkbox
(
220 -name
=> "repeatable$i",
221 -checked
=> $data->{'repeatable'} ?
'checked' : '',
224 -id
=> "repeatable$i"
226 $row_data{mandatory
} = CGI
::checkbox
(
227 -name
=> "mandatory$i",
228 -checked
=> $data->{'mandatory'} ?
'checked' : '',
233 $row_data{hidden
} = CGI
::escapeHTML
( $data->{hidden
} );
234 $row_data{isurl
} = CGI
::checkbox
(
237 -checked
=> $data->{'isurl'} ?
'checked' : '',
242 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
243 push( @loop_data, \
%row_data );
247 # add more_subfields empty lines for add if needed
248 my %row_data; # get a fresh hash for the row data
249 $row_data{'new_subfield'} = 1;
250 $row_data{'subfieldcode'} = '';
251 $row_data{'maxlength'} = 9999;
255 default => $data->{'tab'},
257 $row_data{tagsubfield
} =
258 "<input type=\"text\" name=\"tagsubfield\" value=\""
259 . $data->{'tagsubfield'}
260 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
261 $row_data{liblibrarian
} = "";
262 $row_data{libopac
} = "";
263 $row_data{seealso
} = "";
264 $row_data{kohafield
} = {
266 values => \
@kohafields,
267 default => "$data->{'kohafield'}",
269 $row_data{hidden
} = "";
270 $row_data{repeatable
} = CGI
::checkbox
(
271 -name
=> "repeatable$i",
272 -id
=> "repeatable$i",
277 $row_data{mandatory
} = CGI
::checkbox
(
278 -name
=> "mandatory$i",
279 -id
=> "mandatory$i",
284 $row_data{isurl
} = CGI
::checkbox
(
291 $row_data{value_builder
} = {
292 id
=> "value_builder$i",
293 values => \
@value_builder,
294 default => $data->{'value_builder'},
296 $row_data{authorised_value
} = {
297 id
=> "authorised_value$i",
298 values => \
@authorised_values,
299 default => $data->{'authorised_value'},
301 $row_data{authtypes
} = {
302 id
=> "authtypecode$i",
303 values => \
@authtypes,
304 default => $data->{'authtypecode'},
306 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
308 push( @loop_data, \
%row_data );
310 $template->param( 'use_heading_flags_p' => 1 );
311 $template->param( 'heading_edit_subfields_p' => 1 );
313 action
=> "Edit subfields",
314 tagfield
=> $tagfield,
316 more_subfields
=> $more_subfields,
317 more_tag
=> $tagfield
320 # END $OP eq ADD_FORM
321 ################## ADD_VALIDATE ##################################
322 # called by add_form, used to insert/modify data in DB
324 elsif ( $op eq 'add_validate' ) {
325 my $dbh = C4
::Context
->dbh;
326 $template->param( tagfield
=> "$input->param('tagfield')" );
327 # my $sth = $dbh->prepare(
328 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
329 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
331 my $sth_insert = $dbh->prepare(qq{
332 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
)
333 values (?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
335 my $sth_update = $dbh->prepare(qq{
336 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
=?
337 where tagfield
=?
and tagsubfield
=?
and frameworkcode
=?
339 my @tagsubfield = $input->param('tagsubfield');
340 my @liblibrarian = $input->param('liblibrarian');
341 my @libopac = $input->param('libopac');
342 my @kohafield = $input->param('kohafield');
343 my @tab = $input->param('tab');
344 my @seealso = $input->param('seealso');
345 my @hidden = $input->param('hidden');
346 my @authorised_values = $input->param('authorised_value');
347 my @authtypecodes = $input->param('authtypecode');
348 my @value_builder = $input->param('value_builder');
349 my @link = $input->param('link');
350 my @defaultvalue = $input->param('defaultvalue');
351 my @maxlength = $input->param('maxlength');
353 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
354 my $tagfield = $input->param('tagfield');
355 my $tagsubfield = $tagsubfield[$i];
356 $tagsubfield = "@" unless $tagsubfield ne '';
357 $tagsubfield = "@" if $tagsubfield eq '_';
358 my $liblibrarian = $liblibrarian[$i];
359 my $libopac = $libopac[$i];
360 my $repeatable = $input->param("repeatable$i") ?
1 : 0;
361 my $mandatory = $input->param("mandatory$i") ?
1 : 0;
362 my $kohafield = $kohafield[$i];
364 my $seealso = $seealso[$i];
365 my $authorised_value = $authorised_values[$i];
366 my $authtypecode = $authtypecodes[$i];
367 my $value_builder = $value_builder[$i];
368 my $hidden = $hidden[$i]; #input->param("hidden$i");
369 my $isurl = $input->param("isurl$i") ?
1 : 0;
370 my $link = $link[$i];
371 my $defaultvalue = $defaultvalue[$i];
372 my $maxlength = $maxlength[$i] ?
$maxlength[$i] : 9999;
374 if (defined($liblibrarian) && $liblibrarian ne "") {
375 unless ( C4
::Context
->config('demo') eq 1 ) {
376 if (marc_subfield_structure_exists
($tagfield, $tagsubfield, $frameworkcode)) {
377 $sth_update->execute(
403 $sth_insert->execute(
429 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
430 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
432 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
435 # END $OP eq ADD_VALIDATE
436 ################## DELETE_CONFIRM ##################################
437 # called by default form, used to confirm deletion of data in DB
439 elsif ( $op eq 'delete_confirm' ) {
440 my $dbh = C4
::Context
->dbh;
443 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
446 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
447 my $data = $sth->fetchrow_hashref;
450 liblibrarian
=> $data->{'liblibrarian'},
451 tagsubfield
=> $data->{'tagsubfield'},
452 delete_link
=> $script_name,
453 tagfield
=> $tagfield,
454 tagsubfield
=> $tagsubfield,
455 frameworkcode
=> $frameworkcode,
458 # END $OP eq DELETE_CONFIRM
459 ################## DELETE_CONFIRMED ##################################
460 # called by delete_confirm, used to effectively confirm deletion of data in DB
462 elsif ( $op eq 'delete_confirmed' ) {
463 my $dbh = C4
::Context
->dbh;
464 unless ( C4
::Context
->config('demo') eq 1 ) {
467 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
469 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
472 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
473 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
474 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
477 # END $OP eq DELETE_CONFIRMED
478 ################## DEFAULT ##################################
481 my ( $count, $results ) = string_search
( $tagfield, $frameworkcode );
483 for ( my $i = 0; $i < $count; $i++ ) {
484 my %row_data; # get a fresh hash for the row data
485 $row_data{tagfield
} = $results->[$i]{'tagfield'};
486 $row_data{tagsubfield
} = $results->[$i]{'tagsubfield'};
487 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
488 $row_data{kohafield
} = $results->[$i]{'kohafield'};
489 $row_data{repeatable
} = $results->[$i]{'repeatable'};
490 $row_data{mandatory
} = $results->[$i]{'mandatory'};
491 $row_data{tab
} = $results->[$i]{'tab'};
492 $row_data{seealso
} = $results->[$i]{'seealso'};
493 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
494 $row_data{authtypecode
} = $results->[$i]{'authtypecode'};
495 $row_data{value_builder
} = $results->[$i]{'value_builder'};
496 $row_data{hidden
} = $results->[$i]{'hidden'};
497 $row_data{isurl
} = $results->[$i]{'isurl'};
498 $row_data{link} = $results->[$i]{'link'};
500 if ( $row_data{tab
} eq -1 ) {
501 $row_data{subfield_ignored
} = 1;
504 push( @loop_data, \
%row_data );
506 $template->param( loop => \
@loop_data );
508 edit_tagfield
=> $tagfield,
509 edit_frameworkcode
=> $frameworkcode
512 } #---- END $OP eq DEFAULT
514 output_html_with_http_headers
$input, $cookie, $template->output;