Skip to content

Commit

Permalink
feat: add actions to panels #6902 (#6903)
Browse files Browse the repository at this point in the history
* feat: add actions to panels #6902

* Update templates/web/panels/panel.tt.html

Co-authored-by: Alex Garel <alex@garel.org>

* suggestions from code review

Co-authored-by: Alex Garel <alex@garel.org>
  • Loading branch information
stephanegigandet and alexgarel committed Jun 20, 2022
1 parent 8690daf commit e03e2e1
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ sub process_template($$$) {
$template_data_ref->{display_date_tag} = \&display_date_tag;
$template_data_ref->{url_for_text} = \&url_for_text;
$template_data_ref->{product_url} = \&product_url;
$template_data_ref->{product_action_url} = \&product_action_url;
$template_data_ref->{product_name_brand_quantity} = \&product_name_brand_quantity;

# Display one taxonomy entry in the target language
Expand Down Expand Up @@ -7538,8 +7539,8 @@ CSS
# Also activate them for moderators
if (($User{moderator}) or (param('panels'))) {
create_knowledge_panels($product_ref, $lc, $cc, $knowledge_panels_options_ref);
$template_data_ref->{environment_card_panel} = display_knowledge_panel($product_ref->{"knowledge_panels_" . $lc}, "environment_card");
$template_data_ref->{health_card_panel} = display_knowledge_panel($product_ref->{"knowledge_panels_" . $lc}, "health_card");
$template_data_ref->{environment_card_panel} = display_knowledge_panel($product_ref, $product_ref->{"knowledge_panels_" . $lc}, "environment_card");
$template_data_ref->{health_card_panel} = display_knowledge_panel($product_ref, $product_ref->{"knowledge_panels_" . $lc}, "health_card");
}

# On the producers platform, show a link to the public platform
Expand Down
94 changes: 94 additions & 0 deletions lib/ProductOpener/Products.pm
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ BEGIN
&product_name_brand
&product_name_brand_quantity
&product_url
&product_action_url
&normalize_search_terms
&index_product
&log_change
Expand Down Expand Up @@ -2336,6 +2337,16 @@ sub product_name_brand_quantity($) {
}


=head2 product_url ( $code_or_ref )
Returns a relative URL for a product on the website.
=head3 Parameters
=head4 Product code or reference to product object $code_or_ref
=cut

sub product_url($) {

my $code_or_ref = shift;
Expand Down Expand Up @@ -2373,6 +2384,89 @@ sub product_url($) {
return "/$path/$code" . $titleid;
}

=head2 product_url ( $code_or_ref )
Returns a relative URL for a product on the website.
=head3 Parameters
=head4 Product code or reference to product object $code_or_ref
=cut

sub product_url($) {

my $code_or_ref = shift;
my $code;
my $ref;

my $product_lc = $lc;

if (ref($code_or_ref) eq 'HASH') {
$ref = $code_or_ref;
$code = $ref->{code};
#if (defined $ref->{lc}) {
# $product_lc = $ref->{lc};
#}
}
else {
$code = $code_or_ref;
}

my $path = $tag_type_singular{products}{$product_lc};
if (not defined $path) {
$path = $tag_type_singular{products}{en};
}

my $titleid = '';
if (defined $ref) {
my $full_name = product_name_brand($ref);
$titleid = get_url_id_for_lang($product_lc, $full_name);
if ($titleid ne '') {
$titleid = '/' . $titleid;
}
}

$code = ($code // "");
return "/$path/$code" . $titleid;
}


=head2 product_action_url ( $code, $action )
Returns a relative URL for an action on a product on the website.
This function is called by the web/panels/panel.tt.html template for knowledge panels that have associated actions.
=head3 Parameters
=head4 Product code or reference to product object $code_or_ref
=cut

sub product_action_url($$) {

my $code = shift;
my $action = shift;

my $url = "/cgi/product.pl?type=edit&code=" . $code;

if ($action eq "add_categories") {
$url .= "#categories";
}
elsif ($action eq "add_ingredients_text") {
$url .= "#ingredients";
}
elsif ($action eq "add_nutrition_facts") {
$url .= "#nutrition";
}
else {
$log->error("unknown product action", { code => $code, action => $action });
}

return $url;
}


sub index_product($)
{
Expand Down
9 changes: 7 additions & 2 deletions lib/ProductOpener/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -417,19 +417,24 @@ sub display_data_quality_description($$) {
}


=head2 display_knowledge_panel( $panels_ref, $panel_id )
=head2 display_knowledge_panel( $product_ref, $panels_ref, $panel_id )
Generate HTML code corresponding to a specific panel.
The code is generated by the web/panels/panel.tt.html template.
=cut

sub display_knowledge_panel($$) {
sub display_knowledge_panel($$$) {

my $product_ref = shift;
my $panels_ref = shift;
my $panel_id = shift;

my $html = '';

my $template_data_ref = {
product => $product_ref,
panels => $panels_ref,
panel_id => $panel_id,
};
Expand Down
25 changes: 25 additions & 0 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6172,3 +6172,28 @@ msgid "Nutri-Score, Eco-Score and food processing level (NOVA)"
msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)"


msgctxt "actions_add_ingredients"
msgid "Could you add the ingredients list?"
msgstr "Could you add the ingredients list?"

msgctxt "actions_to_compute_nutriscore"
msgid "Could you add the information needed to compute the Nutri-Score?"
msgstr "Could you add the information needed to compute the Nutri-Score?"

msgctxt "actions_to_compute_ecoscore"
msgid "Could you categorize the product so that we can compute the Eco-Score?"
msgstr "Could you categorize the product so that we can compute the Eco-Score?"

msgctxt "action_add_ingredients_text"
msgid "Add the ingredients"
msgstr "Add the ingredients"

msgctxt "action_add_categories"
msgid "Add a category"
msgstr "Add a category"

msgctxt "action_add_nutrition_facts"
msgid "Add nutrition facts"
msgstr "Add nutrition facts"


23 changes: 23 additions & 0 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -6194,4 +6194,27 @@ msgctxt "reset_preferences_details"
msgid "Nutri-Score, Eco-Score and food processing level (NOVA)"
msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)"

msgctxt "actions_add_ingredients"
msgid "Could you add the ingredients list?"
msgstr "Could you add the ingredients list?"

msgctxt "actions_to_compute_nutriscore"
msgid "Could you add the information needed to compute the Nutri-Score?"
msgstr "Could you add the information needed to compute the Nutri-Score?"

msgctxt "actions_to_compute_ecoscore"
msgid "Could you categorize the product so that we can compute the Eco-Score?"
msgstr "Could you categorize the product so that we can compute the Eco-Score?"

msgctxt "action_add_ingredients_text"
msgid "Add the ingredients"
msgstr "Add the ingredients"

msgctxt "action_add_categories"
msgid "Add a category"
msgstr "Add a category"

msgctxt "action_add_nutrition_facts"
msgid "Add nutrition facts"
msgstr "Add nutrition facts"

23 changes: 23 additions & 0 deletions po/common/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -6040,3 +6040,26 @@ msgctxt "reset_preferences_details"
msgid "Nutri-Score, Eco-Score and food processing level (NOVA)"
msgstr "Nutri-Score, Eco-Score et niveau de transformation alimentaire (NOVA)"

msgctxt "actions_add_ingredients"
msgid "Could you add the ingredients list?"
msgstr "Pourriez-vous ajouter la liste des ingrédients ?"

msgctxt "actions_to_compute_nutriscore"
msgid "Could you add the information needed to compute the Nutri-Score?"
msgstr "Pourriez-vous ajouter les informations nécessaires pour calculer le Nutri-Score ?"

msgctxt "actions_to_compute_ecoscore"
msgid "Could you categorize the product so that we can compute the Eco-Score?"
msgstr "Pourriez-vous catégoriser le produit pour que nous puissions calculer l'Eco-Score ?"

msgctxt "action_add_ingredients_text"
msgid "Add the ingredients"
msgstr "Ajouter les ingrédients"

msgctxt "action_add_categories"
msgid "Add a category"
msgstr "Ajouter une catégorie"

msgctxt "action_add_nutrition_facts"
msgid "Add nutrition facts"
msgstr "Ajouter les informations nutritionnelles"
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
"type": "summary",
"html": "[% lang("ecoscore_unknown_call_to_help") %]"
}
},
},
{
"element_type": "action",
"action_element": {
"html": `[% lang("actions_to_compute_ecoscore") %]`,
"actions": [
"add_categories",
]
}
},
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"icon_color_from_evaluation": true,
},
"elements": [
// Display the ingredients list if we have one
[% IF product.ingredients_n.defined && product.ingredients_n > 0 %]
{
"element_type": "text",
"text_element": {
Expand All @@ -23,6 +25,17 @@
"edit_field_value": `[% panel.ingredients_text %]`,
},
},
[% ELSE %]
{
"element_type": "action",
"action_element": {
"html": `[% lang("actions_add_ingredients") %]`,
"actions": [
"add_ingredients_text"
]
}
},
[% END %]
[% IF product.allergens_tags && product.allergens_tags.size %]
{
"element_type": "text",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"grade": "[% panel.nutriscore_grade %]",
},
"elements": [
// First display warnings
[% IF panel.nutriscore_warnings %]
[% FOREACH warning IN panel.nutriscore_warnings %]
{
Expand All @@ -24,7 +25,26 @@
}
},
[% END %]
[% END %]
[% END %]
// Display actions if we are missing data to compute the Nutri-Score
[% IF panel.nutriscore_unknown_reason %]
{
"element_type": "action",
"action_element": {
"html": `[% lang("actions_to_compute_nutriscore") %]`,
"actions": [
[% IF panel.nutriscore_unknown_reason == "missing_category" %]
"add_categories"
[% ELSIF panel.nutriscore_unknown_reason == "missing_nutrition_data" %]
"add_nutrition_facts"
[% ELSIF panel.nutriscore_unknown_reason == "missing_category_and_nutrition_data" %]
"add_categories",
"add_nutrition_facts"
[% END %]
]
}
},
[% END %]
{
"element_type": "text",
"text_element": {
Expand Down
15 changes: 15 additions & 0 deletions templates/web/panels/panel.tt.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,21 @@ <h4 class="panel_title">
<hr class="floatclear">
[% END %]

[% ELSIF element_type == "action" %]
<div>
[% action_element = element_ref.action_element %]

[% action_element.html %]

[% FOREACH action IN action_element.actions %]
<a class="button small action-[% action %]"
href="[% product_action_url(product.code, action) %]">
[% lang("action_$action") %]
</a>
[% END %]

</div>

[% ELSIF element_type == "image" %]
[% image_element = element_ref.image_element %]
[% IF image_element.link_url.defined %]<a href="[% image_element.link_url %]">[% END %]
Expand Down

0 comments on commit e03e2e1

Please sign in to comment.