Bug 30477: Add new UNIMARC installer translation files
[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 C4::Output qw( output_html_with_http_headers );
22 use C4::Auth qw( get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Context;
25
26 use Koha::Authority::Types;
27 use Koha::AuthorisedValueCategories;
28 use Koha::Filter::MARC::ViewPolicy;
29
30 use List::MoreUtils qw( uniq );
31
32 my $input         = CGI->new;
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";
40
41 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
42     {
43         template_name   => "admin/marc_subfields_structure.tt",
44         query           => $input,
45         type            => "intranet",
46         flagsrequired   => { parameters => 'manage_marc_frameworks' },
47     }
48 );
49 my $cache = Koha::Caches->get_instance();
50
51 my $op       = $input->param('op') || "";
52 $tagfield =~ s/\,//g;
53
54 if ($op) {
55     $template->param(
56         script_name   => $script_name,
57         tagfield      => $tagfield,
58         frameworkcode => $frameworkcode,
59         $op           => 1
60     );    # we show only the TMPL_VAR names $op
61 }
62 else {
63     $template->param(
64         script_name   => $script_name,
65         tagfield      => $tagfield,
66         frameworkcode => $frameworkcode,
67         else          => 1
68     );    # we show only the TMPL_VAR names $op
69 }
70
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;
75
76     # builds kohafield tables
77     my @kohafields;
78     push @kohafields, "";
79     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
80     $sth2->execute;
81     while ( ( my $field ) = $sth2->fetchrow_array ) {
82         push @kohafields, "biblio." . $field;
83     }
84     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
85     $sth2->execute;
86     while ( ( my $field ) = $sth2->fetchrow_array ) {
87         if ( $field eq 'notes' ) { $field = 'bnotes'; }
88         push @kohafields, "biblioitems." . $field;
89     }
90     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
91     $sth2->execute;
92     while ( ( my $field ) = $sth2->fetchrow_array ) {
93         push @kohafields, "items." . $field;
94     }
95
96     # build authorised value list
97     $sth2->finish;
98     $sth2 = $dbh->prepare("select distinct category from authorised_values");
99     $sth2->execute;
100     my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
101
102     # build thesaurus categories list
103     my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
104
105     # build value_builder list
106     my @value_builder = ('');
107
108     # read value_builder directory.
109     # 2 cases here : on CVS install, $cgidir does not need a /cgi-bin
110     # on a standard install, /cgi-bin need to be added.
111     # test one, then the other
112     my $cgidir = C4::Context->config('intranetdir') . "/cgi-bin";
113     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
114         $cgidir = C4::Context->config('intranetdir');
115         opendir( DIR, "$cgidir/cataloguing/value_builder" )
116           || die "can't opendir $cgidir/value_builder: $!";
117     }
118     while ( my $line = readdir(DIR) ) {
119         if ( $line =~ /\.pl$/ &&
120              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
121             push( @value_builder, $line );
122         }
123     }
124     @value_builder= sort {$a cmp $b} @value_builder;
125     closedir DIR;
126
127     # build values list
128     my $mss = Koha::MarcSubfieldStructures->search(
129         { tagfield => $tagfield, frameworkcode => $frameworkcode },
130         { order_by => 'display_order' }
131     )->unblessed;
132     my @loop_data = ();
133     my $i         = 0;
134     for my $m ( @$mss ) {
135         my %row_data = %$m;    # get a fresh hash for the row data
136         $row_data{subfieldcode}      = $m->{tagsubfield};
137         $row_data{urisubfieldcode}   = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
138         $row_data{kohafields}        = \@kohafields;
139         $row_data{authorised_values} = \@authorised_values;
140         $row_data{value_builders}    = \@value_builder;
141         $row_data{authtypes}         = \@authtypes;
142         $row_data{row}               = $i;
143
144         if ( defined $m->{kohafield}
145             and $m->{kohafield} eq 'biblio.biblionumber' )
146         {
147             my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
148                     {
149                         frameworkcode => $frameworkcode,
150                         interface     => "opac",
151                     }
152                 )->{biblionumber};
153
154             my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
155                     {
156                         frameworkcode => $frameworkcode,
157                         interface     => "intranet",
158                     }
159                 )->{biblionumber};
160
161             if ( $hidden_opac or $hidden_intranet ) {
162                 # We should allow editing for fixing it
163                 $row_data{hidden_protected} = 0;
164             }
165             else {
166                 $row_data{hidden_protected} = 1;
167             }
168         }
169
170         push( @loop_data, \%row_data );
171         $i++;
172     }
173
174     # Add a new row for the "New" tab
175     my %row_data;    # get a fresh hash for the row data
176     $row_data{'new_subfield'}    = 1;
177     $row_data{'subfieldcode'}    = '';
178     $row_data{'maxlength'}       = 9999;
179     $row_data{tab}               = -1;                    #ignore
180     $row_data{tagsubfield}       = "";
181     $row_data{liblibrarian}      = "";
182     $row_data{libopac}           = "";
183     $row_data{seealso}           = "";
184     $row_data{hidden}            = "";
185     $row_data{repeatable}        = 0;
186     $row_data{mandatory}         = 0;
187     $row_data{important}         = 0;
188     $row_data{isurl}             = 0;
189     $row_data{kohafields}        = \@kohafields;
190     $row_data{authorised_values} = \@authorised_values;
191     $row_data{value_builders}    = \@value_builder;
192     $row_data{authtypes}         = \@authtypes;
193     $row_data{link}              = "";
194     $row_data{row}               = $i;
195     push( @loop_data, \%row_data );
196
197     $template->param( 'use_heading_flags_p'      => 1 );
198     $template->param( 'heading_edit_subfields_p' => 1 );
199     $template->param(
200         action   => "Edit subfields",
201         tagfield => $tagfield,
202         loop           => \@loop_data,
203         more_tag       => $tagfield
204     );
205
206     # END $OP eq ADD_FORM
207 ################## ADD_VALIDATE ##################################
208     # called by add_form, used to insert/modify data in DB
209 }
210 elsif ( $op eq 'add_validate' ) {
211     my $dbh = C4::Context->dbh;
212     $template->param( tagfield => "$input->param('tagfield')" );
213     my @tagsubfield       = $input->multi_param('tagsubfield');
214     my @liblibrarian      = $input->multi_param('liblibrarian');
215     my @libopac           = $input->multi_param('libopac');
216     my @kohafield         = $input->multi_param('kohafield');
217     my @tab               = $input->multi_param('tab');
218     my @seealso           = $input->multi_param('seealso');
219     my @hidden            = $input->multi_param('hidden');
220     my @authorised_values = $input->multi_param('authorised_value');
221     my @authtypecodes     = $input->multi_param('authtypecode');
222     my @value_builder     = $input->multi_param('value_builder');
223     my @link              = $input->multi_param('link');
224     my @defaultvalue      = $input->multi_param('defaultvalue');
225     my @maxlength         = $input->multi_param('maxlength');
226
227     my $display_order;
228     for ( my $i = 0 ; $i <= $#tagsubfield ; $i++ ) {
229         my $tagfield    = $input->param('tagfield');
230         my $tagsubfield = $tagsubfield[$i];
231         $tagsubfield = "@" unless $tagsubfield ne '';
232         my $liblibrarian     = $liblibrarian[$i];
233         my $libopac          = $libopac[$i];
234         my $repeatable       = $input->param("repeatable$i") ? 1 : 0;
235         my $mandatory        = $input->param("mandatory$i") ? 1 : 0;
236         my $important        = $input->param("important$i") ? 1 : 0;
237         my $kohafield        = $kohafield[$i];
238         my $tab              = $tab[$i];
239         my $seealso          = $seealso[$i];
240         my $authorised_value = $authorised_values[$i];
241         my $authtypecode     = $authtypecodes[$i];
242         my $value_builder    = $value_builder[$i];
243         my $hidden = $hidden[$i];                     #input->param("hidden$i");
244         my $isurl  = $input->param("isurl$i") ? 1 : 0;
245         my $link   = $link[$i];
246         my $defaultvalue = $defaultvalue[$i];
247         my $maxlength = $maxlength[$i] ? $maxlength[$i] : 9999;
248         
249         if (defined($liblibrarian) && $liblibrarian ne "") {
250             my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
251             if ($mss) {
252                 $mss->update(
253                     {
254                         liblibrarian     => $liblibrarian,
255                         libopac          => $libopac,
256                         repeatable       => $repeatable,
257                         mandatory        => $mandatory,
258                         important        => $important,
259                         kohafield        => $kohafield,
260                         tab              => $tab,
261                         seealso          => $seealso,
262                         authorised_value => $authorised_value,
263                         authtypecode     => $authtypecode,
264                         value_builder    => $value_builder,
265                         hidden           => $hidden,
266                         isurl            => $isurl,
267                         link             => $link,
268                         defaultvalue     => $defaultvalue,
269                         maxlength        => $maxlength,
270                         display_order    => $display_order->{$tagfield} || 0
271                     }
272                 );
273             } else {
274                 if( $frameworkcode ne q{} ) {
275                     # BZ 19096: Overwrite kohafield from Default when adding a new record
276                      my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
277                     $kohafield = $rec->kohafield if $rec;
278                 }
279                 Koha::MarcSubfieldStructure->new(
280                     {
281                         tagfield         => $tagfield,
282                         tagsubfield      => $tagsubfield,
283                         liblibrarian     => $liblibrarian,
284                         libopac          => $libopac,
285                         repeatable       => $repeatable,
286                         mandatory        => $mandatory,
287                         important        => $important,
288                         kohafield        => $kohafield,
289                         tab              => $tab,
290                         seealso          => $seealso,
291                         authorised_value => $authorised_value,
292                         authtypecode     => $authtypecode,
293                         value_builder    => $value_builder,
294                         hidden           => $hidden,
295                         isurl            => $isurl,
296                         frameworkcode    => $frameworkcode,
297                         link             => $link,
298                         defaultvalue     => $defaultvalue,
299                         maxlength        => $maxlength,
300                         display_order    => $display_order->{$tagfield} || 0,
301                     }
302                 )->store;
303             }
304             $display_order->{$tagfield}++;
305         }
306     }
307     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
308     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
309     $cache->clear_from_cache("default_value_for_mod_marc-");
310     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
311
312     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
313     exit;
314
315     # END $OP eq ADD_VALIDATE
316 ################## DELETE_CONFIRM ##################################
317     # called by default form, used to confirm deletion of data in DB
318 }
319 elsif ( $op eq 'delete_confirm' ) {
320     my $mss = Koha::MarcSubfieldStructures->find(
321         {
322             tagfield      => $tagfield,
323             tagsubfield   => $tagsubfield,
324             frameworkcode => $frameworkcode
325         }
326     );
327     $template->param(
328         mss => $mss,
329         delete_link   => $script_name,
330     );
331
332     # END $OP eq DELETE_CONFIRM
333 ################## DELETE_CONFIRMED ##################################
334   # called by delete_confirm, used to effectively confirm deletion of data in DB
335 }
336 elsif ( $op eq 'delete_confirmed' ) {
337     Koha::MarcSubfieldStructures->find(
338         {
339             tagfield      => $tagfield,
340             tagsubfield   => $tagsubfield,
341             frameworkcode => $frameworkcode
342         }
343     )->delete;
344
345     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
346     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
347     $cache->clear_from_cache("default_value_for_mod_marc-");
348     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
349     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
350     exit;
351
352     # END $OP eq DELETE_CONFIRMED
353 ################## DEFAULT ##################################
354 }
355 else {    # DEFAULT
356     my $mss = Koha::MarcSubfieldStructures->search(
357         {
358             tagfield      => { -like => "$tagfield%" },
359             frameworkcode => $frameworkcode
360         },
361         { order_by => [ 'tagfield', 'display_order' ] }
362     )->unblessed;
363
364     $template->param( loop => $mss );
365     $template->param(
366         edit_tagfield      => $tagfield,
367         edit_frameworkcode => $frameworkcode
368     );
369
370 }    #---- END $OP eq DEFAULT
371
372 output_html_with_http_headers $input, $cookie, $template->output;