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 Encode qw( encode_utf8 );
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
27 use Koha::Authority::Types;
28 use Koha::AuthorisedValueCategories;
29 use Koha::Filter::MARC::ViewPolicy;
31 use List::MoreUtils qw( uniq );
34 my $tagfield = $input->param('tagfield');
35 my $tagsubfield = $input->param('tagsubfield');
36 my $frameworkcode = $input->param('frameworkcode');
37 my $pkfield = "tagfield";
38 my $offset = $input->param('offset');
39 $offset = 0 if not defined $offset or $offset < 0;
40 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "admin/marc_subfields_structure.tt",
47 flagsrequired => { parameters => 'manage_marc_frameworks' },
50 my $cache = Koha::Caches->get_instance();
52 my $op = $input->param('op') || "";
57 script_name => $script_name,
58 tagfield => $tagfield,
59 frameworkcode => $frameworkcode,
61 ); # we show only the TMPL_VAR names $op
65 script_name => $script_name,
66 tagfield => $tagfield,
67 frameworkcode => $frameworkcode,
69 ); # we show only the TMPL_VAR names $op
72 ################## ADD_FORM ##################################
73 # called by default. Used to create form to add or modify a record
74 if ( $op eq 'add_form' ) {
75 my $dbh = C4::Context->dbh;
77 # builds kohafield tables
80 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
82 while ( ( my $field ) = $sth2->fetchrow_array ) {
83 push @kohafields, "biblio." . $field;
85 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
87 while ( ( my $field ) = $sth2->fetchrow_array ) {
88 if ( $field eq 'notes' ) { $field = 'bnotes'; }
89 push @kohafields, "biblioitems." . $field;
91 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
93 while ( ( my $field ) = $sth2->fetchrow_array ) {
94 push @kohafields, "items." . $field;
97 # build authorised value list
99 my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
101 # build thesaurus categories list
102 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
104 # build value_builder list
105 my @value_builder = ('');
107 # read value_builder directory.
108 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
109 # on a standard install, /cgi-bin need to be added.
110 # test one, then the other
111 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
113 unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
114 $cgidir = C4::Context->config('intranetdir');
115 opendir( $dir_h, "$cgidir/cataloguing/value_builder" )
116 || die "can't opendir $cgidir/value_builder: $!";
118 while ( my $line = readdir($dir_h) ) {
119 if ( $line =~ /\.pl$/ &&
120 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
121 push( @value_builder, $line );
124 @value_builder= sort {$a cmp $b} @value_builder;
128 my $mss = Koha::MarcSubfieldStructures->search(
129 { tagfield => $tagfield, frameworkcode => $frameworkcode },
130 { order_by => 'display_order' }
134 for my $m ( @$mss ) {
135 my %row_data = %$m; # get a fresh hash for the row data
136 $row_data{subfieldcode} = $m->{tagsubfield};
137 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
138 $row_data{kohafields} = \@kohafields;
139 $row_data{authorised_values} = \@authorised_values;
140 $row_data{value_builders} = \@value_builder;
141 $row_data{authtypes} = \@authtypes;
144 if ( defined $m->{kohafield}
145 and $m->{kohafield} eq 'biblio.biblionumber' )
147 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
149 frameworkcode => $frameworkcode,
154 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
156 frameworkcode => $frameworkcode,
157 interface => "intranet",
161 if ( $hidden_opac or $hidden_intranet ) {
162 # We should allow editing for fixing it
163 $row_data{hidden_protected} = 0;
166 $row_data{hidden_protected} = 1;
170 push( @loop_data, \%row_data );
174 # Add a new row for the "New" tab
175 my %row_data; # get a fresh hash for the row data
176 $row_data{'new_subfield'} = 1;
177 $row_data{'subfieldcode'} = '';
178 $row_data{'maxlength'} = 9999;
179 $row_data{tab} = -1; #ignore
180 $row_data{tagsubfield} = "";
181 $row_data{liblibrarian} = "";
182 $row_data{libopac} = "";
183 $row_data{seealso} = "";
184 $row_data{hidden} = "";
185 $row_data{repeatable} = 0;
186 $row_data{mandatory} = 0;
187 $row_data{important} = 0;
188 $row_data{isurl} = 0;
189 $row_data{kohafields} = \@kohafields;
190 $row_data{authorised_values} = \@authorised_values;
191 $row_data{value_builders} = \@value_builder;
192 $row_data{authtypes} = \@authtypes;
193 $row_data{link} = "";
195 push( @loop_data, \%row_data );
197 $template->param( 'use_heading_flags_p' => 1 );
198 $template->param( 'heading_edit_subfields_p' => 1 );
200 action => "Edit subfields",
201 tagfield => $tagfield,
202 tagsubfield => $tagsubfield,
204 more_tag => $tagfield
207 # END $OP eq ADD_FORM
208 ################## ADD_VALIDATE ##################################
209 # called by add_form, used to insert/modify data in DB
211 elsif ( $op eq 'add_validate' ) {
212 my $dbh = C4::Context->dbh;
213 $template->param( tagfield => "$input->param('tagfield')" );
214 my $tagfield = $input->param('tagfield');
215 my @tagsubfield = $input->multi_param('tagsubfield');
216 my @tab_ids = $input->multi_param('tab_id');
219 for my $tagsubfield ( @tagsubfield ) {
220 $tagsubfield = "@" unless $tagsubfield ne '';
221 my $id = shift @tab_ids;
222 my $liblibrarian = $input->param("liblibrarian_$id");
223 my $libopac = $input->param("libopac_$id");
224 my $repeatable = $input->param("repeatable_$id") ? 1 : 0;
225 my $mandatory = $input->param("mandatory_$id") ? 1 : 0;
226 my $important = $input->param("important_$id") ? 1 : 0;
227 my $kohafield = $input->param("kohafield_$id");
228 my $tab = $input->param("tab_$id");
229 my $seealso = $input->param("seealso_$id");
230 my $authorised_value = $input->param("authorised_value_$id");
231 my $authtypecode = $input->param("authtypecode_$id");
232 my $value_builder = $input->param("value_builder_$id");
233 my $hidden = $input->param("hidden_$id");
234 my $isurl = $input->param("isurl_$id") ? 1 : 0;
235 my $link = $input->param("link_$id");
236 my $defaultvalue = $input->param("defaultvalue_$id");
237 my $maxlength = $input->param("maxlength_$id") || 9999;
239 if (defined($liblibrarian) && $liblibrarian ne "") {
240 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
244 liblibrarian => $liblibrarian,
246 repeatable => $repeatable,
247 mandatory => $mandatory,
248 important => $important,
249 kohafield => $kohafield,
252 authorised_value => $authorised_value,
253 authtypecode => $authtypecode,
254 value_builder => $value_builder,
258 defaultvalue => $defaultvalue,
259 maxlength => $maxlength,
260 display_order => $display_order->{$tagfield} || 0
264 if( $frameworkcode ne q{} ) {
265 # BZ 19096: Overwrite kohafield from Default when adding a new record
266 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
267 $kohafield = $rec->kohafield if $rec;
269 Koha::MarcSubfieldStructure->new(
271 tagfield => $tagfield,
272 tagsubfield => $tagsubfield,
273 liblibrarian => $liblibrarian,
275 repeatable => $repeatable,
276 mandatory => $mandatory,
277 important => $important,
278 kohafield => $kohafield,
281 authorised_value => $authorised_value,
282 authtypecode => $authtypecode,
283 value_builder => $value_builder,
286 frameworkcode => $frameworkcode,
288 defaultvalue => $defaultvalue,
289 maxlength => $maxlength,
290 display_order => $display_order->{$tagfield} || 0,
294 $display_order->{$tagfield}++;
297 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
298 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
299 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
300 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
302 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
305 # END $OP eq ADD_VALIDATE
306 ################## DELETE_CONFIRM ##################################
307 # called by default form, used to confirm deletion of data in DB
309 elsif ( $op eq 'delete_confirm' ) {
310 my $mss = Koha::MarcSubfieldStructures->find(
312 tagfield => $tagfield,
313 tagsubfield => $tagsubfield,
314 frameworkcode => $frameworkcode
319 delete_link => $script_name,
322 # END $OP eq DELETE_CONFIRM
323 ################## DELETE_CONFIRMED ##################################
324 # called by delete_confirm, used to effectively confirm deletion of data in DB
326 elsif ( $op eq 'delete_confirmed' ) {
327 Koha::MarcSubfieldStructures->find(
329 tagfield => $tagfield,
330 tagsubfield => $tagsubfield,
331 frameworkcode => $frameworkcode
335 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
336 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
337 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
338 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
339 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
342 # END $OP eq DELETE_CONFIRMED
343 ################## DEFAULT ##################################
346 my $mss = Koha::MarcSubfieldStructures->search(
348 tagfield => { -like => "$tagfield%" },
349 frameworkcode => $frameworkcode
351 { order_by => [ 'tagfield', 'display_order' ] }
354 $template->param( loop => $mss );
356 edit_tagfield => $tagfield,
357 edit_frameworkcode => $frameworkcode
360 } #---- END $OP eq DEFAULT
362 output_html_with_http_headers $input, $cookie, $template->output;