From cef7cd40ce5b2fe1505245cf232d4338b10eba3e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 26 Jan 2015 17:24:36 +0100 Subject: [PATCH] Bug 13622: Display a datetime without time in a notice For some needs, a librarian would like to display a datetime or timestamp field without the time. This patch adds filter logic in the notice/letter parsing process. Test plan: 1/ Defined a notice using a datetime or timestamp DB field (biblio.timestamp for instance). 2/ Generate the notice 3/ Verify that the letter is generated with the time 4/ Use the "dateonly" filter like: <> <> 5/ Generate the notice 6/ Confirm the the letter is generated without the time for this field. Signed-off-by: Marcel de Rooy Signed-off-by: Indranil Das Gupta (L2C2 Technologies) Updated the count of tests to 64 for t/db_dependent/Letters.t to pass Signed-off-by: Tomas Cohen Arazi --- C4/Letters.pm | 43 +++++++++++++++++++++++++++------------- t/db_dependent/Letters.t | 40 ++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 17 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 2e6c4cecd2..863da75136 100644 --- a/C4/Letters.pm +++ b/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}) { diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index cc3242c0f6..1ad4c3bf69 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 => 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( -- 2.39.5