Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Editing packaging components on web site #7755

Merged
merged 52 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
c7da7e8
start of packagings edit interface
stephanegigandet Nov 16, 2022
fd55285
Merge branch 'main' into packagings-edit
stephanegigandet Nov 18, 2022
3853ded
select2 for packaging properties
stephanegigandet Nov 18, 2022
46ef946
styling of packaging form
stephanegigandet Nov 21, 2022
fc92546
packaging edit
stephanegigandet Nov 22, 2022
0b551e6
Merge branch 'main' into packagings-edit
stephanegigandet Nov 23, 2022
e33111c
packaging components
stephanegigandet Nov 24, 2022
9bcd05c
packaging components
stephanegigandet Nov 24, 2022
a093636
packagings components edit
stephanegigandet Nov 24, 2022
06fe04e
packagings components edit
stephanegigandet Nov 24, 2022
1ea52f8
Merge branch 'main' into packagings-edit
stephanegigandet Nov 24, 2022
d3d45e7
update tests
stephanegigandet Nov 24, 2022
a50282d
edit packagings
stephanegigandet Nov 24, 2022
b951b99
Merge branch 'main' into packagings-edit
stephanegigandet Nov 25, 2022
aad1557
lint
stephanegigandet Nov 25, 2022
8abaf25
utf8 fix
stephanegigandet Nov 25, 2022
9954e2c
scss fix
stephanegigandet Nov 25, 2022
962f839
add packagings_complete
stephanegigandet Nov 25, 2022
3fdeaad
Merge branch 'main' into packagings-edit
stephanegigandet Nov 25, 2022
3a3015d
update tests
stephanegigandet Nov 25, 2022
cce8bdb
lint
stephanegigandet Nov 25, 2022
3b6294e
perlcritic
stephanegigandet Nov 25, 2022
2d08c77
perlcritic
stephanegigandet Nov 25, 2022
7142a7a
escape existing value
stephanegigandet Nov 25, 2022
d2a865f
remove unused var
stephanegigandet Nov 29, 2022
6a3006c
fix: fields=all returns customized field values
stephanegigandet Nov 29, 2022
826e013
Update lib/ProductOpener/Display.pm
stephanegigandet Nov 29, 2022
aa623ed
Update scss/_off.scss
stephanegigandet Nov 29, 2022
c19de26
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
71a8c07
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
b67d415
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
3545592
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
f988bc6
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
e60af8f
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
0dc5d76
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
99d7e08
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
5486c7d
Update templates/web/pages/product_edit/product_edit_form_packagings.…
stephanegigandet Nov 29, 2022
c8b3ded
more packaging tests
stephanegigandet Nov 29, 2022
306170a
Merge branch 'main' into packagings-edit
stephanegigandet Nov 29, 2022
380d2be
changes from code review
stephanegigandet Nov 29, 2022
a680a18
disable packaging_tags: not used as input for packagings
stephanegigandet Nov 29, 2022
f8116e5
merged fields-all
stephanegigandet Nov 29, 2022
765d1fa
lint
stephanegigandet Nov 29, 2022
0c9eb26
update tests
stephanegigandet Nov 29, 2022
e9c54c6
fix comment
stephanegigandet Nov 30, 2022
f9363c4
disable packagings tags as input
stephanegigandet Nov 30, 2022
2084eaa
Merge branch 'packagings-edit' of github.com:openfoodfacts/openfoodfa…
stephanegigandet Nov 30, 2022
dddae63
update tests
stephanegigandet Nov 30, 2022
34cf044
Merge branch 'main' into packagings-edit
stephanegigandet Nov 30, 2022
66da7f1
update tests
stephanegigandet Nov 30, 2022
dec7616
Merge branch 'main' into packagings-edit
stephanegigandet Nov 30, 2022
2060b08
merge and update tests
stephanegigandet Nov 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,65 @@ ()
return $html;
}

=head2 create_packaging_components_from_request_parameters($product_ref)

Read form parameters related to packaging components, and create the corresponding packagings structure.

=cut

sub create_packaging_components_from_request_parameters ($product_ref) {

# Check that the form is showing inputs for packaging components
if (not defined single_param("packaging_max")) {
return;
}

# The form contains packaging inputs, so we reset the packagings structure
$product_ref->{packagings} = [];

# And then we add each packaging component
for (my $packaging_id = 1; $packaging_id <= single_param("packaging_max"); $packaging_id++) {

my $input_packaging_ref = {};
my $prefix = "packaging_" . $packaging_id . "_";
foreach
my $property ("number_of_units", "shape", "material", "recycling", "quantity_per_unit", "weight_measured")
{
$input_packaging_ref->{$property} = remove_tags_and_quote(decode utf8 => single_param($prefix . $property));
}

my $response_ref = {}; # Currently unused, may be used to display warnings in future versions of the interface

my $packaging_ref
= get_checked_and_taxonomized_packaging_component_data($lc, $input_packaging_ref, $response_ref);

if (defined $packaging_ref) {
apply_rules_to_augment_packaging_component_data($product_ref, $packaging_ref);

push @{$product_ref->{packagings}}, $packaging_ref;

$log->debug(
"added a packaging component",
{
prefix => $prefix,
packaging_id => $packaging_id,
input_packaging => $input_packaging_ref,
packaging => $packaging_ref
}
) if $log->is_debug();
}
}

if (single_param("packagings_complete")) {
$product_ref->{packagings_complete} = 1;
}
else {
$product_ref->{packagings_complete} = 0;
}

return;
}

my $request_ref = ProductOpener::Display::init_request();

if ($User_id eq 'unwanted-user-french') {
Expand Down Expand Up @@ -564,6 +623,9 @@ ()

assign_nutriments_values_from_request_parameters($product_ref, $nutriment_table);

# Process packaging components
create_packaging_components_from_request_parameters($product_ref);

# product check

if ($User{moderator}) {
Expand Down Expand Up @@ -1390,6 +1452,16 @@ ($product_ref, $field, $language)
= display_input_tabs($product_ref, "packaging_image", $product_ref->{sorted_langs}, \%Langs,
\@packaging_fields);

# Add an empty packaging element to the form, that will be hidden and duplicated when the user adds new packaging items,
# and another empty packaging element at the end
if (not defined $product_ref->{packagings}) {
$product_ref->{packagings} = [];
}
my $number_of_packaging_components = scalar @{$product_ref->{packagings}};

unshift(@{$product_ref->{packagings}}, {});
push(@{$product_ref->{packagings}}, {});

# Product check

if ($User{moderator}) {
Expand Down
2 changes: 0 additions & 2 deletions html/js/product-multilingual.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ function select_nutriment(event, ui) {
function add_line() {

$(this).unbind("change");
$(this).unbind("autocompletechange");

var id = parseInt($("#new_max").val(), 10) + 1;
$("#new_max").val(id);
Expand All @@ -195,7 +194,6 @@ function add_line() {
//change: add_line
});

// newline.find(".nutriment_label").bind("autocompletechange", add_line);
newline.find(".nutriment_label").change(add_line);

$(document).foundation('equalizer', 'reflow');
Expand Down
34 changes: 32 additions & 2 deletions lib/ProductOpener/APIProductWrite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,42 @@ sub update_product_fields ($request_ref, $product_ref) {
}

foreach my $input_packaging_ref (@{$value}) {

# Shape, material and recycling
foreach my $property ("shape", "material", "recycling") {
if (defined $input_packaging_ref->{$property}) {

# the API specifies that the property is a hash with either an id or a lc_name field
# (same structure as when the packagings structure is read)
# both will be treated the same way and be canonicalized
# by get_checked_and_taxonomized_packaging_component_data()

if (ref($input_packaging_ref->{$property}) eq 'HASH') {
$input_packaging_ref->{$property} = $input_packaging_ref->{$property}{id}
|| $input_packaging_ref->{$property}{lc_name};
}
else {
add_error(
$response_ref,
{
message => {id => "invalid_type_must_be_object"},
field => {id => $property},
impact => {id => "field_ignored"},
}
);
}
}
}

# Taxonomize the input packaging component data
my $packaging_ref
= get_checked_and_taxonomized_packaging_component_data($request_body_ref->{tags_lc},
$input_packaging_ref, $response_ref);
# Add or combine with the existing packagings components array
add_or_combine_packaging_component_data($product_ref, $packaging_ref, $response_ref);

if (defined $packaging_ref) {
# Add or combine with the existing packagings components array
add_or_combine_packaging_component_data($product_ref, $packaging_ref, $response_ref);
}
}
}
}
Expand Down
64 changes: 60 additions & 4 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ sub process_template ($template_filename, $template_data_ref, $result_content_re
$template_data_ref->{product_action_url} = \&product_action_url;
$template_data_ref->{product_name_brand_quantity} = \&product_name_brand_quantity;

# select2 options generator for all entries in a taxonomy
$template_data_ref->{generate_select2_options_for_taxonomy_to_json} = sub ($tagtype) {
return generate_select2_options_for_taxonomy_to_json($lc, $tagtype);
};

# Return a link to one taxonomy entry in the target language
$template_data_ref->{canonicalize_taxonomy_tag_link} = sub ($tagtype, $tag) {
return canonicalize_taxonomy_tag_link($lc, $tagtype, $tag);
Expand Down Expand Up @@ -1678,7 +1683,7 @@ sub display_list_of_tags ($request_ref, $query_ref) {
if ((defined $request_ref->{tag_prefix}) and ($request_ref->{tag_prefix} ne '')) {
my $prefix = $request_ref->{tag_prefix};
$main_link = add_tag_prefix_to_link($main_link, $prefix);
$log->debug("Found tag prefix for main_link", { request => $request_ref}) if $log->is_debug();
$log->debug("Found tag prefix for main_link", {request => $request_ref}) if $log->is_debug();
}

my %products = (); # number of products by tag, used for histogram of nutrition grades colors
Expand Down Expand Up @@ -2973,7 +2978,7 @@ sub display_tag ($request_ref) {
my $prefix = $request_ref->{tag_prefix};
$request_ref->{current_link} = add_tag_prefix_to_link($request_ref->{current_link}, $prefix);
$request_ref->{world_current_link} = add_tag_prefix_to_link($request_ref->{world_current_link}, $prefix);
$log->debug("Found tag prefix ", { request => $request_ref}) if $log->is_debug();
$log->debug("Found tag prefix ", {request => $request_ref}) if $log->is_debug();
}

$request_ref->{canon_tagid} = $canon_tagid;
Expand Down Expand Up @@ -3042,7 +3047,7 @@ sub display_tag ($request_ref) {
my $prefix = $request_ref->{tag2_prefix};
$request_ref->{current_link} = add_tag_prefix_to_link($request_ref->{current_link}, $prefix);
$request_ref->{world_current_link} = add_tag_prefix_to_link($request_ref->{world_current_link}, $prefix);
$log->debug("Found tag prefix 2 ", { request => $request_ref }) if $log->is_debug();
$log->debug("Found tag prefix 2 ", {request => $request_ref}) if $log->is_debug();
}

$request_ref->{canon_tagid2} = $canon_tagid2;
Expand Down Expand Up @@ -4336,7 +4341,8 @@ sub add_country_and_owner_filters_to_query ($request_ref, $query_ref) {
}
}

$log->debug("result of add_country_and_owner_filters_to_query", { request => $request_ref, query => $query_ref}) if $log->is_debug();
$log->debug("result of add_country_and_owner_filters_to_query", {request => $request_ref, query => $query_ref})
if $log->is_debug();

return;
}
Expand Down Expand Up @@ -11229,4 +11235,54 @@ sub data_to_display_image ($product_ref, $imagetype, $target_lc) {
return $image_ref;
}

=head2 generate_select2_options_for_taxonomy ($target_lc, $tagtype)

Generates an array of taxonomy entries in a specific language, to be used as options
in a select2 input.

See https://select2.org/data-sources/arrays

=head3 Arguments

=head4 Language code $target_lc

=head4 Taxonomy $tagtype

=head3 Return values

- Reference to an array of options

=cut

sub generate_select2_options_for_taxonomy ($target_lc, $tagtype) {

my @entries = ();

# all tags can be retrieved from the $translations_to hash
foreach my $canon_tagid (keys %{$translations_to{$tagtype}}) {
# just_synonyms are not real entries
next if defined $just_synonyms{$tagtype}{$canon_tagid};

push @entries, display_taxonomy_tag($target_lc, $tagtype, $canon_tagid);
}

my @options = ();

foreach my $entry (sort @entries) {
push @options,
{
id => $entry,
text => $entry,
};
}

return \@options;
}

sub generate_select2_options_for_taxonomy_to_json ($target_lc, $tagtype) {

return decode_utf8(
JSON::PP->new->utf8->canonical->encode(generate_select2_options_for_taxonomy($target_lc, $tagtype)));
}

1;
Loading