2d10c41e5759a521c5f6782f8045ac54942182cd
[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     unless ( opendir( DIR, "$cgidir/cataloguing/value_builder" ) ) {
115         $cgidir = C4::Context->config('intranetdir');
116         opendir( DIR, "$cgidir/cataloguing/value_builder" )
117           || die "can't opendir $cgidir/value_builder: $!";
118     }
119     while ( my $line = readdir(DIR) ) {
120         if ( $line =~ /\.pl$/ &&
121              $line !~ /EXAMPLE\.pl$/ ) { # documentation purposes
122             push( @value_builder, $line );
123         }
124     }
125     @value_builder= sort {$a cmp $b} @value_builder;
126     closedir DIR;
127
128     # build values list
129     my $mss = Koha::MarcSubfieldStructures->search(
130         { tagfield => $tagfield, frameworkcode => $frameworkcode },
131         { order_by => 'display_order' }
132     )->unblessed;
133     my @loop_data = ();
134     my $i         = 0;
135     for my $m ( @$mss ) {
136         my %row_data = %$m;    # get a fresh hash for the row data
137         $row_data{subfieldcode}      = $m->{tagsubfield};
138         $row_data{urisubfieldcode}   = $row_data{subfieldcode} eq '%' ? 'pct' : $row_data{subfieldcode};
139         $row_data{kohafields}        = \@kohafields;
140         $row_data{authorised_values} = \@authorised_values;
141         $row_data{value_builders}    = \@value_builder;
142         $row_data{authtypes}         = \@authtypes;
143         $row_data{row}               = $i;
144
145         if ( defined $m->{kohafield}
146             and $m->{kohafield} eq 'biblio.biblionumber' )
147         {
148             my $hidden_opac = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
149                     {
150                         frameworkcode => $frameworkcode,
151                         interface     => "opac",
152                     }
153                 )->{biblionumber};
154
155             my $hidden_intranet = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
156                     {
157                         frameworkcode => $frameworkcode,
158                         interface     => "intranet",
159                     }
160                 )->{biblionumber};
161
162             if ( $hidden_opac or $hidden_intranet ) {
163                 # We should allow editing for fixing it
164                 $row_data{hidden_protected} = 0;
165             }
166             else {
167                 $row_data{hidden_protected} = 1;
168             }
169         }
170
171         push( @loop_data, \%row_data );
172         $i++;
173     }
174
175     # Add a new row for the "New" tab
176     my %row_data;    # get a fresh hash for the row data
177     $row_data{'new_subfield'}    = 1;
178     $row_data{'subfieldcode'}    = '';
179     $row_data{'maxlength'}       = 9999;
180     $row_data{tab}               = -1;                    #ignore
181     $row_data{tagsubfield}       = "";
182     $row_data{liblibrarian}      = "";
183     $row_data{libopac}           = "";
184     $row_data{seealso}           = "";
185     $row_data{hidden}            = "";
186     $row_data{repeatable}        = 0;
187     $row_data{mandatory}         = 0;
188     $row_data{important}         = 0;
189     $row_data{isurl}             = 0;
190     $row_data{kohafields}        = \@kohafields;
191     $row_data{authorised_values} = \@authorised_values;
192     $row_data{value_builders}    = \@value_builder;
193     $row_data{authtypes}         = \@authtypes;
194     $row_data{link}              = "";
195     $row_data{row}               = $i;
196     push( @loop_data, \%row_data );
197
198     $template->param( 'use_heading_flags_p'      => 1 );
199     $template->param( 'heading_edit_subfields_p' => 1 );
200     $template->param(
201         action   => "Edit subfields",
202         tagfield => $tagfield,
203         tagsubfield => $tagsubfield,
204         loop           => \@loop_data,
205         more_tag       => $tagfield
206     );
207
208     # END $OP eq ADD_FORM
209 ################## ADD_VALIDATE ##################################
210     # called by add_form, used to insert/modify data in DB
211 }
212 elsif ( $op eq 'add_validate' ) {
213     my $dbh = C4::Context->dbh;
214     $template->param( tagfield => "$input->param('tagfield')" );
215     my $tagfield    = $input->param('tagfield');
216     my @tagsubfield = $input->multi_param('tagsubfield');
217     my @tab_ids     = $input->multi_param('tab_id');
218
219     my $display_order;
220     for my $tagsubfield ( @tagsubfield ) {
221         $tagsubfield = "@" unless $tagsubfield ne '';
222         my $id = shift @tab_ids;
223         my $liblibrarian     = $input->param("liblibrarian_$id");
224         my $libopac          = $input->param("libopac_$id");
225         my $repeatable       = $input->param("repeatable_$id") ? 1 : 0;
226         my $mandatory        = $input->param("mandatory_$id") ? 1 : 0;
227         my $important        = $input->param("important_$id") ? 1 : 0;
228         my $kohafield        = $input->param("kohafield_$id");
229         my $tab              = $input->param("tab_$id");
230         my $seealso          = $input->param("seealso_$id");
231         my $authorised_value = $input->param("authorised_value_$id");
232         my $authtypecode     = $input->param("authtypecode_$id");
233         my $value_builder    = $input->param("value_builder_$id");
234         my $hidden = $input->param("hidden_$id");
235         my $isurl  = $input->param("isurl_$id") ? 1 : 0;
236         my $link   = $input->param("link_$id");
237         my $defaultvalue = $input->param("defaultvalue_$id");
238         my $maxlength = $input->param("maxlength_$id") || 9999;
239
240         if (defined($liblibrarian) && $liblibrarian ne "") {
241             my $mss = Koha::MarcSubfieldStructures->find({tagfield => $tagfield, tagsubfield => $tagsubfield, frameworkcode => $frameworkcode });
242             if ($mss) {
243                 $mss->update(
244                     {
245                         liblibrarian     => $liblibrarian,
246                         libopac          => $libopac,
247                         repeatable       => $repeatable,
248                         mandatory        => $mandatory,
249                         important        => $important,
250                         kohafield        => $kohafield,
251                         tab              => $tab,
252                         seealso          => $seealso,
253                         authorised_value => $authorised_value,
254                         authtypecode     => $authtypecode,
255                         value_builder    => $value_builder,
256                         hidden           => $hidden,
257                         isurl            => $isurl,
258                         link             => $link,
259                         defaultvalue     => $defaultvalue,
260                         maxlength        => $maxlength,
261                         display_order    => $display_order->{$tagfield} || 0
262                     }
263                 );
264             } else {
265                 if( $frameworkcode ne q{} ) {
266                     # BZ 19096: Overwrite kohafield from Default when adding a new record
267                      my $rec = Koha::MarcSubfieldStructures->find( q{}, $tagfield, $tagsubfield );
268                     $kohafield = $rec->kohafield if $rec;
269                 }
270                 Koha::MarcSubfieldStructure->new(
271                     {
272                         tagfield         => $tagfield,
273                         tagsubfield      => $tagsubfield,
274                         liblibrarian     => $liblibrarian,
275                         libopac          => $libopac,
276                         repeatable       => $repeatable,
277                         mandatory        => $mandatory,
278                         important        => $important,
279                         kohafield        => $kohafield,
280                         tab              => $tab,
281                         seealso          => $seealso,
282                         authorised_value => $authorised_value,
283                         authtypecode     => $authtypecode,
284                         value_builder    => $value_builder,
285                         hidden           => $hidden,
286                         isurl            => $isurl,
287                         frameworkcode    => $frameworkcode,
288                         link             => $link,
289                         defaultvalue     => $defaultvalue,
290                         maxlength        => $maxlength,
291                         display_order    => $display_order->{$tagfield} || 0,
292                     }
293                 )->store;
294             }
295             $display_order->{$tagfield}++;
296         }
297     }
298     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
299     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
300     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
301     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
302
303     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
304     exit;
305
306     # END $OP eq ADD_VALIDATE
307 ################## DELETE_CONFIRM ##################################
308     # called by default form, used to confirm deletion of data in DB
309 }
310 elsif ( $op eq 'delete_confirm' ) {
311     my $mss = Koha::MarcSubfieldStructures->find(
312         {
313             tagfield      => $tagfield,
314             tagsubfield   => $tagsubfield,
315             frameworkcode => $frameworkcode
316         }
317     );
318     $template->param(
319         mss => $mss,
320         delete_link   => $script_name,
321     );
322
323     # END $OP eq DELETE_CONFIRM
324 ################## DELETE_CONFIRMED ##################################
325   # called by delete_confirm, used to effectively confirm deletion of data in DB
326 }
327 elsif ( $op eq 'delete_confirmed' ) {
328     Koha::MarcSubfieldStructures->find(
329         {
330             tagfield      => $tagfield,
331             tagsubfield   => $tagsubfield,
332             frameworkcode => $frameworkcode
333         }
334     )->delete;
335
336     $cache->clear_from_cache("MarcStructure-0-$frameworkcode");
337     $cache->clear_from_cache("MarcStructure-1-$frameworkcode");
338     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
339     $cache->clear_from_cache("MarcCodedFields-$frameworkcode");
340     print $input->redirect("/cgi-bin/koha/admin/marc_subfields_structure.pl?tagfield=$tagfield&amp;frameworkcode=$frameworkcode");
341     exit;
342
343     # END $OP eq DELETE_CONFIRMED
344 ################## DEFAULT ##################################
345 }
346 else {    # DEFAULT
347     my $mss = Koha::MarcSubfieldStructures->search(
348         {
349             tagfield      => { -like => "$tagfield%" },
350             frameworkcode => $frameworkcode
351         },
352         { order_by => [ 'tagfield', 'display_order' ] }
353     )->unblessed;
354
355     $template->param( loop => $mss );
356     $template->param(
357         edit_tagfield      => $tagfield,
358         edit_frameworkcode => $frameworkcode
359     );
360
361 }    #---- END $OP eq DEFAULT
362
363 output_html_with_http_headers $input, $cookie, $template->output;