Skip to content

Commit

Permalink
display knowledge panels based on flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed Apr 25, 2024
1 parent 9f4f066 commit 7fa8230
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 48 deletions.
15 changes: 7 additions & 8 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4358,8 +4358,7 @@ sub display_search_results ($request_ref) {
$current_link =~ s/^\&/\?/;
$current_link = "/search" . $current_link;

if ((defined single_param("user_preferences")) and (single_param("user_preferences")) and not($request_ref->{api}))
{
if (not($request_ref->{api})) {

# The results will be filtered and ranked on the client side

Expand Down Expand Up @@ -4901,7 +4900,7 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa
$limit = $request_ref->{page_size};
}
# If user preferences are turned on, return 100 products per page
elsif ((not defined $request_ref->{api}) and ($request_ref->{user_preferences})) {
elsif (not defined $request_ref->{api}) {
$limit = 100;
}
else {
Expand Down Expand Up @@ -5065,7 +5064,6 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa
{
$fields_ref = {};
}
# - if we use user preferences, we need a lot of fields to compute product attributes: load them all
elsif ($request_ref->{user_preferences}) {
# we restrict the fields that are queried to MongoDB, and use the basic ones and those necessary
# by Attributes.pm to compute attributes.
Expand Down Expand Up @@ -5340,7 +5338,7 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa
# For non API queries with user preferences, we need to add attributes
# For non API queries, we need to compute attributes for personal search
my $fields;
if ((not defined $request_ref->{api}) and ($request_ref->{user_preferences})) {
if (not defined $request_ref->{api}) {
$fields = "code,product_display_name,url,image_front_small_url,attribute_groups";
}
else {
Expand Down Expand Up @@ -5394,8 +5392,6 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa
$html =~ s/(href|src)=("\/)/$1="$formatted_subdomain\//g;
}

if ($request_ref->{user_preferences}) {

my $preferences_text
= sprintf(lang("classify_the_d_products_below_according_to_your_preferences"), $page_count);

Expand Down Expand Up @@ -5436,7 +5432,7 @@ rank_and_display_products("#search_results", products, contributor_prefs);
JS
;

}


process_template('web/common/includes/list_of_products.tt.html', $template_data_ref, \$html)
|| return "template error: " . $tt->error();
Expand Down Expand Up @@ -7672,6 +7668,9 @@ JS
= display_knowledge_panel($product_ref, $product_ref->{"knowledge_panels_" . $lc}, "contribution_card");
}

# User preferences
$template_data_ref->{user_preferences} = $request_ref->{user_preferences};

# The front product image is rendered with the same template as the ingredients, nutrition and packaging images
# that are displayed directly through the knowledge panels
$template_data_ref->{front_image} = data_to_display_image($product_ref, "front", $lc);
Expand Down
99 changes: 64 additions & 35 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,20 @@ sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref)
}

# Create recommendation panels first, as they will be included in cards such has the health card and environment card
create_recommendation_panels($product_ref, $target_lc, $target_cc, $options_ref);
if ($options{product_type} eq "food") {
create_recommendation_panels($product_ref, $target_lc, $target_cc, $options_ref);
}

my $has_health_card;
if ( ($options{product_type} eq "food")
or ($options{product_type} eq "pet_food")
or ($options{product_type} eq "beauty"))
{
$has_health_card = create_health_card_panel($product_ref, $target_lc, $target_cc, $options_ref);
}

create_health_card_panel($product_ref, $target_lc, $target_cc, $options_ref);
create_environment_card_panel($product_ref, $target_lc, $target_cc, $options_ref);

my $has_report_problem_card;
if (not $options_ref->{producers_platform}) {
$has_report_problem_card = create_report_problem_card_panel($product_ref, $target_lc, $target_cc, $options_ref);
Expand All @@ -208,6 +218,7 @@ sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref)
"root",
"api/knowledge-panels/root.tt.json",
{
has_health_card => $has_health_card,
has_report_problem_card => $has_report_problem_card,
has_contribution_card => $has_contribution_card
},
Expand Down Expand Up @@ -679,6 +690,8 @@ sub create_ecoscore_panel ($product_ref, $target_lc, $target_cc, $options_ref) {
Creates a knowledge panel card that contains all knowledge panels related to the environment.
Created for all products (with at least a packaging panel).
=head3 Arguments
=head4 product reference $product_ref
Expand All @@ -703,17 +716,18 @@ sub create_environment_card_panel ($product_ref, $target_lc, $target_cc, $option
my $panel_data_ref = {};

# Create Eco-Score related panels
create_ecoscore_panel($product_ref, $target_lc, $target_cc, $options_ref);
if ($options{product_type} eq "food") {
create_ecoscore_panel($product_ref, $target_lc, $target_cc, $options_ref);

# Create panel for palm oil
if ( (defined $product_ref->{ecoscore_data})
and (defined $product_ref->{ecoscore_data}{adjustments})
and (defined $product_ref->{ecoscore_data}{adjustments}{threatened_species})
and ($product_ref->{ecoscore_data}{adjustments}{threatened_species}{value} != 0))
{
if ( (defined $product_ref->{ecoscore_data})
and (defined $product_ref->{ecoscore_data}{adjustments})
and (defined $product_ref->{ecoscore_data}{adjustments}{threatened_species})
and ($product_ref->{ecoscore_data}{adjustments}{threatened_species}{value} != 0))
{

create_panel_from_json_template("palm_oil", "api/knowledge-panels/environment/palm_oil.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
create_panel_from_json_template("palm_oil", "api/knowledge-panels/environment/palm_oil.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
}
}

# Create panel for packaging components, and packaging materials
Expand All @@ -730,10 +744,15 @@ sub create_environment_card_panel ($product_ref, $target_lc, $target_cc, $option
# Create panel for manufacturing place
create_manufacturing_place_panel($product_ref, $target_lc, $target_cc, $options_ref);

# Origins of ingredients for the environment card
create_panel_from_json_template("origins_of_ingredients",
"api/knowledge-panels/environment/origins_of_ingredients.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
# Origins of ingredients for the environment card, for food, pet food and beauty products
if ( ($options{product_type} eq "food")
or ($options{product_type} eq "pet_food")
or ($options{product_type} eq "beauty"))
{
create_panel_from_json_template("origins_of_ingredients",
"api/knowledge-panels/environment/origins_of_ingredients.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
}

# Create the environment_card panel
$panel_data_ref->{packaging_image} = data_to_display_image($product_ref, "packaging", $target_lc),
Expand Down Expand Up @@ -799,6 +818,8 @@ sub create_manufacturing_place_panel ($product_ref, $target_lc, $target_cc, $opt
Creates a knowledge panel card that contains all knowledge panels related to health.
This panel card is created for food, pet food, and beauty products.
=head3 Arguments
=head4 product reference $product_ref
Expand All @@ -822,28 +843,36 @@ sub create_health_card_panel ($product_ref, $target_lc, $target_cc, $options_ref

$log->debug("create health card panel", {code => $product_ref->{code}}) if $log->is_debug();

create_nutriscore_panel($product_ref, $target_lc, $target_cc, $options_ref);
if ($options_ref->{admin} || $options_ref->{moderator} || $options_ref->{producers_platform}) {
create_nutriscore_2023_panel($product_ref, $target_lc, $target_cc, $options_ref);
}

create_nutrient_levels_panels($product_ref, $target_lc, $target_cc, $options_ref);

create_nutrition_facts_table_panel($product_ref, $target_lc, $target_cc, $options_ref);
# All food, pet food and beauty products have ingredients
create_ingredients_panel($product_ref, $target_lc, $target_cc, $options_ref);

if ($options_ref->{activate_knowledge_panel_physical_activities}) {
create_physical_activities_panel($product_ref, $target_lc, $target_cc, $options_ref);
# Show additives only for food and pet food
if (($options{product_type} eq "food") or ($options{product_type} eq "pet_food")) {
create_additives_panel($product_ref, $target_lc, $target_cc, $options_ref);
}

create_serving_size_panel($product_ref, $target_lc, $target_cc, $options_ref);
create_ingredients_analysis_panel($product_ref, $target_lc, $target_cc, $options_ref);

create_ingredients_panel($product_ref, $target_lc, $target_cc, $options_ref);
# Scores for food products
if ($options{product_type} eq "food") {
create_nova_panel($product_ref, $target_lc, $target_cc, $options_ref);
create_nutriscore_panel($product_ref, $target_lc, $target_cc, $options_ref);
if ($options_ref->{admin} || $options_ref->{moderator} || $options_ref->{producers_platform}) {
create_nutriscore_2023_panel($product_ref, $target_lc, $target_cc, $options_ref);
}

create_additives_panel($product_ref, $target_lc, $target_cc, $options_ref);
create_nutrient_levels_panels($product_ref, $target_lc, $target_cc, $options_ref);

create_ingredients_analysis_panel($product_ref, $target_lc, $target_cc, $options_ref);
if ($options_ref->{activate_knowledge_panel_physical_activities}) {
create_physical_activities_panel($product_ref, $target_lc, $target_cc, $options_ref);
}
}

create_nova_panel($product_ref, $target_lc, $target_cc, $options_ref);
# Nutrition facts for food and pet food
if (($options{product_type} eq "food") or ($options{product_type} eq "pet_food")) {
create_serving_size_panel($product_ref, $target_lc, $target_cc, $options_ref);
create_nutrition_facts_table_panel($product_ref, $target_lc, $target_cc, $options_ref);
}

my $panel_data_ref = {
ingredients_image => data_to_display_image($product_ref, "ingredients", $target_lc),
Expand All @@ -852,7 +881,8 @@ sub create_health_card_panel ($product_ref, $target_lc, $target_cc, $options_ref

create_panel_from_json_template("health_card", "api/knowledge-panels/health/health_card.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);
return;

return 1;
}

=head2 create_nutriscore_panel ( $product_ref, $target_lc, $target_cc, $options_ref )
Expand Down Expand Up @@ -1597,9 +1627,8 @@ sub create_nova_panel ($product_ref, $target_lc, $target_cc, $options_ref) {

my $panel_data_ref = {};

# Do not display the Nutri-Score panel if it is not applicable
if ( (defined $options{product_type})
and ($options{product_type} eq "food")
# Do not display the NOVA panel if it is not applicable
if ( ($options{product_type} eq "food")
and (exists $product_ref->{nova_groups_tags})
and (not $product_ref->{nova_groups_tags}[0] eq "not-applicable"))
{
Expand All @@ -1608,7 +1637,7 @@ sub create_nova_panel ($product_ref, $target_lc, $target_cc, $options_ref) {
$panel_data_ref->{nova_group_name}
= display_taxonomy_tag($target_lc, "nova_groups", $product_ref->{nova_groups_tags}[0]);

# Nutri-Score panel: score + details
# NOVA panel: score + details
create_panel_from_json_template("nova", "api/knowledge-panels/health/ingredients/nova.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref);

Expand Down
5 changes: 5 additions & 0 deletions lib/ProductOpener/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ sub display_knowledge_panel ($product_ref, $panels_ref, $panel_id) {

my $html = '';

# Return undef if there is no panel with the given id
if (not defined $panels_ref->{$panel_id}) {
return;
}

my $template_data_ref = {
product => $product_ref,
panels => $panels_ref,
Expand Down
8 changes: 6 additions & 2 deletions templates/api/knowledge-panels/health/health_card.tt.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
[% IF options.knowledge_panels_client == 'web' %]

[% INCLUDE "api/knowledge-panels/health/ingredients_panels.tt.json" %]
[% INCLUDE "api/knowledge-panels/health/nutrition_panels.tt.json" %]
[% IF product_type == 'food' || product_type == 'pet_food' %]
[% INCLUDE "api/knowledge-panels/health/nutrition_panels.tt.json" %]
[% END %]

[% ELSE %]

[% INCLUDE "api/knowledge-panels/health/nutrition_panels.tt.json" %]
[% IF product_type == 'food' || product_type == 'pet_food' %]
[% INCLUDE "api/knowledge-panels/health/nutrition_panels.tt.json" %]
[% END %]
[% INCLUDE "api/knowledge-panels/health/ingredients_panels.tt.json" %]

[% END %]
Expand Down
13 changes: 10 additions & 3 deletions templates/web/pages/product/product_page.tt.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ <h1 class="title-3" property="food:name" itemprop="name">[% title %]</h1>
<nav id="navbar" class="navbar h-space-tiny">
<ul class="inline-list">
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#product"><span>[% lang("product") %]</span></a></li>
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#match"><span>[% lang("your_criteria") %]</span></a></li>
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#health"><span>[% lang("health") %]</span></a></li>
[% IF user_preferences %]
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#match"><span>[% lang("your_criteria") %]</span></a></li>
[% END %]
[% IF health_card_panel %]
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#health"><span>[% lang("health") %]</span></a></li>
[% END %]
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#environment"><span>[% lang("environment") %]</span></a></li>
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#report_problem"><span>[% lang("report_problem_navigation") %]</span></a></li>
<li class="product-section-button"><a class="nav-link scrollto button small round white-button" href="#contribution"><span>[% lang("contribution_navigation") %]</span></a></li>
Expand Down Expand Up @@ -181,7 +185,7 @@ <h2 class="title-1" property="food:name" itemprop="name">[% title %]</h1>
</div>
</section>


[% IF user_preferences %]
<!-- product summary -->
<section class="row" id="match">
<div class="large-12 column">
Expand All @@ -202,7 +206,9 @@ <h2 id="match_title">[% lang('matching_with_your_preferences') %]</h2>

</div>
</section>
[% END %]

[% IF health_card_panel %]
<section class="row" id="health">
<div class="large-12 column">
<div class="card">
Expand All @@ -212,6 +218,7 @@ <h2 id="match_title">[% lang('matching_with_your_preferences') %]</h2>
</div>
</div>
</section>
[% END %]

<section class="row" id="environment">
<div class="large-12 column">
Expand Down
2 changes: 2 additions & 0 deletions templates/web/pages/search_results/search_results.tt.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<!-- start templates/[% template.name %] -->

[% IF user_preferences %]
<div id="preferences_selected"></div>

<div id="preferences_selection_form" style="display:none"></div>
[% END %]

<div id="search_results" style="clear:left">
[% lang("products_are_being_loaded_please_wait") %]
Expand Down

0 comments on commit 7fa8230

Please sign in to comment.