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