Bug 26922: Regression tests
[koha.git] / admin / koha2marclinks.pl
blob5c679b885ca4fbac97cad896979be8827bc11f4b
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2017 Rijksmuseum
6 # This file is part of Koha.
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.
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.
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>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
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;
32 my $input = CGI->new;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
36 template_name => "admin/koha2marclinks.tt",
37 query => $input,
38 type => "intranet",
39 flagsrequired => { parameters => 'manage_marc_frameworks' },
40 debug => 1,
44 my $schema = Koha::Database->new->schema;
45 my $cache = Koha::Caches->get_instance();
47 # Update data before showing the form
48 my $no_upd;
50 if( $input->param('add_field') && $input->request_method eq 'POST' ) {
51 # add a mapping to all frameworks
52 my ($kohafield, $tag, $sub) = split /,/, $input->param('add_field'), 3;
53 my $rs = Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub });
54 if( $rs->count ) {
55 $rs->update({ kohafield => $kohafield });
56 } else {
57 $template->param( error_add => 1, error_info => "$tag, $sub" );
60 } elsif( $input->param('remove_field') && $input->request_method eq 'POST' ) {
61 # remove a mapping from all frameworks
62 my ($tag, $sub) = split /,/, $input->param('remove_field'), 2;
63 Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => undef });
65 } else {
66 $no_upd = 1;
69 # Clear the cache when needed
70 unless( $no_upd ) {
71 for( qw| default_value_for_mod_marc- MarcSubfieldStructure- | ) {
72 $cache->clear_from_cache($_);
76 # Build/Show the form
77 my $dbix_map = {
78 # Koha to MARC mappings are found in only three tables
79 biblio => 'Biblio',
80 biblioitems => 'Biblioitem',
81 items => 'Item',
83 my @cols;
84 foreach my $tbl ( sort keys %{$dbix_map} ) {
85 push @cols,
86 map { "$tbl.$_" } $schema->source( $dbix_map->{$tbl} )->columns;
88 my $kohafields = Koha::MarcSubfieldStructures->search({
89 frameworkcode => q{},
90 kohafield => { '>', '' },
91 });
92 my @loop_data;
93 foreach my $col ( @cols ) {
94 my $found;
95 my $readonly = $col =~ /\.(biblio|biblioitem|item)number$/;
96 foreach my $row ( $kohafields->search({ kohafield => $col }) ) {
97 $found = 1;
98 push @loop_data, {
99 kohafield => $col,
100 tagfield => $row->tagfield,
101 tagsubfield => $row->tagsubfield,
102 liblibrarian => $row->liblibrarian,
103 readonly => $readonly,
106 push @loop_data, {
107 kohafield => $col,
108 readonly => $readonly,
109 } if !$found;
112 $template->param(
113 loop => \@loop_data,
116 output_html_with_http_headers $input, $cookie, $template->output;