Commit cef7cd40 authored by joubu's avatar joubu Committed by Tomas Cohen Arazi

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:
  <<your_table.your_field | dateonly>>
  <<biblio.timestamp | dateonly>>
5/ Generate the notice
6/ Confirm the the letter is generated without the time for this field.
Signed-off-by: default avatarMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: default avatarIndranil Das Gupta (L2C2 Technologies) <indradg@gmail.com>

Updated the count of tests to 64 for t/db_dependent/Letters.t to pass
Signed-off-by: default avatarTomas Cohen Arazi <tomascohen@unc.edu.ar>
parent d671b866
......@@ -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}) {
......
......@@ -18,7 +18,7 @@
# along with Koha; if not, see <http://www.gnu.org/licenses>.
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|<<branches.branchname>> - <<status>>|;
my $content = q|Dear <<borrowers.firstname>> <<borrowers.surname>>,
my $content = q{Dear <<borrowers.firstname>> <<borrowers.surname>>,
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.
<<branches.branchname>>
......@@ -155,7 +155,7 @@ The following item(s) is/are currently <<status>>:
Thank-you for your prompt attention to this matter.
Don't forget your date of birth: <<borrowers.dateofbirth>>.
Look at this wonderful biblio timestamp: <<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: <<biblio.timestamp | dateonly>>.');});
$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:<<timestamp | dateonly>>.' 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:<<timestamp|dateonly >>.' 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','<<aqbooksellers.name>>|<<aqcontacts.name>>|<order>Ordernumber <<aqorders.ordernumber>> (<<biblio.title>>) (<<aqorders.quantity>> ordered)</order>');});
my $booksellerid = C4::Bookseller::AddBookseller(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment