Commit 461d51d5 authored by lpsolit%gmail.com's avatar lpsolit%gmail.com

Bug 471871: Bugzilla::Version has duplicated code compared to Bugzilla::Object…

Bug 471871: Bugzilla::Version has duplicated code compared to Bugzilla::Object (make Bugzilla::Version really a subclass of Bugzilla::Object) - Patch by Fré©ric Buclin <LpSolit@gmail.com> r/a=mkanat
parent cec56973
...@@ -240,7 +240,8 @@ sub create_default_product { ...@@ -240,7 +240,8 @@ sub create_default_product {
my $product = new Bugzilla::Product({name => $default_prod->{name}}); my $product = new Bugzilla::Product({name => $default_prod->{name}});
# The default version. # The default version.
Bugzilla::Version::create(Bugzilla::Version::DEFAULT_VERSION, $product); Bugzilla::Version->create({name => Bugzilla::Version::DEFAULT_VERSION,
product => $product});
# And we automatically insert the default milestone. # And we automatically insert the default milestone.
$dbh->do(q{INSERT INTO milestones (product_id, value, sortkey) $dbh->do(q{INSERT INTO milestones (product_id, value, sortkey)
......
...@@ -112,7 +112,7 @@ sub create { ...@@ -112,7 +112,7 @@ sub create {
my $product = $class->insert_create_data($params); my $product = $class->insert_create_data($params);
# Add the new version and milestone into the DB as valid values. # Add the new version and milestone into the DB as valid values.
Bugzilla::Version::create($version, $product); Bugzilla::Version->create({name => $version, product => $product});
Bugzilla::Milestone->create({name => $params->{defaultmilestone}, product => $product}); Bugzilla::Milestone->create({name => $params->{defaultmilestone}, product => $product});
# Create groups and series for the new product, if requested. # Create groups and series for the new product, if requested.
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# #
# Contributor(s): Tiago R. Mello <timello@async.com.br> # Contributor(s): Tiago R. Mello <timello@async.com.br>
# Max Kanat-Alexander <mkanat@bugzilla.org> # Max Kanat-Alexander <mkanat@bugzilla.org>
# Frédéric Buclin <LpSolit@gmail.com>
use strict; use strict;
...@@ -32,6 +33,10 @@ use Bugzilla::Error; ...@@ -32,6 +33,10 @@ use Bugzilla::Error;
use constant DEFAULT_VERSION => 'unspecified'; use constant DEFAULT_VERSION => 'unspecified';
use constant DB_TABLE => 'versions'; use constant DB_TABLE => 'versions';
use constant NAME_FIELD => 'value';
# This is "id" because it has to be filled in and id is probably the fastest.
# We do a custom sort in new_from_list below.
use constant LIST_ORDER => 'id';
use constant DB_COLUMNS => qw( use constant DB_COLUMNS => qw(
id id
...@@ -39,10 +44,26 @@ use constant DB_COLUMNS => qw( ...@@ -39,10 +44,26 @@ use constant DB_COLUMNS => qw(
product_id product_id
); );
use constant NAME_FIELD => 'value'; use constant REQUIRED_CREATE_FIELDS => qw(
# This is "id" because it has to be filled in and id is probably the fastest. name
# We do a custom sort in new_from_list below. product
use constant LIST_ORDER => 'id'; );
use constant UPDATE_COLUMNS => qw(
value
);
use constant VALIDATORS => {
product => \&_check_product,
};
use constant UPDATE_VALIDATORS => {
value => \&_check_value,
};
################################
# Methods
################################
sub new { sub new {
my $class = shift; my $class = shift;
...@@ -79,6 +100,18 @@ sub new_from_list { ...@@ -79,6 +100,18 @@ sub new_from_list {
return [sort { vers_cmp(lc($a->name), lc($b->name)) } @$list]; return [sort { vers_cmp(lc($a->name), lc($b->name)) } @$list];
} }
sub run_create_validators {
my $class = shift;
my $params = $class->SUPER::run_create_validators(@_);
my $product = delete $params->{product};
$params->{product_id} = $product->id;
$params->{value} = $class->_check_value($params->{name}, $product);
delete $params->{name};
return $params;
}
sub bug_count { sub bug_count {
my $self = shift; my $self = shift;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
...@@ -92,87 +125,71 @@ sub bug_count { ...@@ -92,87 +125,71 @@ sub bug_count {
return $self->{'bug_count'}; return $self->{'bug_count'};
} }
sub remove_from_db { sub update {
my $self = shift; my $self = shift;
my $dbh = Bugzilla->dbh; my ($changes, $old_self) = $self->SUPER::update(@_);
# The version cannot be removed if there are bugs if (exists $changes->{value}) {
# associated with it. my $dbh = Bugzilla->dbh;
if ($self->bug_count) { $dbh->do('UPDATE bugs SET version = ?
ThrowUserError("version_has_bugs", { nb => $self->bug_count }); WHERE version = ? AND product_id = ?',
undef, ($self->name, $old_self->name, $self->product_id));
} }
return $changes;
$dbh->do(q{DELETE FROM versions WHERE product_id = ? AND value = ?},
undef, ($self->product_id, $self->name));
} }
sub update { sub remove_from_db {
my $self = shift; my $self = shift;
my ($name, $product) = @_;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
$name || ThrowUserError('version_not_specified'); # The version cannot be removed if there are bugs
# associated with it.
# Remove unprintable characters if ($self->bug_count) {
$name = clean_text($name); ThrowUserError("version_has_bugs", { nb => $self->bug_count });
return 0 if ($name eq $self->name);
my $version = new Bugzilla::Version({ product => $product, name => $name });
if ($version) {
ThrowUserError('version_already_exists',
{'name' => $version->name,
'product' => $product->name});
} }
$self->SUPER::remove_from_db();
trick_taint($name);
$dbh->do("UPDATE bugs SET version = ?
WHERE version = ? AND product_id = ?", undef,
($name, $self->name, $self->product_id));
$dbh->do("UPDATE versions SET value = ?
WHERE product_id = ? AND value = ?", undef,
($name, $self->product_id, $self->name));
$self->{'value'} = $name;
return 1;
} }
############################### ###############################
##### Accessors #### ##### Accessors ####
############################### ###############################
sub name { return $_[0]->{'value'}; }
sub product_id { return $_[0]->{'product_id'}; } sub product_id { return $_[0]->{'product_id'}; }
############################### sub product {
##### Subroutines ### my $self = shift;
###############################
sub create { require Bugzilla::Product;
my ($name, $product) = @_; $self->{'product'} ||= new Bugzilla::Product($self->product_id);
my $dbh = Bugzilla->dbh; return $self->{'product'};
}
# Cleanups and validity checks ################################
$name || ThrowUserError('version_blank_name'); # Validators
################################
sub set_name { $_[0]->set('value', $_[1]); }
sub _check_value {
my ($invocant, $name, $product) = @_;
$name = trim($name);
$name || ThrowUserError('version_blank_name');
# Remove unprintable characters # Remove unprintable characters
$name = clean_text($name); $name = clean_text($name);
$product = $invocant->product if (ref $invocant);
my $version = new Bugzilla::Version({ product => $product, name => $name }); my $version = new Bugzilla::Version({ product => $product, name => $name });
if ($version) { if ($version && (!ref $invocant || $version->id != $invocant->id)) {
ThrowUserError('version_already_exists', ThrowUserError('version_already_exists', { name => $version->name,
{'name' => $version->name, product => $product->name });
'product' => $product->name});
} }
return $name;
}
# Add the new version sub _check_product {
trick_taint($name); my ($invocant, $product) = @_;
$dbh->do(q{INSERT INTO versions (value, product_id) return Bugzilla->user->check_can_admin_product($product->name);
VALUES (?, ?)}, undef, ($name, $product->id));
return new Bugzilla::Version($dbh->bz_last_key('versions', 'id'));
} }
1; 1;
...@@ -187,37 +204,33 @@ Bugzilla::Version - Bugzilla product version class. ...@@ -187,37 +204,33 @@ Bugzilla::Version - Bugzilla product version class.
use Bugzilla::Version; use Bugzilla::Version;
my $version = new Bugzilla::Version(1, 'version_value'); my $version = new Bugzilla::Version({ name => $name, product => $product });
my $value = $version->name;
my $product_id = $version->product_id; my $product_id = $version->product_id;
my $value = $version->value; my $product = $version->product;
$version->remove_from_db; my $version = Bugzilla::Version->create(
{ name => $name, product => $product });
my $updated = $version->update($version_name, $product);
my $version = $hash_ref->{'version_value'}; $version->set_name($new_name);
$version->update();
my $version = Bugzilla::Version::create($version_name, $product); $version->remove_from_db;
=head1 DESCRIPTION =head1 DESCRIPTION
Version.pm represents a Product Version object. Version.pm represents a Product Version object. It is an implementation
of L<Bugzilla::Object>, and thus provides all methods that
L<Bugzilla::Object> provides.
The methods that are specific to C<Bugzilla::Version> are listed
below.
=head1 METHODS =head1 METHODS
=over =over
=item C<new($product_id, $value)>
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 product id.
$value - String with a version value.
Returns: A Bugzilla::Version object.
=item C<bug_count()> =item C<bug_count()>
Description: Returns the total of bugs that belong to the version. Description: Returns the total of bugs that belong to the version.
...@@ -226,38 +239,6 @@ Version.pm represents a Product Version object. ...@@ -226,38 +239,6 @@ Version.pm represents a Product Version object.
Returns: Integer with the number of bugs. Returns: Integer with the number of bugs.
=item C<remove_from_db()>
Description: Removes the version from the database.
Params: none.
Retruns: none.
=item C<update($name, $product)>
Description: Update the value of the version.
Params: $name - String with the new version value.
$product - Bugzilla::Product object the version belongs to.
Returns: An integer - 1 if the version has been updated, else 0.
=back
=head1 SUBROUTINES
=over
=item C<create($version_name, $product)>
Description: Create a new version for the given product.
Params: $version_name - String with a version value.
$product - A Bugzilla::Product object.
Returns: A Bugzilla::Version object.
=back =back
=cut =cut
...@@ -119,7 +119,8 @@ if ($action eq 'add') { ...@@ -119,7 +119,8 @@ if ($action eq 'add') {
if ($action eq 'new') { if ($action eq 'new') {
check_token_data($token, 'add_version'); check_token_data($token, 'add_version');
my $version = Bugzilla::Version::create($version_name, $product); my $version = Bugzilla::Version->create(
{name => $version_name, product => $product});
delete_token($token); delete_token($token);
$vars->{'message'} = 'version_created'; $vars->{'message'} = 'version_created';
...@@ -202,7 +203,8 @@ if ($action eq 'update') { ...@@ -202,7 +203,8 @@ if ($action eq 'update') {
$dbh->bz_start_transaction(); $dbh->bz_start_transaction();
$vars->{'updated'} = $version->update($version_name, $product); $version->set_name($version_name);
my $changes = $version->update();
$dbh->bz_commit_transaction(); $dbh->bz_commit_transaction();
delete_token($token); delete_token($token);
...@@ -210,6 +212,7 @@ if ($action eq 'update') { ...@@ -210,6 +212,7 @@ if ($action eq 'update') {
$vars->{'message'} = 'version_updated'; $vars->{'message'} = 'version_updated';
$vars->{'version'} = $version; $vars->{'version'} = $version;
$vars->{'product'} = $product; $vars->{'product'} = $product;
$vars->{'changes'} = $changes;
$template->process("admin/versions/list.html.tmpl", $vars) $template->process("admin/versions/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error()); || ThrowTemplateError($template->error());
......
...@@ -796,7 +796,13 @@ ...@@ -796,7 +796,13 @@
[% ELSIF message_tag == "version_updated" %] [% ELSIF message_tag == "version_updated" %]
[% title = "Version Updated" %] [% title = "Version Updated" %]
Version renamed as <em>[% version.name FILTER html %]</em>. [% IF changes.size %]
[% IF changes.value.defined %]
Version renamed to <em>[% version.name FILTER html %]</em>.
[% END %]
[% ELSE %]
No changes made to version <em>[% version.name FILTER html %]</em>.
[% END %]
[% ELSIF message_tag == "workflow_updated" %] [% ELSIF message_tag == "workflow_updated" %]
The workflow has been updated. The workflow has been updated.
......
...@@ -1579,10 +1579,6 @@ ...@@ -1579,10 +1579,6 @@
version! You must reassign those [% terms.bugs %] to another version version! You must reassign those [% terms.bugs %] to another version
before you can delete this one. before you can delete this one.
[% ELSIF error == "version_not_specified" %]
[% title = "No Version Specified" %]
No version specified when trying to edit versions.
[% ELSIF error == "users_deletion_disabled" %] [% ELSIF error == "users_deletion_disabled" %]
[% title = "Deletion not activated" %] [% title = "Deletion not activated" %]
[% admindocslinks = {'useradmin.html' => 'User administration'} %] [% admindocslinks = {'useradmin.html' => 'User administration'} %]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment