From d9d9a70872e443e6c7e5f6f2982f3dccc660921c Mon Sep 17 00:00:00 2001 From: Antoine B <56827368+4nt0ineB@users.noreply.github.com> Date: Tue, 2 Jul 2024 17:08:58 +0200 Subject: [PATCH] refactor: global variables removal (#10474) * $admin global var removed * $styles global var removed * $script global var removed * $initjs global var removed * $header global var removed * $cc global var removed * $bodyabout global var removed --- cgi/display_org_table.pl | 2 +- cgi/export_products.pl | 9 +- cgi/generate_sample_import_file.pl | 4 +- cgi/import_file_process.pl | 10 +- cgi/import_file_select_format.pl | 2 +- cgi/import_file_upload.pl | 9 +- cgi/import_photos_upload.pl | 16 +- ...roducts_categories_from_public_database.pl | 8 +- cgi/manifest.pl | 8 +- cgi/opensearch.pl | 6 +- cgi/org.pl | 22 +- cgi/product_image_move.pl | 2 +- cgi/product_image_upload.pl | 2 +- cgi/product_jqm_multilingual.pl | 4 +- cgi/product_multilingual.pl | 86 +++-- cgi/search.pl | 11 +- cgi/top_translators.pl | 6 +- cgi/user.pl | 14 +- lib/ProductOpener/API.pm | 13 +- lib/ProductOpener/APIProductRevert.pm | 2 +- lib/ProductOpener/APIProductServices.pm | 2 +- lib/ProductOpener/APIProductWrite.pm | 2 +- lib/ProductOpener/APITagRead.pm | 5 +- lib/ProductOpener/Display.pm | 330 +++++++++--------- lib/ProductOpener/HTTP.pm | 4 +- lib/ProductOpener/Images.pm | 14 +- lib/ProductOpener/KnowledgePanels.pm | 31 +- lib/ProductOpener/Producers.pm | 2 +- lib/ProductOpener/Routing.pm | 4 +- lib/ProductOpener/Users.pm | 27 +- scripts/export_data.pl | 5 +- scripts/gen_top_tags_per_country.pl | 2 +- scripts/generate_madenearme_page.pl | 13 +- scripts/scanbot.pl | 2 +- ...new_pro_account_admin_notification.tt.html | 2 +- templates/web/pages/tag/tag.tt.html | 2 +- tests/unit/display.t | 2 +- 37 files changed, 362 insertions(+), 323 deletions(-) diff --git a/cgi/display_org_table.pl b/cgi/display_org_table.pl index eb77f6fbbd8df..2d9817e574a1f 100644 --- a/cgi/display_org_table.pl +++ b/cgi/display_org_table.pl @@ -42,7 +42,7 @@ display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 401); } -if ((not $admin) or (not $User{pro_moderator})) { +if ((not $request_ref->{admin}) or (not $User{pro_moderator})) { $log->debug("user does not have permission to view organisation list", {User_id => $User_id}) if $log->is_debug(); display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403); } diff --git a/cgi/export_products.pl b/cgi/export_products.pl index dfadfcdf451c0..6cf0dd558622e 100755 --- a/cgi/export_products.pl +++ b/cgi/export_products.pl @@ -118,7 +118,7 @@ $template_data_ref->{allow_submit} = 1; } - process_template('web/pages/export_products/export_products.tt.html', $template_data_ref, \$html) + process_template('web/pages/export_products/export_products.tt.html', $template_data_ref, \$html, $request_ref) || ($html .= 'template error: ' . $tt->error()); } @@ -150,7 +150,7 @@ $args_ref->{query}{states_tags} = 'en:to-be-exported'; } - if ($admin) { + if ($request_ref->{admin}) { if ((defined single_param("overwrite_owner")) and (single_param("overwrite_owner"))) { $args_ref->{overwrite_owner} = 1; } @@ -171,7 +171,7 @@ $html .= "

" . lang("export_job_import") . " -

"; $html .= "

" . lang("export_job_status_update") . " -

"; - $initjs .= <{initjs} .= <error()); } diff --git a/cgi/generate_sample_import_file.pl b/cgi/generate_sample_import_file.pl index 925bfb46825f8..6cdacdc111e2b 100755 --- a/cgi/generate_sample_import_file.pl +++ b/cgi/generate_sample_import_file.pl @@ -28,7 +28,7 @@ use CGI::Carp qw(fatalsToBrowser); use ProductOpener::Config qw/:all/; -use ProductOpener::Display qw/init_request $admin/; +use ProductOpener::Display qw/init_request/; use ProductOpener::Users qw/:all/; use ProductOpener::Lang qw/lang/; use ProductOpener::Mail qw/:all/; @@ -49,7 +49,7 @@ my $request_ref = ProductOpener::Display::init_request(); # sync CRM -if (defined $Org_id and not $admin and not $User{moderator} and not $User{pro_moderator}) { +if (defined $Org_id and not $request_ref->{admin} and not $User{moderator} and not $User{pro_moderator}) { update_template_download_date($Org_id); } diff --git a/cgi/import_file_process.pl b/cgi/import_file_process.pl index 8ff0797233b4b..a1378b672cf36 100755 --- a/cgi/import_file_process.pl +++ b/cgi/import_file_process.pl @@ -143,7 +143,7 @@ # Default values: use the language and country of the interface my $default_values_ref = { lc => $lc, - countries => $cc, + countries => $request_ref->{cc}, }; $results_ref = convert_file($default_values_ref, $file, $columns_fields_file, $converted_file); @@ -208,12 +208,12 @@ $template_data_ref->{process_import_id} = $import_id; $template_data_ref->{link} = "/cgi/import_file_job_status.pl?file_id=$file_id&import_id=$import_id"; -process_template('web/pages/import_file_process/import_file_process.tt.html', $template_data_ref, \$html); -process_template('web/pages/import_file_process/import_file_process.tt.js', $template_data_ref, \$js); +process_template('web/pages/import_file_process/import_file_process.tt.html', $template_data_ref, \$html, $request_ref); +process_template('web/pages/import_file_process/import_file_process.tt.js', $template_data_ref, \$js, $request_ref); -$initjs .= $js; +$request_ref->{initjs} .= $js; -$scripts .= <{scripts} .= < HTML diff --git a/cgi/import_file_select_format.pl b/cgi/import_file_select_format.pl index 79ee4b492cc38..dda5740324e1f 100755 --- a/cgi/import_file_select_format.pl +++ b/cgi/import_file_select_format.pl @@ -192,7 +192,7 @@ process_template('web/pages/import_file_select_format/import_file_select_format.tt.html', $template_data_ref, \$html); process_template('web/pages/import_file_select_format/import_file_select_format.tt.js', $template_data_ref, \$js); - $initjs .= $js; + $request_ref->{initjs} .= $js; $request_ref->{title} = $title; $request_ref->{content_ref} = \$html; diff --git a/cgi/import_file_upload.pl b/cgi/import_file_upload.pl index 73c717a5d6770..e7164d4689d02 100755 --- a/cgi/import_file_upload.pl +++ b/cgi/import_file_upload.pl @@ -132,12 +132,13 @@ url => "/cgi/import_file_upload.pl", }; - process_template('web/pages/import_file_upload/import_file_upload.tt.html', $template_data_ref, \$html); - process_template('web/pages/import_file_upload/import_file_upload.tt.js', $template_data_ref, \$js); + process_template('web/pages/import_file_upload/import_file_upload.tt.html', + $template_data_ref, \$html, $request_ref); + process_template('web/pages/import_file_upload/import_file_upload.tt.js', $template_data_ref, \$js, $request_ref); - $initjs .= $js; + $request_ref->{initjs} .= $js; - $scripts .= <{scripts} .= < HTML diff --git a/cgi/import_photos_upload.pl b/cgi/import_photos_upload.pl index b66c5538370d0..3285ac5aec5b1 100755 --- a/cgi/import_photos_upload.pl +++ b/cgi/import_photos_upload.pl @@ -87,7 +87,7 @@ $template_data_ref->{i} = $i; $template_data_ref->{add_fields_options} = \@add_fields_options; - $scripts .= <{scripts} .= < @@ -813,7 +815,7 @@ ($product_ref, $field, $language) my $thumb_selectable_size = $thumb_size + 20; - $styles .= <{styles} .= <{obsolete_checked} = $checked; $template_data_ref_display->{display_field_obsolete} - = display_input_field($product_ref, "obsolete_since_date", undef); + = display_input_field($product_ref, "obsolete_since_date", undef, $request_ref); } @@ -895,7 +897,9 @@ ($product_ref, $field, $language) $template_data_ref_display->{product_ref_sorted_langs} = join(',', @{$product_ref->{sorted_langs}}); - sub display_input_tabs ($product_ref, $tabsid, $tabsids_array_ref, $tabsids_hash_ref, $fields_array_ref) { + sub display_input_tabs ($product_ref, $tabsid, $tabsids_array_ref, $tabsids_hash_ref, $fields_array_ref, + $request_ref) + { my $template_data_ref_tab = {}; my @display_tabs; @@ -943,16 +947,18 @@ ($product_ref, $field, $language) if ($field =~ /^(.*)_image/) { my $image_field = $1 . "_" . $display_lc; - $display_div = display_select_crop($product_ref, $image_field, $language); + $display_div = display_select_crop($product_ref, $image_field, $language, $request_ref); } elsif ($field eq 'ingredients_text') { $image_full_id = "ingredients_" . ${display_lc} . "_image_full"; - $display_div = display_input_field($product_ref, $field . "_" . $display_lc, $language); + $display_div + = display_input_field($product_ref, $field . "_" . $display_lc, $language, $request_ref); } else { $log->debug("display_field", {field_name => $field, field_value => $product_ref->{$field}}) if $log->is_debug(); - $display_div = display_input_field($product_ref, $field . "_" . $display_lc, $language); + $display_div + = display_input_field($product_ref, $field . "_" . $display_lc, $language, $request_ref); } push( @@ -999,24 +1005,26 @@ ($product_ref, $field, $language) $template_data_ref_tab->{display_tabs} = \@display_tabs; my $html_tab = ''; - process_template('web/pages/product_edit/display_input_tabs.tt.html', $template_data_ref_tab, \$html_tab) + process_template('web/pages/product_edit/display_input_tabs.tt.html', + $template_data_ref_tab, \$html_tab, $request_ref) or $html_tab = "

" . $tt->error() . "

"; return $html_tab; } $template_data_ref_display->{display_tab_product_picture} - = display_input_tabs($product_ref, "front_image", $product_ref->{sorted_langs}, \%Langs, ["front_image"]); + = display_input_tabs($product_ref, "front_image", $product_ref->{sorted_langs}, + \%Langs, ["front_image"], $request_ref); $template_data_ref_display->{display_tab_product_characteristics} = display_input_tabs($product_ref, "product", $product_ref->{sorted_langs}, - \%Langs, ["product_name", "generic_name"]); + \%Langs, ["product_name", "generic_name"], $request_ref); my @display_fields_arr; foreach my $field (@fields) { # hide packaging field & origins are now displayed below allergens and traces in the ingredients section next if $field eq "origins" || $field eq "packaging"; $log->debug("display_field", {field_name => $field, field_value => $product_ref->{$field}}) if $log->is_debug(); - my $display_field = display_input_field($product_ref, $field, undef); + my $display_field = display_input_field($product_ref, $field, undef, $request_ref); push(@display_fields_arr, $display_field); } @@ -1035,16 +1043,20 @@ ($product_ref, $field, $language) $template_data_ref_display->{nutrition_checked} = $checked; $template_data_ref_display->{display_tab_ingredients_image} = display_input_tabs($product_ref, "ingredients_image", $product_ref->{sorted_langs}, - \%Langs, \@ingredients_fields); - $template_data_ref_display->{display_field_allergens} = display_input_field($product_ref, "allergens", undef); - $template_data_ref_display->{display_field_traces} = display_input_field($product_ref, "traces", undef); - $template_data_ref_display->{display_field_origins} = display_input_field($product_ref, "origins", undef); + \%Langs, \@ingredients_fields, $request_ref); + $template_data_ref_display->{display_field_allergens} + = display_input_field($product_ref, "allergens", undef, $request_ref); + $template_data_ref_display->{display_field_traces} + = display_input_field($product_ref, "traces", undef, $request_ref); + $template_data_ref_display->{display_field_origins} + = display_input_field($product_ref, "origins", undef, $request_ref); $template_data_ref_display->{display_tab_nutrition_image} - = display_input_tabs($product_ref, "nutrition_image", $product_ref->{sorted_langs}, \%Langs, - ["nutrition_image"]); - $template_data_ref_display->{display_field_serving_size} = display_input_field($product_ref, "serving_size", undef); + = display_input_tabs($product_ref, "nutrition_image", $product_ref->{sorted_langs}, + \%Langs, ["nutrition_image"], $request_ref); + $template_data_ref_display->{display_field_serving_size} + = display_input_field($product_ref, "serving_size", undef, $request_ref); - $initjs .= display_select_crop_init($product_ref); + $request_ref->{initjs} .= display_select_crop_init($product_ref); my $hidden_inputs = ''; @@ -1142,7 +1154,7 @@ ($product_ref, $field, $language) if (exists_taxonomy_tag("nutrients", "zz:$nid")) { $nutriment_ref->{name} = display_taxonomy_tag($lc, "nutrients", "zz:$nid"); # We may have a unit specific to the country (e.g. US nutrition facts table using the International Unit for this nutrient, and Europe using mg) - $unit = get_nutrient_unit($nid, $cc); + $unit = get_nutrient_unit($nid, $request_ref->{cc}); } else { if (defined $product_ref->{nutriments}{$nid . "_unit"}) { @@ -1481,7 +1493,7 @@ ($product_ref, $field, $language) $nutriments =~ s/,\n$//s; $other_nutriments =~ s/,\n$//s; - $scripts .= <{scripts} .= < var nutriments = { $nutriments @@ -1499,8 +1511,8 @@ ($product_ref, $field, $language) my @packaging_fields = ("packaging_image", "packaging_text"); $template_data_ref_display->{display_tab_packaging} - = display_input_tabs($product_ref, "packaging_image", $product_ref->{sorted_langs}, \%Langs, - \@packaging_fields); + = display_input_tabs($product_ref, "packaging_image", $product_ref->{sorted_langs}, + \%Langs, \@packaging_fields, $request_ref); # 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 @@ -1536,7 +1548,8 @@ ($product_ref, $field, $language) $template_data_ref_display->{display_product_history} = display_product_history($request_ref, $code, $product_ref); $template_data_ref_display->{product} = $product_ref; - process_template('web/pages/product_edit/product_edit_form_display.tt.html', $template_data_ref_display, \$html) + process_template('web/pages/product_edit/product_edit_form_display.tt.html', + $template_data_ref_display, \$html, $request_ref) or $html = "

" . $tt->error() . "

"; $request_ref->{page_type} = "product_edit"; @@ -1554,7 +1567,7 @@ ($product_ref, $field, $language) $template_data_ref_moderator->{code} = $code; process_template('web/pages/product_edit/product_edit_form_display_user-moderator.tt.html', - $template_data_ref_moderator, \$html) + $template_data_ref_moderator, \$html, $request_ref) or $html = "

" . $tt->error() . "

"; } @@ -1634,12 +1647,13 @@ ($product_ref, $field, $language) $knowledge_panels_options_ref = {}; initialize_knowledge_panels_options($knowledge_panels_options_ref, $request_ref); $knowledge_panels_options_ref->{knowledge_panels_client} = "web"; - create_contribution_card_panel($product_ref, $lc, $cc, $knowledge_panels_options_ref); + create_contribution_card_panel($product_ref, $lc, $request_ref->{cc}, $knowledge_panels_options_ref); $template_data_ref_process->{contribution_card_panel} = display_knowledge_panel($product_ref, $product_ref->{"knowledge_panels_" . $lc}, "contribution_card"); } $template_data_ref_process->{code} = $product_ref->{code}; - process_template('web/pages/product_edit/product_edit_form_process.tt.html', $template_data_ref_process, \$html) + process_template('web/pages/product_edit/product_edit_form_process.tt.html', + $template_data_ref_process, \$html, $request_ref) or $html = "

" . $tt->error() . "

"; } diff --git a/cgi/search.pl b/cgi/search.pl index b77a2eca6db00..332638f9202f0 100755 --- a/cgi/search.pl +++ b/cgi/search.pl @@ -112,7 +112,7 @@ = qw(brands categories packaging labels origins manufacturing_places emb_codes purchase_places stores countries ingredients additives allergens traces nutrition_grades nova_groups ecoscore languages creator editors states); -$admin and push @search_fields, "lang"; +$request_ref->{admin} and push @search_fields, "lang"; my %search_tags_fields = ( packaging => 1, @@ -474,19 +474,19 @@ } - $styles .= <{styles} .= < .select2-results__options { max-height: 400px } CSS ; - $scripts .= <{scripts} .= < HTML ; - $initjs .= <{initjs} .= <error() . "

"; ${$request_ref->{content_ref}} .= $html; diff --git a/cgi/top_translators.pl b/cgi/top_translators.pl index 4d25039569dcd..480ef815d7388 100644 --- a/cgi/top_translators.pl +++ b/cgi/top_translators.pl @@ -38,13 +38,13 @@ # Passing values to the template my $template_data_ref = {}; -$scripts .= <{scripts} .= < SCRIPTS ; -$header .= <
{header} .= <
HEADER ; @@ -92,7 +92,7 @@ }); JS ; -$initjs .= $js; +$request_ref->{initjs} .= $js; my $html; process_template('web/pages/top_translators/top_translators.tt.html', $template_data_ref, \$html) or $html = ''; diff --git a/cgi/user.pl b/cgi/user.pl index 9b174a0bd7149..7bfa09a66aa59 100644 --- a/cgi/user.pl +++ b/cgi/user.pl @@ -70,7 +70,7 @@ $userid = single_param('userid'); # The userid looks like an e-mail - if ($admin and ($userid =~ /\@/)) { + if ($request_ref->{admin} and ($userid =~ /\@/)) { my $user_by_email = retrieve_user_by_email($userid); if (defined $user_by_email) { $userid = $user_by_email->{userid}; @@ -96,7 +96,7 @@ $type = 'add'; } -if (($type =~ /^edit/) and ($User_id ne $userid) and not $admin) { +if (($type =~ /^edit/) and ($User_id ne $userid) and not $request_ref->{admin}) { display_error_and_exit($request_ref, $Lang{error_no_permission}{$lc}, 403); } @@ -114,7 +114,7 @@ # change organization if ($type eq 'edit_owner') { # only admin and pro moderators can change organization freely - if ($admin or $User{pro_moderator}) { + if ($request_ref->{admin} or $User{pro_moderator}) { ProductOpener::Users::check_edit_owner($user_ref, \@errors); } else { @@ -307,7 +307,7 @@ push @{$template_data_ref->{sections}}, {%$contributor_section_ref}; # Admin section - if ($admin) { + if ($request_ref->{admin}) { my $administrator_section_ref = { id => "administrator", name => "Administrator fields", @@ -443,11 +443,11 @@ else { $log->debug("user form - template data", {template_data_ref => $template_data_ref}) if $log->is_debug(); - process_template('web/pages/user_form/user_form_page.tt.html', $template_data_ref, \$html) + process_template('web/pages/user_form/user_form_page.tt.html', $template_data_ref, \$html, $request_ref) or $html = "

" . $tt->error() . "

"; - process_template('web/pages/user_form/user_form.tt.js', $template_data_ref, \$js); + process_template('web/pages/user_form/user_form.tt.js', $template_data_ref, \$js, $request_ref); - $initjs .= $js; + $request_ref->{initjs} .= $js; $request_ref->{title} = lang($type . '_user_' . $action); $request_ref->{content_ref} = \$html; diff --git a/lib/ProductOpener/API.pm b/lib/ProductOpener/API.pm index 4118e2c723503..caee01ea7de41 100644 --- a/lib/ProductOpener/API.pm +++ b/lib/ProductOpener/API.pm @@ -711,7 +711,7 @@ sub customize_response_for_product ($request_ref, $product_ref, $fields_comma_se } # Localize the Eco-Score fields that depend on the country of the request - localize_ecoscore($cc, $product_ref); + localize_ecoscore($request_ref->{cc}, $product_ref); # lets compute each requested field foreach my $field (@fields) { @@ -742,7 +742,7 @@ sub customize_response_for_product ($request_ref, $product_ref, $fields_comma_se # Allow apps to request a HTML nutrition table by passing &fields=nutrition_table_html if ($field eq "nutrition_table_html") { - $customized_product_ref->{$field} = display_nutrition_table($product_ref, undef); + $customized_product_ref->{$field} = display_nutrition_table($product_ref, undef, $request_ref); next; } @@ -750,7 +750,8 @@ sub customize_response_for_product ($request_ref, $product_ref, $fields_comma_se if ($field eq "ecoscore_details_simple_html") { if ((1 or $show_ecoscore) and (defined $product_ref->{ecoscore_data})) { $customized_product_ref->{$field} - = display_ecoscore_calculation_details_simple_html($cc, $product_ref->{ecoscore_data}); + = display_ecoscore_calculation_details_simple_html($request_ref->{cc}, + $product_ref->{ecoscore_data}); } next; } @@ -828,14 +829,14 @@ sub customize_response_for_product ($request_ref, $product_ref, $fields_comma_se # Product attributes requested in a specific language (or data only) if ($field =~ /^attribute_groups_([a-z]{2}|data)$/) { my $target_lc = $1; - compute_attributes($product_ref, $target_lc, $cc, $attributes_options_ref); + compute_attributes($product_ref, $target_lc, $request_ref->{cc}, $attributes_options_ref); $customized_product_ref->{$field} = $product_ref->{$field}; next; } # Product attributes in the $lc language if ($field eq "attribute_groups") { - compute_attributes($product_ref, $lc, $cc, $attributes_options_ref); + compute_attributes($product_ref, $lc, $request_ref->{cc}, $attributes_options_ref); $customized_product_ref->{$field} = $product_ref->{"attribute_groups_" . $lc}; next; } @@ -843,7 +844,7 @@ sub customize_response_for_product ($request_ref, $product_ref, $fields_comma_se # Knowledge panels in the $lc language if ($field eq "knowledge_panels") { initialize_knowledge_panels_options($knowledge_panels_options_ref, $request_ref); - create_knowledge_panels($product_ref, $lc, $cc, $knowledge_panels_options_ref); + create_knowledge_panels($product_ref, $lc, $request_ref->{cc}, $knowledge_panels_options_ref, $request_ref); $customized_product_ref->{$field} = $product_ref->{"knowledge_panels_" . $lc}; next; } diff --git a/lib/ProductOpener/APIProductRevert.pm b/lib/ProductOpener/APIProductRevert.pm index 71cc4c75a332d..2ecb0cff974ac 100644 --- a/lib/ProductOpener/APIProductRevert.pm +++ b/lib/ProductOpener/APIProductRevert.pm @@ -55,7 +55,7 @@ use ProductOpener::Mail qw/send_email_to_admin/; use Encode; -=head2 revert_product_api() +=head2 revert_product_api($request_ref) Process API v3 requests to revert a product to a specific revision. diff --git a/lib/ProductOpener/APIProductServices.pm b/lib/ProductOpener/APIProductServices.pm index 0e49e0281c4db..e70ccbc068195 100644 --- a/lib/ProductOpener/APIProductServices.pm +++ b/lib/ProductOpener/APIProductServices.pm @@ -170,7 +170,7 @@ sub check_product_services_api_input ($request_ref) { return $error; } -=head2 product_services_api() +=head2 product_services_api($request_ref) Process API v3 product services requests. diff --git a/lib/ProductOpener/APIProductWrite.pm b/lib/ProductOpener/APIProductWrite.pm index cd0ebd337b921..abe224347b228 100644 --- a/lib/ProductOpener/APIProductWrite.pm +++ b/lib/ProductOpener/APIProductWrite.pm @@ -353,7 +353,7 @@ sub update_product_fields ($request_ref, $product_ref, $response_ref) { return; } -=head2 write_product_api() +=head2 write_product_api($request_ref) Process API v3 WRITE product requests. diff --git a/lib/ProductOpener/APITagRead.pm b/lib/ProductOpener/APITagRead.pm index 62f9d7b9238cb..7ac927654a2b1 100644 --- a/lib/ProductOpener/APITagRead.pm +++ b/lib/ProductOpener/APITagRead.pm @@ -44,7 +44,7 @@ BEGIN { use vars @EXPORT_OK; use ProductOpener::Config qw/:all/; -use ProductOpener::Display qw/$cc $knowledge_panels_options_ref/; +use ProductOpener::Display qw/$knowledge_panels_options_ref/; use ProductOpener::Users qw/:all/; use ProductOpener::Lang qw/$lc lang/; use ProductOpener::API qw/add_error/; @@ -130,7 +130,8 @@ sub read_tag_api ($request_ref) { initialize_knowledge_panels_options($knowledge_panels_options_ref, $request_ref); my $tag_ref = {}; # Object to store the knowledge panels my $panels_created - = create_tag_knowledge_panels($tag_ref, $lc, $cc, $knowledge_panels_options_ref, $tagtype, $canon_tagid); + = create_tag_knowledge_panels($tag_ref, $lc, $request_ref->{cc}, $knowledge_panels_options_ref, $tagtype, + $canon_tagid); if ($panels_created) { $response_ref->{tag}{knowledge_panels} = $tag_ref->{"knowledge_panels" . "_" . $lc}; diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index 376bf578bbed9..95db6fa3be7c9 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -108,13 +108,6 @@ BEGIN { @search_series - $admin - - $scripts - $initjs - $styles - $header - $original_subdomain $subdomain $formatted_subdomain @@ -123,7 +116,6 @@ BEGIN { $producers_platform_url $test @lcs - $cc $country $tt @@ -220,8 +212,6 @@ use Apache2::Const qw(:http :common); use URI::Find; -my $bodyabout; - my $uri_finder = URI::Find->new( sub ($uri, $orig_uri) { if ($uri =~ /\http/) { @@ -251,7 +241,7 @@ in order to make sure the browser will not serve an old cached version. =head3 Synopsis - $scripts .= <{scripts} .= < HTML ; @@ -377,13 +367,13 @@ sub url_for_text ($textid) { } } -=head2 process_template ( $template_filename , $template_data_ref , $result_content_ref ) +=head2 process_template ( $template_filename , $template_data_ref , $result_content_ref, $request_ref = {} ) Add some functions and variables needed by many templates and process the template with template toolkit. =cut -sub process_template ($template_filename, $template_data_ref, $result_content_ref) { +sub process_template ($template_filename, $template_data_ref, $result_content_ref, $request_ref = {}) { # Add functions and values that are passed to all templates @@ -404,7 +394,7 @@ sub process_template ($template_filename, $template_data_ref, $result_content_re $template_data_ref->{flavor} = $flavor; $template_data_ref->{options} = \%options; $template_data_ref->{product_type} = $options{product_type}; - $template_data_ref->{admin} = $admin; + $template_data_ref->{admin} = $request_ref->{admin}; $template_data_ref->{moderator} = $User{moderator}; $template_data_ref->{pro_moderator} = $User{pro_moderator}; $template_data_ref->{sep} = separator_before_colon($lc); @@ -424,7 +414,7 @@ sub process_template ($template_filename, $template_data_ref, $result_content_re $template_data_ref->{edq} = sub {escape_char(@_, '"')}; # edq as escape_double_quote $template_data_ref->{lang_sprintf} = \&lang_sprintf; $template_data_ref->{lc} = $lc; - $template_data_ref->{cc} = $cc; + $template_data_ref->{cc} = $request_ref->{cc}; $template_data_ref->{display_icon} = \&display_icon; $template_data_ref->{time_t} = time(); $template_data_ref->{display_date_without_time} = \&display_date_without_time; @@ -435,12 +425,15 @@ 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; $template_data_ref->{has_permission} = sub ($permission) { - # Note: we pass a fake $request_ref object with only the fields admin, moderator and pro_moderator - # an alternative would be to pass the $request_ref object to process_template() calls - return has_permission({admin => $admin, moderator => $User{moderator}, pro_moderator => $User{pro_moderator}}, - $permission); + return has_permission($request_ref, $permission); }; + $template_data_ref->{scripts} = $request_ref->{scripts}; + $template_data_ref->{initjs} = $request_ref->{initjs}; + $template_data_ref->{header} = $request_ref->{header}; + $template_data_ref->{styles} = $request_ref->{styles}; + $template_data_ref->{bodyabout} = $request_ref->{bodyabout}; + # 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); @@ -635,17 +628,16 @@ sub init_request ($request_ref = {}) { } # TODO: global variables should be moved to $request_ref - $styles = ''; - $scripts = ''; - $initjs = ''; - $header = ''; - $bodyabout = ''; - $admin = 0; + $request_ref->{styles} = ''; + $request_ref->{scripts} = ''; + $request_ref->{initjs} = ''; + $request_ref->{header} = ''; + $request_ref->{bodyabout} = ''; my $r = Apache2::RequestUtil->request(); $request_ref->{method} = $r->method(); - $cc = 'world'; + my $cc = 'world'; $lc = 'en'; @lcs = (); $country = 'en:world'; @@ -887,13 +879,12 @@ sub init_request ($request_ref = {}) { $request_ref->{user_id} = $User_id; + $request_ref->{admin} = 0; # %admin is defined in Config.pm # admins can change permissions for all users if (is_admin_user($User_id)) { - $admin = 1; + $request_ref->{admin} = 1; } - $request_ref->{admin} = $admin; - # TODO: remove the $admin global variable, and use $request_ref->{admin} instead. $request_ref->{moderator} = $User{moderator}; $request_ref->{pro_moderator} = $User{pro_moderator}; @@ -903,13 +894,13 @@ sub init_request ($request_ref = {}) { if (($server_options{producers_platform}) and not((defined $Owner_id) and (($Owner_id =~ /^org-/) or ($User{moderator}) or $User{pro_moderator}))) { - $styles .= <{styles} .= <{styles} .= <{styles} .= <{styles} .= <error(); return $html; @@ -1332,7 +1324,7 @@ sub display_text ($request_ref) { sub display_stats ($request_ref) { my $textid = $request_ref->{text}; my $stats_dir = "$BASE_DIRS{PUBLIC_DATA}/products_stats/" . $request_ref->{lc}; - my $file = "$stats_dir/products_stats_$cc.html"; + my $file = "$stats_dir/products_stats_$request_ref->{cc}.html"; display_text_content($request_ref, $textid, $request_ref->{lc}, $file); return; } @@ -1347,7 +1339,7 @@ sub display_text_content ($request_ref, $textid, $text_lc, $file) { my $country_name = display_taxonomy_tag($lc, "countries", $country); - $html =~ s//$cc/g; + $html =~ s//$request_ref->{cc}/g; $html =~ s//$country_name/g; my $title = undef; @@ -1372,7 +1364,7 @@ sub display_text_content ($request_ref, $textid, $text_lc, $file) { } $log->debug("displaying text from file", - {cc => $cc, lc => $lc, textid => $textid, text_lc => $text_lc, file => $file}) + {cc => $request_ref->{cc}, lc => $lc, textid => $textid, text_lc => $text_lc, file => $file}) if $log->is_debug(); # if page number is higher than 1, then keep only the h1 header @@ -1421,12 +1413,12 @@ sub display_text_content ($request_ref, $textid, $text_lc, $file) { while ($html =~ /(.*?)<\/scripts>/s) { $html = $` . $'; - $scripts .= $1; + $request_ref->{scripts} .= $1; } while ($html =~ /(.*?)<\/initjs>/s) { $html = $` . $'; - $initjs .= $1; + $request_ref->{initjs} .= $1; } # wikipedia style links [url text] @@ -1520,12 +1512,12 @@ sub display_text_content ($request_ref, $textid, $text_lc, $file) { if ($html =~ /(.*)<\/styles>/s) { $html = $` . $'; - $styles .= $1; + $request_ref->{styles} .= $1; } if ($html =~ /
(.*)<\/header>/s) { $html = $` . $'; - $header .= $1; + $request_ref->{header} .= $1; } if ((defined $request_ref->{page}) and ($request_ref->{page} > 1)) { @@ -1662,7 +1654,7 @@ sub query_list_of_tags ($request_ref, $query_ref) { and ($groupby_tagtype =~ /^(users|correctors|editors|informers|correctors|photographers|checkers)$/)) { - $header .= '' . "\n"; + $request_ref->{header} .= '' . "\n"; } # support for returning json / xml results @@ -1937,7 +1929,7 @@ sub display_list_of_tags ($request_ref, $query_ref) { my $th_nutriments = ''; - my $categories_nutriments_ref = $categories_nutriments_per_country{$cc}; + my $categories_nutriments_ref = $categories_nutriments_per_country{$request_ref->{cc}}; my @cols = (); if ($tagtype eq 'categories') { @@ -2491,9 +2483,9 @@ HTML }); JS ; - $initjs .= $js; + $request_ref->{initjs} .= $js; - $scripts .= <{scripts} .= < SCRIPTS ; @@ -2508,13 +2500,14 @@ HTML # countries map? if (keys %{$countries_map_data} > 0) { - $initjs .= 'var countries_map_data=JSON.parse(' . $json->encode($json->encode($countries_map_data)) . ');' + $request_ref->{initjs} + .= 'var countries_map_data=JSON.parse(' . $json->encode($json->encode($countries_map_data)) . ');' .= 'var countries_map_links=JSON.parse(' . $json->encode($json->encode($countries_map_links)) . ');' .= 'var countries_map_names=JSON.parse(' . $json->encode($json->encode($countries_map_names)) . ');' .= <<"JS"; displayWorldMap('#world-map', { 'data': countries_map_data, 'links': countries_map_links, 'names': countries_map_names }); JS - $scripts .= <{scripts} .= < @@ -2541,7 +2534,7 @@ HTML $extra_column_searchable .= ', {"searchable": false}'; } - $initjs .= <{initjs} .= <{scripts} .= < SCRIPTS ; - $header .= <
{header} .= <
HEADER ; @@ -2810,7 +2803,7 @@ sub display_list_of_tags_translate ($request_ref, $query_ref) { my $tagstable_search = lang_in_other_lc($request_lc, "tagstable_search"); my $tagstable_filtered = lang_in_other_lc($request_lc, "tagstable_filtered"); - $initjs .= <{initjs} .= <{scripts} .= < SCRIPTS ; - $header .= <
{header} .= <
HEADER ; @@ -2880,14 +2873,16 @@ HEADER $log->debug("end", {}) if $log->is_debug(); - process_template('web/common/includes/display_list_of_tags_translate.tt.html', - $template_data_ref_tags_translate, \$html) - || return "template error: " . $tt->error(); + process_template( + 'web/common/includes/display_list_of_tags_translate.tt.html', + $template_data_ref_tags_translate, + \$html, $request_ref + ) || return "template error: " . $tt->error(); return $html; } -sub display_points_ranking ($tagtype, $tagid) { +sub display_points_ranking ($tagtype, $tagid, $request_ref) { local $log->context->{tagtype} = $tagtype; local $log->context->{tagid} = $tagid; @@ -2981,7 +2976,7 @@ sub display_points_ranking ($tagtype, $tagid) { my $tagstable_search = lang_in_other_lc($lc, "tagstable_search"); my $tagstable_filtered = lang_in_other_lc($lc, "tagstable_filtered"); - $initjs .= <{initjs} .= <{cc} ne 'world') { $tagtype = 'countries'; $tagid = $country; $title = display_taxonomy_tag($lc, $tagtype, $tagid); @@ -3113,24 +3108,24 @@ sub display_points ($request_ref) { } if (defined $tagtype) { - $html .= display_points_ranking($tagtype, $tagid); + $html .= display_points_ranking($tagtype, $tagid, $request_ref); $request_ref->{title} = "Open Food Hunt" . lang("title_separator") . lang("points_ranking") . lang("title_separator") . $title; } else { - $html .= display_points_ranking("users", "_all_"); - $html .= display_points_ranking("countries", "_all_"); + $html .= display_points_ranking("users", "_all_", $request_ref); + $html .= display_points_ranking("countries", "_all_", $request_ref); $request_ref->{title} = "Open Food Hunt" . lang("title_separator") . lang("points_ranking_users_and_countries"); } $request_ref->{content_ref} = \$html; - $scripts .= <{scripts} .= < SCRIPTS ; - $header .= <
{header} .= <
HEADER @@ -3241,7 +3236,7 @@ sub canonicalize_request_tags_and_redirect_to_canonical_url ($request_ref) { # Ask search engines to not index the page if it is related to a user if ($header_meta_noindex) { - $header .= '' . "\n"; + $request_ref->{header} .= '' . "\n"; } return; @@ -3268,7 +3263,7 @@ sub generate_title_from_request_tags ($tags_ref) { return $title; } -=head2 generate_description_from_display_tag_options ($tagtype, $tagid, $display_tag, $canon_tagid) +=head2 generate_description_from_display_tag_options ($tagtype, $tagid, $display_tag, $canon_tagid, $request_ref) Generate a description for some tag types, like additives, if there is a template set in the Config.pm file. @@ -3278,7 +3273,7 @@ It is in maintenance mode, and should be reimplemented as facets knowledge panel =cut -sub generate_description_from_display_tag_options ($tagtype, $tagid, $display_tag, $canon_tagid) { +sub generate_description_from_display_tag_options ($tagtype, $tagid, $display_tag, $canon_tagid, $request_ref) { my $description = ""; @@ -3360,7 +3355,7 @@ sub generate_description_from_display_tag_options ($tagtype, $tagid, $display_ta } } - $styles .= <{styles} .= < \@wikidata_objects, pointers => \@markers }; - process_template('web/pages/tags_map/map_of_tags.tt.html', $map_template_data_ref, \$map_html) + process_template('web/pages/tags_map/map_of_tags.tt.html', $map_template_data_ref, \$map_html, $request_ref) || ($map_html .= 'template error: ' . $tt->error()); } @@ -4164,13 +4160,13 @@ HTML # Display the organization profile - if (is_user_in_org_group($user_or_org_ref, $User_id, "admins") or $admin) { + if (is_user_in_org_group($user_or_org_ref, $User_id, "admins") or $request_ref->{admin}) { $user_template_data_ref->{edit_profile} = 1; $user_template_data_ref->{orgid} = $orgid; } process_template('web/pages/org_profile/org_profile.tt.html', - $user_template_data_ref, \$profile_html) + $user_template_data_ref, \$profile_html, $request_ref) or $profile_html = "

web/pages/org_profile/org_profile.tt.html template error: " . $tt->error() . "

"; } @@ -4178,7 +4174,7 @@ HTML # Display the user profile - if (($tagid eq $User_id) or $admin) { + if (($tagid eq $User_id) or $request_ref->{admin}) { $user_template_data_ref->{edit_profile} = 1; $user_template_data_ref->{userid} = $tagid; } @@ -4216,10 +4212,10 @@ HTML and ($tagtype eq 'categories')) { - my $categories_nutriments_ref = $categories_nutriments_per_country{$cc}; + my $categories_nutriments_ref = $categories_nutriments_per_country{$request_ref->{cc}}; $log->debug("checking if this category has stored statistics", - {cc => $cc, tagtype => $tagtype, tagid => $tagid}) + {cc => $request_ref->{cc}, tagtype => $tagtype, tagid => $tagid}) if $log->is_debug(); if ( (defined $categories_nutriments_ref) and (defined $categories_nutriments_ref->{$canon_tagid}) @@ -4227,7 +4223,7 @@ HTML { $log->debug( "statistics found for the tag, addind stats to description", - {cc => $cc, tagtype => $tagtype, tagid => $tagid} + {cc => $request_ref->{cc}, tagtype => $tagtype, tagid => $tagid} ) if $log->is_debug(); $description @@ -4239,7 +4235,7 @@ HTML $display_tag, $categories_nutriments_ref->{$canon_tagid}{count} ) . "

" - . display_nutrition_table($categories_nutriments_ref->{$canon_tagid}, undef); + . display_nutrition_table($categories_nutriments_ref->{$canon_tagid}, undef, $request_ref); } } @@ -4288,8 +4284,8 @@ HTML initialize_knowledge_panels_options($knowledge_panels_options_ref, $request_ref); my $tag_ref = {}; # Object to store the knowledge panels my $panels_created - = create_tag_knowledge_panels($tag_ref, $lc, $cc, $knowledge_panels_options_ref, $tagtype, - $canon_tagid); + = create_tag_knowledge_panels($tag_ref, $lc, $request_ref->{cc}, $knowledge_panels_options_ref, + $tagtype, $canon_tagid); if ($panels_created) { $tag_template_data_ref->{tag_panels} = display_knowledge_panel($tag_ref, $tag_ref->{"knowledge_panels_" . $lc}, "root"); @@ -4299,7 +4295,7 @@ HTML } # end of if (defined $tagtype) $tag_template_data_ref->{country} = $country; - $tag_template_data_ref->{country_code} = $cc; + $tag_template_data_ref->{country_code} = $request_ref->{cc}; $tag_template_data_ref->{facets_kp_url} = $facets_kp_url; if ($country ne 'en:world') { @@ -4494,7 +4490,7 @@ sub display_search_results ($request_ref) { my $preferences_text = lang("classify_products_according_to_your_preferences"); - $scripts .= <{scripts} .= < var page_type = "products"; var preferences_text = "$preferences_text"; @@ -4504,13 +4500,13 @@ var products = []; JS ; - $scripts .= <{scripts} .= < JS ; - $initjs .= <{initjs} .= <{scripts} .= < var page_type = "products"; var preferences_text = "$preferences_text"; @@ -5611,13 +5607,13 @@ var products = $products_json; JS ; - $scripts .= <{scripts} .= < JS ; - $initjs .= <{initjs} .= <{admin}) and (defined single_param("export_limit"))) { $max_count = single_param("export_limit"); } my $args_ref = { - cc => $cc, # used to localize Eco-Score fields + cc => $request_ref->{cc}, # used to localize Eco-Score fields format => $format, filehandle => \*STDOUT, filename => "openfoodfacts_export." . $format, @@ -6039,7 +6035,7 @@ List of search results from search_and_graph_products() =cut -sub display_scatter_plot ($graph_ref, $products_ref) { +sub display_scatter_plot ($graph_ref, $products_ref, $request_ref) { my @products = @{$products_ref}; my $count = scalar @products; @@ -6349,11 +6345,11 @@ JS }); JS ; - $initjs .= $js; + $request_ref->{initjs} .= $js; my $count_string = sprintf(lang("graph_count"), $count, $i); - $scripts .= <{scripts} .= < SCRIPTS ; @@ -6371,7 +6367,7 @@ HTML compute_stats_for_products($stats_ref, \%nutriments, $count, $i, 5, 'search'); - $html .= display_nutrition_table($stats_ref, undef); + $html .= display_nutrition_table($stats_ref, undef, $request_ref); $html .= "

 

"; @@ -6379,7 +6375,7 @@ HTML } -=head2 display_histogram ($graph_ref, $products_ref) +=head2 display_histogram ($graph_ref, $products_ref, $request_ref) Called by search_and_graph_products() to display an histogram of products on 1 axis @@ -6395,7 +6391,7 @@ List of search results from search_and_graph_products() =cut -sub display_histogram ($graph_ref, $products_ref) { +sub display_histogram ($graph_ref, $products_ref, $request_ref) { my @products = @{$products_ref}; my $count = @products; @@ -6730,11 +6726,11 @@ JS }); JS ; - $initjs .= $js; + $request_ref->{initjs} .= $js; my $count_string = sprintf(lang("graph_count"), $count, $i); - $scripts .= <{scripts} .= < SCRIPTS ; @@ -6760,7 +6756,7 @@ sub search_and_graph_products ($request_ref, $query_ref, $graph_ref) { $log->debug("retrieving products from MongoDB to display them in a graph") if $log->is_debug(); - if ($admin) { + if ($request_ref->{admin}) { $log->debug("Executing MongoDB query", {query => $query_ref}) if $log->is_debug(); } @@ -6848,10 +6844,10 @@ sub search_and_graph_products ($request_ref, $query_ref, $graph_ref) { or ($graph_ref->{axis_y} eq "") or ($graph_ref->{axis_y} eq 'products_n')) { - $html .= display_histogram($graph_ref, \@products); + $html .= display_histogram($graph_ref, \@products, $request_ref); } else { - $html .= display_scatter_plot($graph_ref, \@products); + $html .= display_scatter_plot($graph_ref, \@products, $request_ref); } if (defined $request_ref->{current_link}) { @@ -7080,7 +7076,7 @@ sub map_of_products ($products_iter, $request_ref, $graph_ref) { pointers => \@pointers, current_link => $request_ref->{current_link}, }; - process_template('web/pages/products_map/map_of_products.tt.html', $map_template_data_ref, \$html) + process_template('web/pages/products_map/map_of_products.tt.html', $map_template_data_ref, \$html, $request_ref) || ($html .= 'template error: ' . $tt->error()); return $html; @@ -7334,7 +7330,7 @@ sub display_page ($request_ref) { $template_data_ref->{formatted_subdomain} = $formatted_subdomain; $template_data_ref->{css_timestamp} = $file_timestamps{'css/dist/app-' . lang_in_other_lc($request_lc, 'text_direction') . '.css'}; - $template_data_ref->{header} = $header; + $template_data_ref->{header} = $request_ref->{header}; $template_data_ref->{page_type} = $request_ref->{page_type} // "other"; $template_data_ref->{page_format} = $request_ref->{page_format} // "normal"; @@ -7354,16 +7350,16 @@ sub display_page ($request_ref) { $google_analytics = $server_options{google_analytics}; } - $template_data_ref->{styles} = $styles; + $template_data_ref->{styles} = $request_ref->{styles}; $template_data_ref->{google_analytics} = $google_analytics; - $template_data_ref->{bodyabout} = $bodyabout; + $template_data_ref->{bodyabout} = $request_ref->{bodyabout}; $template_data_ref->{site_name} = $site_name; my $en = 0; my $langs = ''; my $selected_lang = ''; - foreach my $olc (@{$country_languages{$cc}}, 'en') { + foreach my $olc (@{$country_languages{$request_ref->{cc}}}, 'en') { if ($olc eq 'en') { if ($en) { next; @@ -7373,9 +7369,9 @@ sub display_page ($request_ref) { } } if (exists $Langs{$olc}) { - my $osubdomain = "$cc-$olc"; - if ($olc eq $country_languages{$cc}[0]) { - $osubdomain = $cc; + my $osubdomain = "$request_ref->{cc}-$olc"; + if ($olc eq $country_languages{$request_ref->{cc}}[0]) { + $osubdomain = $request_ref->{cc}; } if (($olc eq $lc)) { $selected_lang = "$Langs{$olc}\n"; @@ -7477,11 +7473,11 @@ sub display_page ($request_ref) { while ($$content_ref =~ /(.*?)<\/initjs>/s) { $$content_ref = $` . $'; - $initjs .= $1; + $request_ref->{initjs} .= $1; } while ($$content_ref =~ /(.*?)<\/scripts>/s) { $$content_ref = $` . $'; - $scripts .= $1; + $request_ref->{scripts} .= $1; } $template_data_ref->{search_terms} = ${search_terms}; @@ -7493,12 +7489,12 @@ sub display_page ($request_ref) { # init javascript code - $template_data_ref->{scripts} = $scripts; - $template_data_ref->{initjs} = $initjs; + $template_data_ref->{scripts} = $request_ref->{scripts}; + $template_data_ref->{initjs} = $request_ref->{initjs}; $template_data_ref->{request} = $request_ref; my $html; - process_template('web/common/site_layout.tt.html', $template_data_ref, \$html) + process_template('web/common/site_layout.tt.html', $template_data_ref, \$html, $request_ref) || ($html = "template error: " . $tt->error()); # disable equalizer @@ -7552,7 +7548,7 @@ sub display_page ($request_ref) { return; } -sub display_image_box ($product_ref, $id, $minheight_ref) { +sub display_image_box ($product_ref, $id, $minheight_ref, $request_ref) { my $img = display_image($product_ref, $id, $small_size); if ($img ne '') { @@ -7619,7 +7615,7 @@ HTML $img .= $html; - $initjs .= <{initjs} .= < $request_ref,}; - $scripts .= <{scripts} .= < @@ -7734,7 +7730,7 @@ SCRIPTS ; # call equalizer when dropdown content is shown - $initjs .= <{initjs} .= <debug("displaying product revision") if $log->is_debug(); $product_ref = retrieve_product_rev($product_id, $rev); - $header .= ''; + $request_ref->{header} .= ''; } else { $product_ref = retrieve_product($product_id); @@ -7832,20 +7828,20 @@ JS if ((feature_enabled("ecoscore")) and (defined $product_ref->{ecoscore_data})) { - localize_ecoscore($cc, $product_ref); + localize_ecoscore($request_ref->{cc}, $product_ref); $template_data_ref->{ecoscore_grade} = uc($product_ref->{ecoscore_data}{"grade"}); $template_data_ref->{ecoscore_grade_lc} = $product_ref->{ecoscore_data}{"grade"}; $template_data_ref->{ecoscore_score} = $product_ref->{ecoscore_data}{"score"}; $template_data_ref->{ecoscore_data} = $product_ref->{ecoscore_data}; $template_data_ref->{ecoscore_calculation_details} - = display_ecoscore_calculation_details($cc, $product_ref->{ecoscore_data}); + = display_ecoscore_calculation_details($request_ref->{cc}, $product_ref->{ecoscore_data}); } # Activate knowledge panels for all users initialize_knowledge_panels_options($knowledge_panels_options_ref, $request_ref); - create_knowledge_panels($product_ref, $lc, $cc, $knowledge_panels_options_ref); + create_knowledge_panels($product_ref, $lc, $request_ref->{cc}, $knowledge_panels_options_ref, $request_ref); $template_data_ref->{environment_card_panel} = display_knowledge_panel($product_ref, $product_ref->{"knowledge_panels_" . $lc}, "environment_card"); $template_data_ref->{health_card_panel} @@ -7890,7 +7886,7 @@ JS # my @fields = qw(generic_name quantity packaging br brands br categories br labels origins br manufacturing_places br emb_codes link purchase_places stores countries); my @fields = @ProductOpener::Config::display_fields; - $bodyabout = " about=\"" . product_url($product_ref) . "\" typeof=\"food:foodProduct\""; + $request_ref->{bodyabout} = " about=\"" . product_url($product_ref) . "\" typeof=\"food:foodProduct\""; $template_data_ref->{user_id} = $User_id; $template_data_ref->{robotoff_url} = $robotoff_url; @@ -8018,7 +8014,7 @@ JS } my $minheight = 0; - my $front_image = display_image_box($product_ref, 'front', \$minheight); + my $front_image = display_image_box($product_ref, 'front', \$minheight, $request_ref); $front_image =~ s/ width="/ itemprop="image" width="/; # Take the last (biggest) image @@ -8074,7 +8070,7 @@ JS = " (" . display_taxonomy_tag($lc, 'languages', $language_codes{$ingredients_text_lang}) . ")"; } - $template_data_ref->{ingredients_image} = display_image_box($product_ref, 'ingredients', \$minheight); + $template_data_ref->{ingredients_image} = display_image_box($product_ref, 'ingredients', \$minheight, $request_ref); $template_data_ref->{ingredients_text_lang} = $ingredients_text_lang; $template_data_ref->{ingredients_text} = $ingredients_text; @@ -8084,7 +8080,7 @@ JS my $ilc = $ingredients_text_lang; $template_data_ref->{ilc} = $ingredients_text_lang; - $initjs .= <{initjs} .= <{no_nutrition_data} = 'on'; } - my $comparisons_ref = compare_product_nutrition_facts_to_categories($product_ref, $cc, undef); + my $comparisons_ref = compare_product_nutrition_facts_to_categories($product_ref, $request_ref->{cc}, undef); - $template_data_ref->{display_nutrition_table} = display_nutrition_table($product_ref, $comparisons_ref); - $template_data_ref->{nutrition_image} = display_image_box($product_ref, 'nutrition', \$minheight); + $template_data_ref->{display_nutrition_table} + = display_nutrition_table($product_ref, $comparisons_ref, $request_ref); + $template_data_ref->{nutrition_image} = display_image_box($product_ref, 'nutrition', \$minheight, $request_ref); if (has_tag($product_ref, "categories", "en:alcoholic-beverages")) { $template_data_ref->{has_tag} = 'categories-en:alcoholic-beverages'; @@ -8267,7 +8264,7 @@ JS # Packaging - $template_data_ref->{packaging_image} = display_image_box($product_ref, 'packaging', \$minheight); + $template_data_ref->{packaging_image} = display_image_box($product_ref, 'packaging', \$minheight, $request_ref); # try to display packaging in the local language if available @@ -8296,7 +8293,7 @@ JS # Forest footprint # 2020-12-07 - We currently display the forest footprint in France # and for moderators so that we can extend it to other countries - if (($cc eq "fr") or ($User{moderator})) { + if (($request_ref->{cc} eq "fr") or ($User{moderator})) { # Forest footprint data structure $template_data_ref->{forest_footprint_data} = $product_ref->{forest_footprint_data}; } @@ -8313,7 +8310,7 @@ JS $template_data_ref->{other_fields} = $other_fields; } - $template_data_ref->{admin} = $admin; + $template_data_ref->{admin} = $request_ref->{admin}; # Platform for producers: data quality issues and improvements opportunities @@ -8411,7 +8408,7 @@ HTML ; } - $header .= <{header} .= < @@ -8423,7 +8420,7 @@ HTML if (defined $product_ref->{brands}) { # print only first brand if multiple exist. my @brands = split(',', $product_ref->{brands}); - $header .= <{header} .= < HTML @@ -8433,14 +8430,14 @@ HTML # get most specific category (the last one) my $data2 = display_taxonomy_tag($lc, "categories", $product_ref->{categories_tags}[-1]); if ($data2) { - $header .= <{header} .= < HTML ; } - $header .= <{header} .= <{cc}, $attributes_options_ref); my $product_attribute_groups_json = $json->encode({"attribute_groups" => $product_ref->{"attribute_groups_" . $lc}}); my $preferences_text = lang("classify_products_according_to_your_preferences"); - $scripts .= <{scripts} .= < var page_type = "product"; var preferences_text = "$preferences_text"; @@ -8471,7 +8468,7 @@ var product = $product_attribute_groups_json; JS ; - $initjs .= <{initjs} .= <error()); $html .= $html_display_product; @@ -8568,12 +8565,12 @@ sub display_product_jqm ($request_ref) { # jquerymobile process_template( 'web/pages/product/includes/nutriscore.tt.html', $template_data_nutriscore_and_nutrient_levels_ref, - \$nutriscore_html + \$nutriscore_html, $request_ref ) || return "template error: " . $tt->error(); process_template( 'web/pages/product/includes/nutrient_levels.tt.html', $template_data_nutriscore_and_nutrient_levels_ref, - \$nutrient_levels_html + \$nutrient_levels_html, $request_ref ) || return "template error: " . $tt->error(); } @@ -8654,7 +8651,7 @@ HTML my $minheight = 0; $product_ref->{jqm} = 1; - my $html_image = display_image_box($product_ref, 'front', \$minheight); + my $html_image = display_image_box($product_ref, 'front', \$minheight, $request_ref); $html .= <
@@ -8669,7 +8666,7 @@ HTML $html .= display_field($product_ref, $field); } - $html_image = display_image_box($product_ref, 'ingredients', \$minheight); + $html_image = display_image_box($product_ref, 'ingredients', \$minheight, $request_ref); # try to display ingredients in the local language @@ -8788,7 +8785,7 @@ HTML } - $html_image = display_image_box($product_ref, 'nutrition', \$minheight); + $html_image = display_image_box($product_ref, 'nutrition', \$minheight, $request_ref); $html .= "
"; @@ -9382,7 +9379,7 @@ sub compute_stats_for_products ($stats_ref, $nutriments_ref, $count, $n, $min_pr = sprintf("%.2e", $values[int(($nutriments_ref->{"${nid}_n"}) * 0.50)]) + 0.0; #print STDERR "-> lc: lc -category $tagid - count: $count - n: nutriments: " . $nn . "$n \n"; - #print "categories stats - cc: $cc - n: $n- values for category $id: " . join(", ", @values) . "\n"; + #print "categories stats - cc: $request_ref->{cc} - n: $n- values for category $id: " . join(", ", @values) . "\n"; #print "tagid: $id - nid: $nid - 100g: " . $stats_ref->{nutriments}{"${nid}_100g"} . " min: " . $stats_ref->{nutriments}{"${nid}_min"} . " - max: " . $stats_ref->{nutriments}{"${nid}_max"} . # "mean: " . $stats_ref->{nutriments}{"${nid}_mean"} . " - median: " . $stats_ref->{nutriments}{"${nid}_50"} . "\n"; @@ -9488,7 +9485,7 @@ Reference to a data structure with needed data to display. =cut -sub data_to_display_nutrition_table ($product_ref, $comparisons_ref) { +sub data_to_display_nutrition_table ($product_ref, $comparisons_ref, $request_ref) { # This function populates a data structure that is used by the template to display the nutrition facts table my $template_data_ref = { @@ -9619,7 +9616,7 @@ sub data_to_display_nutrition_table ($product_ref, $comparisons_ref) { $checked = 1; } else { - $styles .= <{styles} .= <{cc} ne $1) and (not($1 eq 'fr'))) { $shown = 0; } @@ -10198,13 +10195,14 @@ HTML for the nutrition table. =cut -sub display_nutrition_table ($product_ref, $comparisons_ref) { +sub display_nutrition_table ($product_ref, $comparisons_ref, $request_ref) { my $html = ''; - my $template_data_ref = data_to_display_nutrition_table($product_ref, $comparisons_ref); + my $template_data_ref = data_to_display_nutrition_table($product_ref, $comparisons_ref, $request_ref); - process_template('web/pages/product/includes/nutrition_facts_table.tt.html', $template_data_ref, \$html) + process_template('web/pages/product/includes/nutrition_facts_table.tt.html', + $template_data_ref, \$html, $request_ref) || return "template error: " . $tt->error(); return $html; @@ -10352,6 +10350,8 @@ sub display_taxonomy_api ($request_ref) { sub display_product_api ($request_ref) { + my $cc = $request_ref->{cc}; + # Is a sample product requested? if ((defined $request_ref->{code}) and ($request_ref->{code} eq "example")) { @@ -10411,7 +10411,8 @@ sub display_product_api ($request_ref) { Lang => \%Lang, }; my $html; - process_template('web/common/includes/display_product_api.tt.html', $template_data_ref, \$html) + process_template('web/common/includes/display_product_api.tt.html', + $template_data_ref, \$html, $request_ref) || return "template error: " . $tt->error(); $response{jqm} .= $html; } @@ -10485,7 +10486,7 @@ sub display_product_api ($request_ref) { display_product_jqm($request_ref); $response{jqm} = $request_ref->{jqm_content}; - $response{jqm} =~ s/(href|src)=("\/)/$1="https:\/\/$cc.${server_domain}\//g; + $response{jqm} =~ s/(href|src)=("\/)/$1="https:\/\/$request_ref->{cc}.${server_domain}\//g; $response{title} = $request_ref->{title}; } } @@ -10597,7 +10598,7 @@ sub display_product_history ($request_ref, $code, $product_ref) { }; my $html; - process_template('web/pages/product/includes/edit_history.tt.html', $template_data_ref, \$html) + process_template('web/pages/product/includes/edit_history.tt.html', $template_data_ref, \$html, $request_ref) || return 'template error: ' . $tt->error(); return $html; @@ -10710,11 +10711,11 @@ sub display_structured_response_opensearch_rss ($request_ref) { my $short_name = $options{site_name}; my $long_name = $short_name; - if ($cc eq 'world') { + if ($request_ref->{cc} eq 'world') { $long_name .= " " . uc($lc); } else { - $long_name .= " " . uc($cc) . "/" . uc($lc); + $long_name .= " " . uc($request_ref->{cc}) . "/" . uc($lc); } $long_name = $xs->escape_value(encode_utf8($long_name)); @@ -10860,7 +10861,7 @@ sub display_recent_changes ($request_ref, $query_ref, $limit, $page) { my $changes_ref = {}; # security: Do not expose IP addresses to non-admin or anonymous users. - delete $change_hash->{ip} unless $admin; + delete $change_hash->{ip} unless $request_ref->{admin}; push @{$request_ref->{structured_response}{changes}}, $change_hash; my $diffs = compute_changes_diff_text($change_ref); @@ -10903,7 +10904,8 @@ sub display_recent_changes ($request_ref, $query_ref, $limit, $page) { $template_data_ref_changes->{changes} = \@changes; $template_data_ref_changes->{display_pagination} = display_pagination($request_ref, $count, $limit, $page); - process_template('web/common/includes/display_recent_changes.tt.html', $template_data_ref_changes, \$html) + process_template('web/common/includes/display_recent_changes.tt.html', + $template_data_ref_changes, \$html, $request_ref) || ($html .= 'template error: ' . $tt->error()); ${$request_ref->{content_ref}} .= $html; @@ -11315,13 +11317,13 @@ sub _format_comment ($comment) { return $comment; } -=head2 display_ecoscore_calculation_details( $cc, $ecoscore_data_ref ) +=head2 display_ecoscore_calculation_details( $ecoscore_cc, $ecoscore_data_ref ) Generates HTML code with information on how the Eco-score was computed for a particular product. =head3 Parameters -=head4 country code $cc +=head4 country code $ecoscore_cc =head4 ecoscore data $ecoscore_data_ref @@ -11379,7 +11381,7 @@ sub search_and_analyze_recipes ($request_ref, $query_ref) { $log->debug("retrieving products from MongoDB to analyze their recipes") if $log->is_debug(); - if ($admin) { + if ($request_ref->{admin}) { $log->debug("Executing MongoDB query", {query => $query_ref}) if $log->is_debug(); } @@ -11474,7 +11476,7 @@ sub search_and_analyze_recipes ($request_ref, $query_ref) { debug => $debug, }; - process_template('web/pages/recipes/recipes.tt.html', $template_data_ref, \$html) + process_template('web/pages/recipes/recipes.tt.html', $template_data_ref, \$html, $request_ref) or $html = "template error: " . $tt->error(); } @@ -11482,7 +11484,7 @@ sub search_and_analyze_recipes ($request_ref, $query_ref) { return $html; } -=head2 display_properties( $cc, $ecoscore_data_ref ) +=head2 display_properties ($request_ref) Load the Folksonomy Engine properties script @@ -11491,7 +11493,7 @@ Load the Folksonomy Engine properties script sub display_properties ($request_ref) { my $html; - process_template('web/common/includes/folksonomy_script.tt.html', {}, \$html) + process_template('web/common/includes/folksonomy_script.tt.html', {}, \$html, $request_ref) || return "template error: " . $tt->error(); $request_ref->{content_ref} = \$html; diff --git a/lib/ProductOpener/HTTP.pm b/lib/ProductOpener/HTTP.pm index c6d04b5737a92..d124c64029636 100644 --- a/lib/ProductOpener/HTTP.pm +++ b/lib/ProductOpener/HTTP.pm @@ -177,13 +177,13 @@ sub set_http_response_header($request_ref, $header_name, $header_value) { return; } -=head2 write_http_response_headers($http_response_headers_ref) +=head2 write_http_response_headers($request_ref) This function writes the headers in the response. =head3 Parameters -=head4 $http_response_headers_ref - Reference to a hash with headers. +=head4 $request_ref - Reference to the request object. =cut diff --git a/lib/ProductOpener/Images.pm b/lib/ProductOpener/Images.pm index c612400cc2514..41c085b847c51 100644 --- a/lib/ProductOpener/Images.pm +++ b/lib/ProductOpener/Images.pm @@ -176,7 +176,7 @@ HTML return $html; } -sub display_select_crop ($object_ref, $id_lc, $language) { +sub display_select_crop ($object_ref, $id_lc, $language, $request_ref) { # $id_lc = shift -> id_lc = [front|ingredients|nutrition|packaging]_[new_]?[lc] my $id = $id_lc; @@ -197,7 +197,7 @@ sub display_select_crop ($object_ref, $id_lc, $language) { my $label = $Lang{"image_" . $imagetype}{$lc}; my $html = ''; - if (is_protected_image($object_ref, $id_lc) and (not $User{moderator}) and (not $admin)) { + if (is_protected_image($object_ref, $id_lc) and (not $User{moderator}) and (not $request_ref->{admin})) { $html .= <$message

@@ -398,7 +398,7 @@ sub scan_code ($file) { return $code; } -sub display_search_image_form ($id) { +sub display_search_image_form ($id, $request_ref) { my $html = ''; @@ -412,9 +412,9 @@ sub display_search_image_form ($id) { # Do not load jquery file upload twice, if it was loaded by another form - if ($scripts !~ /jquery.fileupload.js/) { + if ($request_ref->{scripts} !~ /jquery.fileupload.js/) { - $scripts .= <{scripts} .= < @@ -424,7 +424,7 @@ JS } - $initjs .= <{initjs} .= <error(); return $html; diff --git a/lib/ProductOpener/KnowledgePanels.pm b/lib/ProductOpener/KnowledgePanels.pm index cf72df964f406..50916cb70fce5 100644 --- a/lib/ProductOpener/KnowledgePanels.pm +++ b/lib/ProductOpener/KnowledgePanels.pm @@ -112,7 +112,7 @@ sub initialize_knowledge_panels_options ($knowledge_panels_options_ref, $request return; } -=head2 create_knowledge_panels( $product_ref, $target_lc, $target_cc, $options_ref ) +=head2 create_knowledge_panels( $product_ref, $target_lc, $target_cc, $options_ref, $request_ref) Create all knowledge panels for a product, with strings (descriptions, recommendations etc.) in a specific language, and return them in an array of panels. @@ -141,6 +141,10 @@ Defines how some panels should be created (or not created) - deactivate_[panel_id] : do not create a default panel -- currently unimplemented - activate_[panel_id] : create an on demand panel -- currently only for physical_activities panel +=head4 request reference $request_ref + +Contains the request parameters, including the API request parameters. + =head3 Return values Panels are returned in the "knowledge_panels_[$target_lc]" hash of the product reference @@ -148,7 +152,7 @@ passed as input. =cut -sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref) { +sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref, $request_ref) { $log->debug("create knowledge panels for product", {code => $product_ref->{code}, target_lc => $target_lc}) if $log->is_debug(); @@ -200,10 +204,10 @@ sub create_knowledge_panels ($product_ref, $target_lc, $target_cc, $options_ref) my $has_health_card; if (feature_enabled("health_card")) { - $has_health_card = create_health_card_panel($product_ref, $target_lc, $target_cc, $options_ref); + $has_health_card = create_health_card_panel($product_ref, $target_lc, $target_cc, $options_ref, $request_ref); } - create_environment_card_panel($product_ref, $target_lc, $target_cc, $options_ref); + create_environment_card_panel($product_ref, $target_lc, $target_cc, $options_ref, $request_ref); my $has_report_problem_card; if (not $options_ref->{producers_platform}) { @@ -317,7 +321,8 @@ sub create_panel_from_json_template ($panel_id, $panel_template, $panel_data_ref product => $product_ref, knowledge_panels_options => $options_ref, }, - \$panel_json + \$panel_json, + {cc => $target_cc} ) ) { @@ -506,11 +511,13 @@ The Eco-Score depends on the country of the consumer (as the transport bonus/mal =cut -sub create_ecoscore_panel ($product_ref, $target_lc, $target_cc, $options_ref) { +sub create_ecoscore_panel ($product_ref, $target_lc, $target_cc, $options_ref, $request_ref) { $log->debug("create ecoscore panel", {code => $product_ref->{code}, ecoscore_data => $product_ref->{ecoscore_data}}) if $log->is_debug(); + my $cc = $request_ref->{cc}; + if ((defined $product_ref->{ecoscore_data}) and ($product_ref->{ecoscore_data}{status} eq "known")) { my $score = $product_ref->{ecoscore_data}{score}; @@ -707,7 +714,7 @@ The Eco-Score depends on the country of the consumer (as the transport bonus/mal =cut -sub create_environment_card_panel ($product_ref, $target_lc, $target_cc, $options_ref) { +sub create_environment_card_panel ($product_ref, $target_lc, $target_cc, $options_ref, $request_ref) { $log->debug("create environment card panel", {code => $product_ref->{code}}) if $log->is_debug(); @@ -715,7 +722,7 @@ sub create_environment_card_panel ($product_ref, $target_lc, $target_cc, $option # Create Eco-Score related panels if ($options{product_type} eq "food") { - create_ecoscore_panel($product_ref, $target_lc, $target_cc, $options_ref); + create_ecoscore_panel($product_ref, $target_lc, $target_cc, $options_ref, $request_ref); if ( (defined $product_ref->{ecoscore_data}) @@ -840,7 +847,7 @@ We may display country specific recommendations from health authorities, or coun =cut -sub create_health_card_panel ($product_ref, $target_lc, $target_cc, $options_ref) { +sub create_health_card_panel ($product_ref, $target_lc, $target_cc, $options_ref, $request_ref) { $log->debug("create health card panel", {code => $product_ref->{code}}) if $log->is_debug(); @@ -886,7 +893,7 @@ sub create_health_card_panel ($product_ref, $target_lc, $target_cc, $options_ref # Nutrition facts for food and pet food if (feature_enabled("nutrition")) { 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); + create_nutrition_facts_table_panel($product_ref, $target_lc, $target_cc, $options_ref, $request_ref); } my $panel_data_ref = { @@ -1109,7 +1116,7 @@ This parameter sets the desired language for the user facing strings. =cut -sub create_nutrition_facts_table_panel ($product_ref, $target_lc, $target_cc, $options_ref) { +sub create_nutrition_facts_table_panel ($product_ref, $target_lc, $target_cc, $options_ref, $request_ref) { $log->debug("create nutrition facts panel", {code => $product_ref->{code}, nutriscore_data => $product_ref->{nutriscore_data}}) @@ -1122,7 +1129,7 @@ sub create_nutrition_facts_table_panel ($product_ref, $target_lc, $target_cc, $o # Compare the product nutrition facts to the most specific category my $comparisons_ref = compare_product_nutrition_facts_to_categories($product_ref, $target_cc, 1); - my $panel_data_ref = data_to_display_nutrition_table($product_ref, $comparisons_ref); + my $panel_data_ref = data_to_display_nutrition_table($product_ref, $comparisons_ref, $request_ref); create_panel_from_json_template("nutrition_facts_table", "api/knowledge-panels/health/nutrition/nutrition_facts_table.tt.json", diff --git a/lib/ProductOpener/Producers.pm b/lib/ProductOpener/Producers.pm index f6f6f0ac1ab9d..5dcc38cea4988 100644 --- a/lib/ProductOpener/Producers.pm +++ b/lib/ProductOpener/Producers.pm @@ -77,7 +77,7 @@ use ProductOpener::Products qw/:all/; use ProductOpener::Food qw/%cc_nutriment_table %nutriments_tables/; use ProductOpener::Ingredients qw/:all/; use ProductOpener::Lang qw/$lc %Lang lang/; -use ProductOpener::Display qw/$header $nutriment_table/; +use ProductOpener::Display qw/$nutriment_table/; use ProductOpener::Export qw/export_csv/; use ProductOpener::Import qw/$IMPORT_MAX_PACKAGING_COMPONENTS import_csv_file import_products_categories_from_public_database/; diff --git a/lib/ProductOpener/Routing.pm b/lib/ProductOpener/Routing.pm index 107377d48d393..a0c6b1f90d1e3 100644 --- a/lib/ProductOpener/Routing.pm +++ b/lib/ProductOpener/Routing.pm @@ -46,7 +46,7 @@ use vars @EXPORT_OK; use ProductOpener::Config qw/:all/; use ProductOpener::Paths qw/:all/; use ProductOpener::Display - qw/$formatted_subdomain $admin %index_tag_types_set display_robots_txt_and_exit init_request redirect_to_url single_param/; + qw/$formatted_subdomain %index_tag_types_set display_robots_txt_and_exit init_request redirect_to_url single_param/; use ProductOpener::Users qw/:all/; use ProductOpener::Lang qw/%tag_type_from_plural %tag_type_from_singular %tag_type_plural %tag_type_singular lang/; use ProductOpener::API qw/:all/; @@ -242,7 +242,7 @@ sub org_route($request_ref, @components) { if ($orgid ne $Org_id) { $log->debug("checking edit owner", {orgid => $orgid, ownerid => $Owner_id}) if $log->is_debug(); my @errors = (); - if ($admin or $User{pro_moderator}) { + if ($request_ref->{admin} or $User{pro_moderator}) { ProductOpener::Users::check_edit_owner(\%User, \@errors, $orgid); } else { diff --git a/lib/ProductOpener/Users.pm b/lib/ProductOpener/Users.pm index 7726759fc0ef5..03af643fc6778 100644 --- a/lib/ProductOpener/Users.pm +++ b/lib/ProductOpener/Users.pm @@ -442,11 +442,11 @@ sub check_user_form ($request_ref, $type, $user_ref, $errors_ref) { $user_ref->{discussion} = remove_tags_and_quote(single_param('discussion')); $user_ref->{ip} = remote_addr(); $user_ref->{initial_lc} = $lc; - $user_ref->{initial_cc} = $cc; + $user_ref->{initial_cc} = $request_ref->{cc}; $user_ref->{initial_user_agent} = user_agent(); } - if ($admin) { + if ($request_ref->{admin}) { # Org check_user_org($user_ref, remove_tags_and_quote(decode utf8 => single_param('org'))); @@ -521,7 +521,7 @@ sub check_user_form ($request_ref, $type, $user_ref, $errors_ref) { return; } -=head2 notify_user_requested_org($user_ref, $org_created) +=head2 notify_user_requested_org($user_ref, $org_created, $request_ref) Notify admin that a user requested to be part of an org @@ -533,20 +533,25 @@ Notify admin that a user requested to be part of an org Is the org newly created ? +=head4 Request object $request_ref + +the request object + =cut -sub notify_user_requested_org ($user_ref, $org_created) { +sub notify_user_requested_org ($user_ref, $org_created, $request_ref) { # the template for the email, we will build it gradually my $template_data_ref = { userid => $user_ref->{userid}, user => $user_ref, requested_org => $user_ref->{requested_org_id}, + cc => $request_ref->{cc}, }; # construct first part of the mail about new pro account my $mail = ''; - process_template("emails/user_new_pro_account.tt.txt", $template_data_ref, \$mail); + process_template("emails/user_new_pro_account.tt.txt", $template_data_ref, \$mail, $request_ref); if ($mail =~ /^\s*Subject:\s*(.*)\n/im) { my $subject = $1; my $body = $'; @@ -563,7 +568,8 @@ sub notify_user_requested_org ($user_ref, $org_created) { # The requested org already exists # build second part of the mail about it and alter the subject $mail = ''; - process_template("emails/user_new_pro_account_org_request_validated.tt.txt", $template_data_ref, \$mail); + process_template("emails/user_new_pro_account_org_request_validated.tt.txt", + $template_data_ref, \$mail, $request_ref); if ($mail =~ /^\s*Subject:\s*(.*)\n/im) { my $subject = $1; my $body = $'; @@ -582,7 +588,8 @@ sub notify_user_requested_org ($user_ref, $org_created) { # Send an e-mail notification to admins, with links to the organization $mail = ''; - process_template("emails/user_new_pro_account_admin_notification.tt.html", $template_data_ref, \$mail); + process_template("emails/user_new_pro_account_admin_notification.tt.html", $template_data_ref, \$mail, + $request_ref); if ($mail =~ /^\s*Subject:\s*(.*)\n/im) { my $subject = $1; my $body = $'; @@ -607,7 +614,7 @@ Process it. =cut -sub process_user_requested_org ($user_ref) { +sub process_user_requested_org ($user_ref, $request_ref) { (defined $user_ref->{requested_org_id}) or return 1; @@ -629,7 +636,7 @@ sub process_user_requested_org ($user_ref) { $org_created = 1; } # send a notification to admins - notify_user_requested_org($user_ref, $org_created); + notify_user_requested_org($user_ref, $org_created, $request_ref); return 1; } @@ -659,7 +666,7 @@ sub process_user_form ($type, $user_ref, $request_ref) { $log->debug("process_user_form", {type => $type, user_ref => $user_ref}) if $log->is_debug(); # Professional account with a requested org (existing or new) - process_user_requested_org($user_ref); + process_user_requested_org($user_ref, $request_ref); # save user store_user($user_ref); diff --git a/scripts/export_data.pl b/scripts/export_data.pl index 75fbe272ab4e9..6a5be3c4547c5 100755 --- a/scripts/export_data.pl +++ b/scripts/export_data.pl @@ -25,7 +25,7 @@ use ProductOpener::Config qw/:all/; use ProductOpener::Export qw/:all/; -use ProductOpener::Display qw/$cc $nutriment_table $subdomain search_and_export_products/; +use ProductOpener::Display qw/$nutriment_table $subdomain search_and_export_products/; use ProductOpener::Lang qw/$lc/; use ProductOpener::Food qw/%cc_nutriment_table/; @@ -70,7 +70,7 @@ my $include_images_paths; my $query_codes_from_file; my $format = "csv"; -$cc = "world"; +my $cc = "world"; $lc = "en"; GetOptions( @@ -100,6 +100,7 @@ my $request_ref = {}; $request_ref->{skip_http_headers} = 1; $request_ref->{batch} = 1; +$request_ref->{cc} = $cc; foreach my $field (sort keys %query_fields_values) { print STDERR "-- $field: $query_fields_values{$field}\n"; diff --git a/scripts/gen_top_tags_per_country.pl b/scripts/gen_top_tags_per_country.pl index e36d95602ff0f..8bc8bf3cb2710 100755 --- a/scripts/gen_top_tags_per_country.pl +++ b/scripts/gen_top_tags_per_country.pl @@ -27,7 +27,7 @@ use ProductOpener::Paths qw/%BASE_DIRS ensure_dir_created_or_die/; use ProductOpener::Store qw/get_string_id_for_lang store/; use ProductOpener::Index qw/:all/; -use ProductOpener::Display qw/$cc $country $static_subdomain add_product_nutriment_to_stats compute_stats_for_products/; +use ProductOpener::Display qw/$country $static_subdomain add_product_nutriment_to_stats compute_stats_for_products/; use ProductOpener::Tags qw/%country_languages %properties canonicalize_taxonomy_tag_link display_taxonomy_tag exists_taxonomy_tag/; use ProductOpener::Users qw/:all/; diff --git a/scripts/generate_madenearme_page.pl b/scripts/generate_madenearme_page.pl index d6533ccfa92c3..2406ee986b297 100755 --- a/scripts/generate_madenearme_page.pl +++ b/scripts/generate_madenearme_page.pl @@ -116,12 +116,14 @@ ($jsonl_path, $country, $verbose = undef) my $verbose = undef; GetOptions("verbose" => \$verbose) or die($usage); -$cc = $ARGV[0]; +my $request_ref = {}; +my $cc = $ARGV[0]; $lc = $ARGV[1]; $subdomain = $cc; $formatted_subdomain = format_subdomain($subdomain); -$header = ""; -$initjs = ""; +$request_ref->{header} = ""; +$request_ref->{initjs} = ""; +$request_ref->{cc} = $cc; if ((not defined $cc) or (not defined $lc)) { die("$usage\nError: Pass country code (or world) and language code as arguments.\n"); @@ -143,7 +145,6 @@ ($jsonl_path, $country, $verbose = undef) my %map_options = (uk => "map.setView(new L.LatLng(54.0617609,-3.4433238),6);",); -my $request_ref = {}; my $graph_ref = {}; $log->info("finding products", {lc => $lc, cc => $cc, country => $country}) if $log->is_info(); @@ -154,8 +155,8 @@ ($jsonl_path, $country, $verbose = undef) $request_ref->{map_options} = $map_options{$cc} || ""; my $map_html = map_of_products($products_iter, $request_ref, $graph_ref); -$html =~ s/
/$header/; -$html =~ s//$initjs/; +$html =~ s/
/$request_ref->{header}/; +$html =~ s//$request_ref->{initjs}/; $html =~ s//$map_html/; binmode(STDOUT, ":encoding(UTF-8)"); diff --git a/scripts/scanbot.pl b/scripts/scanbot.pl index 8cc60259b92b1..5e574f44c6676 100755 --- a/scripts/scanbot.pl +++ b/scripts/scanbot.pl @@ -33,7 +33,7 @@ use ProductOpener::Paths qw/%BASE_DIRS ensure_dir_created_or_die/; use ProductOpener::Store qw/retrieve_json store store_json/; use ProductOpener::Index qw/:all/; -use ProductOpener::Display qw/$cc $country/; +use ProductOpener::Display qw/$country/; use ProductOpener::Tags qw/add_tags_to_field canonicalize_taxonomy_tag/; use ProductOpener::Users qw/:all/; use ProductOpener::Images qw/:all/; diff --git a/templates/emails/user_new_pro_account_admin_notification.tt.html b/templates/emails/user_new_pro_account_admin_notification.tt.html index 65cc45fb004ec..a9a2ff8cfa144 100644 --- a/templates/emails/user_new_pro_account_admin_notification.tt.html +++ b/templates/emails/user_new_pro_account_admin_notification.tt.html @@ -54,7 +54,7 @@ [% user.org_id or user.requested_org_id %] Signed up to the platform on [% display_date_ymd(time_t) %] - $cc + [% cc %] [% user.name %] [% user.org_id or user.requested_org_id %] diff --git a/templates/web/pages/tag/tag.tt.html b/templates/web/pages/tag/tag.tt.html index 67a7196d269a7..4e1bbb35dd4ef 100644 --- a/templates/web/pages/tag/tag.tt.html +++ b/templates/web/pages/tag/tag.tt.html @@ -83,7 +83,7 @@

} // Adding language code to get translated data - params += "&lang_code=[% lc %]&country=[% cc %]"; + params += "&lang_code=[% lc %]&country=[% country_code %]"; fetch(facet_kp + params) .then((response) => { diff --git a/tests/unit/display.t b/tests/unit/display.t index 94d87c688d52c..13afa7af99689 100644 --- a/tests/unit/display.t +++ b/tests/unit/display.t @@ -14,7 +14,6 @@ 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), ''); @@ -52,6 +51,7 @@ $lc = 'en'; my $request_ref = { lc => "en", current_link => '/cgi/search.pl?action=process&sort_by=unique_scans_n&page_size=24', + cc => 'world', }; my $count = 25;