Skip to content

Commit 121f06d

Browse files
areeshatariqBan3
andauthored
NutritionScore Calculation Details HTML Template (#3503)
* NutritionScore Calculation Details HTML Template * Fixed minor issues * Renamed to * Add Github syntax highlighting for *.tt files Co-authored-by: Jussi Timperi <[email protected]>
1 parent 4c8ef25 commit 121f06d

File tree

6 files changed

+139
-58
lines changed

6 files changed

+139
-58
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
# Test files
1212
# ============
1313
*.t text
14+
15+
# Template::Toolkit
16+
# =================
17+
*.tt linguist-language=HTML

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ requires 'experimental'; # libexperimental-perl
2323
requires 'Apache2::Request'; # libapache2-request-perl
2424
requires 'Digest::MD5'; # libdigest-md5-perl
2525
requires 'Time::Local'; # libtime-local-perl
26+
requires 'Template','3.008'; # libtemplate-perl
2627

2728
# Probably not available as Debian packages
2829
requires 'MongoDB', '>= 2.2.1, < 2.3'; # libmongodb-perl has an older version

docker/backend-dev/conf/po-foreground.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ then
4141
ln -sf /opt/product-opener/packager-codes /mnt/podata/packager-codes
4242
fi
4343

44+
if [ ! -e /mnt/podata/templates ]
45+
then
46+
ln -sf /opt/product-opener/templates /mnt/podata/templates
47+
fi
48+
4449
perl -I/opt/product-opener/lib -I/opt/perl/local/lib/perl5 /opt/product-opener/scripts/build_lang.pl
4550
chown -R www-data:www-data /mnt/podata
4651
chown -R www-data:www-data /opt/product-opener/html/images/products

docker/backend/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ RUN set -x \
3636
libapache2-request-perl \
3737
libdigest-md5-perl \
3838
libtime-local-perl \
39-
libdbd-pg-perl
39+
libdbd-pg-perl \
40+
libtemplate-perl
4041

4142
# Stage for installing/compiling cpanfile dependencies
4243
FROM modperl AS builder

lib/ProductOpener/Display.pm

Lines changed: 90 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ use Storable qw(freeze);
159159
use Digest::MD5 qw(md5_hex);
160160
use boolean;
161161
use Excel::Writer::XLSX;
162+
use Template;
162163

163164
use Log::Any '$log', default_adapter => 'Stderr';
164165

@@ -8650,79 +8651,111 @@ the rounded value according to the Nutri-Score rules, and the corresponding poin
86508651
sub display_nutriscore_calculation_details($) {
86518652
86528653
my $nutriscore_data_ref = shift;
8653-
8654-
my $html = '<p><a data-dropdown="nutriscore_drop" aria-controls="nutriscore_drop" aria-expanded="false">' . lang("nutriscore_calculation_details") . " &raquo;</a><p>"
8655-
. '<div id="nutriscore_drop" data-dropdown-content class="f-dropdown content large" aria-hidden="true" tabindex="-1">';
8654+
8655+
my $beverage_view;
86568656
86578657
if ($nutriscore_data_ref->{is_beverage}) {
8658-
$html .= "<p>" . lang("nutriscore_is_beverage") . "</p>";
8658+
$beverage_view = lang("nutriscore_is_beverage");
86598659
}
86608660
else {
8661-
$html .= "<p>" . lang("nutriscore_is_not_beverage") . "</p>";
8662-
}
8663-
8664-
if ($nutriscore_data_ref->{is_fat}) {
8665-
$html .= "<p>" . lang("nutriscore_proteins_is_added_fat") . "</p>";
8666-
}
8667-
8668-
my @points = (
8669-
["positive", ["proteins", "fiber", "fruits_vegetables_nuts_colza_walnut_olive_oils"]],
8670-
["negative", ["energy", "sugars", "saturated_fat", "sodium"]],
8671-
);
8672-
8673-
foreach my $points_ref (@points) {
8674-
8675-
$html .= "<p><strong>" . lang("nutriscore_" . $points_ref->[0] . "_points") . lang("sep") . ": "
8676-
. $nutriscore_data_ref->{$points_ref->[0] . "_points"} . "</strong></p><ul>";
8677-
8678-
foreach my $nutrient (@{$points_ref->[1]}) {
8679-
8680-
my $nutrient_threshold_id = $nutrient;
8681-
8682-
if ((defined $nutriscore_data_ref->{is_beverage}) and ($nutriscore_data_ref->{is_beverage})
8683-
and (defined $points_thresholds{$nutrient_threshold_id . "_beverages"})) {
8684-
$nutrient_threshold_id .= "_beverages";
8685-
}
8686-
if (($nutriscore_data_ref->{is_fat}) and ($nutrient eq "saturated_fat")) {
8687-
$nutrient = "saturated_fat_ratio";
8688-
$nutrient_threshold_id = "saturated_fat_ratio";
8689-
}
8690-
8691-
$html .= "<li><strong>" . lang("nutriscore_points_for_" . $nutrient) . lang("sep") . ": "
8692-
. $nutriscore_data_ref->{$nutrient . "_points"} . "&nbsp;</strong>/&nbsp;" . scalar(@{$points_thresholds{$nutrient_threshold_id}}) . lang("points")
8693-
. " (" . lang("nutriscore_source_value") . lang("sep") . ": " . $nutriscore_data_ref->{$nutrient} . ", "
8694-
. lang("nutriscore_rounded_value") . lang("sep") . ": " . $nutriscore_data_ref->{$nutrient . "_value"} . ")" . "</li>";
8695-
}
8696-
8697-
$html .= "</ul>";
8661+
$beverage_view = lang("nutriscore_is_not_beverage");
86988662
}
86998663
8664+
# Select message that explains the reason why the proteins points have been counted or not
8665+
8666+
my $nutriscore_protein_info;
87008667
if ($nutriscore_data_ref->{negative_points} < 11) {
8701-
$html .= "<p>" . lang("nutriscore_proteins_negative_points_less_than_11") . "</p>";
8668+
$nutriscore_protein_info = lang("nutriscore_proteins_negative_points_less_than_11");
87028669
}
87038670
elsif ((defined $nutriscore_data_ref->{is_cheese}) and ($nutriscore_data_ref->{is_cheese})) {
8704-
$html .= "<p>" . lang("nutriscore_proteins_is_cheese") . "</p>";
8671+
$nutriscore_protein_info = lang("nutriscore_proteins_is_cheese");
87058672
}
87068673
elsif ((((defined $nutriscore_data_ref->{is_beverage}) and ($nutriscore_data_ref->{is_beverage}))
87078674
and ($nutriscore_data_ref->{fruits_vegetables_nuts_colza_walnut_olive_oils_points} == 10))
8708-
or (((not defined $nutriscore_data_ref->{is_beverage}) or (not $nutriscore_data_ref->{is_beverage}))
8675+
or (((not defined $nutriscore_data_ref->{is_beverage}) or (not $nutriscore_data_ref->{is_beverage}))
87098676
and ($nutriscore_data_ref->{fruits_vegetables_nuts_colza_walnut_olive_oils_points} == 5)) ) {
8710-
$html .= "<p>" . lang("nutriscore_proteins_maximum_fruits_points") . "</p>";
8677+
8678+
$nutriscore_protein_info = lang("nutriscore_proteins_maximum_fruits_points");
87118679
}
87128680
else {
8713-
$html .= "<p>" . lang("nutriscore_proteins_negative_points_greater_or_equal_to_11") . "</p>";
8714-
}
8715-
8716-
$html .= "<p><strong>" . lang("nutriscore_score") . lang("sep"). ": " . ($nutriscore_data_ref->{score})
8717-
. "</strong> (" . $nutriscore_data_ref->{negative_points} . " - " . $nutriscore_data_ref->{positive_points} . ")<p>";
8718-
8719-
$html .= "<p><strong>" . lang("nutriscore_grade") . lang("sep"). ": " . uc($nutriscore_data_ref->{grade}) . "</strong></p>";
8720-
8721-
$html .= "</div>";
8722-
8681+
$nutriscore_protein_info = lang("nutriscore_proteins_negative_points_greater_or_equal_to_11");
8682+
}
8683+
8684+
# Generate a data structure that we will pass to the template engine
8685+
8686+
my $template_data_ref = {
8687+
8688+
lang => \&lang,
8689+
8690+
beverage_view => $beverage_view,
8691+
is_fat => $nutriscore_data_ref->{is_fat},
8692+
8693+
nutriscore_protein_info => $nutriscore_protein_info,
8694+
8695+
score => $nutriscore_data_ref->{score},
8696+
grade => uc($nutriscore_data_ref->{grade}),
8697+
positive_points => $nutriscore_data_ref->{positive_points},
8698+
negative_points => $nutriscore_data_ref->{negative_points},
8699+
8700+
# Details of positive and negative points, filled dynamically below
8701+
# as the nutrients and thresholds are different for some products (beverages and fats)
8702+
points_groups => []
8703+
};
8704+
8705+
my %points_groups = (
8706+
"positive" => ["proteins", "fiber", "fruits_vegetables_nuts_colza_walnut_olive_oils"],
8707+
"negative" => ["energy", "sugars", "saturated_fat", "sodium"],
8708+
);
8709+
8710+
foreach my $type ("positive", "negative") {
8711+
8712+
# Initiate a data structure for the points of the group
8713+
8714+
my $points_group_ref = {
8715+
type => $type,
8716+
points => $nutriscore_data_ref->{$type . "_points"},
8717+
nutrients => [],
8718+
};
8719+
8720+
# Add the nutrients for the group
8721+
foreach my $nutrient (@{$points_groups{$type}}) {
8722+
8723+
my $nutrient_threshold_id = $nutrient;
8724+
8725+
if ((defined $nutriscore_data_ref->{is_beverage}) and ($nutriscore_data_ref->{is_beverage})
8726+
and (defined $points_thresholds{$nutrient_threshold_id . "_beverages"})) {
8727+
$nutrient_threshold_id .= "_beverages";
8728+
}
8729+
if (($nutriscore_data_ref->{is_fat}) and ($nutrient eq "saturated_fat")) {
8730+
$nutrient = "saturated_fat_ratio";
8731+
$nutrient_threshold_id = "saturated_fat_ratio";
8732+
}
8733+
push @{$points_group_ref->{nutrients}}, {
8734+
id => $nutrient,
8735+
points => $nutriscore_data_ref->{$nutrient . "_points"},
8736+
maximum => scalar(@{$points_thresholds{$nutrient_threshold_id}}),
8737+
value => $nutriscore_data_ref->{$nutrient},
8738+
rounded => $nutriscore_data_ref->{$nutrient . "_value"},
8739+
};
8740+
}
8741+
8742+
push @{$template_data_ref->{points_groups}}, $points_group_ref;
8743+
}
8744+
8745+
# Nutrition Score Calculation Template
8746+
my $config = {
8747+
INCLUDE_PATH => $data_root . '/templates',
8748+
INTERPOLATE => 1,
8749+
EVAL_PERL => 1,
8750+
};
8751+
8752+
my $tt = Template->new($config);
8753+
8754+
my $html;
8755+
$tt->process('nutrition_score.tt', $template_data_ref, \$html) || return "template error: " . $tt->error();
8756+
87238757
return $html;
8724-
}
8725-
8758+
}
87268759
87278760
sub display_nutrient_levels($) {
87288761

templates/nutrition_score.tt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<p>
2+
<a data-dropdown="nutriscore_drop" aria-controls="nutriscore_drop" aria-expanded="false"> [% lang('nutriscore_calculation_details') %] &raquo</a>
3+
</p>
4+
<div id="nutriscore_drop" data-dropdown-content class="f-dropdown content large" aria-hidden="true" tabindex="-1">
5+
6+
<p> [% beverage_view %] </p>
7+
8+
[% IF is_fat %]
9+
<p> [% lang('nutriscore_proteins_is_added_fat') %] </p>
10+
[% END %]
11+
12+
[% FOREACH group IN points_groups %]
13+
14+
<p>
15+
<strong> [% lang("nutriscore_${group.type}_points") %][% lang('sep') %]: [% group.points %] </strong>
16+
</p>
17+
18+
<ul>
19+
[% FOREACH nutrient IN group.nutrients %]
20+
<li>
21+
<strong> [% lang("nutriscore_points_for_${nutrient.id}") %][% lang('sep') %]: [% nutrient.points %]&nbsp;</strong>
22+
/&nbsp; [% nutrient.maximum %][% lang("points") %] ([% lang("nutriscore_source_value") %][% lang("sep") %]: [% nutrient.value %],
23+
[% lang('nutriscore_rounded_value') %][% lang('sep') %]: [% nutrient.rounded %])
24+
</li>
25+
[% END %]
26+
</ul>
27+
28+
[% END %]
29+
30+
<p>[% nutriscore_protein_info %] </p>
31+
<p>
32+
<strong> [% lang('nutriscore_score') %][% lang('sep') %]: [% score %] </strong> (
33+
[% negative_points %] - [% positive_points %])
34+
</p>
35+
<p><strong> [% lang('nutriscore_grade') %][% lang('sep') %]: [% grade %] </strong></p>
36+
37+
</div>

0 commit comments

Comments
 (0)