@@ -15,7 +15,8 @@ use Bugzilla::Util;
1515use Bugzilla::Error;
1616use 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 {
152153sub description { return $_ [0]-> {' description' }; }
153154sub 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+
1551881;
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
0 commit comments