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;
31 use List::MoreUtils qw( uniq );
34 my $tagfield = $input->param('tagfield');
35 my $tagsubfield = $input->param('tagsubfield');
36 my $frameworkcode = $input->param('frameworkcode');
37 my $pkfield = "tagfield";
38 my $offset = $input->param('offset');
39 $offset = 0 if not defined $offset or $offset < 0;
40 my $script_name = "/cgi-bin/koha/admin/marc_subfields_structure.pl";
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 template_name => "admin/marc_subfields_structure.tt",
47 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 @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
103 # build thesaurus categories list
104 my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
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";
115 unless ( opendir( $dir_h, "$cgidir/cataloguing/value_builder" ) ) {
116 $cgidir = C4::Context->config('intranetdir');
117 opendir( $dir_h, "$cgidir/cataloguing/value_builder" )
118 || die "can't opendir $cgidir/value_builder: $!";
120 while ( my $line = readdir($dir_h) ) {
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,
204 tagsubfield => $tagsubfield,
206 more_tag => $tagfield
209 # END $OP eq ADD_FORM
210 ################## ADD_VALIDATE ##################################
211 # called by add_form, used to insert/modify data in DB
213 elsif ( $op eq 'add_validate' ) {
214 my $dbh = C4::Context->dbh;
215 $template->param( tagfield => "$input->param('tagfield')" );
216 my $tagfield = $input->param('tagfield');
217 my @tagsubfield = $input->multi_param('tagsubfield');
218 my @tab_ids = $input->multi_param('tab_id');
221 for my $tagsubfield ( @tagsubfield ) {
222 $tagsubfield = "@" unless $tagsubfield ne '';
223 my $id = shift @tab_ids;
224 my $liblibrarian = $input->param("liblibrarian_$id");
225 my $libopac = $input->param("libopac_$id");
226 my $repeatable = $input->param("repeatable_$id") ? 1 : 0;
227 my $mandatory = $input->param("mandatory_$id") ? 1 : 0;
228 my $important = $input->param("important_$id") ? 1 : 0;
229 my $kohafield = $input->param("kohafield_$id");
230 my $tab = $input->param("tab_$id");
231 my $seealso = $input->param("seealso_$id");
232 my $authorised_value = $input->param("authorised_value_$id");
233 my $authtypecode = $input->param("authtypecode_$id");
234 my $value_builder = $input->param("value_builder_$id");
235 my $hidden = $input->param("hidden_$id");
236 my $isurl = $input->param("isurl_$id") ? 1 : 0;
237 my $link = $input->param("link_$id");
238 my $defaultvalue = $input->param("defaultvalue_$id");
239 my $maxlength = $input->param("maxlength_$id") || 9999;
241 if (defined($liblibrarian) && $liblibrarian ne "") {
242 my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
246 liblibrarian => $liblibrarian,
248 repeatable => $repeatable,
249 mandatory => $mandatory,
250 important => $important,
251 kohafield => $kohafield,
254 authorised_value => $authorised_value,
255 authtypecode => $authtypecode,
256 value_builder => $value_builder,
260 defaultvalue => $defaultvalue,
261 maxlength => $maxlength,
262 display_order => $display_order->{$tagfield} || 0
266 if( $frameworkcode ne q{} ) {
267 # BZ 19096: Overwrite kohafield from Default when adding a new record
268 my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
269 $kohafield = $rec->kohafield if $rec;
271 Koha::MarcSubfieldStructure->new(
273 tagfield => $tagfield,
274 tagsubfield => $tagsubfield,
275 liblibrarian => $liblibrarian,
277 repeatable => $repeatable,
278 mandatory => $mandatory,
279 important => $important,
280 kohafield => $kohafield,
283 authorised_value => $authorised_value,
284 authtypecode => $authtypecode,
285 value_builder => $value_builder,
288 frameworkcode => $frameworkcode,
290 defaultvalue => $defaultvalue,
291 maxlength => $maxlength,
292 display_order => $display_order->{$tagfield} || 0,
296 $display_order->{$tagfield}++;
299 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
300 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
301 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
302 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
304 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
307 # END $OP eq ADD_VALIDATE
308 ################## DELETE_CONFIRM ##################################
309 # called by default form, used to confirm deletion of data in DB
311 elsif ( $op eq 'delete_confirm' ) {
312 my $mss = Koha::MarcSubfieldStructures->find(
314 tagfield => $tagfield,
315 tagsubfield => $tagsubfield,
316 frameworkcode => $frameworkcode
321 delete_link => $script_name,
324 # END $OP eq DELETE_CONFIRM
325 ################## DELETE_CONFIRMED ##################################
326 # called by delete_confirm, used to effectively confirm deletion of data in DB
328 elsif ( $op eq 'delete_confirmed' ) {
329 Koha::MarcSubfieldStructures->find(
331 tagfield => $tagfield,
332 tagsubfield => $tagsubfield,
333 frameworkcode => $frameworkcode
337 $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
338 $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
339 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
340 $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
341 print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&frameworkcode=$frameworkcode");
344 # END $OP eq DELETE_CONFIRMED
345 ################## DEFAULT ##################################
348 my $mss = Koha::MarcSubfieldStructures->search(
350 tagfield => { -like => "$tagfield%" },
351 frameworkcode => $frameworkcode
353 { order_by => [ 'tagfield', 'display_order' ] }
356 $template->param( loop => $mss );
358 edit_tagfield => $tagfield,
359 edit_frameworkcode => $frameworkcode
362 } #---- END $OP eq DEFAULT
364 output_html_with_http_headers $input, $cookie, $template->output;