From fe2791ca1da81050bcaf45f935e2e28963258198 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 8 Dec 2016 17:51:44 +0100 Subject: [PATCH] Bug 17762: Add the lang parameter to C4::Letters::getletter Sponsored-by: Orex Digital Signed-off-by: Hugo Agud --- C4/Letters.pm | 16 +++++++++++----- t/db_dependent/Letters.t | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index d191604..1d8ddb7 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -198,8 +198,9 @@ sub GetLettersAvailableForALibrary { } sub getletter { - my ( $module, $code, $branchcode, $message_transport_type ) = @_; + my ( $module, $code, $branchcode, $message_transport_type, $lang ) = @_; $message_transport_type //= '%'; + $lang //= 'default'; if ( C4::Context->preference('IndependentBranches') and $branchcode @@ -215,9 +216,10 @@ sub getletter { FROM letter WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '') AND message_transport_type LIKE ? + AND lang = ? ORDER BY branchcode DESC LIMIT 1 }); - $sth->execute( $module, $code, $branchcode, $message_transport_type ); + $sth->execute( $module, $code, $branchcode, $message_transport_type, $lang ); my $line = $sth->fetchrow_hashref or return; $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html}; @@ -687,9 +689,13 @@ sub GetPreparedLetter { my $mtt = $params{message_transport_type} || 'email'; my $lang = $params{lang} || 'default'; - my $letter = getletter( $module, $letter_code, $branchcode, $mtt ) - or warn( "No $module $letter_code letter transported by " . $mtt ), - return; + my $letter = getletter( $module, $letter_code, $branchcode, $mtt, $lang ); + + unless ( $letter ) { + $letter = getletter( $module, $letter_code, $branchcode, $mtt, 'default' ) + or warn( "No $module $letter_code letter transported by " . $mtt ), + return; + } my $tables = $params{tables}; my $substitute = $params{substitute}; diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 8219e5b..6a0afc7 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 79; +use Test::More tests => 80; use Test::MockModule; use Test::Warn; @@ -489,3 +489,48 @@ is($err2, 1, "Successfully sent serial notification"); is($mail{'To'}, 'john.smith@test.de', "mailto correct in sent serial notification"); is($mail{'Message'}, 'Silence in the library,'.$subscriptionid.',No. 0', 'Serial notification text constructed successfully'); } + +subtest 'TranslateNotices' => sub { + plan tests => 3; + $dbh->do( + q| + INSERT INTO letter (module, code, branchcode, name, title, content, message_transport_type, lang) VALUES + ('test', 'code', '', 'test', 'a test', 'just a test', 'email', 'default'), + ('test', 'code', '', 'test', 'una prueba', 'solo una prueba', 'email', 'es-ES'); + | ); + my $substitute = {}; + my $letter = C4::Letters::GetPreparedLetter( + module => 'test', + letter_code => 'code', + message_transport_type => 'email', + substitute => $substitute, + ); + is( + $letter->{title}, + 'a test', + 'GetPreparedLetter should return the default one if the lang parameter is not provided' + ); + + $letter = C4::Letters::GetPreparedLetter( + module => 'test', + letter_code => 'code', + message_transport_type => 'email', + substitute => $substitute, + lang => 'es-ES', + ); + is( $letter->{title}, 'una prueba', + 'GetPreparedLetter should return the required notice if it exists' ); + + $letter = C4::Letters::GetPreparedLetter( + module => 'test', + letter_code => 'code', + message_transport_type => 'email', + substitute => $substitute, + lang => 'fr-FR', + ); + is( + $letter->{title}, + 'a test', + 'GetPreparedLetter should return the default notice if the one required does not exist' + ); +}; -- 2.9.3