return $self->{'bug_count'};
}
+sub remove_from_db {
+ my $self = shift;
+ my $dbh = Bugzilla->dbh;
+
+ # The version cannot be removed if there are bugs
+ # associated with it.
+ if ($self->bug_count) {
+ ThrowUserError("version_has_bugs", { nb => $self->bug_count });
+ }
+
+ $dbh->do(q{DELETE FROM versions WHERE product_id = ? AND value = ?},
+ undef, ($self->product_id, $self->name));
+}
+
+sub update {
+ my $self = shift;
+ my ($name, $product) = @_;
+ my $dbh = Bugzilla->dbh;
+
+ $name || ThrowUserError('version_not_specified');
+
+ # Remove unprintable characters
+ $name = clean_text($name);
+
+ return 0 if ($name eq $self->name);
+ my $version = new Bugzilla::Version($self->product_id, $name);
+
+ if ($version) {
+ ThrowUserError('version_already_exists',
+ {'name' => $version->name,
+ 'product' => $product->name});
+ }
+
+ 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 ####
###############################
return $version;
}
+sub create {
+ my ($name, $product) = @_;
+ my $dbh = Bugzilla->dbh;
+
+ # Cleanups and validity checks
+ $name || ThrowUserError('version_blank_name');
+
+ # Remove unprintable characters
+ $name = clean_text($name);
+
+ my $version = new Bugzilla::Version($product->id, $name);
+ if ($version) {
+ ThrowUserError('version_already_exists',
+ {'name' => $version->name,
+ 'product' => $product->name});
+ }
+
+ # Add the new version
+ trick_taint($name);
+ $dbh->do(q{INSERT INTO versions (value, product_id)
+ VALUES (?, ?)}, undef, ($name, $product->id));
+
+ $version = new Bugzilla::Version($product->id, $name);
+ return $version;
+}
+
1;
__END__
my $product_id = $version->product_id;
my $value = $version->value;
+ $version->remove_from_db;
+
+ my $updated = $version->update($version_name, $product);
+
my $version = $hash_ref->{'version_value'};
my $version = Bugzilla::Version::check_version($product_obj,
'acme_version');
+ my $version = Bugzilla::Version::create($version_name, $product);
+
=head1 DESCRIPTION
Version.pm represents a Product Version object.
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
Returns: Bugzilla::Version object.
+=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
=cut
unless ($action) {
$vars->{'showbugcounts'} = $showbugcounts;
$vars->{'product'} = $product;
- $template->process("admin/versions/list.html.tmpl",
- $vars)
+ $template->process("admin/versions/list.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
if ($action eq 'add') {
$vars->{'product'} = $product;
- $template->process("admin/versions/create.html.tmpl",
- $vars)
+ $template->process("admin/versions/create.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
if ($action eq 'new') {
- # Cleanups and validity checks
- $version_name || ThrowUserError('version_blank_name');
+ my $version = Bugzilla::Version::create($version_name, $product);
- # Remove unprintable characters
- $version_name = clean_text($version_name);
-
- my $version = new Bugzilla::Version($product->id, $version_name);
- if ($version) {
- ThrowUserError('version_already_exists',
- {'name' => $version->name,
- 'product' => $product->name});
- }
-
- # Add the new version
- trick_taint($version_name);
- $dbh->do("INSERT INTO versions (value, product_id)
- VALUES (?, ?)", undef, ($version_name, $product->id));
-
- $version = new Bugzilla::Version($product->id, $version_name);
$vars->{'version'} = $version;
$vars->{'product'} = $product;
- $template->process("admin/versions/created.html.tmpl",
- $vars)
+ $template->process("admin/versions/created.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
if ($action eq 'del') {
- my $version = Bugzilla::Version::check_version($product,
- $version_name);
+ my $version = Bugzilla::Version::check_version($product, $version_name);
$vars->{'version'} = $version;
$vars->{'product'} = $product;
- $template->process("admin/versions/confirm-delete.html.tmpl",
- $vars)
+ $template->process("admin/versions/confirm-delete.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
if ($action eq 'delete') {
- my $version = Bugzilla::Version::check_version($product,
- $version_name);
-
- # The version cannot be removed if there are bugs
- # associated with it.
- 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->name));
+ my $version = Bugzilla::Version::check_version($product, $version_name);
+ $version->remove_from_db;
$vars->{'version'} = $version;
$vars->{'product'} = $product;
$template->process("admin/versions/deleted.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
+
exit;
}
if ($action eq 'edit') {
- my $version = Bugzilla::Version::check_version($product,
- $version_name);
+ my $version = Bugzilla::Version::check_version($product, $version_name);
$vars->{'version'} = $version;
$vars->{'product'} = $product;
- $template->process("admin/versions/edit.html.tmpl",
- $vars)
+ $template->process("admin/versions/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
if ($action eq 'update') {
- $version_name || ThrowUserError('version_not_specified');
-
- # Remove unprintable characters
- $version_name = clean_text($version_name);
-
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'
+ my $version =
+ Bugzilla::Version::check_version($product, $version_old_name);
$dbh->bz_lock_tables('bugs WRITE', 'versions WRITE');
- if ($version_name ne $version_old->name) {
-
- my $version = new Bugzilla::Version($product->id,
- $version_name);
-
- if ($version) {
- ThrowUserError('version_already_exists',
- {'name' => $version->name,
- 'product' => $product->name});
- }
-
- 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));
-
- $vars->{'updated_name'} = 1;
- }
-
- $dbh->bz_unlock_tables();
+ $vars->{'updated'} = $version->update($version_name, $product);
+
+ $dbh->bz_unlock_tables();
- my $version =
- Bugzilla::Version::check_version($product,
- $version_name);
$vars->{'version'} = $version;
$vars->{'product'} = $product;
- $template->process("admin/versions/updated.html.tmpl",
- $vars)
+ $template->process("admin/versions/updated.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;