From e5c807ebfa49fbd858164863f61a3081eb6d788f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 8 Dec 2016 15:01:37 +0000 Subject: [PATCH] Bug 17762: Update the letter form interface If the pref is on, the notice template will be translatable in different languages Sponsored-by: Orex Digital Signed-off-by: Hugo Agud --- C4/Letters.pm | 16 ++-- .../intranet-tmpl/prog/en/modules/tools/letter.tt | 36 ++++++++- t/db_dependent/Letters/GetLetterTemplates.t | 20 ++--- tools/letter.pl | 89 ++++++++++++++-------- 4 files changed, 108 insertions(+), 53 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 3ff39b1..0158a1a 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -106,7 +106,6 @@ sub GetLetters { ); Return a hashref of letter templates. - The key will be the message transport type. =cut @@ -117,16 +116,15 @@ sub GetLetterTemplates { my $code = $params->{code}; my $branchcode = $params->{branchcode} // ''; my $dbh = C4::Context->dbh; - my $letters = $dbh->selectall_hashref( + my $letters = $dbh->selectall_arrayref( q| - SELECT module, code, branchcode, name, is_html, title, content, message_transport_type + SELECT module, code, branchcode, name, is_html, title, content, message_transport_type, lang FROM letter WHERE module = ? AND code = ? and branchcode = ? | - , 'message_transport_type' - , undef + , { Slice => {} } , $module, $code, $branchcode ); @@ -249,14 +247,17 @@ sub DelLetter { my $module = $params->{module}; my $code = $params->{code}; my $mtt = $params->{mtt}; + my $lang = $params->{lang}; my $dbh = C4::Context->dbh; $dbh->do(q| DELETE FROM letter WHERE branchcode = ? AND module = ? AND code = ? - | . ( $mtt ? q| AND message_transport_type = ?| : q|| ) - , undef, $branchcode, $module, $code, ( $mtt ? $mtt : () ) ); + | + . ( $mtt ? q| AND message_transport_type = ?| : q|| ) + . ( $lang? q| AND lang = ?| : q|| ) + , undef, $branchcode, $module, $code, ( $mtt ? $mtt : () ), ( $lang ? $lang : () ) ); } =head2 addalert ($borrowernumber, $type, $externalid) @@ -684,6 +685,7 @@ sub GetPreparedLetter { my $letter_code = $params{letter_code} or croak "No letter_code"; my $branchcode = $params{branchcode} || ''; 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 ), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt index 129e1bf..08c6076 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt @@ -105,11 +105,13 @@ $(document).ready(function() { $("#sms_counter").css("color", "black"); } }); - $( "#transport-types" ).accordion({ collapsible: true, active:false, animate: 200 }); + $( ".transport-types" ).accordion({ collapsible: true, active:false, animate: 200 }); $(".insert").on("click",function(){ var containerid = $(this).data("containerid"); insertValueQuery( containerid ); }) + + $("#tabs").tabs(); }); [% IF add_form or copy_form %] @@ -374,8 +376,28 @@ $(document).ready(function() { -
- [% FOREACH letter IN letters %] + [% IF Koha.Preference('TranslateNotices') %] +
+
+ + [% END %] + + [% FOREACH lang IN letters.keys %] +
+
+ [% FOR mtt IN letters.$lang.templates.keys.sort %] + [% SET letter = letters.$lang.templates.$mtt %]

[% SWITCH letter.message_transport_type %] [% CASE 'email' %] @@ -404,6 +426,7 @@ $(document).ready(function() {
  1. + [% IF letter.is_html %] @@ -437,8 +460,13 @@ $(document).ready(function() {
- [% END %] + [% END %] +

+ [% END %] + [% IF Koha.Preference('TranslateNotices') %] +
+ [% END %] [% IF code.search('DGST') %] Warning, this is a template for a Digest, as such, any references to branch data ( e.g. branches.branchname ) will refer to the borrower's home branch. [% END %]
Cancel
diff --git a/t/db_dependent/Letters/GetLetterTemplates.t b/t/db_dependent/Letters/GetLetterTemplates.t index 5beb658..fd74285 100644 --- a/t/db_dependent/Letters/GetLetterTemplates.t +++ b/t/db_dependent/Letters/GetLetterTemplates.t @@ -105,26 +105,26 @@ for my $l (@$letters) { my $letter_templates; $letter_templates = C4::Letters::GetLetterTemplates; -is_deeply( $letter_templates, {}, +is_deeply( $letter_templates, [], 'GetLetterTemplates should not return templates if not param is given' ); $letter_templates = C4::Letters::GetLetterTemplates( { module => 'circulation', code => 'code1', branchcode => '' } ); -is( scalar( keys %$letter_templates ), +is( scalar( @$letter_templates ), 2, '2 default templates should exist for circulation code1' ); -is( exists( $letter_templates->{email} ), - 1, 'The mtt email should exist for circulation code1' ); -is( exists( $letter_templates->{sms} ), - 1, 'The mtt sms should exist for circulation code1' ); +my $has_email = grep { $_->{message_transport_type} eq 'email' } @$letter_templates; +is( $has_email, 1, 'The mtt email should exist for circulation code1' ); +my $has_sms = grep { $_->{message_transport_type} eq 'sms' } @$letter_templates; +is( $has_sms, 1, 'The mtt sms should exist for circulation code1' ); $letter_templates = C4::Letters::GetLetterTemplates( { module => 'circulation', code => 'code1', branchcode => 'CPL' } ); -is( scalar( keys %$letter_templates ), +is( scalar( @$letter_templates ), 1, '1 template should exist for circulation CPL code1' ); -is( exists( $letter_templates->{email} ), - 1, 'The mtt should be email for circulation CPL code1' ); +$has_email = grep { $_->{message_transport_type} eq 'email' } @$letter_templates; +is( $has_email, 1, 'The mtt should be email for circulation CPL code1' ); $letter_templates = C4::Letters::GetLetterTemplates( { module => 'circulation', code => 'code1' } ); -is( scalar( keys %$letter_templates ), +is( scalar( @$letter_templates ), 2, '2 default templates should exist for circulation code1 (even if branchcode is not given)' ); diff --git a/tools/letter.pl b/tools/letter.pl index 351e87c..5920a84 100755 --- a/tools/letter.pl +++ b/tools/letter.pl @@ -146,48 +146,70 @@ sub add_form { } my $message_transport_types = GetMessageTransportTypes(); - my @letter_loop; + my $templates = { map { $_ => { message_transport_type => $_ } } sort @$message_transport_types }; + my %letters = ( default => { templates => $templates } ); + + if ( C4::Context->preference('TranslateNotices') ) { + my $translated_languages = + C4::Languages::getTranslatedLanguages( 'opac', + C4::Context->preference('template') ); + for my $language (@$translated_languages) { + for my $sublanguage( @{ $language->{sublanguages_loop} } ) { + if ( $language->{plural} ) { + $letters{ $sublanguage->{rfc4646_subtag} } = { + description => $sublanguage->{native_description} + . ' ' + . $sublanguage->{region_description} . ' (' + . $sublanguage->{rfc4646_subtag} . ')', + templates => { %$templates }, + }; + } + else { + $letters{ $sublanguage->{rfc4646_subtag} } = { + description => $sublanguage->{native_description} + . ' (' + . $sublanguage->{rfc4646_subtag} . ')', + templates => { %$templates }, + }; + } + } + } + $template->param( languages => $translated_languages ); + } if ($letters) { $template->param( modify => 1, code => $code, - branchcode => $branchcode, ); - my $first_flag = 1; + my $first_flag_name = 1; + my ( $lang, @templates ); # The letter name is contained into each mtt row. # So we can only sent the first one to the template. - for my $mtt ( @$message_transport_types ) { + for my $letter ( @$letters ) { # The letter_name - if ( $first_flag and $letters->{$mtt}{name} ) { + if ( $first_flag_name and $letter->{name} ) { $template->param( - letter_name=> $letters->{$mtt}{name}, + letter_name=> $letter->{name}, ); - $first_flag = 0; + $first_flag_name = 0; } - push @letter_loop, { - message_transport_type => $mtt, - is_html => $letters->{$mtt}{is_html}, - title => $letters->{$mtt}{title}, - content => $letters->{$mtt}{content}//'', + my $lang = $letter->{lang}; + my $mtt = $letter->{message_transport_type}; + $letters{ $lang }{templates}{$mtt} = { + message_transport_type => $letter->{message_transport_type}, + is_html => $letter->{is_html}, + title => $letter->{title}, + content => $letter->{content} // '', }; } } - else { # initialize the new fields - for my $mtt ( @$message_transport_types ) { - push @letter_loop, { - message_transport_type => $mtt, - } - } - $template->param( - branchcode => $branchcode, - module => $module, - ); + else { $template->param( adding => 1 ); } $template->param( - letters => \@letter_loop, + letters => \%letters, ); my $field_selection; @@ -258,11 +280,13 @@ sub add_validate { my @mtt = $input->multi_param('message_transport_type'); my @title = $input->multi_param('title'); my @content = $input->multi_param('content'); + my @lang = $input->multi_param('lang'); for my $mtt ( @mtt ) { my $is_html = $input->param("is_html_$mtt"); my $title = shift @title; my $content = shift @content; - my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt); + my $lang = shift @lang; + my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt, $lang ); # getletter can return the default letter even if we pass a branchcode # If we got the default one and we needed the specific one, we didn't get the one we needed! @@ -271,25 +295,25 @@ sub add_validate { } unless ( $title and $content ) { # Delete this mtt if no title or content given - delete_confirmed( $branchcode, $oldmodule, $code, $mtt ); + delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang ); next; } - elsif ( $letter and $letter->{message_transport_type} eq $mtt ) { + elsif ( $letter and $letter->{message_transport_type} eq $mtt and $letter->{lang} eq $lang ) { $dbh->do( q{ UPDATE letter - SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ? + SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?, lang = ? WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ? }, undef, - $branchcode || '', $module, $name, $is_html || 0, $title, $content, + $branchcode || '', $module, $name, $is_html || 0, $title, $content, $lang, $branchcode, $oldmodule, $code, $mtt ); } else { $dbh->do( - q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)}, + q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type, lang) VALUES (?,?,?,?,?,?,?,?,?)}, undef, - $branchcode || '', $module, $code, $name, $is_html || 0, $title, $content, $mtt + $branchcode || '', $module, $code, $name, $is_html || 0, $title, $content, $mtt, $lang ); } } @@ -310,13 +334,14 @@ sub delete_confirm { } sub delete_confirmed { - my ($branchcode, $module, $code, $mtt) = @_; + my ($branchcode, $module, $code, $mtt, $lang) = @_; C4::Letters::DelLetter( { branchcode => $branchcode || '', module => $module, code => $code, - mtt => $mtt + mtt => $mtt, + lang => $lang, } ); # setup default display for screen -- 2.9.3