Bug 27948: (QA follow-up) Remove unused $disclaimer code
[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;
26 use C4::Biblio;
27 use C4::Output;
28 use Koha::BiblioFrameworks;
29 use Koha::Caches;
30 use Koha::MarcSubfieldStructures;
31
32 my $input       = CGI->new;
33
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
35     {
36         template_name   => "admin/koha2marclinks.tt",
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { parameters => 'manage_marc_frameworks' },
40     }
41 );
42
43 my $schema = Koha::Database->new->schema;
44 my $cache = Koha::Caches->get_instance();
45
46 # Update data before showing the form
47 my $no_upd;
48
49 if( $input->param('add_field') && $input->request_method eq 'POST' ) {
50     # add a mapping to all frameworks
51     my ($kohafield, $tag, $sub) = split /,/, $input->param('add_field'), 3;
52     my $rs = Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub });
53     if( $rs->count ) {
54         $rs->update({ kohafield => $kohafield });
55     } else {
56         $template->param( error_add => 1, error_info => "$tag, $sub" );
57     }
58
59 } elsif( $input->param('remove_field') && $input->request_method eq 'POST' ) {
60     # remove a mapping from all frameworks
61     my ($tag, $sub) = split /,/, $input->param('remove_field'), 2;
62     Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => undef });
63
64 } else {
65     $no_upd = 1;
66 }
67
68 # Clear the cache when needed
69 unless( $no_upd ) {
70     for( qw| default_value_for_mod_marc- MarcSubfieldStructure- | ) {
71         $cache->clear_from_cache($_);
72     }
73 }
74
75 # Build/Show the form
76 my $dbix_map = {
77     # Koha to MARC mappings are found in only three tables
78     biblio => 'Biblio',
79     biblioitems => 'Biblioitem',
80     items => 'Item',
81 };
82 my @cols;
83 foreach my $tbl ( sort keys %{$dbix_map} ) {
84     push @cols,
85         map { "$tbl.$_" } $schema->source( $dbix_map->{$tbl} )->columns;
86 }
87 my $kohafields = Koha::MarcSubfieldStructures->search({
88     frameworkcode => q{},
89     kohafield => { '>', '' },
90 });
91 my @loop_data;
92 foreach my $col ( @cols ) {
93     my $found;
94     my $readonly = $col =~ /\.(biblio|biblioitem|item)number$/;
95     foreach my $row ( $kohafields->search({ kohafield => $col }) ) {
96         $found = 1;
97         push @loop_data, {
98             kohafield    => $col,
99             tagfield     => $row->tagfield,
100             tagsubfield  => $row->tagsubfield,
101             liblibrarian => $row->liblibrarian,
102             readonly     => $readonly,
103         };
104     }
105     push @loop_data, {
106             kohafield    => $col,
107             readonly     => $readonly,
108     } if !$found;
109 }
110
111 $template->param(
112     loop       => \@loop_data,
113 );
114
115 output_html_with_http_headers $input, $cookie, $template->output;