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

fix: allow unchecking checked boxes in product edit form #6203

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 20 additions & 14 deletions lib/ProductOpener/Food.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2576,22 +2576,27 @@ sub assign_nutriments_values_from_request_parameters($$) {

$log->debug("Nutrition data") if $log->is_debug();

if (defined param("no_nutrition_data")) {
$product_ref->{no_nutrition_data} = remove_tags_and_quote(decode utf8=>param("no_nutrition_data"));
}
# Note: browsers do not send any value for checkboxes that are unchecked,
# so the web form also has a field (suffixed with _displayed) to allow us to uncheck the box.

my $no_nutrition_data = 0;
if ((defined $product_ref->{no_nutrition_data}) and ($product_ref->{no_nutrition_data} eq 'on')) {
$no_nutrition_data = 1;
}
# API:
# - check: no_nutrition_data is passed "on" or 1
# - uncheck: no_nutrition_data is passed an empty value ""
# - no action: the no_nutrition_data field is not sent, and no_nutrition_data_displayed is not sent
#
# Web:
# - check: no_nutrition_data is passed "on"
# - uncheck: no_nutrition_data is not sent but no_nutrition_data_displayed is sent

if (defined param("nutrition_data")) {
$product_ref->{nutrition_data} = remove_tags_and_quote(decode utf8=>param("nutrition_data"));
}
foreach my $checkbox ("no_nutrition_data", "nutrition_data", "nutrition_data_prepared") {

if (defined param("nutrition_data_prepared")) {
$product_ref->{nutrition_data_prepared} = remove_tags_and_quote(decode utf8=>param("nutrition_data_prepared"));
}
if (defined param($checkbox)) {
$product_ref->{$checkbox} = remove_tags_and_quote(decode utf8=>param($checkbox));
}
elsif (defined param($checkbox . "_displayed")) {
$product_ref->{$checkbox} = "";
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool


# Assign all the nutrient values

Expand Down Expand Up @@ -2724,7 +2729,8 @@ sub assign_nutriments_values_from_request_parameters($$) {
}
}

if ($no_nutrition_data) {
if ((defined $product_ref->{no_nutrition_data}) and ($product_ref->{no_nutrition_data} eq 'on')) {

# Delete all non-carbon-footprint nids.
foreach my $key (keys %{$product_ref->{nutriments}}) {
next if $key =~ /_/;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@

<!--nutrient fieldset-->
<div class="fieldset" id="nutrition"><legend>[% lang('nutrition_data') %]</legend>
<!-- extra field suffixed with _displayed to allow detecting when a box is unchecked -->
<input type="hidden" name="no_nutrition_data_displayed" value="1" />
<input type="checkbox" id="no_nutrition_data" name="no_nutrition_data" [% nutrition_checked %] />
<label for="no_nutrition_data" class="checkbox_label">[% lang('no_nutrition_data') %]</label><br/>
[% display_tab_nutrition_image %]
[% display_field_serving_size %]

[% FOREACH nutrient IN nutrition_products %]
<!-- extra field suffixed with _displayed to allow detecting when a box is unchecked -->
<input type="hidden" name="[% nutrient.nutrition_data %]_displayed" value="1" />
<input type="checkbox" id="[% nutrient.nutrition_data %]" name="[% nutrient.nutrition_data %]" [% nutrient.checked %] />
<label for="[% nutrient.nutrition_data %]" class="checkbox_label">[% nutrient.nutrition_data_exists %]</label> &nbsp;
<input type="radio" id="[% nutrient.nutrition_data_per %]_100g" value="100g" name="[% nutrient.nutrition_data_per %]" [% nutrient.checked_per_100g %] />
Expand All @@ -116,7 +120,7 @@

<div style="position:relative">

<table id="nutrition_data_table" class="data_table" style = "[% tablestyle %]" aria-label="nutrition table">
<table id="nutrition_data_table" class="data_table" style="[% tablestyle %]" aria-label="nutrition table">
<thead class="nutriment_header">
<th id="col_1">
[% lang('nutrition_data_table') %]
Expand Down