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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
->intranetdir . "/cgi-bin";
158 unless ( opendir( DIR
, "$cgidir/cataloguing/value_builder" ) ) {
159 $cgidir = C4
::Context
->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 push( @value_builder, $line );
168 @value_builder= sort {$a cmp $b} @value_builder;
174 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
175 ); # and tagsubfield='$tagsubfield'");
176 $sth->execute( $tagfield, $frameworkcode );
179 while ( $data = $sth->fetchrow_hashref ) {
180 my %row_data; # get a fresh hash for the row data
181 $row_data{defaultvalue
} = $data->{defaultvalue
};
182 $row_data{maxlength
} = $data->{maxlength
};
185 default => $data->{'tab'},
188 $row_data{tagsubfield
} =
189 $data->{'tagsubfield'}
190 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
191 . $data->{'tagsubfield'}
192 . "\" id=\"tagsubfield\" />";
193 $row_data{subfieldcode
} = $data->{'tagsubfield'} eq '@'?
'_':$data->{'tagsubfield'};
194 $row_data{urisubfieldcode
} = $row_data{subfieldcode
} eq '%' ?
'pct' : $row_data{subfieldcode
};
195 $row_data{liblibrarian
} = CGI
::escapeHTML
( $data->{'liblibrarian'} );
196 $row_data{libopac
} = CGI
::escapeHTML
( $data->{'libopac'} );
197 $row_data{seealso
} = CGI
::escapeHTML
( $data->{'seealso'} );
198 $row_data{kohafield
} = {
200 values => \
@kohafields,
201 default => "$data->{'kohafield'}",
203 $row_data{authorised_value
} = {
204 id
=> "authorised_value$i",
205 values => \
@authorised_values,
206 default => $data->{'authorised_value'},
208 $row_data{value_builder
} = {
209 id
=> "value_builder$i",
210 values => \
@value_builder,
211 default => $data->{'value_builder'},
213 $row_data{authtypes
} = {
214 id
=> "authtypecode$i",
215 values => \
@authtypes,
216 default => $data->{'authtypecode'},
218 $row_data{repeatable
} = CGI
::checkbox
(
219 -name
=> "repeatable$i",
220 -checked
=> $data->{'repeatable'} ?
'checked' : '',
223 -id
=> "repeatable$i"
225 $row_data{mandatory
} = CGI
::checkbox
(
226 -name
=> "mandatory$i",
227 -checked
=> $data->{'mandatory'} ?
'checked' : '',
232 $row_data{hidden
} = CGI
::escapeHTML
( $data->{hidden
} );
233 $row_data{isurl
} = CGI
::checkbox
(
236 -checked
=> $data->{'isurl'} ?
'checked' : '',
241 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
242 push( @loop_data, \
%row_data );
246 # add more_subfields empty lines for add if needed
247 my %row_data; # get a fresh hash for the row data
248 $row_data{'new_subfield'} = 1;
249 $row_data{'subfieldcode'} = '';
250 $row_data{'maxlength'} = 9999;
254 default => $data->{'tab'},
256 $row_data{tagsubfield
} =
257 "<input type=\"text\" name=\"tagsubfield\" value=\""
258 . $data->{'tagsubfield'}
259 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
260 $row_data{liblibrarian
} = "";
261 $row_data{libopac
} = "";
262 $row_data{seealso
} = "";
263 $row_data{kohafield
} = {
265 values => \
@kohafields,
266 default => "$data->{'kohafield'}",
268 $row_data{hidden
} = "";
269 $row_data{repeatable
} = CGI
::checkbox
(
270 -name
=> "repeatable$i",
271 -id
=> "repeatable$i",
276 $row_data{mandatory
} = CGI
::checkbox
(
277 -name
=> "mandatory$i",
278 -id
=> "mandatory$i",
283 $row_data{isurl
} = CGI
::checkbox
(
290 $row_data{value_builder
} = {
291 id
=> "value_builder$i",
292 values => \
@value_builder,
293 default => $data->{'value_builder'},
295 $row_data{authorised_value
} = {
296 id
=> "authorised_value$i",
297 values => \
@authorised_values,
298 default => $data->{'authorised_value'},
300 $row_data{authtypes
} = {
301 id
=> "authtypecode$i",
302 values => \
@authtypes,
303 default => $data->{'authtypecode'},
305 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
307 push( @loop_data, \
%row_data );
309 $template->param( 'use_heading_flags_p' => 1 );
310 $template->param( 'heading_edit_subfields_p' => 1 );
312 action
=> "Edit subfields",
313 tagfield
=> $tagfield,
315 more_subfields
=> $more_subfields,
316 more_tag
=> $tagfield
319 # END $OP eq ADD_FORM
320 ################## ADD_VALIDATE ##################################
321 # called by add_form, used to insert/modify data in DB
323 elsif ( $op eq 'add_validate' ) {
324 my $dbh = C4
::Context
->dbh;
325 $template->param( tagfield
=> "$input->param('tagfield')" );
326 # my $sth = $dbh->prepare(
327 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
328 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
330 my $sth_insert = $dbh->prepare(qq{
331 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
)
332 values (?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
334 my $sth_update = $dbh->prepare(qq{
335 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
=?
336 where tagfield
=?
and tagsubfield
=?
and frameworkcode
=?
338 my @tagsubfield = $input->param('tagsubfield');
339 my @liblibrarian = $input->param('liblibrarian');
340 my @libopac = $input->param('libopac');
341 my @kohafield = $input->param('kohafield');
342 my @tab = $input->param('tab');
343 my @seealso = $input->param('seealso');
344 my @hidden = $input->param('hidden');
345 my @authorised_values = $input->param('authorised_value');
346 my @authtypecodes = $input->param('authtypecode');
347 my @value_builder = $input->param('value_builder');
348 my @link = $input->param('link');
349 my @defaultvalue = $input->param('defaultvalue');
350 my @maxlength = $input->param('maxlength');
352 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
353 my $tagfield = $input->param('tagfield');
354 my $tagsubfield = $tagsubfield[$i];
355 $tagsubfield = "@" unless $tagsubfield ne '';
356 $tagsubfield = "@" if $tagsubfield eq '_';
357 my $liblibrarian = $liblibrarian[$i];
358 my $libopac = $libopac[$i];
359 my $repeatable = $input->param("repeatable$i") ?
1 : 0;
360 my $mandatory = $input->param("mandatory$i") ?
1 : 0;
361 my $kohafield = $kohafield[$i];
363 my $seealso = $seealso[$i];
364 my $authorised_value = $authorised_values[$i];
365 my $authtypecode = $authtypecodes[$i];
366 my $value_builder = $value_builder[$i];
367 my $hidden = $hidden[$i]; #input->param("hidden$i");
368 my $isurl = $input->param("isurl$i") ?
1 : 0;
369 my $link = $link[$i];
370 my $defaultvalue = $defaultvalue[$i];
371 my $maxlength = $maxlength[$i] ?
$maxlength[$i] : 9999;
373 if (defined($liblibrarian) && $liblibrarian ne "") {
374 unless ( C4
::Context
->config('demo') eq 1 ) {
375 if (marc_subfield_structure_exists
($tagfield, $tagsubfield, $frameworkcode)) {
376 $sth_update->execute(
402 $sth_insert->execute(
428 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
429 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
432 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
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");
475 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
477 $template->param( tagfield
=> $tagfield );
479 # END $OP eq DELETE_CONFIRMED
480 ################## DEFAULT ##################################
483 my ( $count, $results ) = string_search
( $tagfield, $frameworkcode );
485 for ( my $i = 0; $i < $count; $i++ ) {
486 my %row_data; # get a fresh hash for the row data
487 $row_data{tagfield
} = $results->[$i]{'tagfield'};
488 $row_data{tagsubfield
} = $results->[$i]{'tagsubfield'};
489 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
490 $row_data{kohafield
} = $results->[$i]{'kohafield'};
491 $row_data{repeatable
} = $results->[$i]{'repeatable'};
492 $row_data{mandatory
} = $results->[$i]{'mandatory'};
493 $row_data{tab
} = $results->[$i]{'tab'};
494 $row_data{seealso
} = $results->[$i]{'seealso'};
495 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
496 $row_data{authtypecode
} = $results->[$i]{'authtypecode'};
497 $row_data{value_builder
} = $results->[$i]{'value_builder'};
498 $row_data{hidden
} = $results->[$i]{'hidden'};
499 $row_data{isurl
} = $results->[$i]{'isurl'};
500 $row_data{link} = $results->[$i]{'link'};
502 if ( $row_data{tab
} eq -1 ) {
503 $row_data{subfield_ignored
} = 1;
506 push( @loop_data, \
%row_data );
508 $template->param( loop => \
@loop_data );
510 edit_tagfield
=> $tagfield,
511 edit_frameworkcode
=> $frameworkcode
514 } #---- END $OP eq DEFAULT
516 output_html_with_http_headers
$input, $cookie, $template->output;