Skip to content

Commit

Permalink
fix: display percent and sort options (#9440)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet authored Nov 30, 2023
1 parent d3f8740 commit 90f96ee
Showing 1 changed file with 56 additions and 21 deletions.
77 changes: 56 additions & 21 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,26 @@ sub query_list_of_tags ($request_ref, $query_ref) {
$page = 1;
}

# by default sort tags by descending product count
my $default_sort_by = "count";

# except for scores where we sort alphabetically (A to E, and 1 to 4)
if (($groupby_tagtype =~ /^nutriscore|nutrition_grades|ecoscore|nova_groups/)) {
$default_sort_by = "tag";
}

# allow sorting by tagname
my $sort_by = request_param($request_ref, "sort_by") // $default_sort_by;
my $sort_ref;

if ($sort_by eq "tag") {
$sort_ref = {"_id" => 1};
}
else {
$sort_ref = {"count" => -1};
$sort_by = "count";
}

# groupby_tagtype

my $aggregate_count_parameters = [
Expand All @@ -1614,7 +1634,7 @@ sub query_list_of_tags ($request_ref, $query_ref) {
{"\$match" => $query_ref},
{"\$unwind" => ("\$" . $groupby_tagtype . "_tags")},
{"\$group" => {"_id" => ("\$" . $groupby_tagtype . "_tags"), "count" => {"\$sum" => 1}}},
{"\$sort" => {"count" => -1}},
{"\$sort" => $sort_ref},
{"\$skip" => $skip},
{"\$limit" => $limit}
];
Expand All @@ -1623,16 +1643,7 @@ sub query_list_of_tags ($request_ref, $query_ref) {
$aggregate_parameters = [
{"\$match" => $query_ref},
{"\$group" => {"_id" => ("\$creator"), "count" => {"\$sum" => 1}}},
{"\$sort" => {"count" => -1}}
];
}

if (($groupby_tagtype eq 'nutrition_grades') or ($groupby_tagtype eq 'nova_groups')) {
$aggregate_parameters = [
{"\$match" => $query_ref},
{"\$unwind" => ("\$" . $groupby_tagtype . "_tags")},
{"\$group" => {"_id" => ("\$" . $groupby_tagtype . "_tags"), "count" => {"\$sum" => 1}}},
{"\$sort" => {"_id" => 1}}
{"\$sort" => $sort_ref}
];
}

Expand Down Expand Up @@ -1757,12 +1768,18 @@ sub query_list_of_tags ($request_ref, $query_ref) {
}
}

return $results;
return ($results, $sort_by);
}

sub display_list_of_tags ($request_ref, $query_ref) {

my $results = query_list_of_tags($request_ref, $query_ref);
my ($results, $sort_by) = query_list_of_tags($request_ref, $query_ref);

# Column that will be sorted by using JS
my $sort_order = '[[ 1, "desc" ]]';
if ($sort_by eq "tag") {
$sort_order = '[[ 0, "asc" ]]';
}

my $html = '';
my $html_pages = '';
Expand Down Expand Up @@ -1886,6 +1903,15 @@ sub display_list_of_tags ($request_ref, $query_ref) {
$log->debug("missing_property defined", {missing_property => $missing_property});
}

# display_percent parameter: display the percentage of products for each tag
# This is useful only for tags that have unique values like Nutri-Score and Eco-Score
my $display_percent = single_param("display_percent");
foreach my $tagcount_ref (@tags) {
my $count = $tagcount_ref->{count};
$stats{all_tags}++;
$stats{all_tags_products} += $count;
}

foreach my $tagcount_ref (@tags) {

$i++;
Expand All @@ -1907,9 +1933,6 @@ sub display_list_of_tags ($request_ref, $query_ref) {

$products{$tagid} = $count;

$stats{all_tags}++;
$stats{all_tags_products} += $count;

my $link;
my $products = $count;
if ($products == 0) {
Expand Down Expand Up @@ -2069,7 +2092,8 @@ sub display_list_of_tags ($request_ref, $query_ref) {
. $grade
. "\" title=\"$Lang{nutrition_grade_fr_alt}{$lc} "
. $grade
. "\" style=\"max-height:80px;\">";
. "\" style=\"max-height:80px;\"> "
. $grade;
}
elsif ($tagtype eq 'ecoscore') {
my $grade;
Expand All @@ -2088,7 +2112,8 @@ sub display_list_of_tags ($request_ref, $query_ref) {
. $grade
. "\" title=\"$Lang{ecoscore}{$lc} "
. $grade
. "\" style=\"max-height:80px;\">";
. "\" style=\"max-height:80px;\"> "
. $grade;
}
elsif ($tagtype eq 'nova_groups') {
if ($tagid =~ /^en:(1|2|3|4)/) {
Expand All @@ -2113,10 +2138,20 @@ sub display_list_of_tags ($request_ref, $query_ref) {
$display = display_tag_name($tagtype, $display);
}

# Display the percent of products for each tag
my $percent = '';
if (($display_percent) and ($stats{all_tags})) {
$percent = ' (' . sprintf("%2.2f", $products / $stats{all_tags_products} * 100) . '%)';
}

$css_class =~ s/^\s+|\s+$//g;
$info .= ' class="' . $css_class . '"';
$html .= "<a href=\"$tag_link\"$info$nofollow>" . $display . "</a>";
$html .= "</td>\n<td style=\"text-align:right\">$products</td>" . $td_nutriments . $extra_td . "</tr>\n";
$html
.= "</td>\n<td style=\"text-align:right\"><a href=\"$tag_link\"$info$nofollow>${products}${percent}</a></td>"
. $td_nutriments
. $extra_td
. "</tr>\n";

my $tagentry = {
id => $tagid,
Expand Down Expand Up @@ -2408,7 +2443,7 @@ oTable = \$('#tagstable').DataTable({
infoFiltered: " - $Lang{tagstable_filtered}{$lang}"
},
paging: false,
order: [[ 1, "desc" ]],
order: $sort_order,
columns: [
null,
{"searchable": false} $extra_column_searchable
Expand Down Expand Up @@ -2436,7 +2471,7 @@ HEADER

sub display_list_of_tags_translate ($request_ref, $query_ref) {

my $results = query_list_of_tags($request_ref, $query_ref);
my ($results, $sort_by) = query_list_of_tags($request_ref, $query_ref);

my $html = '';
my $html_pages = '';
Expand Down

0 comments on commit 90f96ee

Please sign in to comment.