From afe3fa1d071aae2078cd3c52ad1b7e242ddcf3ef Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 13 Mar 2017 18:17:13 -0300 Subject: [PATCH] Bug 18269: Move field mappings related code to Koha::FieldMapping[s] MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The 3 subroutines GetFieldMapping, SetFieldMapping and DeleteFieldMapping from the C4::Biblio module were only used from the field mappings admin page. They can easily replaced with new packages Koha::FieldMappings based on Koha::Object[s] Test plan: Add and delete field mappings (admin/fieldmapping.pl, Home › Administration › Keyword to MARC mapping). Add an existing mapping > Nothing should be added Note that this page has not been rewritten and you will not get any feedbacks, but it's not the goal of this page to improve it. Followed test plan, works as expected. Signed-off-by: Marc Véron Signed-off-by: Nick Clemens Signed-off-by: Kyle M Hall --- C4/Biblio.pm | 63 ------------------- admin/fieldmapping.pl | 46 +++++++------- .../prog/en/modules/admin/fieldmapping.tt | 11 ++-- t/db_dependent/Charset.t | 2 +- 4 files changed, 31 insertions(+), 91 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 07f2b16a23..4b33d9a131 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -72,9 +72,6 @@ BEGIN { GetBiblionumberFromItemnumber &GetRecordValue - &GetFieldMapping - &SetFieldMapping - &DeleteFieldMapping &GetISBDView @@ -686,66 +683,6 @@ sub GetRecordValue { return \@result; } -=head2 SetFieldMapping - - SetFieldMapping($framework, $field, $fieldcode, $subfieldcode); - -Set a Field to MARC mapping value, if it already exists we don't add a new one. - -=cut - -sub SetFieldMapping { - my ( $framework, $field, $fieldcode, $subfieldcode ) = @_; - my $dbh = C4::Context->dbh; - - my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?'); - $sth->execute( $fieldcode, $subfieldcode, $framework, $field ); - if ( not $sth->fetchrow_hashref ) { - my @args; - $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)'); - - $sth->execute( $fieldcode, $subfieldcode, $framework, $field ); - } -} - -=head2 DeleteFieldMapping - - DeleteFieldMapping($id); - -Delete a field mapping from an $id. - -=cut - -sub DeleteFieldMapping { - my ($id) = @_; - my $dbh = C4::Context->dbh; - - my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?'); - $sth->execute($id); -} - -=head2 GetFieldMapping - - GetFieldMapping($frameworkcode); - -Get all field mappings for a specified frameworkcode - -=cut - -sub GetFieldMapping { - my ($framework) = @_; - my $dbh = C4::Context->dbh; - - my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?'); - $sth->execute($framework); - - my @return; - while ( my $row = $sth->fetchrow_hashref ) { - push @return, $row; - } - return \@return; -} - =head2 GetBiblioData $data = &GetBiblioData($biblionumber); diff --git a/admin/fieldmapping.pl b/admin/fieldmapping.pl index 75fd892960..631e7d4380 100755 --- a/admin/fieldmapping.pl +++ b/admin/fieldmapping.pl @@ -1,5 +1,6 @@ #!/usr/bin/perl # Copyright 2009 SARL BibLibre +# Copyright 2017 Koha Development Team # # This file is part of Koha. # @@ -16,15 +17,14 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see . -use strict; -use warnings; +use Modern::Perl; use CGI qw ( -utf8 ); use C4::Auth; use C4::Biblio; -use C4::Koha; use C4::Output; use Koha::BiblioFrameworks; +use Koha::FieldMappings; my $query = new CGI; @@ -35,34 +35,36 @@ my $subfieldcode = $query->param('marcsubfield'); my $op = $query->param('op') || q{}; my $id = $query->param('id'); -my ($template, $loggedinuser, $cookie) - = get_template_and_user({template_name => "admin/fieldmapping.tt", - query => $query, - type => "intranet", - authnotrequired => 0, - flagsrequired => {parameters => 'parameters_remaining_permissions'}, - debug => 1, - }); - -if($op eq "delete" and $id){ - DeleteFieldMapping($id); - print $query->redirect("/cgi-bin/koha/admin/fieldmapping.pl?framework=".$frameworkcode); - exit; -} +my ( $template, $loggedinuser, $cookie ) = get_template_and_user( + { + template_name => "admin/fieldmapping.tt", + query => $query, + type => "intranet", + authnotrequired => 0, + flagsrequired => { parameters => 'parameters_remaining_permissions' }, + debug => 1, + } +); -# insert operation -if($field and $fieldcode){ - SetFieldMapping($frameworkcode, $field, $fieldcode, $subfieldcode); +# FIXME Add exceptions +if ( $op eq "delete" and $id ) { + Koha::FieldMappings->find($id)->delete; +} elsif ( $field and $fieldcode ) { + my $params = { frameworkcode => $frameworkcode, field => $field, fieldcode => $fieldcode, subfieldcode => $subfieldcode }; + my $exists = Koha::FieldMappings->search( $params )->count;; + unless ( $exists ) { + Koha::FieldMapping->new( $params )->store; + } } -my $fieldloop = GetFieldMapping($frameworkcode); +my $fields = Koha::FieldMappings->search({ frameworkcode => $frameworkcode }); my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] }); my $framework = $frameworks->search( { frameworkcode => $frameworkcode } )->next; $template->param( frameworks => $frameworks, framework => $framework, - fields => $fieldloop, + fields => $fields, ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/fieldmapping.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/fieldmapping.tt index fed90a93c5..d1827503e7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/fieldmapping.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/fieldmapping.tt @@ -24,9 +24,9 @@ $(document).ready(function() {

Keyword to MARC mapping

- [% UNLESS ( fields ) %] -

There are no mappings for the [% IF framework.frameworktext %][% framework.frameworktext %][% ELSE %]default[% END %] framework.

- [% END %] + [% UNLESS ( fields.count ) %] +

There are no mappings for the [% IF framework.frameworktext %][% framework.frameworktext %][% ELSE %]default[% END %] framework.

+ [% END %]