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: 3 fixes to make /products/[code1],[code2] work again #10669

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions html/js/product-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function rank_and_display_products(target, products, contributor_prefs) {

/* exported search_products */

function search_products(target, products, search_api_url) {
function search_products(target, products, search_api_url, contributor_prefs) {

// Retrieve generic search results from the search API

Expand All @@ -476,7 +476,7 @@ function search_products(target, products, search_api_url) {
if (data.products) {

Array.prototype.push.apply(products, data.products);
rank_and_display_products(target, products);
rank_and_display_products(target, products, contributor_prefs);
}
});
}
187 changes: 96 additions & 91 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4550,7 +4550,7 @@ JS
display_user_product_preferences("#preferences_selected", "#preferences_selection_form", function () {
rank_and_display_products("#search_results", products, contributor_prefs);
});
search_products("#search_results", products, "$search_api_url");
search_products("#search_results", products, "$search_api_url", contributor_prefs);
JS
;

Expand Down Expand Up @@ -5486,90 +5486,6 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa

if ($count > 0) {

# Show a download link only for search queries (and not for the home page of facets)

if ($request_ref->{search}) {
$request_ref->{current_link_query_download} = $request_ref->{current_link};
if ($request_ref->{current_link} =~ /\?/) {
$request_ref->{current_link_query_download} .= "&download=on";
}
else {
$request_ref->{current_link_query_download} .= "?download=on";
}
}

$template_data_ref->{current_link_query_download} = $request_ref->{current_link_query_download};
$template_data_ref->{export_limit} = $export_limit;

if ($log->is_debug()) {
my $debug_log = "search - count: $count";
defined $request_ref->{search} and $debug_log .= " - request_ref->{search}: " . $request_ref->{search};
defined $request_ref->{tagid2} and $debug_log .= " - tagid2 " . $request_ref->{tagid2};
$log->debug($debug_log);
}

if ( (not defined $request_ref->{search})
and ($count >= 5)
and (not defined $request_ref->{tagid2})
and (not defined $request_ref->{product_changes_saved}))
{
$template_data_ref->{explore_products} = 'true';
my $nofollow = '';
if (defined $request_ref->{tagid}) {
# Prevent crawlers from going too deep in facets #938:
# Make the 2nd facet level "nofollow"
$nofollow = ' rel="nofollow"';
}

my @current_drilldown_fields = @ProductOpener::Config::drilldown_fields;
if ($country eq 'en:world') {
unshift(@current_drilldown_fields, "countries");
}

foreach my $newtagtype (@current_drilldown_fields) {

# Eco-score: currently only for moderators

if ($newtagtype eq 'ecoscore') {
next if not(feature_enabled("ecoscore"));
}

push @{$template_data_ref->{current_drilldown_fields}},
{
current_link => get_owner_pretty_path() . $request_ref->{current_link},
tag_type_plural => $tag_type_plural{$newtagtype}{$lc},
nofollow => $nofollow,
tagtype => $newtagtype,
};
}
}

$template_data_ref->{separator_before_colon} = separator_before_colon($lc);
$template_data_ref->{jqm_loadmore} = $request_ref->{jqm_loadmore};

my $jqm = single_param("jqm"); # Assigning to a scalar to make sure we get a scalar

for my $product_ref (@{$request_ref->{structured_response}{products}}) {

my $product_display_name = $product_ref->{product_display_name};
# Prevent the quantity "750 g" to be split on two lines
$product_display_name =~ s/(.*) (.*?)/$1\ $2/;

push @{$template_data_ref->{structured_response_products}},
{
code => $product_ref->{code},
product_name => $product_display_name,
img => $product_ref->{image_front_small_html},
jqm => $jqm,
url => $product_ref->{product_url_path},
};

# remove some debug info
delete $product_ref->{additives};
delete $product_ref->{additives_prev};
delete $product_ref->{additives_next};
}

# For API queries, if the request specified a value for the fields parameter, return only the fields listed
# For non API queries with user preferences, we need to add attributes
# For non API queries, we need to compute attributes for personal search
Expand All @@ -5583,8 +5499,31 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa

my $customized_products_ref = [];

my $jqm = single_param("jqm"); # Assigning to a scalar to make sure we get a scalar

for my $product_ref (@{$request_ref->{structured_response}{products}}) {

# remove some debug info
delete $product_ref->{additives};
delete $product_ref->{additives_prev};
delete $product_ref->{additives_next};

# For non API queries, we will display the products in a template (this is for SEO, before personal search products are displayed through Javascript)
if (not defined $request_ref->{api}) {
my $product_display_name = $product_ref->{product_display_name};
# Prevent the quantity "750 g" to be split on two lines
$product_display_name =~ s/(.*) (.*?)/$1\ $2/;

push @{$template_data_ref->{structured_response_products}},
{
code => $product_ref->{code},
product_name => $product_display_name,
img => $product_ref->{image_front_small_html},
jqm => $jqm,
url => $product_ref->{product_url_path},
};
}

my $customized_product_ref = customize_response_for_product($request_ref, $product_ref, $fields);

push @{$customized_products_ref}, $customized_product_ref;
Expand All @@ -5611,12 +5550,76 @@ sub search_and_display_products ($request_ref, $query_ref, $sort_by, $limit, $pa
}
}

$template_data_ref->{request} = $request_ref;
$template_data_ref->{page_count} = $page_count;
$template_data_ref->{page_limit} = $limit;
$template_data_ref->{page} = $page;
$template_data_ref->{current_link} = $request_ref->{current_link};
$template_data_ref->{pagination} = display_pagination($request_ref, $count, $limit, $page);
# Compute data needed for the template to display the search results
if (not defined $request_ref->{api}) {

# Show a download link only for search queries (and not for the home page of facets)

if ($request_ref->{search}) {
$request_ref->{current_link_query_download} = $request_ref->{current_link};
if ($request_ref->{current_link} =~ /\?/) {
$request_ref->{current_link_query_download} .= "&download=on";
}
else {
$request_ref->{current_link_query_download} .= "?download=on";
}
}

$template_data_ref->{current_link_query_download} = $request_ref->{current_link_query_download};
$template_data_ref->{export_limit} = $export_limit;

if ($log->is_debug()) {
my $debug_log = "search - count: $count";
defined $request_ref->{search} and $debug_log .= " - request_ref->{search}: " . $request_ref->{search};
defined $request_ref->{tagid2} and $debug_log .= " - tagid2 " . $request_ref->{tagid2};
$log->debug($debug_log);
}

if ( (not defined $request_ref->{search})
and ($count >= 5)
and (not defined $request_ref->{tagid2})
and (not defined $request_ref->{product_changes_saved}))
{
$template_data_ref->{explore_products} = 'true';
my $nofollow = '';
if (defined $request_ref->{tagid}) {
# Prevent crawlers from going too deep in facets #938:
# Make the 2nd facet level "nofollow"
$nofollow = ' rel="nofollow"';
}

my @current_drilldown_fields = @ProductOpener::Config::drilldown_fields;
if ($country eq 'en:world') {
unshift(@current_drilldown_fields, "countries");
}

foreach my $newtagtype (@current_drilldown_fields) {

# Eco-score: currently only for moderators

if ($newtagtype eq 'ecoscore') {
next if not(feature_enabled("ecoscore"));
}

push @{$template_data_ref->{current_drilldown_fields}},
{
current_link => get_owner_pretty_path() . $request_ref->{current_link},
tag_type_plural => $tag_type_plural{$newtagtype}{$lc},
nofollow => $nofollow,
tagtype => $newtagtype,
};
}
}

$template_data_ref->{separator_before_colon} = separator_before_colon($lc);
$template_data_ref->{jqm_loadmore} = $request_ref->{jqm_loadmore};
$template_data_ref->{request} = $request_ref;
$template_data_ref->{page_count} = $page_count;
$template_data_ref->{page_limit} = $limit;
$template_data_ref->{page} = $page;
$template_data_ref->{current_link} = $request_ref->{current_link};
$template_data_ref->{pagination} = display_pagination($request_ref, $count, $limit, $page);
}
}

# if cc and/or lc have been overridden, change the relative paths to absolute paths using the new subdomain
Expand Down Expand Up @@ -5761,6 +5764,8 @@ sub display_pagination ($request_ref, $count, $limit, $page) {
my $html = '';
my $html_pages = '';

$log->debug("PAGINATION: START\n", {count => $count, limit => $limit, page => $page}) if $log->is_debug();

my $nb_pages = int(($count - 1) / $limit) + 1;

my $current_link = $request_ref->{current_link};
Expand Down
10 changes: 8 additions & 2 deletions lib/ProductOpener/Routing.pm
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ sub properties_route($request_ref) {
# products/[code](+[code])*
# e.g. /8024884500403+3263855093192
sub products_route($request_ref) {
param("code", $request_ref->{components}[0]);
param("code", $request_ref->{components}[1]);
$request_ref->{search} = 1;
set_request_stats_value($request_ref->{stats}, "route", "search");
return 1;
Expand Down Expand Up @@ -613,7 +613,13 @@ sub register_route($routes_to_register) {
}
else {
# use a hash key for fast match
$routes{$pattern} = {handler => $handler, opt => $opt};
# do not overwrite existing routes (e.g. a text route that matches a well known route)
if (exists $routes{$pattern}) {
$log->warn("route already exists", {pattern => $pattern}) if $log->is_warn();
}
else {
$routes{$pattern} = {handler => $handler, opt => $opt};
}
}
}
return 1;
Expand Down
Loading
Loading