Bug 34883: Stop patron expiry date being set to NULL during import
[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 use Koha::BiblioFrameworks;
31
32 use List::MoreUtils qw( uniq );
33
34 my $input         = CGI->new;
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";
42
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44     {
45         template_name   => "admin/marc_subfields_structure.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { parameters => 'manage_marc_frameworks' },
49     }
50 );
51 my $cache = Koha::Caches->get_instance();
52
53 my $op       = $input->param('op') || "";
54 $tagfield =~ s/\,//g;
55
56 my $framework = Koha::BiblioFrameworks->search({ frameworkcode => $frameworkcode })->next;
57
58 if ($op) {
59     $template->param(
60         script_name   => $script_name,
61         tagfield      => $tagfield,
62         frameworkcode => $frameworkcode,
63         framework     => $framework,
64         $op           => 1
65     );    # we show only the TMPL_VAR names $op
66 }
67 else {
68     $template->param(
69         script_name   => $script_name,
70         tagfield      => $tagfield,
71         frameworkcode => $frameworkcode,
72         framework     => $framework,
73         else          => 1
74     );    # we show only the TMPL_VAR names $op
75 }
76
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;
81
82     # builds kohafield tables
83     my @kohafields;
84     push @kohafields, "";
85     my $sth2 = $dbh->prepare("SHOW COLUMNS from biblio");
86     $sth2->execute;
87     while ( ( my $field ) = $sth2->fetchrow_array ) {
88         push @kohafields, "biblio." . $field;
89     }
90     $sth2 = $dbh->prepare("SHOW COLUMNS from biblioitems");
91     $sth2->execute;
92     while ( ( my $field ) = $sth2->fetchrow_array ) {
93         if ( $field eq 'notes' ) { $field = 'bnotes'; }
94         push @kohafields, "biblioitems." . $field;
95     }
96     $sth2 = $dbh->prepare("SHOW COLUMNS from items");
97     $sth2->execute;
98     while ( ( my $field ) = $sth2->fetchrow_array ) {
99         push @kohafields, "items." . $field;
100     }
101
102     # build authorised value list
103     $sth2->finish;
104     $sth2 = $dbh->prepare("select distinct category from authorised_values");
105     $sth2->execute;
106     my @authorised_values= Koha::AuthorisedValueCategories->search->get_column('category_name');
107
108     # build thesaurus categories list
109     my @authtypes = uniq( "", map { $_->authtypecode } Koha::Authority::Types->search->as_list );
110
111     # build value_builder list
112     my @value_builder = ('');
113
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";
119     my $dir_h;
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: $!";
124     }
125     while ( my $line = readdir($dir_h) ) {
126         if ( $line =~ /\.pl$/ &&
127              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
128             push( @value_builder, $line );
129         }
130     }
131     @value_builder= sort {$a cmp $b} @value_builder;
132     closedir $dir_h;
133
134     # build values list
135     my $mss = Koha::MarcSubfieldStructures->search(
136         { tagfield => $tagfield, frameworkcode => $frameworkcode },
137         { order_by => 'display_order' }
138     )->unblessed;
139     my @loop_data = ();
140     my $i         = 0;
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;
149         $row_data{row}               = $i;
150
151         if ( defined $m->{kohafield}
152             and $m->{kohafield} eq 'biblio.biblionumber' )
153         {
154             my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
155                     {
156                         frameworkcode => $frameworkcode,
157                         interface     => "opac",
158                     }
159                 )->{biblionumber};
160
161             my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
162                     {
163                         frameworkcode => $frameworkcode,
164                         interface     => "intranet",
165                     }
166                 )->{biblionumber};
167
168             if ( $hidden_opac or $hidden_intranet ) {
169                 # We should allow editing for fixing it
170                 $row_data{hidden_protected} = 0;
171             }
172             else {
173                 $row_data{hidden_protected} = 1;
174             }
175         }
176
177         push( @loop_data, \%row_data );
178         $i++;
179     }
180
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}              = "";
201     $row_data{row}               = $i;
202     push( @loop_data, \%row_data );
203
204     $template->param( 'use_heading_flags_p'      => 1 );
205     $template->param( 'heading_edit_subfields_p' => 1 );
206     $template->param(
207         tagfield => $tagfield,
208         tagsubfield => $tagsubfield,
209         loop           => \@loop_data,
210         more_tag       => $tagfield
211     );
212
213     # END $OP eq ADD_FORM
214 ################## ADD_VALIDATE ##################################
215     # called by add_form, used to insert/modify data in DB
216 }
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');
223
224     my $display_order;
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;
244
245         if (defined($liblibrarian) && $liblibrarian ne "") {
246             my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
247             if ($mss) {
248                 $mss->update(
249                     {
250                         liblibrarian     => $liblibrarian,
251                         libopac          => $libopac,
252                         repeatable       => $repeatable,
253                         mandatory        => $mandatory,
254                         important        => $important,
255                         kohafield        => $kohafield,
256                         tab              => $tab,
257                         seealso          => $seealso,
258                         authorised_value => $authorised_value,
259                         authtypecode     => $authtypecode,
260                         value_builder    => $value_builder,
261                         hidden           => $hidden,
262                         isurl            => $isurl,
263                         link             => $link,
264                         defaultvalue     => $defaultvalue,
265                         maxlength        => $maxlength,
266                         display_order    => $display_order->{$tagfield} || 0
267                     }
268                 );
269             } else {
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;
274                 }
275                 Koha::MarcSubfieldStructure->new(
276                     {
277                         tagfield         => $tagfield,
278                         tagsubfield      => $tagsubfield,
279                         liblibrarian     => $liblibrarian,
280                         libopac          => $libopac,
281                         repeatable       => $repeatable,
282                         mandatory        => $mandatory,
283                         important        => $important,
284                         kohafield        => $kohafield,
285                         tab              => $tab,
286                         seealso          => $seealso,
287                         authorised_value => $authorised_value,
288                         authtypecode     => $authtypecode,
289                         value_builder    => $value_builder,
290                         hidden           => $hidden,
291                         isurl            => $isurl,
292                         frameworkcode    => $frameworkcode,
293                         link             => $link,
294                         defaultvalue     => $defaultvalue,
295                         maxlength        => $maxlength,
296                         display_order    => $display_order->{$tagfield} || 0,
297                     }
298                 )->store;
299             }
300             $display_order->{$tagfield}++;
301         }
302     }
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");
307
308     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
309     exit;
310
311     # END $OP eq ADD_VALIDATE
312 ################## DELETE_CONFIRM ##################################
313     # called by default form, used to confirm deletion of data in DB
314 }
315 elsif ( $op eq 'delete_confirm' ) {
316     my $mss = Koha::MarcSubfieldStructures->find(
317         {
318             tagfield      => $tagfield,
319             tagsubfield   => $tagsubfield,
320             frameworkcode => $frameworkcode
321         }
322     );
323     $template->param(
324         mss => $mss,
325         delete_link   => $script_name,
326     );
327
328     # END $OP eq DELETE_CONFIRM
329 ################## DELETE_CONFIRMED ##################################
330   # called by delete_confirm, used to effectively confirm deletion of data in DB
331 }
332 elsif ( $op eq 'delete_confirmed' ) {
333     Koha::MarcSubfieldStructures->find(
334         {
335             tagfield      => $tagfield,
336             tagsubfield   => $tagsubfield,
337             frameworkcode => $frameworkcode
338         }
339     )->delete;
340
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&amp;frameworkcode=$frameworkcode");
346     exit;
347
348     # END $OP eq DELETE_CONFIRMED
349 ################## DEFAULT ##################################
350 }
351 else {    # DEFAULT
352     my $mss = Koha::MarcSubfieldStructures->search(
353         {
354             tagfield      => { -like => "$tagfield%" },
355             frameworkcode => $frameworkcode
356         },
357         { order_by => [ 'tagfield', 'display_order' ] }
358     )->unblessed;
359
360     $template->param( loop => $mss );
361     $template->param(
362         edit_tagfield      => $tagfield,
363         edit_frameworkcode => $frameworkcode
364     );
365
366 }    #---- END $OP eq DEFAULT
367
368 output_html_with_http_headers $input, $cookie, $template->output;