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;
27 use Koha::AuthorisedValueCategories;
28 use Koha::Filter::MARC::ViewPolicy;
30 use List::MoreUtils qw( uniq );
33 my ( $searchstring, $frameworkcode ) = @_;
34 my $dbh = C4::Context->dbh;
35 $searchstring =~ s/\'/\\\'/g;
36 my @data = split( ' ', $searchstring );
40 "Select * from marc_subfield_structure where (tagfield like ? and frameworkcode=?) order by tagfield"
42 $sth->execute( "$searchstring%", $frameworkcode );
47 while ( my $data = $sth->fetchrow_hashref ) {
48 push( @results, $data );
53 return ( $cnt, \@results );
56 sub marc_subfield_structure_exists {
57 my ($tagfield, $tagsubfield, $frameworkcode) = @_;
58 my $dbh = C4::Context->dbh;
59 my $sql = "select tagfield from marc_subfield_structure where tagfield = ? and tagsubfield = ? and frameworkcode = ?";
60 my $rows = $dbh->selectall_arrayref($sql, {}, $tagfield, $tagsubfield, $frameworkcode);
65 my $tagfield = $input->param('tagfield');
66 my $tagsubfield = $input->param('tagsubfield');
67 my $frameworkcode = $input->param('frameworkcode');
68 my $pkfield = "tagfield";
69 my $offset = $input->param('offset');
70 $offset = 0 if not defined $offset or $offset < 0;
71 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
73 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
75 template_name => "admin/marc_subfields_structure.tt",
78 flagsrequired => { parameters => 'manage_marc_frameworks' },
82 my $cache = Koha::Caches->get_instance();
84 my $op = $input->param('op') || "";
89 script_name => $script_name,
90 tagfield => $tagfield,
91 frameworkcode => $frameworkcode,
93 ); # we show only the TMPL_VAR names $op
97 script_name => $script_name,
98 tagfield => $tagfield,
99 frameworkcode => $frameworkcode,
101 ); # we show only the TMPL_VAR names $op
104 ################## ADD_FORM ##################################
105 # called by default. Used to create form to add or modify a record
106 if ( $op eq 'add_form' ) {
107 my $dbh = C4::Context->dbh;
109 # builds kohafield tables
111 push @kohafields, "";
112 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
114 while ( ( my $field ) = $sth2->fetchrow_array ) {
115 push @kohafields, "biblio." . $field;
117 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
119 while ( ( my $field ) = $sth2->fetchrow_array ) {
120 if ( $field eq 'notes' ) { $field = 'bnotes'; }
121 push @kohafields, "biblioitems." . $field;
123 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
125 while ( ( my $field ) = $sth2->fetchrow_array ) {
126 push @kohafields, "items." . $field;
129 # build authorised value list
131 $sth2 = $dbh->prepare("select distinct category from authorised_values");
133 my @av_cat = Koha::AuthorisedValueCategories->search;
134 my @authorised_values = map { $_->category_name } @av_cat;
136 # build thesaurus categories list
137 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
139 # build value_builder list
140 my @value_builder = ('');
142 # read value_builder directory.
143 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
144 # on a standard install, /cgi-bin need to be added.
145 # test one, then the other
146 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
147 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
148 $cgidir = C4::Context->config('intranetdir');
149 opendir( DIR, "$cgidir/cataloguing/value_builder" )
150 || die "can't opendir $cgidir/value_builder: $!";
152 while ( my $line = readdir(DIR) ) {
153 if ( $line =~ /\.pl$/ &&
154 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
155 push( @value_builder, $line );
158 @value_builder= sort {$a cmp $b} @value_builder;
164 "select * from marc_subfield_structure where tagfield=? and frameworkcode=?"
165 ); # and tagsubfield='$tagsubfield'");
166 $sth->execute( $tagfield, $frameworkcode );
169 while ( my $data = $sth->fetchrow_hashref ) {
170 my %row_data; # get a fresh hash for the row data
171 $row_data{defaultvalue} = $data->{defaultvalue};
172 $row_data{maxlength} = $data->{maxlength};
173 $row_data{tab} = $data->{tab};
174 $row_data{tagsubfield} = $data->{tagsubfield};
175 $row_data{subfieldcode} = $data->{'tagsubfield'};
176 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
177 $row_data{liblibrarian} = $data->{'liblibrarian'};
178 $row_data{libopac} = $data->{'libopac'};
179 $row_data{seealso} = $data->{'seealso'};
180 $row_data{kohafields} = \@kohafields;
181 $row_data{kohafield} = $data->{kohafield};
182 $row_data{authorised_values} = \@authorised_values;
183 $row_data{authorised_value} = $data->{authorised_value};
184 $row_data{value_builders} = \@value_builder;
185 $row_data{value_builder} = $data->{'value_builder'};
186 $row_data{authtypes} = \@authtypes;
187 $row_data{authtypecode} = $data->{'authtypecode'};
188 $row_data{repeatable} = $data->{repeatable};
189 $row_data{mandatory} = $data->{mandatory};
190 $row_data{hidden} = $data->{hidden};
191 $row_data{isurl} = $data->{isurl};
193 $row_data{link} = $data->{'link'};
195 if ( defined $data->{kohafield}
196 and $data->{kohafield} eq 'biblio.biblionumber' )
198 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
200 frameworkcode => $frameworkcode,
205 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
207 frameworkcode => $frameworkcode,
208 interface => "intranet",
212 if ( $hidden_opac or $hidden_intranet ) {
213 # We should allow editing for fixing it
214 $row_data{hidden_protected} = 0;
217 $row_data{hidden_protected} = 1;
221 push( @loop_data, \%row_data );
225 # Add a new row for the "New" tab
226 my %row_data; # get a fresh hash for the row data
227 $row_data{'new_subfield'} = 1;
228 $row_data{'subfieldcode'} = '';
229 $row_data{'maxlength'} = 9999;
230 $row_data{tab} = -1; #ignore
231 $row_data{tagsubfield} = "";
232 $row_data{liblibrarian} = "";
233 $row_data{libopac} = "";
234 $row_data{seealso} = "";
235 $row_data{hidden} = "";
236 $row_data{repeatable} = 0;
237 $row_data{mandatory} = 0;
238 $row_data{isurl} = 0;
239 $row_data{kohafields} = \@kohafields;
240 $row_data{authorised_values} = \@authorised_values;
241 $row_data{value_builders} = \@value_builder;
242 $row_data{authtypes} = \@authtypes;
243 $row_data{link} = "";
245 push( @loop_data, \%row_data );
247 $template->param( 'use_heading_flags_p' => 1 );
248 $template->param( 'heading_edit_subfields_p' => 1 );
250 action => "Edit subfields",
251 tagfield => $tagfield,
253 more_tag => $tagfield
256 # END $OP eq ADD_FORM
257 ################## ADD_VALIDATE ##################################
258 # called by add_form, used to insert/modify data in DB
260 elsif ( $op eq 'add_validate' ) {
261 my $dbh = C4::Context->dbh;
262 $template->param( tagfield => "$input->param('tagfield')" );
263 my $sth_update = $dbh->prepare(qq{
264 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=?
265 where tagfield=? and tagsubfield=? and frameworkcode=?
267 my @tagsubfield = $input->multi_param('tagsubfield');
268 my @liblibrarian = $input->multi_param('liblibrarian');
269 my @libopac = $input->multi_param('libopac');
270 my @kohafield = $input->multi_param('kohafield');
271 my @tab = $input->multi_param('tab');
272 my @seealso = $input->multi_param('seealso');
273 my @hidden = $input->multi_param('hidden');
274 my @authorised_values = $input->multi_param('authorised_value');
275 my @authtypecodes = $input->multi_param('authtypecode');
276 my @value_builder = $input->multi_param('value_builder');
277 my @link = $input->multi_param('link');
278 my @defaultvalue = $input->multi_param('defaultvalue');
279 my @maxlength = $input->multi_param('maxlength');
281 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
282 my $tagfield = $input->param('tagfield');
283 my $tagsubfield = $tagsubfield[$i];
284 $tagsubfield = "@" unless $tagsubfield ne '';
285 my $liblibrarian = $liblibrarian[$i];
286 my $libopac = $libopac[$i];
287 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
288 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
289 my $kohafield = $kohafield[$i];
291 my $seealso = $seealso[$i];
292 my $authorised_value = $authorised_values[$i];
293 my $authtypecode = $authtypecodes[$i];
294 my $value_builder = $value_builder[$i];
295 my $hidden = $hidden[$i]; #input->param("hidden$i");
296 my $isurl = $input->param("isurl$i") ? 1 : 0;
297 my $link = $link[$i];
298 my $defaultvalue = $defaultvalue[$i];
299 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
301 if (defined($liblibrarian) && $liblibrarian ne "") {
302 if (marc_subfield_structure_exists($tagfield, $tagsubfield, $frameworkcode)) {
303 $sth_update->execute(
329 if( $frameworkcode ne q{} ) {
330 # BZ 19096: Overwrite kohafield from Default when adding a new record
331 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
332 $kohafield = $rec->kohafield if $rec;
334 Koha::MarcSubfieldStructure->new(
336 tagfield => $tagfield,
337 tagsubfield => $tagsubfield,
338 liblibrarian => $liblibrarian,
340 repeatable => $repeatable,
341 mandatory => $mandatory,
342 kohafield => $kohafield,
345 authorised_value => $authorised_value,
346 authtypecode => $authtypecode,
347 value_builder => $value_builder,
350 frameworkcode => $frameworkcode,
352 defaultvalue => $defaultvalue,
353 maxlength => $maxlength,
360 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
361 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
362 $cache->clear_from_cache("default_value_for_mod_marc-");
363 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
365 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
368 # END $OP eq ADD_VALIDATE
369 ################## DELETE_CONFIRM ##################################
370 # called by default form, used to confirm deletion of data in DB
372 elsif ( $op eq 'delete_confirm' ) {
373 my $dbh = C4::Context->dbh;
376 "select * from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
379 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
380 my $data = $sth->fetchrow_hashref;
383 liblibrarian => $data->{'liblibrarian'},
384 tagsubfield => $data->{'tagsubfield'},
385 delete_link => $script_name,
386 tagfield => $tagfield,
387 tagsubfield => $tagsubfield,
388 frameworkcode => $frameworkcode,
391 # END $OP eq DELETE_CONFIRM
392 ################## DELETE_CONFIRMED ##################################
393 # called by delete_confirm, used to effectively confirm deletion of data in DB
395 elsif ( $op eq 'delete_confirmed' ) {
396 my $dbh = C4::Context->dbh;
399 "delete from marc_subfield_structure where tagfield=? and tagsubfield=? and frameworkcode=?"
401 $sth->execute( $tagfield, $tagsubfield, $frameworkcode );
403 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
404 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
405 $cache->clear_from_cache("default_value_for_mod_marc-");
406 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
407 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
410 # END $OP eq DELETE_CONFIRMED
411 ################## DEFAULT ##################################
414 my ( $count, $results ) = string_search( $tagfield, $frameworkcode );
416 for ( my $i = 0; $i < $count; $i++ ) {
417 my %row_data; # get a fresh hash for the row data
418 $row_data{tagfield} = $results->[$i]{'tagfield'};
419 $row_data{tagsubfield} = $results->[$i]{'tagsubfield'};
420 $row_data{liblibrarian} = $results->[$i]{'liblibrarian'};
421 $row_data{kohafield} = $results->[$i]{'kohafield'};
422 $row_data{repeatable} = $results->[$i]{'repeatable'};
423 $row_data{mandatory} = $results->[$i]{'mandatory'};
424 $row_data{tab} = $results->[$i]{'tab'};
425 $row_data{seealso} = $results->[$i]{'seealso'};
426 $row_data{authorised_value} = $results->[$i]{'authorised_value'};
427 $row_data{authtypecode} = $results->[$i]{'authtypecode'};
428 $row_data{value_builder} = $results->[$i]{'value_builder'};
429 $row_data{hidden} = $results->[$i]{'hidden'};
430 $row_data{isurl} = $results->[$i]{'isurl'};
431 $row_data{link} = $results->[$i]{'link'};
433 if ( $row_data{tab} eq -1 ) {
434 $row_data{subfield_ignored} = 1;
437 push( @loop_data, \%row_data );
439 $template->param( loop => \@loop_data );
441 edit_tagfield => $tagfield,
442 edit_frameworkcode => $frameworkcode
445 } #---- END $OP eq DEFAULT
447 output_html_with_http_headers $input, $cookie, $template->output;