Bug 300532: Update editversions.cgi to use routines from Version.pm and Product.pm - Patch by Tiago R. Mello <timello@async.com.br> r=LpSolit a=myk

This commit is contained in:
lpsolit%gmail.com 2005-07-19 00:05:11 +00:00
Родитель f18712f92b
Коммит 4948577775
3 изменённых файлов: 168 добавлений и 188 удалений

Просмотреть файл

@ -205,6 +205,33 @@ sub get_products_by_classification ($) {
return $products;
}
sub get_all_products () {
my $dbh = Bugzilla->dbh;
my $ids = $dbh->selectcol_arrayref(q{
SELECT id FROM products ORDER BY name});
my @products;
foreach my $id (@$ids) {
push @products, new Bugzilla::Product($id);
}
return @products;
}
sub check_product ($) {
my ($product_name) = @_;
unless ($product_name) {
ThrowUserError('product_not_specified');
}
my $product = new Bugzilla::Product({name => $product_name});
unless ($product) {
ThrowUserError('product_doesnt_exist',
{'product' => $product_name});
}
return $product;
}
1;
__END__
@ -326,11 +353,28 @@ Product.pm represents a product object.
Description: Returns all products for a specific classification id.
Params: none.
Params: $class_id - Integer with classification id.
Returns: A hash with product id as key and a Bugzilla::Product
object as value.
=item C<get_all_products()>
Description: Returns all products from the database.
Params: none.
Returns: Bugzilla::Product object list.
=item C<check_product($product_name)>
Description: Checks if the product name was passed in and if is a valid
product.
Params: $product_name - String with a product name.
Returns: Bugzilla::Product object.
=back
=cut

Просмотреть файл

@ -73,11 +73,24 @@ sub _init {
return $self;
}
sub bug_count {
my $self = shift;
my $dbh = Bugzilla->dbh;
if (!defined $self->{'bug_count'}) {
$self->{'bug_count'} = $dbh->selectrow_array(qq{
SELECT COUNT(*) FROM bugs
WHERE product_id = ? AND version = ?}, undef,
($self->product_id, $self->name)) || 0;
}
return $self->{'bug_count'};
}
###############################
##### Accessors ####
###############################
sub value { return $_[0]->{'value'}; }
sub name { return $_[0]->{'value'}; }
sub product_id { return $_[0]->{'product_id'}; }
###############################
@ -103,12 +116,24 @@ sub get_versions_by_product ($) {
SELECT value FROM versions
WHERE product_id = ?}, undef, $product_id);
my $versions;
my @versions;
foreach my $value (@$values) {
$versions->{$value} = new Bugzilla::Version($product_id,
$value);
push @versions, new Bugzilla::Version($product_id, $value);
}
return $versions;
return @versions;
}
sub check_version ($$) {
my ($product, $version_name) = @_;
$version_name || ThrowUserError('version_not_specified');
my $version = new Bugzilla::Version($product->id, $version_name);
unless ($version) {
ThrowUserError('version_not_valid',
{'product' => $product->name,
'version' => $version_name});
}
return $version;
}
1;
@ -131,6 +156,9 @@ Bugzilla::Version - Bugzilla product version class.
my $hash_ref = Bugzilla::Version::get_versions_by_product(1);
my $version = $hash_ref->{'version_value'};
my $version = Bugzilla::Version::check_version($product_obj,
'acme_version');
=head1 DESCRIPTION
Version.pm represents a Product Version object.
@ -144,11 +172,19 @@ Version.pm represents a Product Version object.
Description: The constructor is used to load an existing version
by passing a product id and a version value.
Params: $product_id - Integer with a Bugzilla product id.
Params: $product_id - Integer with a product id.
$value - String with a version value.
Returns: A Bugzilla::Version object.
=item C<bug_count()>
Description: Returns the total of bugs that belong to the version.
Params: none.
Returns: Integer with the number of bugs.
=back
=head1 SUBROUTINES
@ -157,13 +193,21 @@ Version.pm represents a Product Version object.
=item C<get_versions_by_product($product_id)>
Description: Returns all Bugzilla product versions that belong
Description: Returns all product versions that belong
to the supplied product.
Params: $product_id - Integer with a Bugzilla product id.
Params: $product_id - Integer with a product id.
Returns: A hash with version value as key and a Bugzilla::Version
objects as value.
Returns: Bugzilla::Version object list.
=item C<check_version($product, $version_name)>
Description: Checks if the version name exists for the product name.
Params: $product - A Bugzilla::Product object.
$version_name - String with a version name.
Returns: Bugzilla::Version object.
=back

Просмотреть файл

@ -37,82 +37,21 @@ require "globals.pl";
use Bugzilla::Constants;
use Bugzilla::Config qw(:DEFAULT $datadir);
use Bugzilla::User;
use Bugzilla::Product;
use Bugzilla::Version;
use vars qw($template $vars);
my $cgi = Bugzilla->cgi;
my $dbh = Bugzilla->dbh;
# TestProduct: just returns if the specified product does exists
# CheckProduct: same check, optionally emit an error text
# TestVersion: just returns if the specified product/version combination exists
# CheckVersion: same check, optionally emit an error text
sub TestProduct ($)
{
my $prod = shift;
# does the product exist?
SendSQL("SELECT name
FROM products
WHERE name = " . SqlQuote($prod));
return FetchOneColumn();
}
sub CheckProduct ($)
{
my $prod = shift;
# do we have a product?
unless ($prod) {
ThrowUserError('product_not_specified');
}
unless (TestProduct $prod) {
ThrowUserError('product_doesnt_exist',
{'product' => $prod});
}
}
sub TestVersion ($$)
{
my ($prod,$ver) = @_;
# does the product exist?
SendSQL("SELECT products.name, value
FROM versions, products
WHERE versions.product_id = products.id
AND products.name = " . SqlQuote($prod) . "
AND value = " . SqlQuote($ver));
return FetchOneColumn();
}
sub CheckVersion ($$)
{
my ($prod, $ver) = @_;
# do we have the version?
unless ($ver) {
ThrowUserError('version_not_specified');
}
CheckProduct($prod);
unless (TestVersion $prod, $ver) {
ThrowUserError('version_not_valid',
{'product' => $prod,
'version' => $ver});
}
}
#
# Preliminary checks:
#
Bugzilla->login(LOGIN_REQUIRED);
print Bugzilla->cgi->header();
print $cgi->header();
UserInGroup("editcomponents")
|| ThrowUserError("auth_failure", {group => "editcomponents",
@ -122,34 +61,17 @@ UserInGroup("editcomponents")
#
# often used variables
#
my $product = trim($cgi->param('product') || '');
my $version = trim($cgi->param('version') || '');
my $action = trim($cgi->param('action') || '');
my $product_name = trim($cgi->param('product') || '');
my $version_name = trim($cgi->param('version') || '');
my $action = trim($cgi->param('action') || '');
#
# product = '' -> Show nice list of versions
# product = '' -> Show nice list of products
#
unless ($product) {
my @products = ();
SendSQL("SELECT products.name, products.description
FROM products
ORDER BY products.name");
while ( MoreSQLData() ) {
my ($product, $description) = FetchSQLData();
my $prod = {};
$prod->{'name'} = $product;
$prod->{'description'} = $description;
push(@products, $prod);
}
unless ($product_name) {
my @products = Bugzilla::Product::get_all_products();
$vars->{'products'} = \@products;
$template->process("admin/versions/select-product.html.tmpl",
$vars)
@ -158,33 +80,17 @@ unless ($product) {
exit;
}
my $product = Bugzilla::Product::check_product($product_name);
#
# action='' -> Show nice list of versions
#
unless ($action) {
my @versions =
Bugzilla::Version::get_versions_by_product($product->id);
CheckProduct($product);
my $product_id = get_product_id($product);
my @versions = ();
SendSQL("SELECT value
FROM versions
WHERE product_id = $product_id
ORDER BY value");
while ( MoreSQLData() ) {
my $name = FetchOneColumn();
my $version = {};
$version->{'name'} = $name;
push(@versions, $version);
}
$vars->{'product'} = $product;
$vars->{'product'} = $product->name;
$vars->{'versions'} = \@versions;
$template->process("admin/versions/list.html.tmpl",
$vars)
@ -204,10 +110,7 @@ unless ($action) {
if ($action eq 'add') {
CheckProduct($product);
my $product_id = get_product_id($product);
$vars->{'product'} = $product;
$vars->{'product'} = $product->name;
$template->process("admin/versions/create.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
@ -223,33 +126,26 @@ if ($action eq 'add') {
if ($action eq 'new') {
CheckProduct($product);
my $product_id = get_product_id($product);
# Cleanups and valididy checks
$version_name || ThrowUserError('version_blank_name');
unless ($version) {
ThrowUserError('version_blank_name',
{'name' => $version});
}
if (TestVersion($product,$version)) {
my $version = new Bugzilla::Version($product->id, $version_name);
if ($version) {
ThrowUserError('version_already_exists',
{'name' => $version,
'product' => $product});
{'name' => $version->name,
'product' => $product->name});
}
# Add the new version
SendSQL("INSERT INTO versions ( " .
"value, product_id" .
" ) VALUES ( " .
SqlQuote($version) . ", $product_id)");
trick_taint($version_name);
$dbh->do("INSERT INTO versions (value, product_id)
VALUES (?, ?)", undef, ($version_name, $product->id));
# Make versioncache flush
unlink "$datadir/versioncache";
$vars->{'name'} = $version;
$vars->{'product'} = $product;
$vars->{'name'} = $version_name;
$vars->{'product'} = $product->name;
$template->process("admin/versions/created.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
@ -268,18 +164,13 @@ if ($action eq 'new') {
if ($action eq 'del') {
CheckVersion($product, $version);
my $product_id = get_product_id($product);
SendSQL("SELECT count(bug_id)
FROM bugs
WHERE product_id = $product_id
AND version = " . SqlQuote($version));
my $bugs = FetchOneColumn() || 0;
my $version = Bugzilla::Version::check_version($product,
$version_name);
my $bugs = $version->bug_count;
$vars->{'bug_count'} = $bugs;
$vars->{'name'} = $version;
$vars->{'product'} = $product;
$vars->{'name'} = $version->name;
$vars->{'product'} = $product->name;
$template->process("admin/versions/confirm-delete.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());
@ -294,29 +185,24 @@ if ($action eq 'del') {
#
if ($action eq 'delete') {
CheckVersion($product, $version);
my $product_id = get_product_id($product);
trick_taint($version);
my $nb_bugs =
$dbh->selectrow_array("SELECT COUNT(bug_id) FROM bugs
WHERE product_id = ? AND version = ?",
undef, ($product_id, $version));
my $version = Bugzilla::Version::check_version($product,
$version_name);
# The version cannot be removed if there are bugs
# associated with it.
if ($nb_bugs) {
ThrowUserError("version_has_bugs", { nb => $nb_bugs });
if ($version->bug_count) {
ThrowUserError("version_has_bugs",
{ nb => $version->bug_count });
}
$dbh->do("DELETE FROM versions WHERE product_id = ? AND value = ?",
undef, ($product_id, $version));
undef, ($product->id, $version->name));
unlink "$datadir/versioncache";
$vars->{'name'} = $version;
$vars->{'product'} = $product;
$vars->{'name'} = $version->name;
$vars->{'product'} = $product->name;
$template->process("admin/versions/deleted.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
@ -333,11 +219,11 @@ if ($action eq 'delete') {
if ($action eq 'edit') {
CheckVersion($product,$version);
my $product_id = get_product_id($product);
my $version = Bugzilla::Version::check_version($product,
$version_name);
$vars->{'name'} = $version;
$vars->{'product'} = $product;
$vars->{'name'} = $version->name;
$vars->{'product'} = $product->name;
$template->process("admin/versions/edit.html.tmpl",
$vars)
@ -354,10 +240,11 @@ if ($action eq 'edit') {
if ($action eq 'update') {
my $versionold = trim($cgi->param('versionold') || '');
CheckVersion($product,$versionold);
my $product_id = get_product_id($product);
$version_name || ThrowUserError('version_not_specified');
my $version_old_name = trim($cgi->param('versionold') || '');
my $version_old =
Bugzilla::Version::check_version($product,
$version_old_name);
# Note that the order of this tests is important. If you change
# them, be sure to test for WHERE='$version' or WHERE='$versionold'
@ -366,23 +253,28 @@ if ($action eq 'update') {
'versions WRITE',
'products READ');
if ($version ne $versionold) {
unless ($version) {
ThrowUserError('version_blank_name');
}
if (TestVersion($product,$version)) {
if ($version_name ne $version_old->name) {
my $version = new Bugzilla::Version($product->id,
$version_name);
if ($version) {
ThrowUserError('version_already_exists',
{'name' => $version,
'product' => $product});
{'name' => $version->name,
'product' => $product->name});
}
SendSQL("UPDATE bugs
SET version=" . SqlQuote($version) . "
WHERE version=" . SqlQuote($versionold) . "
AND product_id = $product_id");
SendSQL("UPDATE versions
SET value = " . SqlQuote($version) . "
WHERE product_id = $product_id
AND value = " . SqlQuote($versionold));
trick_taint($version_name);
$dbh->do("UPDATE bugs
SET version = ?
WHERE version = ? AND product_id = ?", undef,
($version_name, $version_old->name, $product->id));
$dbh->do("UPDATE versions
SET value = ?
WHERE product_id = ? AND value = ?", undef,
($version_name, $product->id, $version_old->name));
unlink "$datadir/versioncache";
$vars->{'updated_name'} = 1;
@ -390,8 +282,8 @@ if ($action eq 'update') {
$dbh->bz_unlock_tables();
$vars->{'name'} = $version;
$vars->{'product'} = $product;
$vars->{'name'} = $version_name;
$vars->{'product'} = $product->name;
$template->process("admin/versions/updated.html.tmpl",
$vars)
|| ThrowTemplateError($template->error());