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 $sth2 = $dbh->prepare("select distinct category from authorised_values");
106 my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
108 # build thesaurus categories list
109 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
111 # build value_builder list
112 my @value_builder = ('');
114 # read value_builder directory.
115 # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
116 # on a standard install, /cgi-bin need to be added.
117 # test one, then the other
118 my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
120 unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
121 $cgidir = C4::Context->config('intranetdir');
122 opendir( $dir_h, "$cgidir/cataloguing/value_builder" )
123 || die "can't opendir $cgidir/value_builder: $!";
125 while ( my $line = readdir($dir_h) ) {
126 if ( $line =~ /\.pl$/ &&
127 $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
128 push( @value_builder, $line );
131 @value_builder= sort {$a cmp $b} @value_builder;
135 my $mss = Koha::MarcSubfieldStructures->search(
136 { tagfield => $tagfield, frameworkcode => $frameworkcode },
137 { order_by => 'display_order' }
141 for my $m ( @$mss ) {
142 my %row_data = %$m; # get a fresh hash for the row data
143 $row_data{subfieldcode} = $m->{tagsubfield};
144 $row_data{urisubfieldcode} = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
145 $row_data{kohafields} = \@kohafields;
146 $row_data{authorised_values} = \@authorised_values;
147 $row_data{value_builders} = \@value_builder;
148 $row_data{authtypes} = \@authtypes;
151 if ( defined $m->{kohafield}
152 and $m->{kohafield} eq 'biblio.biblionumber' )
154 my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
156 frameworkcode => $frameworkcode,
161 my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
163 frameworkcode => $frameworkcode,
164 interface => "intranet",
168 if ( $hidden_opac or $hidden_intranet ) {
169 # We should allow editing for fixing it
170 $row_data{hidden_protected} = 0;
173 $row_data{hidden_protected} = 1;
177 push( @loop_data, \%row_data );
181 # Add a new row for the "New" tab
182 my %row_data; # get a fresh hash for the row data
183 $row_data{'new_subfield'} = 1;
184 $row_data{'subfieldcode'} = '';
185 $row_data{'maxlength'} = 9999;
186 $row_data{tab} = -1; #ignore
187 $row_data{tagsubfield} = "";
188 $row_data{liblibrarian} = "";
189 $row_data{libopac} = "";
190 $row_data{seealso} = "";
191 $row_data{hidden} = "";
192 $row_data{repeatable} = 0;
193 $row_data{mandatory} = 0;
194 $row_data{important} = 0;
195 $row_data{isurl} = 0;
196 $row_data{kohafields} = \@kohafields;
197 $row_data{authorised_values} = \@authorised_values;
198 $row_data{value_builders} = \@value_builder;
199 $row_data{authtypes} = \@authtypes;
200 $row_data{link} = "";
202 push( @loop_data, \%row_data );
204 $template->param( 'use_heading_flags_p' => 1 );
205 $template->param( 'heading_edit_subfields_p' => 1 );
207 tagfield => $tagfield,
208 tagsubfield => $tagsubfield,
210 more_tag => $tagfield
213 # END $OP eq ADD_FORM
214 ################## ADD_VALIDATE ##################################
215 # called by add_form, used to insert/modify data in DB
217 elsif ( $op eq 'add_validate' ) {
218 my $dbh = C4::Context->dbh;
219 $template->param( tagfield => "$input->param('tagfield')" );
220 my $tagfield = $input->param('tagfield');
221 my @tagsubfield = $input->multi_param('tagsubfield');
222 my @tab_ids = $input->multi_param('tab_id');
225 for my $tagsubfield ( @tagsubfield ) {
226 $tagsubfield = "@" unless $tagsubfield ne '';
227 my $id = shift @tab_ids;
228 my $liblibrarian = $input->param("liblibrarian_$id");
229 my $libopac = $input->param("libopac_$id");
230 my $repeatable = $input->param("repeatable_$id") ? 1 : 0;
231 my $mandatory = $input->param("mandatory_$id") ? 1 : 0;
232 my $important = $input->param("important_$id") ? 1 : 0;
233 my $kohafield = $input->param("kohafield_$id");
234 my $tab = $input->param("tab_$id");
235 my $seealso = $input->param("seealso_$id");
236 my $authorised_value = $input->param("authorised_value_$id");
237 my $authtypecode = $input->param("authtypecode_$id");
238 my $value_builder = $input->param("value_builder_$id");
239 my $hidden = $input->param("hidden_$id");
240 my $isurl = $input->param("isurl_$id") ? 1 : 0;
241 my $link = $input->param("link_$id");
242 my $defaultvalue = $input->param("defaultvalue_$id");
243 my $maxlength = $input->param("maxlength_$id") || 9999;
245 if (defined($liblibrarian) && $liblibrarian ne "") {
246 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
250 liblibrarian => $liblibrarian,
252 repeatable => $repeatable,
253 mandatory => $mandatory,
254 important => $important,
255 kohafield => $kohafield,
258 authorised_value => $authorised_value,
259 authtypecode => $authtypecode,
260 value_builder => $value_builder,
264 defaultvalue => $defaultvalue,
265 maxlength => $maxlength,
266 display_order => $display_order->{$tagfield} || 0
270 if( $frameworkcode ne q{} ) {
271 # BZ 19096: Overwrite kohafield from Default when adding a new record
272 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
273 $kohafield = $rec->kohafield if $rec;
275 Koha::MarcSubfieldStructure->new(
277 tagfield => $tagfield,
278 tagsubfield => $tagsubfield,
279 liblibrarian => $liblibrarian,
281 repeatable => $repeatable,
282 mandatory => $mandatory,
283 important => $important,
284 kohafield => $kohafield,
287 authorised_value => $authorised_value,
288 authtypecode => $authtypecode,
289 value_builder => $value_builder,
292 frameworkcode => $frameworkcode,
294 defaultvalue => $defaultvalue,
295 maxlength => $maxlength,
296 display_order => $display_order->{$tagfield} || 0,
300 $display_order->{$tagfield}++;
303 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
304 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
305 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
306 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
308 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
311 # END $OP eq ADD_VALIDATE
312 ################## DELETE_CONFIRM ##################################
313 # called by default form, used to confirm deletion of data in DB
315 elsif ( $op eq 'delete_confirm' ) {
316 my $mss = Koha::MarcSubfieldStructures->find(
318 tagfield => $tagfield,
319 tagsubfield => $tagsubfield,
320 frameworkcode => $frameworkcode
325 delete_link => $script_name,
328 # END $OP eq DELETE_CONFIRM
329 ################## DELETE_CONFIRMED ##################################
330 # called by delete_confirm, used to effectively confirm deletion of data in DB
332 elsif ( $op eq 'delete_confirmed' ) {
333 Koha::MarcSubfieldStructures->find(
335 tagfield => $tagfield,
336 tagsubfield => $tagsubfield,
337 frameworkcode => $frameworkcode
341 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
342 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
343 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
344 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
345 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
348 # END $OP eq DELETE_CONFIRMED
349 ################## DEFAULT ##################################
352 my $mss = Koha::MarcSubfieldStructures->search(
354 tagfield => { -like => "$tagfield%" },
355 frameworkcode => $frameworkcode
357 { order_by => [ 'tagfield', 'display_order' ] }
360 $template->param( loop => $mss );
362 edit_tagfield => $tagfield,
363 edit_frameworkcode => $frameworkcode
366 } #---- END $OP eq DEFAULT
368 output_html_with_http_headers $input, $cookie, $template->output;