@@ -159,6 +159,7 @@ use Storable qw(freeze);
159
159
use Digest::MD5 qw( md5_hex) ;
160
160
use boolean;
161
161
use Excel::Writer::XLSX;
162
+ use Template;
162
163
163
164
use Log::Any ' $log' , default_adapter => ' Stderr' ;
164
165
@@ -8650,79 +8651,111 @@ the rounded value according to the Nutri-Score rules, and the corresponding poin
8650
8651
sub display_nutriscore_calculation_details($) {
8651
8652
8652
8653
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 ") . " » ;</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 ;
8656
8656
8657
8657
if ($nutriscore _data _ref- >{is _beverage }) {
8658
- $html .= "< p >" . lang ("nutriscore _is _beverage ") . "</ p >" ;
8658
+ $beverage _ view = lang ("nutriscore _is _beverage ");
8659
8659
}
8660
8660
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 "} . " </strong > / " . 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 ");
8698
8662
}
8699
8663
8664
+ # Select message that explains the reason why the proteins points have been counted or not
8665
+
8666
+ my $nutriscore_protein_info;
8700
8667
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");
8702
8669
}
8703
8670
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 ");
8705
8672
}
8706
8673
elsif ((((defined $nutriscore_data_ref- > {is _beverage }) and ($nutriscore_data_ref- > {is _beverage }))
8707
8674
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 }))
8709
8676
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 ");
8711
8679
}
8712
8680
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
+
8723
8757
return $html;
8724
- }
8725
-
8758
+ }
8726
8759
8727
8760
sub display_nutrient_levels($) {
8728
8761
0 commit comments