Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / editclassifications.cgi
blob7e744d9937e239c350a8dfbd1fccd72e3d974555
1 #!/usr/bin/env perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil; cperl-indent-level: 4 -*-
4 # The contents of this file are subject to the Mozilla Public
5 # License Version 1.1 (the "License"); you may not use this file
6 # except in compliance with the License. You may obtain a copy of
7 # the License at http://www.mozilla.org/MPL/
9 # Software distributed under the License is distributed on an "AS
10 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11 # implied. See the License for the specific language governing
12 # rights and limitations under the License.
14 # The Original Code is the Bugzilla Bug Tracking System.
16 # The Initial Developer of the Original Code is Albert Ting
18 # Contributor(s): Albert Ting <alt@sonic.net>
19 # Max Kanat-Alexander <mkanat@bugzilla.org>
20 # Frédéric Buclin <LpSolit@gmail.com>
23 use strict;
24 use lib qw(. lib);
26 use Bugzilla;
27 use Bugzilla::Constants;
28 use Bugzilla::Util;
29 use Bugzilla::Error;
30 use Bugzilla::Classification;
31 use Bugzilla::Token;
33 my $dbh = Bugzilla->dbh;
34 my $cgi = Bugzilla->cgi;
35 my $template = Bugzilla->template;
36 local our $vars = {};
38 sub LoadTemplate {
39 my $action = shift;
40 my $cgi = Bugzilla->cgi;
41 my $template = Bugzilla->template;
43 $vars->{'classifications'} = [Bugzilla::Classification::get_all_classifications()]
44 if ($action eq 'select');
45 # There is currently only one section about classifications,
46 # so all pages point to it. Let's define it here.
47 $vars->{'doc_section'} = 'classifications.html';
49 $action =~ /(\w+)/;
50 $action = $1;
51 print $cgi->header();
52 $template->process("admin/classifications/$action.html.tmpl", $vars)
53 || ThrowTemplateError($template->error());
54 exit;
58 # Preliminary checks:
61 Bugzilla->login(LOGIN_REQUIRED);
63 print $cgi->header();
65 exists Bugzilla->user->groups->{'editclassifications'}
66 || ThrowUserError("auth_failure", {group => "editclassifications",
67 action => "edit",
68 object => "classifications"});
70 ThrowUserError("auth_classification_not_enabled")
71 unless Bugzilla->params->{"useclassification"};
74 # often used variables
76 my $action = trim($cgi->param('action') || '');
77 my $class_name = trim($cgi->param('classification') || '');
78 my $token = $cgi->param('token');
81 # action='' -> Show nice list of classifications
83 LoadTemplate('select') unless $action;
86 # action='add' -> present form for parameters for new classification
88 # (next action will be 'new')
91 if ($action eq 'add') {
92 $vars->{'token'} = issue_session_token('add_classification');
93 LoadTemplate($action);
97 # action='new' -> add classification entered in the 'action=add' screen
100 if ($action eq 'new') {
101 check_token_data($token, 'add_classification');
103 $class_name || ThrowUserError("classification_not_specified");
105 my $classification =
106 new Bugzilla::Classification({name => $class_name});
108 if ($classification) {
109 ThrowUserError("classification_already_exists",
110 { name => $classification->name });
113 my $description = trim($cgi->param('description') || '');
115 my $sortkey = trim($cgi->param('sortkey') || 0);
116 my $stored_sortkey = $sortkey;
117 detaint_natural($sortkey)
118 || ThrowUserError('classification_invalid_sortkey', {'name' => $class_name,
119 'sortkey' => $stored_sortkey});
121 trick_taint($description);
122 trick_taint($class_name);
124 # Add the new classification.
125 $dbh->do("INSERT INTO classifications (name, description, sortkey)
126 VALUES (?, ?, ?)", undef, ($class_name, $description, $sortkey));
128 delete_token($token);
130 $vars->{'message'} = 'classification_created';
131 $vars->{'classification'} = new Bugzilla::Classification({name => $class_name});
132 $vars->{'classifications'} = [Bugzilla::Classification::get_all_classifications];
133 $vars->{'token'} = issue_session_token('reclassify_classifications');
134 LoadTemplate('reclassify');
138 # action='del' -> ask if user really wants to delete
140 # (next action would be 'delete')
143 if ($action eq 'del') {
145 my $classification =
146 Bugzilla::Classification::check_classification($class_name);
148 if ($classification->id == 1) {
149 ThrowUserError("classification_not_deletable");
152 if ($classification->product_count()) {
153 ThrowUserError("classification_has_products");
156 $vars->{'classification'} = $classification;
157 $vars->{'token'} = issue_session_token('delete_classification');
159 LoadTemplate($action);
163 # action='delete' -> really delete the classification
166 if ($action eq 'delete') {
167 check_token_data($token, 'delete_classification');
169 my $classification =
170 Bugzilla::Classification::check_classification($class_name);
172 if ($classification->id == 1) {
173 ThrowUserError("classification_not_deletable");
176 # lock the tables before we start to change everything:
177 $dbh->bz_start_transaction();
179 # update products just in case
180 $dbh->do("UPDATE products SET classification_id = 1
181 WHERE classification_id = ?", undef, $classification->id);
183 # delete
184 $dbh->do("DELETE FROM classifications WHERE id = ?", undef,
185 $classification->id);
187 $dbh->bz_commit_transaction();
189 $vars->{'message'} = 'classification_deleted';
190 $vars->{'classification'} = $class_name;
191 delete_token($token);
192 LoadTemplate('select');
196 # action='edit' -> present the edit classifications from
198 # (next action would be 'update')
201 if ($action eq 'edit') {
203 my $classification =
204 Bugzilla::Classification::check_classification($class_name);
206 $vars->{'classification'} = $classification;
207 $vars->{'token'} = issue_session_token('edit_classification');
209 LoadTemplate($action);
213 # action='update' -> update the classification
216 if ($action eq 'update') {
217 check_token_data($token, 'edit_classification');
219 $class_name || ThrowUserError("classification_not_specified");
221 my $class_old_name = trim($cgi->param('classificationold') || '');
223 my $class_old =
224 Bugzilla::Classification::check_classification($class_old_name);
226 my $description = trim($cgi->param('description') || '');
228 my $sortkey = trim($cgi->param('sortkey') || 0);
229 my $stored_sortkey = $sortkey;
230 detaint_natural($sortkey)
231 || ThrowUserError('classification_invalid_sortkey', {'name' => $class_old->name,
232 'sortkey' => $stored_sortkey});
234 $dbh->bz_start_transaction();
236 if ($class_name ne $class_old->name) {
238 my $class = new Bugzilla::Classification({name => $class_name});
239 if ($class) {
240 ThrowUserError("classification_already_exists",
241 { name => $class->name });
243 trick_taint($class_name);
244 $dbh->do("UPDATE classifications SET name = ? WHERE id = ?",
245 undef, ($class_name, $class_old->id));
247 $vars->{'updated_classification'} = 1;
250 if ($description ne $class_old->description) {
251 trick_taint($description);
252 $dbh->do("UPDATE classifications SET description = ?
253 WHERE id = ?", undef,
254 ($description, $class_old->id));
256 $vars->{'updated_description'} = 1;
259 if ($sortkey ne $class_old->sortkey) {
260 $dbh->do("UPDATE classifications SET sortkey = ?
261 WHERE id = ?", undef,
262 ($sortkey, $class_old->id));
264 $vars->{'updated_sortkey'} = 1;
267 $dbh->bz_commit_transaction();
269 $vars->{'message'} = 'classification_updated';
270 $vars->{'classification'} = $class_name;
271 delete_token($token);
272 LoadTemplate('select');
276 # action='reclassify' -> reclassify products for the classification
279 if ($action eq 'reclassify') {
281 my $classification =
282 Bugzilla::Classification::check_classification($class_name);
284 my $sth = $dbh->prepare("UPDATE products SET classification_id = ?
285 WHERE name = ?");
287 if (defined $cgi->param('add_products')) {
288 check_token_data($token, 'reclassify_classifications');
289 if (defined $cgi->param('prodlist')) {
290 foreach my $prod ($cgi->param("prodlist")) {
291 trick_taint($prod);
292 $sth->execute($classification->id, $prod);
295 delete_token($token);
296 } elsif (defined $cgi->param('remove_products')) {
297 check_token_data($token, 'reclassify_classifications');
298 if (defined $cgi->param('myprodlist')) {
299 foreach my $prod ($cgi->param("myprodlist")) {
300 trick_taint($prod);
301 $sth->execute(1,$prod);
304 delete_token($token);
307 my @classifications =
308 Bugzilla::Classification::get_all_classifications;
309 $vars->{'classifications'} = \@classifications;
310 $vars->{'classification'} = $classification;
311 $vars->{'token'} = issue_session_token('reclassify_classifications');
313 LoadTemplate($action);
317 # No valid action found
320 ThrowCodeError("action_unrecognized", {action => $action});