X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=admin%2Fkoha2marclinks.pl;h=e731cbebe2ae3e7bc3cf74503c70e2fbbaad060d;hb=9d6d641d1f8b77271800f43bc027b651f9aea52b;hp=8e0e105bfa569cb0a14ec12699c350cdd9b5aab0;hpb=7f0f83a6ec38078d0b8a5b2b02e5488edd7f70a4;p=srvgit diff --git a/admin/koha2marclinks.pl b/admin/koha2marclinks.pl index 8e0e105bfa..e731cbebe2 100755 --- a/admin/koha2marclinks.pl +++ b/admin/koha2marclinks.pl @@ -1,169 +1,114 @@ #!/usr/bin/perl # Copyright 2000-2002 Katipo Communications +# Copyright 2017 Rijksmuseum # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use strict; -use warnings; -use C4::Output; -use C4::Auth; -use CGI; -use C4::Context; -use C4::Biblio; +use Modern::Perl; +use CGI qw ( -utf8 ); +use Koha::Database; +use C4::Auth qw( get_template_and_user ); +use C4::Output qw( output_html_with_http_headers ); +use Koha::BiblioFrameworks; +use Koha::Caches; +use Koha::MarcSubfieldStructures; -my $input = new CGI; -my $tablename = $input->param('tablename'); -$tablename = "biblio" unless ($tablename); -my $kohafield = $input->param('kohafield'); -my $op = $input->param('op'); -my $script_name = 'koha2marclinks.pl'; +my $input = CGI->new; my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( { - template_name => "admin/koha2marclinks.tmpl", + template_name => "admin/koha2marclinks.tt", query => $input, type => "intranet", - authnotrequired => 0, - flagsrequired => { parameters => 1 }, - debug => 1, + flagsrequired => { parameters => 'manage_marc_frameworks' }, } ); -if ($op) { - $template->param( - script_name => $script_name, - $op => 1 - ); # we show only the TMPL_VAR names $op -} -else { - $template->param( - script_name => $script_name, - else => 1 - ); # we show only the TMPL_VAR names $op - $op = q{}; -} - -my $dbh = C4::Context->dbh; +my $schema = Koha::Database->new->schema; +my $cache = Koha::Caches->get_instance(); -################## ADD_FORM ################################## -# called by default. Used to create form to add or modify a record -if ( $op eq 'add_form' ) { - my $sth = $dbh->prepare( -"select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where kohafield=?" - ); - $sth->execute( $tablename . "." . $kohafield ); - my ( $defaulttagfield, $defaulttagsubfield, $defaultliblibrarian ) = - $sth->fetchrow; +# Update data before showing the form +my $no_upd; - for ( my $i = 0 ; $i <= 9 ; $i++ ) { - my $sth2 = $dbh->prepare( -"select tagfield,tagsubfield,liblibrarian as lib,tab from marc_subfield_structure where tagfield like ?" - ); - $sth2->execute("$i%"); - my @marcarray; - push @marcarray, " "; - while ( my ( $field, $tagsubfield, $liblibrarian ) = - $sth2->fetchrow_array ) - { - push @marcarray, "$field $tagsubfield - $liblibrarian"; - } - my $marclist = CGI::scrolling_list( - -name => 'marc', - -values => \@marcarray, - -default => - "$defaulttagfield $defaulttagsubfield - $defaultliblibrarian", - -size => 1, - -multiple => 0, - ); - $template->param( "marclist$i" => $marclist ); +if( $input->param('add_field') && $input->request_method eq 'POST' ) { + # add a mapping to all frameworks + my ($kohafield, $tag, $sub) = split /,/, $input->param('add_field'), 3; + my $rs = Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub }); + if( $rs->count ) { + $rs->update({ kohafield => $kohafield }); + } else { + $template->param( error_add => 1, error_info => "$tag, $sub" ); } - $template->param( - tablename => $tablename, - kohafield => $kohafield - ); - # END $OP eq ADD_FORM -################## ADD_VALIDATE ################################## - # called by add_form, used to insert/modify data in DB -} -elsif ( $op eq 'add_validate' ) { +} elsif( $input->param('remove_field') && $input->request_method eq 'POST' ) { + # remove a mapping from all frameworks + my ($tag, $sub) = split /,/, $input->param('remove_field'), 2; + Koha::MarcSubfieldStructures->search({ tagfield => $tag, tagsubfield => $sub })->update({ kohafield => undef }); - #----- empty koha field : - $dbh->do( -"update marc_subfield_structure set kohafield='' where kohafield='$tablename.$kohafield'" - ); +} else { + $no_upd = 1; +} - #---- reload if not empty - my @temp = split / /, $input->param('marc'); - $dbh->do( -"update marc_subfield_structure set kohafield='$tablename.$kohafield' where tagfield='$temp[0]' and tagsubfield='$temp[1]'" - ); - print -"Content-Type: text/html\n\n"; - exit; +# Clear the cache when needed +unless( $no_upd ) { + for( qw| default_value_for_mod_marc- MarcSubfieldStructure- | ) { + $cache->clear_from_cache($_); + } +} - # END $OP eq ADD_VALIDATE -################## DEFAULT ################################## +# Build/Show the form +my $dbix_map = { + # Koha to MARC mappings are found in only three tables + biblio => 'Biblio', + biblioitems => 'Biblioitem', + items => 'Item', +}; +my @cols; +foreach my $tbl ( sort keys %{$dbix_map} ) { + push @cols, + map { "$tbl.$_" } $schema->source( $dbix_map->{$tbl} )->columns; } -else { # DEFAULT - my $sth = - $dbh->prepare( -q|select tagfield,tagsubfield,liblibrarian,kohafield from marc_subfield_structure where kohafield is not NULL and kohafield != ''| - ); - $sth->execute; - my %fields; - while ( ( my $tagfield, my $tagsubfield, my $liblibrarian, my $kohafield ) = - $sth->fetchrow ) { - $fields{$kohafield}->{tagfield} = $tagfield; - $fields{$kohafield}->{tagsubfield} = $tagsubfield; - $fields{$kohafield}->{liblibrarian} = $liblibrarian; +my $kohafields = Koha::MarcSubfieldStructures->search({ + frameworkcode => q{}, + kohafield => { '>', '' }, +}); +my @loop_data; +foreach my $col ( @cols ) { + my $found; + my $readonly = $col =~ /\.(biblio|biblioitem|item)number$/; + foreach my $row ( $kohafields->search({ kohafield => $col }) ) { + $found = 1; + push @loop_data, { + kohafield => $col, + tagfield => $row->tagfield, + tagsubfield => $row->tagsubfield, + liblibrarian => $row->liblibrarian, + readonly => $readonly, + }; } + push @loop_data, { + kohafield => $col, + readonly => $readonly, + } if !$found; +} - #XXX: This might not work. Maybe should use a DBI call instead of SHOW COLUMNS - my $sth2 = $dbh->prepare("SHOW COLUMNS from $tablename"); - $sth2->execute; +$template->param( + loop => \@loop_data, +); - my @loop_data = (); - while ( ( my $field ) = $sth2->fetchrow_array ) { - my %row_data; # get a fresh hash for the row data - $row_data{tagfield} = $fields{ $tablename . "." . $field }->{tagfield}; - $row_data{tagsubfield} = - $fields{ $tablename . "." . $field }->{tagsubfield}; - $row_data{liblibrarian} = - $fields{ $tablename . "." . $field }->{liblibrarian}; - $row_data{kohafield} = $field; - $row_data{edit} = -"$script_name?op=add_form&tablename=$tablename&kohafield=$field"; - push( @loop_data, \%row_data ); - } - $template->param( - loop => \@loop_data, - tablename => CGI::scrolling_list( - -name => 'tablename', - -values => [ - 'biblio', - 'biblioitems', - 'items', - ], - -default => $tablename, - -size => 1, - -multiple => 0 - ) - ); -} #---- END $OP eq DEFAULT output_html_with_http_headers $input, $cookie, $template->output;