Bug 17600: Standardize our EXPORT_OK
[koha.git] / admin / koha2marclinks.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2017 Rijksmuseum
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23
24 use Koha::Database;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use Koha::BiblioFrameworks;
28 use Koha::Caches;
29 use Koha::MarcSubfieldStructures;
30
31 my $input       = CGI->new;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
34     {
35         template_name   => "admin/koha2marclinks.tt",
36         query           => $input,
37         type            => "intranet",
38         flagsrequired   => { parameters => 'manage_marc_frameworks' },
39     }
40 );
41
42 my $schema = Koha::Database->new->schema;
43 my $cache = Koha::Caches->get_instance();
44
45 # Update data before showing the form
46 my $no_upd;
47
48 if( $input->param('add_field') && $input->request_method eq 'POST' ) {
49     # add a mapping to all frameworks
50     my ($kohafield, $tag, $sub) = split /,/, $input->param('add_field'), 3;
51     my $rs = Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub });
52     if( $rs->count ) {
53         $rs->update({ kohafield => $kohafield });
54     } else {
55         $template->param( error_add => 1, error_info => "$tag, $sub" );
56     }
57
58 } elsif( $input->param('remove_field') && $input->request_method eq 'POST' ) {
59     # remove a mapping from all frameworks
60     my ($tag, $sub) = split /,/, $input->param('remove_field'), 2;
61     Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => undef });
62
63 } else {
64     $no_upd = 1;
65 }
66
67 # Clear the cache when needed
68 unless( $no_upd ) {
69     for( qw| default_value_for_mod_marc- MarcSubfieldStructure- | ) {
70         $cache->clear_from_cache($_);
71     }
72 }
73
74 # Build/Show the form
75 my $dbix_map = {
76     # Koha to MARC mappings are found in only three tables
77     biblio => 'Biblio',
78     biblioitems => 'Biblioitem',
79     items => 'Item',
80 };
81 my @cols;
82 foreach my $tbl ( sort keys %{$dbix_map} ) {
83     push @cols,
84         map { "$tbl.$_" } $schema->source( $dbix_map->{$tbl} )->columns;
85 }
86 my $kohafields = Koha::MarcSubfieldStructures->search({
87     frameworkcode => q{},
88     kohafield => { '>', '' },
89 });
90 my @loop_data;
91 foreach my $col ( @cols ) {
92     my $found;
93     my $readonly = $col =~ /\.(biblio|biblioitem|item)number$/;
94     foreach my $row ( $kohafields->search({ kohafield => $col }) ) {
95         $found = 1;
96         push @loop_data, {
97             kohafield    => $col,
98             tagfield     => $row->tagfield,
99             tagsubfield  => $row->tagsubfield,
100             liblibrarian => $row->liblibrarian,
101             readonly     => $readonly,
102         };
103     }
104     push @loop_data, {
105             kohafield    => $col,
106             readonly     => $readonly,
107     } if !$found;
108 }
109
110 $template->param(
111     loop       => \@loop_data,
112 );
113
114 output_html_with_http_headers $input, $cookie, $template->output;