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 C4::Output qw( output_html_with_http_headers );
22 use C4::Auth qw( get_template_and_user );
26 use Koha::Authority::Types;
27 use Koha::AuthorisedValueCategories;
28 use Koha::Filter::MARC::ViewPolicy;
30 use List::MoreUtils qw( uniq );
33 my $tagfield = $input->param('tagfield');
34 my $tagsubfield = $input->param('tagsubfield');
35 my $frameworkcode = $input->param('frameworkcode');
36 my $pkfield = "tagfield";
37 my $offset = $input->param('offset');
38 $offset = 0 if not defined $offset or $offset < 0;
39 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43 template_name => "admin/marc_subfields_structure.tt",
46 flagsrequired => { parameters => 'manage_marc_frameworks' },
49 my $cache = Koha::Caches->get_instance();
51 my $op = $input->param('op') || "";
56 script_name => $script_name,
57 tagfield => $tagfield,
58 frameworkcode => $frameworkcode,
60 ); # we show only the TMPL_VAR names $op
64 script_name => $script_name,
65 tagfield => $tagfield,
66 frameworkcode => $frameworkcode,
68 ); # we show only the TMPL_VAR names $op
71 ################## ADD_FORM ##################################
72 # called by default. Used to create form to add or modify a record
73 if ( $op eq 'add_form' ) {
74 my $dbh = C4::Context->dbh;
76 # builds kohafield tables
79 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
81 while ( ( my $field ) = $sth2->fetchrow_array ) {
82 push @kohafields, "biblio." . $field;
84 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
86 while ( ( my $field ) = $sth2->fetchrow_array ) {
87 if ( $field eq 'notes' ) { $field = 'bnotes'; }
88 push @kohafields, "biblioitems." . $field;
90 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
92 while ( ( my $field ) = $sth2->fetchrow_array ) {
93 push @kohafields, "items." . $field;
96 # build authorised value list
98 $sth2 = $dbh->prepare("select distinct category from authorised_values");
100 my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
102 # build thesaurus categories list
103 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
105 # build value_builder list
106 my @value_builder = ('');
108 # read value_builder directory.
109 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
110 # on a standard install, /cgi-bin need to be added.
111 # test one, then the other
112 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
113 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
114 $cgidir = C4::Context->config('intranetdir');
115 opendir( DIR, "$cgidir/cataloguing/value_builder" )
116 || die "can't opendir $cgidir/value_builder: $!";
118 while ( my $line = readdir(DIR) ) {
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,
203 more_tag => $tagfield
206 # END $OP eq ADD_FORM
207 ################## ADD_VALIDATE ##################################
208 # called by add_form, used to insert/modify data in DB
210 elsif ( $op eq 'add_validate' ) {
211 my $dbh = C4::Context->dbh;
212 $template->param( tagfield => "$input->param('tagfield')" );
213 my @tagsubfield = $input->multi_param('tagsubfield');
214 my @liblibrarian = $input->multi_param('liblibrarian');
215 my @libopac = $input->multi_param('libopac');
216 my @kohafield = $input->multi_param('kohafield');
217 my @tab = $input->multi_param('tab');
218 my @seealso = $input->multi_param('seealso');
219 my @hidden = $input->multi_param('hidden');
220 my @authorised_values = $input->multi_param('authorised_value');
221 my @authtypecodes = $input->multi_param('authtypecode');
222 my @value_builder = $input->multi_param('value_builder');
223 my @link = $input->multi_param('link');
224 my @defaultvalue = $input->multi_param('defaultvalue');
225 my @maxlength = $input->multi_param('maxlength');
228 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
229 my $tagfield = $input->param('tagfield');
230 my $tagsubfield = $tagsubfield[$i];
231 $tagsubfield = "@" unless $tagsubfield ne '';
232 my $liblibrarian = $liblibrarian[$i];
233 my $libopac = $libopac[$i];
234 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
235 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
236 my $important = $input->param("important$i") ? 1 : 0;
237 my $kohafield = $kohafield[$i];
239 my $seealso = $seealso[$i];
240 my $authorised_value = $authorised_values[$i];
241 my $authtypecode = $authtypecodes[$i];
242 my $value_builder = $value_builder[$i];
243 my $hidden = $hidden[$i]; #input->param("hidden$i");
244 my $isurl = $input->param("isurl$i") ? 1 : 0;
245 my $link = $link[$i];
246 my $defaultvalue = $defaultvalue[$i];
247 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
249 if (defined($liblibrarian) && $liblibrarian ne "") {
250 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
254 liblibrarian => $liblibrarian,
256 repeatable => $repeatable,
257 mandatory => $mandatory,
258 important => $important,
259 kohafield => $kohafield,
262 authorised_value => $authorised_value,
263 authtypecode => $authtypecode,
264 value_builder => $value_builder,
268 defaultvalue => $defaultvalue,
269 maxlength => $maxlength,
270 display_order => $display_order->{$tagfield} || 0
274 if( $frameworkcode ne q{} ) {
275 # BZ 19096: Overwrite kohafield from Default when adding a new record
276 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
277 $kohafield = $rec->kohafield if $rec;
279 Koha::MarcSubfieldStructure->new(
281 tagfield => $tagfield,
282 tagsubfield => $tagsubfield,
283 liblibrarian => $liblibrarian,
285 repeatable => $repeatable,
286 mandatory => $mandatory,
287 important => $important,
288 kohafield => $kohafield,
291 authorised_value => $authorised_value,
292 authtypecode => $authtypecode,
293 value_builder => $value_builder,
296 frameworkcode => $frameworkcode,
298 defaultvalue => $defaultvalue,
299 maxlength => $maxlength,
300 display_order => $display_order->{$tagfield} || 0,
304 $display_order->{$tagfield}++;
307 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
308 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
309 $cache->clear_from_cache("default_value_for_mod_marc-");
310 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
312 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
315 # END $OP eq ADD_VALIDATE
316 ################## DELETE_CONFIRM ##################################
317 # called by default form, used to confirm deletion of data in DB
319 elsif ( $op eq 'delete_confirm' ) {
320 my $mss = Koha::MarcSubfieldStructures->find(
322 tagfield => $tagfield,
323 tagsubfield => $tagsubfield,
324 frameworkcode => $frameworkcode
329 delete_link => $script_name,
332 # END $OP eq DELETE_CONFIRM
333 ################## DELETE_CONFIRMED ##################################
334 # called by delete_confirm, used to effectively confirm deletion of data in DB
336 elsif ( $op eq 'delete_confirmed' ) {
337 Koha::MarcSubfieldStructures->find(
339 tagfield => $tagfield,
340 tagsubfield => $tagsubfield,
341 frameworkcode => $frameworkcode
345 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
346 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
347 $cache->clear_from_cache("default_value_for_mod_marc-");
348 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
349 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
352 # END $OP eq DELETE_CONFIRMED
353 ################## DEFAULT ##################################
356 my $mss = Koha::MarcSubfieldStructures->search(
358 tagfield => { -like => "$tagfield%" },
359 frameworkcode => $frameworkcode
361 { order_by => [ 'tagfield', 'display_order' ] }
364 $template->param( loop => $mss );
366 edit_tagfield => $tagfield,
367 edit_frameworkcode => $frameworkcode
370 } #---- END $OP eq DEFAULT
372 output_html_with_http_headers $input, $cookie, $template->output;