Bug 18269: Move field mappings related code to Koha::FieldMapping[s]
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 13 Mar 2017 21:17:13 +0000 (18:17 -0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 31 Mar 2017 10:20:00 +0000 (10:20 +0000)
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 <veron@veron.ch>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Biblio.pm
admin/fieldmapping.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/fieldmapping.tt
t/db_dependent/Charset.t

index 07f2b16..4b33d9a 100644 (file)
@@ -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);
index 75fd892..631e7d4 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/perl
 # Copyright 2009 SARL BibLibre
+# Copyright 2017 Koha Development Team
 #
 # This file is part of Koha.
 #
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-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;
index fed90a9..d182750 100644 (file)
@@ -24,9 +24,9 @@ $(document).ready(function() {
        <div id="yui-main">
                <div class="yui-b">
             <h2>Keyword to MARC mapping</h2>
-                       [% UNLESS ( fields ) %]
-            <div class="dialog message"><p>There are no mappings for the [% IF framework.frameworktext %]<em>[% framework.frameworktext %]</em>[% ELSE %]default[% END %] framework. </p></div>
-                       [% END %]
+            [% UNLESS ( fields.count ) %]
+                <div class="dialog message"><p>There are no mappings for the [% IF framework.frameworktext %]<em>[% framework.frameworktext %]</em>[% ELSE %]default[% END %] framework. </p></div>
+            [% END %]
                        <form method="get" action="/cgi-bin/koha/admin/fieldmapping.pl" id="selectframework">
                                <label for="framework">Framework:</label>
                 <select name="framework" id="framework" style="width:20em;">
@@ -58,7 +58,8 @@ $(document).ready(function() {
                                </fieldset>
                        </form>
 
-                               [% IF ( fields ) %]<table>
+                [% IF ( fields.count ) %]
+                    <table>
                     <caption>Mappings for the [% IF framework.frameworktext %]<em>[% framework.frameworktext %]</em>[% ELSE %]default[% END %] framework</caption>
                                                                        <tr>
                                                                                <th>Field</th>
@@ -71,7 +72,7 @@ $(document).ready(function() {
                                                                                <td>[% field.field %]</td>
                                                                                <td>[% field.fieldcode %]</td>
                                                                                <td>[% field.subfieldcode %]</td>
-                                        <td><a class="btn btn-default btn-xs" href="?op=delete&amp;id=[% field.id %]&amp;framework=[% field.framework %]"><i class="fa fa-trash"></i> Delete</a></td>
+                                        <td><a class="btn btn-default btn-xs" href="?op=delete&amp;id=[% field.id %]&amp;framework=[% field.frameworkcode %]"><i class="fa fa-trash"></i> Delete</a></td>
                                                                        </tr>
                                                                        [% END %]
                                                                </table>[% END %]
index 9a48aef..ea649ae 100644 (file)
@@ -2,7 +2,7 @@ use Modern::Perl;
 use Test::More tests => 4;
 use MARC::Record;
 
-use C4::Biblio qw( AddBiblio SetFieldMapping GetMarcFromKohaField );
+use C4::Biblio qw( AddBiblio GetMarcFromKohaField );
 use C4::Context;
 use C4::Charset qw( SanitizeRecord );