From 25cdde65832a29445849edfbe358e372655efbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Thu, 23 May 2024 13:09:40 +0200 Subject: [PATCH] fix: remove some warnings, fix search form template and Nutri-Score 2021 details (#10335) --- cgi/product_multilingual.pl | 46 ++++++------ cgi/search.pl | 5 +- lib/ProductOpener/API.pm | 2 +- lib/ProductOpener/Display.pm | 31 +++++--- lib/ProductOpener/Products.pm | 2 +- taxonomies/nutrients.txt | 1 + .../product_edit_form_packagings.tt.html | 2 +- .../web/pages/search_form/search_form.tt.html | 7 -- .../web_html/fr-edit-product.html | 6 +- .../web_html/fr-product-2.html | 4 +- .../web_html/fr-product.html | 4 +- .../web_html/fr-search-form.html | 7 -- .../web_html/world-edit-product.html | 6 +- .../web_html/world-search-form.html | 7 -- tests/unit/display.t | 75 ++++++++++--------- 15 files changed, 102 insertions(+), 103 deletions(-) diff --git a/cgi/product_multilingual.pl b/cgi/product_multilingual.pl index b5be9eb1a9438..14b936670da0d 100755 --- a/cgi/product_multilingual.pl +++ b/cgi/product_multilingual.pl @@ -67,12 +67,10 @@ use File::Copy qw(move); use Data::Dumper; -my $request_ref = ProductOpener::Display::init_request(); - # Function to display a form to add a product with a specific barcode (either typed in a field, or extracted from a barcode photo) # or without a barcode -sub display_search_or_add_form() { +sub display_search_or_add_form($request_ref) { # Producer platform and no org or not admin: do not offer to add products if (($server_options{producers_platform}) @@ -167,6 +165,8 @@ ($product_ref) return; } +my $request_ref = ProductOpener::Display::init_request(); + if ($User_id eq 'unwanted-user-french') { display_error_and_exit( $request_ref, @@ -209,7 +209,7 @@ ($product_ref) my $title = lang("add_product"); - $html = display_search_or_add_form(); + $html = display_search_or_add_form($request_ref); $request_ref->{title} = lang('add_product'); $request_ref->{content_ref} = \$html; @@ -956,26 +956,27 @@ ($product_ref, $field, $language) } $display_tab_ref->{fields} = \@fields_arr; - } - # For moderators, add a checkbox to move all data and photos to the main language - # this needs to be below the "add (language name) in all field labels" above, so that it does not change this label. - if (($User{moderator}) and ($tabsid eq "front_image")) { + # For moderators, add a checkbox to move all data and photos to the main language + # this needs to be below the "add (language name) in all field labels" above, so that it does not change this label. + if (($User{moderator}) and ($tabsid eq "front_image")) { - my $msg = f_lang( - "f_move_data_and_photos_to_main_language", - { - language => '' . $language . '', - main_language => '' - . lang("lang_" . $product_ref->{lc}) - . '' - } - ); + my $msg = f_lang( + "f_move_data_and_photos_to_main_language", + { + language => '' . $language . '', + main_language => '' + . lang("lang_" . $product_ref->{lc}) + . '' + } + ); + + my $moveid = "move_" . $tabid . "_data_and_images_to_main_language"; - my $moveid = "move_" . $tabid . "_data_and_images_to_main_language"; + $display_tab_ref->{moveid} = $moveid; + $display_tab_ref->{msg} = $msg; + } - $display_tab_ref->{moveid} = $moveid; - $display_tab_ref->{msg} = $msg; } push(@display_tabs, $display_tab_ref); @@ -1197,9 +1198,8 @@ ($product_ref, $field, $language) # They may be prefixed with a ! to indicate that the nutrient is always shown when displaying the nutrition facts table if (($shown) and ($nutriment =~ /^!?-/)) { $class = 'sub'; - $prefix = $Lang{nutrition_data_table_sub}{$lc} . " "; if ($nutriment =~ /^--/) { - $prefix = "  " . $prefix; + $prefix = "  "; } } @@ -1421,7 +1421,7 @@ ($product_ref, $field, $language) $supports_iu = "true"; } - my $other_nutriment_unit = get_property("nutrients", "zz:$nid", "unit:en"); + my $other_nutriment_unit = get_property("nutrients", "zz:$nid", "unit:en") || ''; $other_nutriments .= '{ "value" : "' . $other_nutriment_value diff --git a/cgi/search.pl b/cgi/search.pl index 05d629cc660be..94eb92039f875 100755 --- a/cgi/search.pl +++ b/cgi/search.pl @@ -726,7 +726,10 @@ my $download = single_param("download") || ''; open(my $OUT, ">>:encoding(UTF-8)", "$BASE_DIRS{LOGS}/search_log_debug"); - print $OUT remote_addr() . "\t" . time() . "\t" . decode utf8 => single_param('search_terms') . " - map: $map + print $OUT remote_addr() . "\t" + . time() . "\t" + . (decode utf8 => single_param('search_terms') || "no_search_terms") + . " - map: $map - graph: $graph - download: $download - page: $page\n"; close($OUT); diff --git a/lib/ProductOpener/API.pm b/lib/ProductOpener/API.pm index e7d52d86f49cb..4118e2c723503 100644 --- a/lib/ProductOpener/API.pm +++ b/lib/ProductOpener/API.pm @@ -620,7 +620,7 @@ sub customize_packagings ($request_ref, $product_ref) { my $customized_packaging_ref = dclone($packaging_ref); - if ($request_ref->{api_version} >= 3) { + if ((defined $request_ref->{api_version}) and ($request_ref->{api_version} >= 3)) { # Shape, material and recycling are localized foreach my $property ("shape", "material", "recycling") { if (defined $packaging_ref->{$property}) { diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index e28344e75eee4..08e40f56c47db 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -135,7 +135,7 @@ BEGIN { $attributes_options_ref $knowledge_panels_options_ref - &display_nutriscore_calculation_details + &display_nutriscore_calculation_details_2021 ); # symbols to export on request %EXPORT_TAGS = (all => [@EXPORT_OK]); } @@ -5493,7 +5493,7 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa # 2021-02-25: we now store only nested ingredients, flatten them if the API is <= 1 - if ($request_ref->{api_version} <= 1) { + if ((defined $request_ref->{api_version}) and ($request_ref->{api_version} <= 1)) { for my $product_ref (@{$request_ref->{structured_response}{products}}) { if (defined $product_ref->{ingredients}) { @@ -5677,8 +5677,6 @@ sub display_pagination ($request_ref, $count, $limit, $page) { $nofollow = ' nofollow'; } - print STDERR "zzz lc: $lc - request_ref->lc: $request_ref->{lc}\n"; - if ((($nb_pages > 1) and (defined $current_link)) and (not defined $request_ref->{product_changes_saved})) { my $prev = ''; @@ -8864,7 +8862,9 @@ HTML return; } -=head2 display_nutriscore_calculation_details( $nutriscore_data_ref, $version = "2021" ) +=head2 display_nutriscore_calculation_details_2021 ( $product_ref ) + +Warning: This function only works with Nutri-Score 2021. Generates HTML code with information on how the Nutri-Score was computed for a particular product. @@ -8873,7 +8873,12 @@ the rounded value according to the Nutri-Score rules, and the corresponding poin =cut -sub display_nutriscore_calculation_details ($nutriscore_data_ref, $version = "2021") { +sub display_nutriscore_calculation_details_2021 ($product_ref) { + + my $version = "2021"; + my $nutriscore_grade = deep_get($product_ref, "nutriscore", $version, "grade"); + my $nutriscore_score = deep_get($product_ref, "nutriscore", $version, "score"); + my $nutriscore_data_ref = deep_get($product_ref, "nutriscore", $version, "data"); my $beverage_view; @@ -8918,8 +8923,8 @@ sub display_nutriscore_calculation_details ($nutriscore_data_ref, $version = "20 nutriscore_protein_info => $nutriscore_protein_info, - score => $nutriscore_data_ref->{score}, - grade => uc($nutriscore_data_ref->{grade}), + score => $nutriscore_score, + grade => uc($nutriscore_grade), positive_points => $nutriscore_data_ref->{positive_points}, negative_points => $nutriscore_data_ref->{negative_points}, @@ -9207,9 +9212,13 @@ sub data_to_display_nutriscore ($product_ref, $version = "2021") { $result_data_ref->{nutriscore_warnings} = \@nutriscore_warnings; } - # Display the details of the computation of the Nutri-Score if we computed one - if ((defined $product_ref->{nutriscore_grade}) and ($product_ref->{nutriscore_grade} =~ /^[a-e]$/)) { - $result_data_ref->{nutriscore_details} = display_nutriscore_calculation_details($nutriscore_data_ref, $version); + # Generate HTML for the details of the computation of the Nutri-Score if we computed one + # only for version 2021 + if ( ($version eq "2021") + and (defined $product_ref->{nutriscore_grade}) + and ($product_ref->{nutriscore_grade} =~ /^[a-e]$/)) + { + $result_data_ref->{nutriscore_details} = display_nutriscore_calculation_details_2021($product_ref); } return $result_data_ref; diff --git a/lib/ProductOpener/Products.pm b/lib/ProductOpener/Products.pm index e603c1df14b88..75884ed931535 100644 --- a/lib/ProductOpener/Products.pm +++ b/lib/ProductOpener/Products.pm @@ -2338,7 +2338,7 @@ sub compute_product_history_and_completeness ($product_data_root, $current_produ my $number_of_units = $packagings_ref->{number_of_units}; my $weight_measured = $packagings_ref->{weight_measured}; - $packagings_data_signature .= "number_of_units:" . $number_of_units . ','; + $packagings_data_signature .= "number_of_units:" . ($number_of_units || '') . ','; foreach my $property (qw(shape material recycling quantity_per_unit)) { $packagings_data_signature .= $property . ":" . ($packagings_ref->{$property} || '') . ','; } diff --git a/taxonomies/nutrients.txt b/taxonomies/nutrients.txt index 2da49e9703fbf..9029f110f1af7 100644 --- a/taxonomies/nutrients.txt +++ b/taxonomies/nutrients.txt @@ -1056,6 +1056,7 @@ zz:unsaturated-fat xx:Unsaturated fat en:Unsaturated fat nl:Onverzadigde vetzuren, Onverzadigd vet, onverzadigde vetten +unit:en: g zz:monounsaturated-fat xx:Monounsaturated fat diff --git a/templates/web/pages/product_edit/product_edit_form_packagings.tt.html b/templates/web/pages/product_edit/product_edit_form_packagings.tt.html index 81120e7fdc3bb..5529e5f119f75 100644 --- a/templates/web/pages/product_edit/product_edit_form_packagings.tt.html +++ b/templates/web/pages/product_edit/product_edit_form_packagings.tt.html @@ -65,7 +65,7 @@ - [% IF packaging_max > 0 %] + [% IF packaging_max.defined && packaging_max > 0 %] configure_packaging_select2("[% select2_id %]", "[% taxonomy %]", "[% lang("packaging_$property") %]" ) diff --git a/templates/web/pages/search_form/search_form.tt.html b/templates/web/pages/search_form/search_form.tt.html index 4145f320fa6c7..c8163d4a821d0 100644 --- a/templates/web/pages/search_form/search_form.tt.html +++ b/templates/web/pages/search_form/search_form.tt.html @@ -11,13 +11,6 @@

[% lang('search_products') %]

- -
-
- - -
-
diff --git a/tests/integration/expected_test_results/web_html/fr-edit-product.html b/tests/integration/expected_test_results/web_html/fr-edit-product.html index 4411812ddd180..59b45061c3c04 100644 --- a/tests/integration/expected_test_results/web_html/fr-edit-product.html +++ b/tests/integration/expected_test_results/web_html/fr-edit-product.html @@ -2057,7 +2057,7 @@

- + @@ -2281,7 +2281,7 @@

- + @@ -3899,7 +3899,7 @@

Découvrez le projet

{ "value" : "Acide cérotique", "unit" : "g", "iu": false }, { "value" : "Acide montanique", "unit" : "g", "iu": false }, { "value" : "Acide mélissique", "unit" : "g", "iu": false }, -{ "value" : "Unsaturated fat", "unit" : "", "iu": false }, +{ "value" : "Unsaturated fat", "unit" : "g", "iu": false }, { "value" : "Acides gras monoinsaturés", "unit" : "g", "iu": false }, { "value" : "Acides gras Oméga 9", "unit" : "mg", "iu": false }, { "value" : "Acides gras polyinsaturés", "unit" : "g", "iu": false }, diff --git a/tests/integration/expected_test_results/web_html/fr-product-2.html b/tests/integration/expected_test_results/web_html/fr-product-2.html index a294b67b61335..dab3c29cedc97 100644 --- a/tests/integration/expected_test_results/web_html/fr-product-2.html +++ b/tests/integration/expected_test_results/web_html/fr-product-2.html @@ -712,10 +712,10 @@

NutritionLes points pour les protéines ne sont pas comptés car les points négatifs sont supérieurs ou égaux à 11.

- Score nutritionnel : + Score nutritionnel : 12 (16 - 4)

-

Nutri-Score :

+

Nutri-Score : D

diff --git a/tests/integration/expected_test_results/web_html/fr-product.html b/tests/integration/expected_test_results/web_html/fr-product.html index 01a77c5997ca0..a1d1316a6d392 100644 --- a/tests/integration/expected_test_results/web_html/fr-product.html +++ b/tests/integration/expected_test_results/web_html/fr-product.html @@ -727,10 +727,10 @@

NutritionLes points pour les protéines ne sont pas comptés car les points négatifs sont supérieurs ou égaux à 11.

- Score nutritionnel : + Score nutritionnel : 11 (15 - 4)

-

Nutri-Score :

+

Nutri-Score : D

diff --git a/tests/integration/expected_test_results/web_html/fr-search-form.html b/tests/integration/expected_test_results/web_html/fr-search-form.html index 93bc8a5261ac5..a1c8bef46f283 100644 --- a/tests/integration/expected_test_results/web_html/fr-search-form.html +++ b/tests/integration/expected_test_results/web_html/fr-search-form.html @@ -326,13 +326,6 @@

Recherche de produits

- -
-
- - -
-
diff --git a/tests/integration/expected_test_results/web_html/world-edit-product.html b/tests/integration/expected_test_results/web_html/world-edit-product.html index 867ee91464ddd..b12f507accb31 100644 --- a/tests/integration/expected_test_results/web_html/world-edit-product.html +++ b/tests/integration/expected_test_results/web_html/world-edit-product.html @@ -2055,7 +2055,7 @@

- + @@ -2279,7 +2279,7 @@

- + @@ -3897,7 +3897,7 @@

Discover the project

{ "value" : "Cerotic acid", "unit" : "g", "iu": false }, { "value" : "Montanic acid", "unit" : "g", "iu": false }, { "value" : "Melissic acid", "unit" : "g", "iu": false }, -{ "value" : "Unsaturated fat", "unit" : "", "iu": false }, +{ "value" : "Unsaturated fat", "unit" : "g", "iu": false }, { "value" : "Monounsaturated fat", "unit" : "g", "iu": false }, { "value" : "Omega 9 fat", "unit" : "mg", "iu": false }, { "value" : "Polyunsaturated fat", "unit" : "g", "iu": false }, diff --git a/tests/integration/expected_test_results/web_html/world-search-form.html b/tests/integration/expected_test_results/web_html/world-search-form.html index e4d8ba20d5527..48510f9d52d48 100644 --- a/tests/integration/expected_test_results/web_html/world-search-form.html +++ b/tests/integration/expected_test_results/web_html/world-search-form.html @@ -326,13 +326,6 @@

Products search

- -
-
- - -
-
diff --git a/tests/unit/display.t b/tests/unit/display.t index fd5f01d0da769..94d87c688d52c 100644 --- a/tests/unit/display.t +++ b/tests/unit/display.t @@ -14,6 +14,7 @@ use ProductOpener::Lang qw/$lc lang separator_before_colon/; # date tests my $t = 1472292529; +$cc = 'world'; $lc = 'en'; is(display_date($t), 'August 27, 2016, 12:08:49 PM CEST'); is(display_date_tag($t), ''); @@ -88,42 +89,48 @@ is($request_ref->{structured_response}[3]{id}, 'mandatory'); is($request_ref->{structured_response}[3]{factor}, 4); is($request_ref->{structured_response}[3]{minimum_match}, 20); -my $nutriscore_data_ref = { - 'negative_points' => 8, - 'proteins_points' => 2, - 'proteins' => '3.9', - 'sodium_points' => 1, - 'sugars_value' => 15, - 'positive_points' => 2, - 'is_water' => 0, - 'fruits_vegetables_nuts_colza_walnut_olive_oils_points' => 0, - 'fruits_vegetables_nuts_colza_walnut_olive_oils' => 0, - 'energy_points' => 1, - 'fruits_vegetables_nuts_colza_walnut_olive_oils_value' => 0, - 'fiber_value' => 0, - 'sugars' => 15, - 'is_fat' => 0, - 'proteins_value' => '3.9', - 'is_beverage' => 0, - 'sodium' => 160, - 'score' => 6, - 'saturated_fat_ratio' => 70, - 'energy' => 573, - 'fiber' => 0, - 'saturated_fat' => '3.5', - 'grade' => 'c', - 'saturated_fat_ratio_value' => 70, - 'saturated_fat_value' => '3.5', - 'sugars_points' => 3, - 'sodium_value' => 160, - 'fiber_points' => 0, - 'is_cheese' => 0, - 'energy_value' => 573, - 'saturated_fat_ratio_points' => 10, - 'saturated_fat_points' => 3 +my $product_nutriscore_data_ref = { + nutriscore => { + "2021" => { + 'score' => 6, + 'grade' => 'c', + data => { + 'negative_points' => 8, + 'proteins_points' => 2, + 'proteins' => '3.9', + 'sodium_points' => 1, + 'sugars_value' => 15, + 'positive_points' => 2, + 'is_water' => 0, + 'fruits_vegetables_nuts_colza_walnut_olive_oils_points' => 0, + 'fruits_vegetables_nuts_colza_walnut_olive_oils' => 0, + 'energy_points' => 1, + 'fruits_vegetables_nuts_colza_walnut_olive_oils_value' => 0, + 'fiber_value' => 0, + 'sugars' => 15, + 'is_fat' => 0, + 'proteins_value' => '3.9', + 'is_beverage' => 0, + 'sodium' => 160, + 'saturated_fat_ratio' => 70, + 'energy' => 573, + 'fiber' => 0, + 'saturated_fat' => '3.5', + 'saturated_fat_ratio_value' => 70, + 'saturated_fat_value' => '3.5', + 'sugars_points' => 3, + 'sodium_value' => 160, + 'fiber_points' => 0, + 'is_cheese' => 0, + 'energy_value' => 573, + 'saturated_fat_ratio_points' => 10, + 'saturated_fat_points' => 3 + } + } + } }; -my $nutriscore_calculation_detail = display_nutriscore_calculation_details($nutriscore_data_ref); +my $nutriscore_calculation_detail = display_nutriscore_calculation_details_2021($product_nutriscore_data_ref); like($nutriscore_calculation_detail, qr/Nutritional score: 6/); like($nutriscore_calculation_detail, qr/Proteins:\n2 <\/strong>\/ 5/); like($nutriscore_calculation_detail, qr/Positive points: 2/);