@@ -, +, @@ <> <> --- C4/Letters.pm | 43 +++++++++++++++++++++++++++++-------------- t/db_dependent/Letters.t | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 17 deletions(-) --- a/C4/Letters.pm +++ a/C4/Letters.pm @@ -819,14 +819,14 @@ sub _parseletter { } while ( my ($field, $val) = each %$values ) { - my $replacetablefield = "<<$table.$field>>"; - my $replacefield = "<<$field>>"; $val =~ s/\p{P}$// if $val && $table=~/biblio/; #BZ 9886: Assuming that we want to eliminate ISBD punctuation here #Therefore adding the test on biblio. This includes biblioitems, #but excludes items. Removed unneeded global and lookahead. $val = GetAuthorisedValueByCode ('ROADTYPE', $val, 0) if $table=~/^borrowers$/ && $field=~/^streettype$/; + + # Dates replacement my $replacedby = defined ($val) ? $val : ''; if ( $replacedby and not $replacedby =~ m|0000-00-00| @@ -835,19 +835,34 @@ sub _parseletter { { # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date my $dateonly = defined $1 ? 0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds. - eval { - $replacedby = output_pref({ dt => dt_from_string( $replacedby ), dateonly => $dateonly }); - }; - warn "$replacedby seems to be a date but an error occurs on generating it ($@)" if $@; + my $re_dateonly_filter = qr{ $field( \s* \| \s* dateonly\s*)?>> }xms; + + for my $letter_field ( qw( title content ) ) { + my $filter_string_used = q{}; + if ( $letter->{ $letter_field } =~ $re_dateonly_filter ) { + # We overwrite $dateonly if the filter exists and we have a time in the datetime + $filter_string_used = $1 || q{}; + $dateonly = $1 unless $dateonly; + } + eval { + $replacedby = output_pref({ dt => dt_from_string( $replacedby ), dateonly => $dateonly }); + }; + + if ( $letter->{ $letter_field } ) { + $letter->{ $letter_field } =~ s/\Q<<$table.$field$filter_string_used>>\E/$replacedby/g; + $letter->{ $letter_field } =~ s/\Q<<$field$filter_string_used>>\E/$replacedby/g; + } + } + } + # Other fields replacement + else { + for my $letter_field ( qw( title content ) ) { + if ( $letter->{ $letter_field } ) { + $letter->{ $letter_field } =~ s/<<$table.$field>>/$replacedby/g; + $letter->{ $letter_field } =~ s/<<$field>>/$replacedby/g; + } + } } - ($letter->{title} ) and do { - $letter->{title} =~ s/$replacetablefield/$replacedby/g; - $letter->{title} =~ s/$replacefield/$replacedby/g; - }; - ($letter->{content}) and do { - $letter->{content} =~ s/$replacetablefield/$replacedby/g; - $letter->{content} =~ s/$replacefield/$replacedby/g; - }; } if ($table eq 'borrowers' && $letter->{content}) { --- a/t/db_dependent/Letters.t +++ a/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 65; +use Test::More tests => 68; use Test::MockModule; use Test::Warn; @@ -141,7 +141,7 @@ my $letters = C4::Letters::GetLetters(); is( @$letters, 0, 'GetLetters returns the correct number of letters' ); my $title = q|<> - <>|; -my $content = q|Dear <> <>, +my $content = q{Dear <> <>, According to our current records, you have items that are overdue.Your library does not charge late fines, but please return or renew them at the branch below as soon as possible. <> @@ -155,7 +155,7 @@ The following item(s) is/are currently <>: Thank-you for your prompt attention to this matter. Don't forget your date of birth: <>. Look at this wonderful biblio timestamp: <>. -|; +}; $dbh->do( q|INSERT INTO letter(branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES ('CPL','my module','my code','my name',1,?,?,'email')|, undef, $title, $content ); $letters = C4::Letters::GetLetters(); @@ -279,6 +279,7 @@ The following item(s) is/are currently $substitute->{status}: Thank-you for your prompt attention to this matter. Don't forget your date of birth: | . output_pref({ dt => $date, dateonly => 1 }) . q|. Look at this wonderful biblio timestamp: | . output_pref({ dt => $date }) . ".\n"; + is( $prepared_letter->{title}, $my_title_letter, 'GetPreparedLetter returns the title correctly' ); is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' ); @@ -294,6 +295,39 @@ $prepared_letter = GetPreparedLetter(( $my_content_letter = qq|This is a SMS for an $substitute->{status}|; is( $prepared_letter->{content}, $my_content_letter, 'GetPreparedLetter returns the content correctly' ); +$dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('test_date','TEST_DATE','Test dates','Test dates','This one only contains the date: <>.');}); +$prepared_letter = GetPreparedLetter(( + module => 'test_date', + branchcode => '', + letter_code => 'test_date', + tables => $tables, + substitute => $substitute, + repeat => $repeat, +)); +is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.| ); + +$dbh->do(q{UPDATE letter SET content = 'And also this one:<>.' WHERE code = 'test_date';}); +$prepared_letter = GetPreparedLetter(( + module => 'test_date', + branchcode => '', + letter_code => 'test_date', + tables => $tables, + substitute => $substitute, + repeat => $repeat, +)); +is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.| ); + +$dbh->do(q{UPDATE letter SET content = 'And also this one:<>.' WHERE code = 'test_date';}); +$prepared_letter = GetPreparedLetter(( + module => 'test_date', + branchcode => '', + letter_code => 'test_date', + tables => $tables, + substitute => $substitute, + repeat => $repeat, +)); +is( $prepared_letter->{content}, q|This one only contains the date: | . output_pref({ dt => $date, dateonly => 1 }) . q|.| ); + $dbh->do(q{INSERT INTO letter (module, code, name, title, content) VALUES ('claimacquisition','TESTACQCLAIM','Acquisition Claim','Item Not Received','<>|<>|Ordernumber <> (<>) (<> ordered)');}); my $booksellerid = C4::Bookseller::AddBookseller( --