Remove duplicated choose classification code

hinted-selects
Vitaliy Filippov 2014-08-28 15:36:37 +04:00
parent b2c0bc2e04
commit ff0f7fbf19
5 changed files with 78 additions and 134 deletions

View File

@ -1111,8 +1111,79 @@ sub check
# So return all products visible to current user.
sub get_all { @{ Bugzilla->user->get_selectable_products } }
1;
###############################
#### Class Methods ######
###############################
# FIXME: This is a "controller" method and should probably be moved out from "model" class Product
sub choose_product
{
my $class = shift;
my ($products, $query_params, $target) = @_;
$products ||= Bugzilla->user->get_enterable_products;
ThrowUserError('no_products') unless @$products;
return $products->[0] if @$products == 1;
delete $query_params->{classification};
$query_params = http_build_query($query_params);
$query_params .= '&' if length $query_params;
if (!$target)
{
$target = $ENV{REQUEST_URI};
$target =~ s/\?.*//so;
$target =~ s/^\/+//so;
}
my $vars = {
target => $target,
query_params => $query_params,
};
if (Bugzilla->get_field('classification')->enabled)
{
my $classifs;
push @{$classifs->{$_->classification_id}}, $_ for @$products;
$classifs = [
map { { object => $_, products => $classifs->{$_->id} } }
@{ Bugzilla::Classification->new_from_list([ keys %$classifs ]) }
];
if (scalar @$classifs == 1)
{
$vars->{classifications} = [ $classifs->[0] ];
if (scalar @{$classifs->[0]->{products}} == 1)
{
return $classifs->[0]->{products}->[0];
}
}
else
{
my $cl = Bugzilla->input_params->{classification};
if (!$cl || $cl ne '__all' && !(($cl) = grep { $_->{object}->name eq $cl } @$classifs))
{
$vars->{classifications} = [ map { $_->{object} } @$classifs ];
Bugzilla->template->process("global/choose-classification.html.tmpl", $vars)
|| ThrowTemplateError(Bugzilla->template->error());
exit;
}
elsif ($cl eq '__all')
{
$vars->{classifications} = $classifs;
}
else
{
$vars->{classifications} = [ $cl ];
}
}
}
else
{
$vars->{classifications} = [ { object => undef, products => $products } ];
}
Bugzilla->template->process('global/choose-product.html.tmpl', $vars)
|| ThrowTemplateError(Bugzilla->template->error());
exit;
}
1;
__END__
=head1 NAME

View File

@ -18,43 +18,10 @@ my $ARGS = Bugzilla->cgi->VarHash;
Bugzilla->switch_to_shadow_db;
my $product = new Bugzilla::Product({ name => trim($ARGS->{product} || '') });
my $product = Bugzilla::Product->new({ name => trim($ARGS->{product} || '') });
unless ($product && $user->can_access_product($product->name))
{
# Products which the user is allowed to see.
my $products = $user->get_accessible_products;
if (!@$products)
{
ThrowUserError('no_products');
}
if (Bugzilla->get_field('classification')->enabled && $ARGS->{classification} ne '__all')
{
my $cl = Bugzilla::Classification->new({ name => trim($ARGS->{classification} || '') });
if (!$cl)
{
my $acc = [ keys %{ { map { $_->classification_id => 1 } @$products } } ];
$vars->{classifications} = Bugzilla::Classification->new_from_list($acc);
$vars->{target} = 'describecomponents.cgi';
$template->process('global/choose-classification.html.tmpl', $vars)
|| ThrowTemplateError($template->error());
exit;
}
$vars->{classifications} = [ {
object => $cl,
products => [ grep { $_->classification_id == $cl->id } @$products ],
} ];
}
else
{
$vars->{classifications} = [ {
object => undef,
products => $products
} ];
}
$vars->{target} = 'describecomponents.cgi';
$template->process('global/choose-product.html.tmpl', $vars)
|| ThrowTemplateError($template->error());
exit;
$product = Bugzilla::Product->choose_product($user->get_accessible_products);
}
$vars->{product} = $product;

View File

@ -74,101 +74,7 @@ my $product;
if ($product_name eq '')
{
# Save URL parameters
$vars->{query_params} = http_build_query($ARGS);
# If the user cannot enter bugs in any product, stop here.
my @enterable_products = @{$user->get_enterable_products};
ThrowUserError('no_products') unless scalar(@enterable_products);
my $classification = Bugzilla->get_field('classification')->enabled ? $ARGS->{classification} : '__all';
# Unless a real classification name is given, we sort products
# by classification.
my @classifications;
unless ($classification && $classification ne '__all')
{
if (Bugzilla->get_field('classification')->enabled)
{
my $class;
# Get all classifications with at least one enterable product.
foreach my $product (@enterable_products)
{
$class->{$product->classification_id}->{object} ||=
new Bugzilla::Classification($product->classification_id);
# Nice way to group products per classification, without querying
# the DB again.
push(@{$class->{$product->classification_id}->{products}}, $product);
}
@classifications = sort { $a->{object}->sortkey <=> $b->{object}->sortkey
|| lc($a->{object}->name) cmp lc($b->{object}->name) } values %$class;
}
else
{
@classifications = ({ object => undef, products => \@enterable_products });
}
}
unless ($classification)
{
# We know there is at least one classification available,
# else we would have stopped earlier.
if (scalar(@classifications) > 1)
{
# We only need classification objects.
$vars->{classifications} = [ map { $_->{object} } @classifications ];
$vars->{target} = "enter_bug.cgi";
$vars->{format} = $ARGS->{format};
$vars->{cloned_bug_id} = $ARGS->{cloned_bug_id};
$vars->{cloned_comment} = $ARGS->{cloned_comment};
$template->process("global/choose-classification.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
# If we come here, then there is only one classification available.
$classification = $classifications[0]->{object}->name;
}
# Keep only enterable products which are in the specified classification.
if ($classification ne "__all")
{
my $class = new Bugzilla::Classification({ name => $classification });
# If the classification doesn't exist, then there is no product in it.
if ($class)
{
@enterable_products = grep { $_->classification_id == $class->id } @enterable_products;
@classifications = ({object => $class, products => \@enterable_products});
}
else
{
@enterable_products = ();
}
}
if (scalar(@enterable_products) == 0)
{
ThrowUserError('no_products');
}
elsif (scalar(@enterable_products) > 1)
{
$vars->{classifications} = \@classifications;
$vars->{target} = 'enter_bug.cgi';
$vars->{format} = $ARGS->{format};
$vars->{cloned_bug_id} = $ARGS->{cloned_bug_id};
$vars->{cloned_comment} = $ARGS->{cloned_comment};
$template->process('global/choose-product.html.tmpl', $vars)
|| ThrowTemplateError($template->error());
exit;
}
else
{
# Only one product exists.
$product = $enterable_products[0];
}
$product = Bugzilla::Product->choose_product($user->get_enterable_products, $ARGS);
}
else
{

View File

@ -38,7 +38,7 @@
<table class="choose_product">
<tr class="all">
<th><a href="[% target | url_quote %]?classification=__all&[% query_params | html %]">All</a></th>
<th><a href="[% target %]?[% query_params | html %]classification=__all">All</a></th>
<td valign="top">Show all products</td>
</tr>
<tr class="all"><td colspan="2"><hr /></td></tr>
@ -46,7 +46,7 @@
<tbody>
[% FOREACH class = classifications %]
<tr>
<th><a href="[% target | url_quote %]?classification=[% class.name | url_quote -%]&[% query_params | html %]">[% class.name | html %]</a></th>
<th><a href="[% target %]?[% query_params | html %]classification=[% class.name | url_quote -%]">[% class.name | html %]</a></th>
<td>[% class.description | html_light %]</td>
</tr>
[% END %]

View File

@ -56,7 +56,7 @@
[% FOREACH p = c.products %]
<tr>
<th><a href="[% target %]?product=[% p.name | url_quote -%]&[% query_params | html %]">[% p.name | html %]</a></th>
<th><a href="[% target %]?[% query_params | html %]product=[% p.name | url_quote -%]">[% p.name | html %]</a></th>
<td>[% p.description | html_light %]</td>
</tr>
[% END %]