Commit f6c2d743 authored by lpsolit%gmail.com's avatar lpsolit%gmail.com

Bug 313126: Clean up editclassification.cgi and make it use…

Bug 313126: Clean up editclassification.cgi and make it use Bugzilla::Classification methods - Patch by Fré©ric Buclin <LpSolit@gmail.com> r=wicked a=LpSolit
parent 4c1a9a42
...@@ -30,6 +30,7 @@ use base qw(Bugzilla::Object); ...@@ -30,6 +30,7 @@ use base qw(Bugzilla::Object);
############################### ###############################
use constant DB_TABLE => 'classifications'; use constant DB_TABLE => 'classifications';
use constant LIST_ORDER => 'sortkey, name';
use constant DB_COLUMNS => qw( use constant DB_COLUMNS => qw(
id id
...@@ -55,6 +56,26 @@ use constant VALIDATORS => { ...@@ -55,6 +56,26 @@ use constant VALIDATORS => {
}; };
############################### ###############################
#### Constructors #####
###############################
sub remove_from_db {
my $self = shift;
my $dbh = Bugzilla->dbh;
ThrowUserError("classification_not_deletable") if ($self->id == 1);
$dbh->bz_start_transaction();
# Reclassify products to the default classification, if needed.
$dbh->do("UPDATE products SET classification_id = 1
WHERE classification_id = ?", undef, $self->id);
$dbh->do("DELETE FROM classifications WHERE id = ?", undef, $self->id);
$dbh->bz_commit_transaction();
}
###############################
#### Validators #### #### Validators ####
############################### ###############################
...@@ -93,6 +114,10 @@ sub _check_sortkey { ...@@ -93,6 +114,10 @@ sub _check_sortkey {
#### Methods #### #### Methods ####
############################### ###############################
sub set_name { $_[0]->set('name', $_[1]); }
sub set_description { $_[0]->set('description', $_[1]); }
sub set_sortkey { $_[0]->set('sortkey', $_[1]); }
sub product_count { sub product_count {
my $self = shift; my $self = shift;
my $dbh = Bugzilla->dbh; my $dbh = Bugzilla->dbh;
......
...@@ -682,20 +682,12 @@ sub get_selectable_products { ...@@ -682,20 +682,12 @@ sub get_selectable_products {
sub get_selectable_classifications { sub get_selectable_classifications {
my ($self) = @_; my ($self) = @_;
if (defined $self->{selectable_classifications}) { if (!defined $self->{selectable_classifications}) {
return $self->{selectable_classifications}; my $products = $self->get_selectable_products;
} my %class_ids = map { $_->classification_id => 1 } @$products;
my $products = $self->get_selectable_products;
my $class; $self->{selectable_classifications} = Bugzilla::Classification->new_from_list([keys %class_ids]);
foreach my $product (@$products) {
$class->{$product->classification_id} ||=
new Bugzilla::Classification($product->classification_id);
} }
my @sorted_class = sort {$a->sortkey <=> $b->sortkey
|| lc($a->name) cmp lc($b->name)} (values %$class);
$self->{selectable_classifications} = \@sorted_class;
return $self->{selectable_classifications}; return $self->{selectable_classifications};
} }
......
...@@ -100,34 +100,15 @@ if ($action eq 'add') { ...@@ -100,34 +100,15 @@ if ($action eq 'add') {
if ($action eq 'new') { if ($action eq 'new') {
check_token_data($token, 'add_classification'); check_token_data($token, 'add_classification');
$class_name || ThrowUserError("classification_not_specified");
my $classification = my $classification =
new Bugzilla::Classification({name => $class_name}); Bugzilla::Classification->create({name => $class_name,
description => scalar $cgi->param('description'),
if ($classification) { sortkey => scalar $cgi->param('sortkey')});
ThrowUserError("classification_already_exists",
{ name => $classification->name });
}
my $description = trim($cgi->param('description') || '');
my $sortkey = trim($cgi->param('sortkey') || 0);
my $stored_sortkey = $sortkey;
detaint_natural($sortkey)
|| ThrowUserError('classification_invalid_sortkey', {'sortkey' => $stored_sortkey});
trick_taint($description);
trick_taint($class_name);
# Add the new classification.
$dbh->do("INSERT INTO classifications (name, description, sortkey)
VALUES (?, ?, ?)", undef, ($class_name, $description, $sortkey));
delete_token($token); delete_token($token);
$vars->{'message'} = 'classification_created'; $vars->{'message'} = 'classification_created';
$vars->{'classification'} = new Bugzilla::Classification({name => $class_name}); $vars->{'classification'} = $classification;
$vars->{'classifications'} = [Bugzilla::Classification->get_all]; $vars->{'classifications'} = [Bugzilla::Classification->get_all];
$vars->{'token'} = issue_session_token('reclassify_classifications'); $vars->{'token'} = issue_session_token('reclassify_classifications');
LoadTemplate('reclassify'); LoadTemplate('reclassify');
...@@ -165,27 +146,11 @@ if ($action eq 'delete') { ...@@ -165,27 +146,11 @@ if ($action eq 'delete') {
check_token_data($token, 'delete_classification'); check_token_data($token, 'delete_classification');
my $classification = Bugzilla::Classification->check($class_name); my $classification = Bugzilla::Classification->check($class_name);
$classification->remove_from_db;
if ($classification->id == 1) { delete_token($token);
ThrowUserError("classification_not_deletable");
}
# lock the tables before we start to change everything:
$dbh->bz_start_transaction();
# update products just in case
$dbh->do("UPDATE products SET classification_id = 1
WHERE classification_id = ?", undef, $classification->id);
# delete
$dbh->do("DELETE FROM classifications WHERE id = ?", undef,
$classification->id);
$dbh->bz_commit_transaction();
$vars->{'message'} = 'classification_deleted'; $vars->{'message'} = 'classification_deleted';
$vars->{'classification'} = $class_name; $vars->{'classification'} = $classification;
delete_token($token);
LoadTemplate('select'); LoadTemplate('select');
} }
...@@ -196,7 +161,6 @@ if ($action eq 'delete') { ...@@ -196,7 +161,6 @@ if ($action eq 'delete') {
# #
if ($action eq 'edit') { if ($action eq 'edit') {
my $classification = Bugzilla::Classification->check($class_name); my $classification = Bugzilla::Classification->check($class_name);
$vars->{'classification'} = $classification; $vars->{'classification'} = $classification;
...@@ -212,57 +176,19 @@ if ($action eq 'edit') { ...@@ -212,57 +176,19 @@ if ($action eq 'edit') {
if ($action eq 'update') { if ($action eq 'update') {
check_token_data($token, 'edit_classification'); check_token_data($token, 'edit_classification');
$class_name || ThrowUserError("classification_not_specified");
my $class_old_name = trim($cgi->param('classificationold') || ''); my $class_old_name = trim($cgi->param('classificationold') || '');
my $classification = Bugzilla::Classification->check($class_old_name);
my $class_old = Bugzilla::Classification->check($class_old_name); $classification->set_name($class_name);
$classification->set_description(scalar $cgi->param('description'));
my $description = trim($cgi->param('description') || ''); $classification->set_sortkey(scalar $cgi->param('sortkey'));
my $sortkey = trim($cgi->param('sortkey') || 0);
my $stored_sortkey = $sortkey;
detaint_natural($sortkey)
|| ThrowUserError('classification_invalid_sortkey', {'sortkey' => $stored_sortkey});
$dbh->bz_start_transaction();
if ($class_name ne $class_old->name) {
my $class = new Bugzilla::Classification({name => $class_name});
if ($class) {
ThrowUserError("classification_already_exists",
{ name => $class->name });
}
trick_taint($class_name);
$dbh->do("UPDATE classifications SET name = ? WHERE id = ?",
undef, ($class_name, $class_old->id));
$vars->{'updated_classification'} = 1;
}
if ($description ne $class_old->description) { my $changes = $classification->update;
trick_taint($description); delete_token($token);
$dbh->do("UPDATE classifications SET description = ?
WHERE id = ?", undef,
($description, $class_old->id));
$vars->{'updated_description'} = 1;
}
if ($sortkey ne $class_old->sortkey) {
$dbh->do("UPDATE classifications SET sortkey = ?
WHERE id = ?", undef,
($sortkey, $class_old->id));
$vars->{'updated_sortkey'} = 1;
}
$dbh->bz_commit_transaction();
$vars->{'message'} = 'classification_updated'; $vars->{'message'} = 'classification_updated';
$vars->{'classification'} = $class_name; $vars->{'classification'} = $classification;
delete_token($token); $vars->{'changes'} = $changes;
LoadTemplate('select'); LoadTemplate('select');
} }
......
...@@ -186,26 +186,26 @@ ...@@ -186,26 +186,26 @@
[% ELSIF message_tag == "classification_deleted" %] [% ELSIF message_tag == "classification_deleted" %]
[% title = "Classification Deleted" %] [% title = "Classification Deleted" %]
The <em>[% classification FILTER html %]</em> classification has been deleted. The <em>[% classification.name FILTER html %]</em> classification has been deleted.
[% ELSIF message_tag == "classification_updated" %] [% ELSIF message_tag == "classification_updated" %]
[% IF updated_classification || updated_description || updated_sortkey %] [% title = "Classification Updated" %]
[% title = "Classification Updated" %] [% IF changes.keys.size %]
Changes to the <em>[% classification FILTER html %]</em> classification Changes to the <em>[% classification.name FILTER html %]</em> classification
have been saved: have been saved:
<ul> <ul>
[% IF updated_classification %] [% IF changes.name.defined %]
<li>Classification name updated</li> <li>Name updated to '[% classification.name FILTER html %]'</li>
[% END %] [% END %]
[% IF updated_description %] [% IF changes.description.defined %]
<li>Description updated</li> <li>Description updated to '[% classification.description FILTER html %]'</li>
[% END %] [% END %]
[% IF updated_sortkey %] [% IF changes.sortkey.defined %]
<li>Sortkey updated</li> <li>Sortkey updated to '[% classification.sortkey FILTER html %]'</li>
[% END %] [% END %]
</ul> </ul>
[% ELSE %] [% ELSE %]
No changes made to <em>[% classification FILTER html %]</em>. No changes made to <em>[% classification.name FILTER html %]</em>.
[% END %] [% END %]
[% ELSIF message_tag == "component_created" %] [% ELSIF message_tag == "component_created" %]
......
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