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 $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' },
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 $sth2 = $dbh->prepare("select distinct category from authorised_values");
101 my @av_cat = Koha::AuthorisedValueCategories->search;
102 my @authorised_values = map { $_->category_name } @av_cat;
104 # build thesaurus categories list
105 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
107 # build value_builder list
108 my @value_builder = ('');
110 # read value_builder directory.
111 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
112 # on a standard install, /cgi-bin need to be added.
113 # test one, then the other
114 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
115 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
116 $cgidir = C4::Context->config('intranetdir');
117 opendir( DIR, "$cgidir/cataloguing/value_builder" )
118 || die "can't opendir $cgidir/value_builder: $!";
120 while ( my $line = readdir(DIR) ) {
121 if ( $line =~ /\.pl$/ &&
122 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
123 push( @value_builder, $line );
126 @value_builder= sort {$a cmp $b} @value_builder;
130 my $mss = Koha::MarcSubfieldStructures->search(
131 { tagfield => $tagfield, frameworkcode => $frameworkcode },
132 { order_by => 'display_order' }
136 for my $m ( @$mss ) {
137 my %row_data = %$m; # get a fresh hash for the row data
138 $row_data{subfieldcode} = $m->{tagsubfield};
139 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
140 $row_data{kohafields} = \@kohafields;
141 $row_data{authorised_values} = \@authorised_values;
142 $row_data{value_builders} = \@value_builder;
143 $row_data{authtypes} = \@authtypes;
146 if ( defined $m->{kohafield}
147 and $m->{kohafield} eq 'biblio.biblionumber' )
149 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
151 frameworkcode => $frameworkcode,
156 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
158 frameworkcode => $frameworkcode,
159 interface => "intranet",
163 if ( $hidden_opac or $hidden_intranet ) {
164 # We should allow editing for fixing it
165 $row_data{hidden_protected} = 0;
168 $row_data{hidden_protected} = 1;
172 push( @loop_data, \%row_data );
176 # Add a new row for the "New" tab
177 my %row_data; # get a fresh hash for the row data
178 $row_data{'new_subfield'} = 1;
179 $row_data{'subfieldcode'} = '';
180 $row_data{'maxlength'} = 9999;
181 $row_data{tab} = -1; #ignore
182 $row_data{tagsubfield} = "";
183 $row_data{liblibrarian} = "";
184 $row_data{libopac} = "";
185 $row_data{seealso} = "";
186 $row_data{hidden} = "";
187 $row_data{repeatable} = 0;
188 $row_data{mandatory} = 0;
189 $row_data{important} = 0;
190 $row_data{isurl} = 0;
191 $row_data{kohafields} = \@kohafields;
192 $row_data{authorised_values} = \@authorised_values;
193 $row_data{value_builders} = \@value_builder;
194 $row_data{authtypes} = \@authtypes;
195 $row_data{link} = "";
197 push( @loop_data, \%row_data );
199 $template->param( 'use_heading_flags_p' => 1 );
200 $template->param( 'heading_edit_subfields_p' => 1 );
202 action => "Edit subfields",
203 tagfield => $tagfield,
205 more_tag => $tagfield
208 # END $OP eq ADD_FORM
209 ################## ADD_VALIDATE ##################################
210 # called by add_form, used to insert/modify data in DB
212 elsif ( $op eq 'add_validate' ) {
213 my $dbh = C4::Context->dbh;
214 $template->param( tagfield => "$input->param('tagfield')" );
215 my @tagsubfield = $input->multi_param('tagsubfield');
216 my @liblibrarian = $input->multi_param('liblibrarian');
217 my @libopac = $input->multi_param('libopac');
218 my @kohafield = $input->multi_param('kohafield');
219 my @tab = $input->multi_param('tab');
220 my @seealso = $input->multi_param('seealso');
221 my @hidden = $input->multi_param('hidden');
222 my @authorised_values = $input->multi_param('authorised_value');
223 my @authtypecodes = $input->multi_param('authtypecode');
224 my @value_builder = $input->multi_param('value_builder');
225 my @link = $input->multi_param('link');
226 my @defaultvalue = $input->multi_param('defaultvalue');
227 my @maxlength = $input->multi_param('maxlength');
230 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
231 my $tagfield = $input->param('tagfield');
232 my $tagsubfield = $tagsubfield[$i];
233 $tagsubfield = "@" unless $tagsubfield ne '';
234 my $liblibrarian = $liblibrarian[$i];
235 my $libopac = $libopac[$i];
236 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
237 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
238 my $important = $input->param("important$i") ? 1 : 0;
239 my $kohafield = $kohafield[$i];
241 my $seealso = $seealso[$i];
242 my $authorised_value = $authorised_values[$i];
243 my $authtypecode = $authtypecodes[$i];
244 my $value_builder = $value_builder[$i];
245 my $hidden = $hidden[$i]; #input->param("hidden$i");
246 my $isurl = $input->param("isurl$i") ? 1 : 0;
247 my $link = $link[$i];
248 my $defaultvalue = $defaultvalue[$i];
249 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
251 if (defined($liblibrarian) && $liblibrarian ne "") {
252 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
256 liblibrarian => $liblibrarian,
258 repeatable => $repeatable,
259 mandatory => $mandatory,
260 important => $important,
261 kohafield => $kohafield,
264 authorised_value => $authorised_value,
265 authtypecode => $authtypecode,
266 value_builder => $value_builder,
270 defaultvalue => $defaultvalue,
271 maxlength => $maxlength,
272 display_order => $display_order->{$tagfield} || 0
276 if( $frameworkcode ne q{} ) {
277 # BZ 19096: Overwrite kohafield from Default when adding a new record
278 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
279 $kohafield = $rec->kohafield if $rec;
281 Koha::MarcSubfieldStructure->new(
283 tagfield => $tagfield,
284 tagsubfield => $tagsubfield,
285 liblibrarian => $liblibrarian,
287 repeatable => $repeatable,
288 mandatory => $mandatory,
289 important => $important,
290 kohafield => $kohafield,
293 authorised_value => $authorised_value,
294 authtypecode => $authtypecode,
295 value_builder => $value_builder,
298 frameworkcode => $frameworkcode,
300 defaultvalue => $defaultvalue,
301 maxlength => $maxlength,
302 display_order => $display_order->{$tagfield} || 0,
306 $display_order->{$tagfield}++;
309 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
310 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
311 $cache->clear_from_cache("default_value_for_mod_marc-");
312 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
314 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
317 # END $OP eq ADD_VALIDATE
318 ################## DELETE_CONFIRM ##################################
319 # called by default form, used to confirm deletion of data in DB
321 elsif ( $op eq 'delete_confirm' ) {
322 my $mss = Koha::MarcSubfieldStructures->find(
324 tagfield => $tagfield,
325 tagsubfield => $tagsubfield,
326 frameworkcode => $frameworkcode
331 delete_link => $script_name,
334 # END $OP eq DELETE_CONFIRM
335 ################## DELETE_CONFIRMED ##################################
336 # called by delete_confirm, used to effectively confirm deletion of data in DB
338 elsif ( $op eq 'delete_confirmed' ) {
339 Koha::MarcSubfieldStructures->find(
341 tagfield => $tagfield,
342 tagsubfield => $tagsubfield,
343 frameworkcode => $frameworkcode
347 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
348 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
349 $cache->clear_from_cache("default_value_for_mod_marc-");
350 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
351 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
354 # END $OP eq DELETE_CONFIRMED
355 ################## DEFAULT ##################################
358 my $mss = Koha::MarcSubfieldStructures->search(
360 tagfield => { -like => "$tagfield%" },
361 frameworkcode => $frameworkcode
363 { order_by => [ 'tagfield', 'display_order' ] }
366 $template->param( loop => $mss );
368 edit_tagfield => $tagfield,
369 edit_frameworkcode => $frameworkcode
372 } #---- END $OP eq DEFAULT
374 output_html_with_http_headers $input, $cookie, $template->output;