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 @av_cat = Koha::AuthorisedValueCategories->search;
101 my @authorised_values = map { $_->category_name } @av_cat;
103 # build thesaurus categories list
104 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search );
106 # build value_builder list
107 my @value_builder = ('');
109 # read value_builder directory.
110 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
111 # on a standard install, /cgi-bin need to be added.
112 # test one, then the other
113 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
114 unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
115 $cgidir = C4::Context->config('intranetdir');
116 opendir( DIR, "$cgidir/cataloguing/value_builder" )
117 || die "can't opendir $cgidir/value_builder: $!";
119 while ( my $line = readdir(DIR) ) {
120 if ( $line =~ /\.pl$/ &&
121 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
122 push( @value_builder, $line );
125 @value_builder= sort {$a cmp $b} @value_builder;
129 my $mss = Koha::MarcSubfieldStructures->search(
130 { tagfield => $tagfield, frameworkcode => $frameworkcode },
131 { order_by => 'display_order' }
135 for my $m ( @$mss ) {
136 my %row_data = %$m; # get a fresh hash for the row data
137 $row_data{subfieldcode} = $m->{tagsubfield};
138 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
139 $row_data{kohafields} = \@kohafields;
140 $row_data{authorised_values} = \@authorised_values;
141 $row_data{value_builders} = \@value_builder;
142 $row_data{authtypes} = \@authtypes;
145 if ( defined $m->{kohafield}
146 and $m->{kohafield} eq 'biblio.biblionumber' )
148 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
150 frameworkcode => $frameworkcode,
155 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
157 frameworkcode => $frameworkcode,
158 interface => "intranet",
162 if ( $hidden_opac or $hidden_intranet ) {
163 # We should allow editing for fixing it
164 $row_data{hidden_protected} = 0;
167 $row_data{hidden_protected} = 1;
171 push( @loop_data, \%row_data );
175 # Add a new row for the "New" tab
176 my %row_data; # get a fresh hash for the row data
177 $row_data{'new_subfield'} = 1;
178 $row_data{'subfieldcode'} = '';
179 $row_data{'maxlength'} = 9999;
180 $row_data{tab} = -1; #ignore
181 $row_data{tagsubfield} = "";
182 $row_data{liblibrarian} = "";
183 $row_data{libopac} = "";
184 $row_data{seealso} = "";
185 $row_data{hidden} = "";
186 $row_data{repeatable} = 0;
187 $row_data{mandatory} = 0;
188 $row_data{important} = 0;
189 $row_data{isurl} = 0;
190 $row_data{kohafields} = \@kohafields;
191 $row_data{authorised_values} = \@authorised_values;
192 $row_data{value_builders} = \@value_builder;
193 $row_data{authtypes} = \@authtypes;
194 $row_data{link} = "";
196 push( @loop_data, \%row_data );
198 $template->param( 'use_heading_flags_p' => 1 );
199 $template->param( 'heading_edit_subfields_p' => 1 );
201 action => "Edit subfields",
202 tagfield => $tagfield,
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 @tagsubfield = $input->multi_param('tagsubfield');
215 my @liblibrarian = $input->multi_param('liblibrarian');
216 my @libopac = $input->multi_param('libopac');
217 my @kohafield = $input->multi_param('kohafield');
218 my @tab = $input->multi_param('tab');
219 my @seealso = $input->multi_param('seealso');
220 my @hidden = $input->multi_param('hidden');
221 my @authorised_values = $input->multi_param('authorised_value');
222 my @authtypecodes = $input->multi_param('authtypecode');
223 my @value_builder = $input->multi_param('value_builder');
224 my @link = $input->multi_param('link');
225 my @defaultvalue = $input->multi_param('defaultvalue');
226 my @maxlength = $input->multi_param('maxlength');
229 for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
230 my $tagfield = $input->param('tagfield');
231 my $tagsubfield = $tagsubfield[$i];
232 $tagsubfield = "@" unless $tagsubfield ne '';
233 my $liblibrarian = $liblibrarian[$i];
234 my $libopac = $libopac[$i];
235 my $repeatable = $input->param("repeatable$i") ? 1 : 0;
236 my $mandatory = $input->param("mandatory$i") ? 1 : 0;
237 my $important = $input->param("important$i") ? 1 : 0;
238 my $kohafield = $kohafield[$i];
240 my $seealso = $seealso[$i];
241 my $authorised_value = $authorised_values[$i];
242 my $authtypecode = $authtypecodes[$i];
243 my $value_builder = $value_builder[$i];
244 my $hidden = $hidden[$i]; #input->param("hidden$i");
245 my $isurl = $input->param("isurl$i") ? 1 : 0;
246 my $link = $link[$i];
247 my $defaultvalue = $defaultvalue[$i];
248 my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
250 if (defined($liblibrarian) && $liblibrarian ne "") {
251 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
255 liblibrarian => $liblibrarian,
257 repeatable => $repeatable,
258 mandatory => $mandatory,
259 important => $important,
260 kohafield => $kohafield,
263 authorised_value => $authorised_value,
264 authtypecode => $authtypecode,
265 value_builder => $value_builder,
269 defaultvalue => $defaultvalue,
270 maxlength => $maxlength,
271 display_order => $display_order->{$tagfield} || 0
275 if( $frameworkcode ne q{} ) {
276 # BZ 19096: Overwrite kohafield from Default when adding a new record
277 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
278 $kohafield = $rec->kohafield if $rec;
280 Koha::MarcSubfieldStructure->new(
282 tagfield => $tagfield,
283 tagsubfield => $tagsubfield,
284 liblibrarian => $liblibrarian,
286 repeatable => $repeatable,
287 mandatory => $mandatory,
288 important => $important,
289 kohafield => $kohafield,
292 authorised_value => $authorised_value,
293 authtypecode => $authtypecode,
294 value_builder => $value_builder,
297 frameworkcode => $frameworkcode,
299 defaultvalue => $defaultvalue,
300 maxlength => $maxlength,
301 display_order => $display_order->{$tagfield} || 0,
305 $display_order->{$tagfield}++;
308 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
309 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
310 $cache->clear_from_cache("default_value_for_mod_marc-");
311 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
313 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
316 # END $OP eq ADD_VALIDATE
317 ################## DELETE_CONFIRM ##################################
318 # called by default form, used to confirm deletion of data in DB
320 elsif ( $op eq 'delete_confirm' ) {
321 my $mss = Koha::MarcSubfieldStructures->find(
323 tagfield => $tagfield,
324 tagsubfield => $tagsubfield,
325 frameworkcode => $frameworkcode
330 delete_link => $script_name,
333 # END $OP eq DELETE_CONFIRM
334 ################## DELETE_CONFIRMED ##################################
335 # called by delete_confirm, used to effectively confirm deletion of data in DB
337 elsif ( $op eq 'delete_confirmed' ) {
338 Koha::MarcSubfieldStructures->find(
340 tagfield => $tagfield,
341 tagsubfield => $tagsubfield,
342 frameworkcode => $frameworkcode
346 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
347 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
348 $cache->clear_from_cache("default_value_for_mod_marc-");
349 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
350 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
353 # END $OP eq DELETE_CONFIRMED
354 ################## DEFAULT ##################################
357 my $mss = Koha::MarcSubfieldStructures->search(
359 tagfield => { -like => "$tagfield%" },
360 frameworkcode => $frameworkcode
362 { order_by => [ 'tagfield', 'display_order' ] }
365 $template->param( loop => $mss );
367 edit_tagfield => $tagfield,
368 edit_frameworkcode => $frameworkcode
371 } #---- END $OP eq DEFAULT
373 output_html_with_http_headers $input, $cookie, $template->output;