From ff0f7fbf19c6cb7acadea3aa7ae1b2edaabafd3b Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Thu, 28 Aug 2014 15:36:37 +0400 Subject: [PATCH] Remove duplicated choose classification code --- Bugzilla/Product.pm | 73 +++++++++++++- describecomponents.cgi | 37 +------ enter_bug.cgi | 96 +------------------ .../global/choose-classification.html.tmpl | 4 +- .../default/global/choose-product.html.tmpl | 2 +- 5 files changed, 78 insertions(+), 134 deletions(-) diff --git a/Bugzilla/Product.pm b/Bugzilla/Product.pm index 721d8d9b4..f8c0476a7 100644 --- a/Bugzilla/Product.pm +++ b/Bugzilla/Product.pm @@ -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 diff --git a/describecomponents.cgi b/describecomponents.cgi index 0e855b754..4a12b24d7 100755 --- a/describecomponents.cgi +++ b/describecomponents.cgi @@ -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; diff --git a/enter_bug.cgi b/enter_bug.cgi index 647021e83..fcf9c7fb1 100755 --- a/enter_bug.cgi +++ b/enter_bug.cgi @@ -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 { diff --git a/template/en/default/global/choose-classification.html.tmpl b/template/en/default/global/choose-classification.html.tmpl index e0d153be2..012d07b97 100644 --- a/template/en/default/global/choose-classification.html.tmpl +++ b/template/en/default/global/choose-classification.html.tmpl @@ -38,7 +38,7 @@ - + @@ -46,7 +46,7 @@ [% FOREACH class = classifications %] - + [% END %] diff --git a/template/en/default/global/choose-product.html.tmpl b/template/en/default/global/choose-product.html.tmpl index a2e19427e..7e3208283 100644 --- a/template/en/default/global/choose-product.html.tmpl +++ b/template/en/default/global/choose-product.html.tmpl @@ -56,7 +56,7 @@ [% FOREACH p = c.products %] - + [% END %]
AllAll Show all products

[% class.name | html %][% class.name | html %] [% class.description | html_light %]
[% p.name | html %][% p.name | html %] [% p.description | html_light %]