Bug 33594: Update mappings and comment
[koha.git] / admin / marc_subfields_structure.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
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 );
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27 use Koha::Authority::Types;
28 use Koha::AuthorisedValueCategories;
29 use Koha::Filter::MARC::ViewPolicy;
30
31 use List::MoreUtils qw( uniq );
32
33 my $input         = CGI->new;
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";
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {
44         template_name   => "admin/marc_subfields_structure.tt",
45         query           => $input,
46         type            => "intranet",
47         flagsrequired   => { parameters => 'manage_marc_frameworks' },
48     }
49 );
50 my $cache = Koha::Caches->get_instance();
51
52 my $op       = $input->param('op') || "";
53 $tagfield =~ s/\,//g;
54
55 if ($op) {
56     $template->param(
57         script_name   => $script_name,
58         tagfield      => $tagfield,
59         frameworkcode => $frameworkcode,
60         $op           => 1
61     );    # we show only the TMPL_VAR names $op
62 }
63 else {
64     $template->param(
65         script_name   => $script_name,
66         tagfield      => $tagfield,
67         frameworkcode => $frameworkcode,
68         else          => 1
69     );    # we show only the TMPL_VAR names $op
70 }
71
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;
76
77     # builds kohafield tables
78     my @kohafields;
79     push @kohafields, "";
80     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
81     $sth2->execute;
82     while ( ( my $field ) = $sth2->fetchrow_array ) {
83         push @kohafields, "biblio." . $field;
84     }
85     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
86     $sth2->execute;
87     while ( ( my $field ) = $sth2->fetchrow_array ) {
88         if ( $field eq 'notes' ) { $field = 'bnotes'; }
89         push @kohafields, "biblioitems." . $field;
90     }
91     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
92     $sth2->execute;
93     while ( ( my $field ) = $sth2->fetchrow_array ) {
94         push @kohafields, "items." . $field;
95     }
96
97     # build authorised value list
98     $sth2->finish;
99     $sth2 = $dbh->prepare("select distinct category from authorised_values");
100     $sth2->execute;
101     my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
102
103     # build thesaurus categories list
104     my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
105
106     # build value_builder list
107     my @value_builder = ('');
108
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     my $dir_h;
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: $!";
119     }
120     while ( my $line = readdir($dir_h) ) {
121         if ( $line =~ /\.pl$/ &&
122              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
123             push( @value_builder, $line );
124         }
125     }
126     @value_builder= sort {$a cmp $b} @value_builder;
127     closedir $dir_h;
128
129     # build values list
130     my $mss = Koha::MarcSubfieldStructures->search(
131         { tagfield => $tagfield, frameworkcode => $frameworkcode },
132         { order_by => 'display_order' }
133     )->unblessed;
134     my @loop_data = ();
135     my $i         = 0;
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;
144         $row_data{row}               = $i;
145
146         if ( defined $m->{kohafield}
147             and $m->{kohafield} eq 'biblio.biblionumber' )
148         {
149             my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
150                     {
151                         frameworkcode => $frameworkcode,
152                         interface     => "opac",
153                     }
154                 )->{biblionumber};
155
156             my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
157                     {
158                         frameworkcode => $frameworkcode,
159                         interface     => "intranet",
160                     }
161                 )->{biblionumber};
162
163             if ( $hidden_opac or $hidden_intranet ) {
164                 # We should allow editing for fixing it
165                 $row_data{hidden_protected} = 0;
166             }
167             else {
168                 $row_data{hidden_protected} = 1;
169             }
170         }
171
172         push( @loop_data, \%row_data );
173         $i++;
174     }
175
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}              = "";
196     $row_data{row}               = $i;
197     push( @loop_data, \%row_data );
198
199     $template->param( 'use_heading_flags_p'      => 1 );
200     $template->param( 'heading_edit_subfields_p' => 1 );
201     $template->param(
202         action   => "Edit subfields",
203         tagfield => $tagfield,
204         tagsubfield => $tagsubfield,
205         loop           => \@loop_data,
206         more_tag       => $tagfield
207     );
208
209     # END $OP eq ADD_FORM
210 ################## ADD_VALIDATE ##################################
211     # called by add_form, used to insert/modify data in DB
212 }
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');
219
220     my $display_order;
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;
240
241         if (defined($liblibrarian) && $liblibrarian ne "") {
242             my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
243             if ($mss) {
244                 $mss->update(
245                     {
246                         liblibrarian     => $liblibrarian,
247                         libopac          => $libopac,
248                         repeatable       => $repeatable,
249                         mandatory        => $mandatory,
250                         important        => $important,
251                         kohafield        => $kohafield,
252                         tab              => $tab,
253                         seealso          => $seealso,
254                         authorised_value => $authorised_value,
255                         authtypecode     => $authtypecode,
256                         value_builder    => $value_builder,
257                         hidden           => $hidden,
258                         isurl            => $isurl,
259                         link             => $link,
260                         defaultvalue     => $defaultvalue,
261                         maxlength        => $maxlength,
262                         display_order    => $display_order->{$tagfield} || 0
263                     }
264                 );
265             } else {
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;
270                 }
271                 Koha::MarcSubfieldStructure->new(
272                     {
273                         tagfield         => $tagfield,
274                         tagsubfield      => $tagsubfield,
275                         liblibrarian     => $liblibrarian,
276                         libopac          => $libopac,
277                         repeatable       => $repeatable,
278                         mandatory        => $mandatory,
279                         important        => $important,
280                         kohafield        => $kohafield,
281                         tab              => $tab,
282                         seealso          => $seealso,
283                         authorised_value => $authorised_value,
284                         authtypecode     => $authtypecode,
285                         value_builder    => $value_builder,
286                         hidden           => $hidden,
287                         isurl            => $isurl,
288                         frameworkcode    => $frameworkcode,
289                         link             => $link,
290                         defaultvalue     => $defaultvalue,
291                         maxlength        => $maxlength,
292                         display_order    => $display_order->{$tagfield} || 0,
293                     }
294                 )->store;
295             }
296             $display_order->{$tagfield}++;
297         }
298     }
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");
303
304     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
305     exit;
306
307     # END $OP eq ADD_VALIDATE
308 ################## DELETE_CONFIRM ##################################
309     # called by default form, used to confirm deletion of data in DB
310 }
311 elsif ( $op eq 'delete_confirm' ) {
312     my $mss = Koha::MarcSubfieldStructures->find(
313         {
314             tagfield      => $tagfield,
315             tagsubfield   => $tagsubfield,
316             frameworkcode => $frameworkcode
317         }
318     );
319     $template->param(
320         mss => $mss,
321         delete_link   => $script_name,
322     );
323
324     # END $OP eq DELETE_CONFIRM
325 ################## DELETE_CONFIRMED ##################################
326   # called by delete_confirm, used to effectively confirm deletion of data in DB
327 }
328 elsif ( $op eq 'delete_confirmed' ) {
329     Koha::MarcSubfieldStructures->find(
330         {
331             tagfield      => $tagfield,
332             tagsubfield   => $tagsubfield,
333             frameworkcode => $frameworkcode
334         }
335     )->delete;
336
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&amp;frameworkcode=$frameworkcode");
342     exit;
343
344     # END $OP eq DELETE_CONFIRMED
345 ################## DEFAULT ##################################
346 }
347 else {    # DEFAULT
348     my $mss = Koha::MarcSubfieldStructures->search(
349         {
350             tagfield      => { -like => "$tagfield%" },
351             frameworkcode => $frameworkcode
352         },
353         { order_by => [ 'tagfield', 'display_order' ] }
354     )->unblessed;
355
356     $template->param( loop => $mss );
357     $template->param(
358         edit_tagfield      => $tagfield,
359         edit_frameworkcode => $frameworkcode
360     );
361
362 }    #---- END $OP eq DEFAULT
363
364 output_html_with_http_headers $input, $cookie, $template->output;