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' },
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 );
178 while ( $data = $sth->fetchrow_hashref ) {
179 my %row_data; # get a fresh hash for the row data
180 $row_data{defaultvalue
} = $data->{defaultvalue
};
181 $row_data{maxlength
} = $data->{maxlength
};
184 default => $data->{'tab'},
187 $row_data{tagsubfield
} =
188 $data->{'tagsubfield'}
189 . "<input type=\"hidden\" name=\"tagsubfield\" value=\""
190 . $data->{'tagsubfield'}
191 . "\" id=\"tagsubfield\" />";
192 $row_data{subfieldcode
} = $data->{'tagsubfield'} eq '@'?
'_':$data->{'tagsubfield'};
193 $row_data{urisubfieldcode
} = $row_data{subfieldcode
} eq '%' ?
'pct' : $row_data{subfieldcode
};
194 $row_data{liblibrarian
} = CGI
::escapeHTML
( $data->{'liblibrarian'} );
195 $row_data{libopac
} = CGI
::escapeHTML
( $data->{'libopac'} );
196 $row_data{seealso
} = CGI
::escapeHTML
( $data->{'seealso'} );
197 $row_data{kohafield
} = {
199 values => \
@kohafields,
200 default => "$data->{'kohafield'}",
202 $row_data{authorised_value
} = {
203 id
=> "authorised_value$i",
204 values => \
@authorised_values,
205 default => $data->{'authorised_value'},
207 $row_data{value_builder
} = {
208 id
=> "value_builder$i",
209 values => \
@value_builder,
210 default => $data->{'value_builder'},
212 $row_data{authtypes
} = {
213 id
=> "authtypecode$i",
214 values => \
@authtypes,
215 default => $data->{'authtypecode'},
217 $row_data{repeatable
} = CGI
::checkbox
(
218 -name
=> "repeatable$i",
219 -checked
=> $data->{'repeatable'} ?
'checked' : '',
222 -id
=> "repeatable$i"
224 $row_data{mandatory
} = CGI
::checkbox
(
225 -name
=> "mandatory$i",
226 -checked
=> $data->{'mandatory'} ?
'checked' : '',
231 $row_data{hidden
} = CGI
::escapeHTML
( $data->{hidden
} );
232 $row_data{isurl
} = CGI
::checkbox
(
235 -checked
=> $data->{'isurl'} ?
'checked' : '',
240 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
241 push( @loop_data, \
%row_data );
245 # add more_subfields empty lines for add if needed
246 my %row_data; # get a fresh hash for the row data
247 $row_data{'new_subfield'} = 1;
248 $row_data{'subfieldcode'} = '';
249 $row_data{'maxlength'} = 9999;
253 default => $data->{'tab'},
255 $row_data{tagsubfield
} =
256 "<input type=\"text\" name=\"tagsubfield\" value=\""
257 . $data->{'tagsubfield'}
258 . "\" size=\"1\" id=\"tagsubfield\" maxlength=\"1\" />";
259 $row_data{liblibrarian
} = "";
260 $row_data{libopac
} = "";
261 $row_data{seealso
} = "";
262 $row_data{kohafield
} = {
264 values => \
@kohafields,
265 default => "$data->{'kohafield'}",
267 $row_data{hidden
} = "";
268 $row_data{repeatable
} = CGI
::checkbox
(
269 -name
=> "repeatable$i",
270 -id
=> "repeatable$i",
275 $row_data{mandatory
} = CGI
::checkbox
(
276 -name
=> "mandatory$i",
277 -id
=> "mandatory$i",
282 $row_data{isurl
} = CGI
::checkbox
(
289 $row_data{value_builder
} = {
290 id
=> "value_builder$i",
291 values => \
@value_builder,
292 default => $data->{'value_builder'},
294 $row_data{authorised_value
} = {
295 id
=> "authorised_value$i",
296 values => \
@authorised_values,
297 default => $data->{'authorised_value'},
299 $row_data{authtypes
} = {
300 id
=> "authtypecode$i",
301 values => \
@authtypes,
302 default => $data->{'authtypecode'},
304 $row_data{link} = CGI
::escapeHTML
( $data->{'link'} );
306 push( @loop_data, \
%row_data );
308 $template->param( 'use_heading_flags_p' => 1 );
309 $template->param( 'heading_edit_subfields_p' => 1 );
311 action
=> "Edit subfields",
312 tagfield
=> $tagfield,
314 more_subfields
=> $more_subfields,
315 more_tag
=> $tagfield
318 # END $OP eq ADD_FORM
319 ################## ADD_VALIDATE ##################################
320 # called by add_form, used to insert/modify data in DB
322 elsif ( $op eq 'add_validate' ) {
323 my $dbh = C4
::Context
->dbh;
324 $template->param( tagfield
=> "$input->param('tagfield')" );
325 # my $sth = $dbh->prepare(
326 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
327 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
329 my $sth_insert = $dbh->prepare(qq{
330 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
)
331 values (?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
333 my $sth_update = $dbh->prepare(qq{
334 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
=?
335 where tagfield
=?
and tagsubfield
=?
and frameworkcode
=?
337 my @tagsubfield = $input->param('tagsubfield');
338 my @liblibrarian = $input->param('liblibrarian');
339 my @libopac = $input->param('libopac');
340 my @kohafield = $input->param('kohafield');
341 my @tab = $input->param('tab');
342 my @seealso = $input->param('seealso');
343 my @hidden = $input->param('hidden');
344 my @authorised_values = $input->param('authorised_value');
345 my @authtypecodes = $input->param('authtypecode');
346 my @value_builder = $input->param('value_builder');
347 my @link = $input->param('link');
348 my @defaultvalue = $input->param('defaultvalue');
349 my @maxlength = $input->param('maxlength');
351 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
352 my $tagfield = $input->param('tagfield');
353 my $tagsubfield = $tagsubfield[$i];
354 $tagsubfield = "@" unless $tagsubfield ne '';
355 $tagsubfield = "@" if $tagsubfield eq '_';
356 my $liblibrarian = $liblibrarian[$i];
357 my $libopac = $libopac[$i];
358 my $repeatable = $input->param("repeatable$i") ?
1 : 0;
359 my $mandatory = $input->param("mandatory$i") ?
1 : 0;
360 my $kohafield = $kohafield[$i];
362 my $seealso = $seealso[$i];
363 my $authorised_value = $authorised_values[$i];
364 my $authtypecode = $authtypecodes[$i];
365 my $value_builder = $value_builder[$i];
366 my $hidden = $hidden[$i]; #input->param("hidden$i");
367 my $isurl = $input->param("isurl$i") ?
1 : 0;
368 my $link = $link[$i];
369 my $defaultvalue = $defaultvalue[$i];
370 my $maxlength = $maxlength[$i] ?
$maxlength[$i] : 9999;
372 if (defined($liblibrarian) && $liblibrarian ne "") {
373 unless ( C4
::Context
->config('demo') eq 1 ) {
374 if (marc_subfield_structure_exists
($tagfield, $tagsubfield, $frameworkcode)) {
375 $sth_update->execute(
401 $sth_insert->execute(
428 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
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 );
469 "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode\"></html>";
471 $template->param( tagfield
=> $tagfield );
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;