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;
30 use Koha::BiblioFrameworks;
32 use List::MoreUtils qw( uniq );
35 my $tagfield = $input->param('tagfield');
36 my $tagsubfield = $input->param('tagsubfield');
37 my $frameworkcode = $input->param('frameworkcode');
38 my $pkfield = "tagfield";
39 my $offset = $input->param('offset');
40 $offset = 0 if not defined $offset or $offset < 0;
41 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 template_name => "admin/marc_subfields_structure.tt",
48 flagsrequired => { parameters => 'manage_marc_frameworks' },
51 my $cache = Koha::Caches->get_instance();
53 my $op = $input->param('op') || "";
56 my $framework = Koha::BiblioFrameworks->search({ frameworkcode => $frameworkcode })->next;
60 script_name => $script_name,
61 tagfield => $tagfield,
62 frameworkcode => $frameworkcode,
63 framework => $framework,
65 ); # we show only the TMPL_VAR names $op
69 script_name => $script_name,
70 tagfield => $tagfield,
71 frameworkcode => $frameworkcode,
72 framework => $framework,
74 ); # we show only the TMPL_VAR names $op
77 ################## ADD_FORM ##################################
78 # called by default. Used to create form to add or modify a record
79 if ( $op eq 'add_form' ) {
80 my $dbh = C4::Context->dbh;
82 # builds kohafield tables
85 my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
87 while ( ( my $field ) = $sth2->fetchrow_array ) {
88 push @kohafields, "biblio." . $field;
90 $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
92 while ( ( my $field ) = $sth2->fetchrow_array ) {
93 if ( $field eq 'notes' ) { $field = 'bnotes'; }
94 push @kohafields, "biblioitems." . $field;
96 $sth2 = $dbh->prepare("SHOW COLUMNS from items");
98 while ( ( my $field ) = $sth2->fetchrow_array ) {
99 push @kohafields, "items." . $field;
102 # build authorised value list
104 my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
106 # build thesaurus categories list
107 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
109 # build value_builder list
110 my @value_builder = ('');
112 # read value_builder directory.
113 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
114 # on a standard install, /cgi-bin need to be added.
115 # test one, then the other
116 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
118 unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
119 $cgidir = C4::Context->config('intranetdir');
120 opendir( $dir_h, "$cgidir/cataloguing/value_builder" )
121 || die "can't opendir $cgidir/value_builder: $!";
123 while ( my $line = readdir($dir_h) ) {
124 if ( $line =~ /\.pl$/ &&
125 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
126 push( @value_builder, $line );
129 @value_builder= sort {$a cmp $b} @value_builder;
133 my $mss = Koha::MarcSubfieldStructures->search(
134 { tagfield => $tagfield, frameworkcode => $frameworkcode },
135 { order_by => 'display_order' }
139 for my $m ( @$mss ) {
140 my %row_data = %$m; # get a fresh hash for the row data
141 $row_data{subfieldcode} = $m->{tagsubfield};
142 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
143 $row_data{kohafields} = \@kohafields;
144 $row_data{authorised_values} = \@authorised_values;
145 $row_data{value_builders} = \@value_builder;
146 $row_data{authtypes} = \@authtypes;
149 if ( defined $m->{kohafield}
150 and $m->{kohafield} eq 'biblio.biblionumber' )
152 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
154 frameworkcode => $frameworkcode,
159 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
161 frameworkcode => $frameworkcode,
162 interface => "intranet",
166 if ( $hidden_opac or $hidden_intranet ) {
167 # We should allow editing for fixing it
168 $row_data{hidden_protected} = 0;
171 $row_data{hidden_protected} = 1;
175 push( @loop_data, \%row_data );
179 # Add a new row for the "New" tab
180 my %row_data; # get a fresh hash for the row data
181 $row_data{'new_subfield'} = 1;
182 $row_data{'subfieldcode'} = '';
183 $row_data{'maxlength'} = 9999;
184 $row_data{tab} = -1; #ignore
185 $row_data{tagsubfield} = "";
186 $row_data{liblibrarian} = "";
187 $row_data{libopac} = "";
188 $row_data{seealso} = "";
189 $row_data{hidden} = "";
190 $row_data{repeatable} = 0;
191 $row_data{mandatory} = 0;
192 $row_data{important} = 0;
193 $row_data{isurl} = 0;
194 $row_data{kohafields} = \@kohafields;
195 $row_data{authorised_values} = \@authorised_values;
196 $row_data{value_builders} = \@value_builder;
197 $row_data{authtypes} = \@authtypes;
198 $row_data{link} = "";
200 push( @loop_data, \%row_data );
202 $template->param( 'use_heading_flags_p' => 1 );
203 $template->param( 'heading_edit_subfields_p' => 1 );
205 tagfield => $tagfield,
206 tagsubfield => $tagsubfield,
208 more_tag => $tagfield
211 # END $OP eq ADD_FORM
212 ################## ADD_VALIDATE ##################################
213 # called by add_form, used to insert/modify data in DB
215 elsif ( $op eq 'add_validate' ) {
216 my $dbh = C4::Context->dbh;
217 $template->param( tagfield => "$input->param('tagfield')" );
218 my $tagfield = $input->param('tagfield');
219 my @tagsubfield = $input->multi_param('tagsubfield');
220 my @tab_ids = $input->multi_param('tab_id');
223 for my $tagsubfield ( @tagsubfield ) {
224 $tagsubfield = "@" unless $tagsubfield ne '';
225 my $id = shift @tab_ids;
226 my $liblibrarian = $input->param("liblibrarian_$id");
227 my $libopac = $input->param("libopac_$id");
228 my $repeatable = $input->param("repeatable_$id") ? 1 : 0;
229 my $mandatory = $input->param("mandatory_$id") ? 1 : 0;
230 my $important = $input->param("important_$id") ? 1 : 0;
231 my $kohafield = $input->param("kohafield_$id");
232 my $tab = $input->param("tab_$id");
233 my $seealso = $input->param("seealso_$id");
234 my $authorised_value = $input->param("authorised_value_$id");
235 my $authtypecode = $input->param("authtypecode_$id");
236 my $value_builder = $input->param("value_builder_$id");
237 my $hidden = $input->param("hidden_$id");
238 my $isurl = $input->param("isurl_$id") ? 1 : 0;
239 my $link = $input->param("link_$id");
240 my $defaultvalue = $input->param("defaultvalue_$id");
241 my $maxlength = $input->param("maxlength_$id") || 9999;
243 if (defined($liblibrarian) && $liblibrarian ne "") {
244 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
248 liblibrarian => $liblibrarian,
250 repeatable => $repeatable,
251 mandatory => $mandatory,
252 important => $important,
253 kohafield => $kohafield,
256 authorised_value => $authorised_value,
257 authtypecode => $authtypecode,
258 value_builder => $value_builder,
262 defaultvalue => $defaultvalue,
263 maxlength => $maxlength,
264 display_order => $display_order->{$tagfield} || 0
268 if( $frameworkcode ne q{} ) {
269 # BZ 19096: Overwrite kohafield from Default when adding a new record
270 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
271 $kohafield = $rec->kohafield if $rec;
273 Koha::MarcSubfieldStructure->new(
275 tagfield => $tagfield,
276 tagsubfield => $tagsubfield,
277 liblibrarian => $liblibrarian,
279 repeatable => $repeatable,
280 mandatory => $mandatory,
281 important => $important,
282 kohafield => $kohafield,
285 authorised_value => $authorised_value,
286 authtypecode => $authtypecode,
287 value_builder => $value_builder,
290 frameworkcode => $frameworkcode,
292 defaultvalue => $defaultvalue,
293 maxlength => $maxlength,
294 display_order => $display_order->{$tagfield} || 0,
298 $display_order->{$tagfield}++;
301 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
302 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
303 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
304 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
306 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
309 # END $OP eq ADD_VALIDATE
310 ################## DELETE_CONFIRM ##################################
311 # called by default form, used to confirm deletion of data in DB
313 elsif ( $op eq 'delete_confirm' ) {
314 my $mss = Koha::MarcSubfieldStructures->find(
316 tagfield => $tagfield,
317 tagsubfield => $tagsubfield,
318 frameworkcode => $frameworkcode
323 delete_link => $script_name,
326 # END $OP eq DELETE_CONFIRM
327 ################## DELETE_CONFIRMED ##################################
328 # called by delete_confirm, used to effectively confirm deletion of data in DB
330 elsif ( $op eq 'delete_confirmed' ) {
331 Koha::MarcSubfieldStructures->find(
333 tagfield => $tagfield,
334 tagsubfield => $tagsubfield,
335 frameworkcode => $frameworkcode
339 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
340 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
341 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
342 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
343 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
346 # END $OP eq DELETE_CONFIRMED
347 ################## DEFAULT ##################################
350 my $mss = Koha::MarcSubfieldStructures->search(
352 tagfield => { -like => "$tagfield%" },
353 frameworkcode => $frameworkcode
355 { order_by => [ 'tagfield', 'display_order' ] }
358 $template->param( loop => $mss );
360 edit_tagfield => $tagfield,
361 edit_frameworkcode => $frameworkcode
364 } #---- END $OP eq DEFAULT
366 output_html_with_http_headers $input, $cookie, $template->output;