Skip to content

Commit efa1199

Browse files
committed
Bug 779747: The "Browse" link in the page header/footer doesn't sort products by classification
r=dkl a=LpSolit
1 parent 5a68f09 commit efa1199

3 files changed

Lines changed: 54 additions & 22 deletions

File tree

Bugzilla/Classification.pm

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use Bugzilla::Util;
1515
use Bugzilla::Error;
1616
use Bugzilla::Product;
1717

18-
use base qw(Bugzilla::Field::ChoiceInterface Bugzilla::Object);
18+
use base qw(Bugzilla::Field::ChoiceInterface Bugzilla::Object Exporter);
19+
@Bugzilla::Classification::EXPORT = qw(sort_products_by_classification);
1920

2021
###############################
2122
#### Initialization ####
@@ -152,6 +153,38 @@ sub products {
152153
sub description { return $_[0]->{'description'}; }
153154
sub sortkey { return $_[0]->{'sortkey'}; }
154155

156+
157+
###############################
158+
#### Helpers ####
159+
###############################
160+
161+
# This function is a helper to sort products to be listed
162+
# in global/choose-product.html.tmpl.
163+
164+
sub sort_products_by_classification {
165+
my $products = shift;
166+
my $list;
167+
168+
if (Bugzilla->params->{'useclassification'}) {
169+
my $class = {};
170+
# Get all classifications with at least one product.
171+
foreach my $product (@$products) {
172+
$class->{$product->classification_id}->{'object'} ||=
173+
new Bugzilla::Classification($product->classification_id);
174+
# Nice way to group products per classification, without querying
175+
# the DB again.
176+
push(@{$class->{$product->classification_id}->{'products'}}, $product);
177+
}
178+
$list = [sort {$a->{'object'}->sortkey <=> $b->{'object'}->sortkey
179+
|| lc($a->{'object'}->name) cmp lc($b->{'object'}->name)}
180+
(values %$class)];
181+
}
182+
else {
183+
$list = [{object => undef, products => $products}];
184+
}
185+
return $list;
186+
}
187+
155188
1;
156189

157190
__END__
@@ -208,4 +241,21 @@ A Classification is a higher-level grouping of Products.
208241
209242
=back
210243
244+
=head1 SUBROUTINES
245+
246+
=over
247+
248+
=item C<sort_products_by_classification>
249+
250+
Description: This is a helper which returns a list of products sorted
251+
by classification in a form suitable to be passed to the
252+
global/choose-product.html.tmpl template.
253+
254+
Params: An arrayref of product objects.
255+
256+
Returns: An arrayref of hashes suitable to be passed to
257+
global/choose-product.html.tmpl.
258+
259+
=back
260+
211261
=cut

describecomponents.cgi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use Bugzilla;
1313
use Bugzilla::Constants;
1414
use Bugzilla::Util;
1515
use Bugzilla::Error;
16+
use Bugzilla::Classification;
1617
use Bugzilla::Product;
1718

1819
my $user = Bugzilla->login();
@@ -40,7 +41,7 @@ unless ($product && $user->can_access_product($product->name)) {
4041
# product only, to not confuse the user with components of a
4142
# product he didn't request.
4243
elsif (scalar(@products) > 1 || $product_name) {
43-
$vars->{'classifications'} = [{object => undef, products => \@products}];
44+
$vars->{'classifications'} = sort_products_by_classification(\@products);
4445
$vars->{'target'} = "describecomponents.cgi";
4546
# If an invalid product name is given, or the user is not
4647
# allowed to access that product, a message is displayed

enter_bug.cgi

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ use Bugzilla::Constants;
2525
use Bugzilla::Util;
2626
use Bugzilla::Error;
2727
use Bugzilla::Bug;
28-
use Bugzilla::User;
2928
use Bugzilla::Hook;
30-
use Bugzilla::Product;
3129
use Bugzilla::Classification;
32-
use Bugzilla::Keyword;
3330
use Bugzilla::Token;
3431
use Bugzilla::Field;
3532
use Bugzilla::Status;
@@ -67,23 +64,7 @@ if ($product_name eq '') {
6764
my @classifications;
6865

6966
unless ($classification && $classification ne '__all') {
70-
if (Bugzilla->params->{'useclassification'}) {
71-
my $class;
72-
# Get all classifications with at least one enterable product.
73-
foreach my $product (@enterable_products) {
74-
$class->{$product->classification_id}->{'object'} ||=
75-
new Bugzilla::Classification($product->classification_id);
76-
# Nice way to group products per classification, without querying
77-
# the DB again.
78-
push(@{$class->{$product->classification_id}->{'products'}}, $product);
79-
}
80-
@classifications = sort {$a->{'object'}->sortkey <=> $b->{'object'}->sortkey
81-
|| lc($a->{'object'}->name) cmp lc($b->{'object'}->name)}
82-
(values %$class);
83-
}
84-
else {
85-
@classifications = ({object => undef, products => \@enterable_products});
86-
}
67+
@classifications = @{sort_products_by_classification(\@enterable_products)};
8768
}
8869

8970
unless ($classification) {

0 commit comments

Comments
 (0)