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>.
26 use Koha
::Authority
::Types
;
28 use List
::MoreUtils
qw( uniq );
31 my ( $searchstring, $frameworkcode ) = @_;
32 my $dbh = C4
::Context
->dbh;
33 $searchstring =~ s/\'/\\\'/g;
34 my @data = split( ' ', $searchstring );
38 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
40 $sth->execute( "$searchstring%", $frameworkcode );
45 while ( my $data = $sth->fetchrow_hashref ) {
46 push( @results, $data );
51 return ( $cnt, \
@results );
54 sub marc_subfield_structure_exists
{
55 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
56 my $dbh = C4
::Context
->dbh;
57 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
58 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
63 my $tagfield = $input->param('tagfield');
64 my $tagsubfield = $input->param('tagsubfield');
65 my $frameworkcode = $input->param('frameworkcode');
66 my $pkfield = "tagfield";
67 my $offset = $input->param('offset') || 0;
68 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
70 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
72 template_name
=> "admin/marc_subfields_structure.tt",
76 flagsrequired
=> { parameters
=> 'parameters_remaining_permissions' },
80 my $cache = Koha
::Cache
->get_instance();
82 my $op = $input->param('op') || "";
87 script_name
=> $script_name,
88 tagfield
=> $tagfield,
89 frameworkcode
=> $frameworkcode,
91 ); # we show only the TMPL_VAR names $op
95 script_name
=> $script_name,
96 tagfield
=> $tagfield,
97 frameworkcode
=> $frameworkcode,
99 ); # we show only the TMPL_VAR names $op
102 ################## ADD_FORM ##################################
103 # called by default. Used to create form to add or modify a record
104 if ( $op eq 'add_form' ) {
105 my $dbh = C4
::Context
->dbh;
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
141 my @authtypes = uniq
( "", map { $_->authtypecode } Koha
::Authority
::Types
->search );
143 # build value_builder list
144 my @value_builder = ('');
146 # read value_builder directory.
147 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
148 # on a standard install, /cgi-bin need to be added.
149 # test one, then the other
150 my $cgidir = C4
::Context
->config('intranetdir') . "/cgi-bin";
151 unless ( opendir( DIR
, "$cgidir/cataloguing/value_builder" ) ) {
152 $cgidir = C4
::Context
->config('intranetdir');
153 opendir( DIR
, "$cgidir/cataloguing/value_builder" )
154 || die "can't opendir $cgidir/value_builder: $!";
156 while ( my $line = readdir(DIR
) ) {
157 if ( $line =~ /\.pl$/ &&
158 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
159 push( @value_builder, $line );
162 @value_builder= sort {$a cmp $b} @value_builder;
168 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
169 ); # and tagsubfield='$tagsubfield'");
170 $sth->execute( $tagfield, $frameworkcode );
173 while ( my $data = $sth->fetchrow_hashref ) {
174 my %row_data; # get a fresh hash for the row data
175 $row_data{defaultvalue
} = $data->{defaultvalue
};
176 $row_data{maxlength
} = $data->{maxlength
};
177 $row_data{tab
} = $data->{tab
};
178 $row_data{tagsubfield
} = $data->{tagsubfield
};
179 $row_data{subfieldcode
} = $data->{'tagsubfield'} eq '@' ?
'_' : $data->{'tagsubfield'};
180 $row_data{urisubfieldcode
} = $row_data{subfieldcode
} eq '%' ?
'pct' : $row_data{subfieldcode
};
181 $row_data{liblibrarian
} = $data->{'liblibrarian'};
182 $row_data{libopac
} = $data->{'libopac'};
183 $row_data{seealso
} = $data->{'seealso'};
184 $row_data{kohafields
} = \
@kohafields;
185 $row_data{kohafield
} = $data->{kohafield
};
186 $row_data{authorised_values
} = \
@authorised_values;
187 $row_data{authorised_value
} = $data->{authorised_value
};
188 $row_data{value_builders
} = \
@value_builder;
189 $row_data{value_builder
} = $data->{'value_builder'};
190 $row_data{authtypes
} = \
@authtypes;
191 $row_data{authtypecode
} = $data->{'authtypecode'};
192 $row_data{repeatable
} = $data->{repeatable
};
193 $row_data{mandatory
} = $data->{mandatory
};
194 $row_data{hidden
} = $data->{hidden
};
195 $row_data{isurl
} = $data->{isurl
};
197 $row_data{link} = $data->{'link'};
198 push( @loop_data, \
%row_data );
202 # Add a new row for the "New" tab
203 my %row_data; # get a fresh hash for the row data
204 $row_data{'new_subfield'} = 1;
205 $row_data{'subfieldcode'} = '';
206 $row_data{'maxlength'} = 9999;
207 $row_data{tab
} = -1; #ignore
208 $row_data{tagsubfield
} = "";
209 $row_data{liblibrarian
} = "";
210 $row_data{libopac
} = "";
211 $row_data{seealso
} = "";
212 $row_data{hidden
} = "";
213 $row_data{repeatable
} = 0;
214 $row_data{mandatory
} = 0;
215 $row_data{isurl
} = 0;
216 $row_data{kohafields
} = \
@kohafields;
217 $row_data{authorised_values
} = \
@authorised_values;
218 $row_data{value_builders
} = \
@value_builder;
219 $row_data{authtypes
} = \
@authtypes;
220 $row_data{link} = "";
222 push( @loop_data, \
%row_data );
224 $template->param( 'use_heading_flags_p' => 1 );
225 $template->param( 'heading_edit_subfields_p' => 1 );
227 action
=> "Edit subfields",
228 tagfield
=> $tagfield,
230 more_tag
=> $tagfield
233 # END $OP eq ADD_FORM
234 ################## ADD_VALIDATE ##################################
235 # called by add_form, used to insert/modify data in DB
237 elsif ( $op eq 'add_validate' ) {
238 my $dbh = C4
::Context
->dbh;
239 $template->param( tagfield
=> "$input->param('tagfield')" );
240 # my $sth = $dbh->prepare(
241 # "replace marc_subfield_structure (tagfield,tagsubfield,liblibrarian,libopac,repeatable,mandatory,kohafield,tab,seealso,authorised_value,authtypecode,value_builder,hidden,isurl,frameworkcode, link,defaultvalue)
242 # values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
244 my $sth_insert = $dbh->prepare(qq{
245 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
)
246 values (?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
,?
)
248 my $sth_update = $dbh->prepare(qq{
249 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
=?
250 where tagfield
=?
and tagsubfield
=?
and frameworkcode
=?
252 my @tagsubfield = $input->multi_param('tagsubfield');
253 my @liblibrarian = $input->multi_param('liblibrarian');
254 my @libopac = $input->multi_param('libopac');
255 my @kohafield = $input->multi_param('kohafield');
256 my @tab = $input->multi_param('tab');
257 my @seealso = $input->multi_param('seealso');
258 my @hidden = $input->multi_param('hidden');
259 my @authorised_values = $input->multi_param('authorised_value');
260 my @authtypecodes = $input->multi_param('authtypecode');
261 my @value_builder = $input->multi_param('value_builder');
262 my @link = $input->multi_param('link');
263 my @defaultvalue = $input->multi_param('defaultvalue');
264 my @maxlength = $input->multi_param('maxlength');
266 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
267 my $tagfield = $input->param('tagfield');
268 my $tagsubfield = $tagsubfield[$i];
269 $tagsubfield = "@" unless $tagsubfield ne '';
270 $tagsubfield = "@" if $tagsubfield eq '_';
271 my $liblibrarian = $liblibrarian[$i];
272 my $libopac = $libopac[$i];
273 my $repeatable = $input->param("repeatable$i") ?
1 : 0;
274 my $mandatory = $input->param("mandatory$i") ?
1 : 0;
275 my $kohafield = $kohafield[$i];
277 my $seealso = $seealso[$i];
278 my $authorised_value = $authorised_values[$i];
279 my $authtypecode = $authtypecodes[$i];
280 my $value_builder = $value_builder[$i];
281 my $hidden = $hidden[$i]; #input->param("hidden$i");
282 my $isurl = $input->param("isurl$i") ?
1 : 0;
283 my $link = $link[$i];
284 my $defaultvalue = $defaultvalue[$i];
285 my $maxlength = $maxlength[$i] ?
$maxlength[$i] : 9999;
287 if (defined($liblibrarian) && $liblibrarian ne "") {
288 my $is_demo = C4
::Context
->config('demo') || '';
289 if ( $is_demo ne '1' ) {
290 if (marc_subfield_structure_exists
($tagfield, $tagsubfield, $frameworkcode)) {
291 $sth_update->execute(
317 $sth_insert->execute(
343 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
344 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
345 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
346 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
348 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
351 # END $OP eq ADD_VALIDATE
352 ################## DELETE_CONFIRM ##################################
353 # called by default form, used to confirm deletion of data in DB
355 elsif ( $op eq 'delete_confirm' ) {
356 my $dbh = C4
::Context
->dbh;
359 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
362 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
363 my $data = $sth->fetchrow_hashref;
366 liblibrarian
=> $data->{'liblibrarian'},
367 tagsubfield
=> $data->{'tagsubfield'},
368 delete_link
=> $script_name,
369 tagfield
=> $tagfield,
370 tagsubfield
=> $tagsubfield,
371 frameworkcode
=> $frameworkcode,
374 # END $OP eq DELETE_CONFIRM
375 ################## DELETE_CONFIRMED ##################################
376 # called by delete_confirm, used to effectively confirm deletion of data in DB
378 elsif ( $op eq 'delete_confirmed' ) {
379 my $dbh = C4
::Context
->dbh;
380 my $is_demo = C4
::Context
->config('demo') || '';
381 if ( $is_demo ne '1' ) {
384 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
386 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
389 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
390 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
391 $cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
392 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
393 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
396 # END $OP eq DELETE_CONFIRMED
397 ################## DEFAULT ##################################
400 my ( $count, $results ) = string_search
( $tagfield, $frameworkcode );
402 for ( my $i = 0; $i < $count; $i++ ) {
403 my %row_data; # get a fresh hash for the row data
404 $row_data{tagfield
} = $results->[$i]{'tagfield'};
405 $row_data{tagsubfield
} = $results->[$i]{'tagsubfield'};
406 $row_data{liblibrarian
} = $results->[$i]{'liblibrarian'};
407 $row_data{kohafield
} = $results->[$i]{'kohafield'};
408 $row_data{repeatable
} = $results->[$i]{'repeatable'};
409 $row_data{mandatory
} = $results->[$i]{'mandatory'};
410 $row_data{tab
} = $results->[$i]{'tab'};
411 $row_data{seealso
} = $results->[$i]{'seealso'};
412 $row_data{authorised_value
} = $results->[$i]{'authorised_value'};
413 $row_data{authtypecode
} = $results->[$i]{'authtypecode'};
414 $row_data{value_builder
} = $results->[$i]{'value_builder'};
415 $row_data{hidden
} = $results->[$i]{'hidden'};
416 $row_data{isurl
} = $results->[$i]{'isurl'};
417 $row_data{link} = $results->[$i]{'link'};
419 if ( $row_data{tab
} eq -1 ) {
420 $row_data{subfield_ignored
} = 1;
423 push( @loop_data, \
%row_data );
425 $template->param( loop => \
@loop_data );
427 edit_tagfield
=> $tagfield,
428 edit_frameworkcode
=> $frameworkcode
431 } #---- END $OP eq DEFAULT
433 output_html_with_http_headers
$input, $cookie, $template->output;