]>
Commit | Line | Data |
---|---|---|
9f3d18d4 | 1 | #!/usr/bin/perl -T |
fe2a998b FB |
2 | # This Source Code Form is subject to the terms of the Mozilla Public |
3 | # License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
88d26275 | 5 | # |
fe2a998b FB |
6 | # This Source Code Form is "Incompatible With Secondary Licenses", as |
7 | # defined by the Mozilla Public License, v. 2.0. | |
c87cca60 | 8 | |
88d26275 | 9 | |
1001cbba | 10 | use 5.10.1; |
88d26275 | 11 | use strict; |
9f3d18d4 FB |
12 | use warnings; |
13 | ||
415e32d4 | 14 | use lib qw(. lib); |
88d26275 | 15 | |
16 | use Bugzilla; | |
17 | use Bugzilla::Constants; | |
c1d16e42 | 18 | use Bugzilla::Util; |
19 | use Bugzilla::Error; | |
ab199bf5 | 20 | use Bugzilla::Classification; |
93815fc7 | 21 | use Bugzilla::Token; |
88d26275 | 22 | |
88d26275 | 23 | my $dbh = Bugzilla->dbh; |
f1625214 | 24 | my $cgi = Bugzilla->cgi; |
c1d16e42 | 25 | my $template = Bugzilla->template; |
f1625214 | 26 | local our $vars = {}; |
88d26275 | 27 | |
dddc17ec | 28 | sub LoadTemplate { |
88d26275 | 29 | my $action = shift; |
f1625214 | 30 | my $template = Bugzilla->template; |
c87cca60 | 31 | |
4c1a9a42 | 32 | $vars->{'classifications'} = [Bugzilla::Classification->get_all] |
c87cca60 | 33 | if ($action eq 'select'); |
f7b1c511 | 34 | # There is currently only one section about classifications, |
35 | # so all pages point to it. Let's define it here. | |
707773ab | 36 | $vars->{'doc_section'} = 'administering/categorization.html#classifications'; |
88d26275 | 37 | |
38 | $action =~ /(\w+)/; | |
39 | $action = $1; | |
88d26275 | 40 | $template->process("admin/classifications/$action.html.tmpl", $vars) |
41 | || ThrowTemplateError($template->error()); | |
42 | exit; | |
43 | } | |
44 | ||
45 | # | |
46 | # Preliminary checks: | |
47 | # | |
48 | ||
e15dcf48 | 49 | my $user = Bugzilla->login(LOGIN_REQUIRED); |
88d26275 | 50 | |
51 | print $cgi->header(); | |
52 | ||
e15dcf48 | 53 | $user->in_group('editclassifications') |
a1d58085 | 54 | || ThrowUserError("auth_failure", {group => "editclassifications", |
55 | action => "edit", | |
56 | object => "classifications"}); | |
57 | ||
b1f4cf8b | 58 | ThrowUserError("auth_classification_not_enabled") |
59 | unless Bugzilla->params->{"useclassification"}; | |
88d26275 | 60 | |
61 | # | |
62 | # often used variables | |
63 | # | |
ab199bf5 | 64 | my $action = trim($cgi->param('action') || ''); |
65 | my $class_name = trim($cgi->param('classification') || ''); | |
93815fc7 | 66 | my $token = $cgi->param('token'); |
67 | ||
88d26275 | 68 | # |
69 | # action='' -> Show nice list of classifications | |
70 | # | |
c87cca60 | 71 | LoadTemplate('select') unless $action; |
88d26275 | 72 | |
73 | # | |
74 | # action='add' -> present form for parameters for new classification | |
75 | # | |
76 | # (next action will be 'new') | |
77 | # | |
78 | ||
79 | if ($action eq 'add') { | |
93815fc7 | 80 | $vars->{'token'} = issue_session_token('add_classification'); |
88d26275 | 81 | LoadTemplate($action); |
82 | } | |
83 | ||
84 | # | |
85 | # action='new' -> add classification entered in the 'action=add' screen | |
86 | # | |
87 | ||
88 | if ($action eq 'new') { | |
93815fc7 | 89 | check_token_data($token, 'add_classification'); |
ab199bf5 | 90 | |
ab199bf5 | 91 | my $classification = |
f6c2d743 | 92 | Bugzilla::Classification->create({name => $class_name, |
93 | description => scalar $cgi->param('description'), | |
94 | sortkey => scalar $cgi->param('sortkey')}); | |
88d26275 | 95 | |
93815fc7 | 96 | delete_token($token); |
c87cca60 | 97 | |
98 | $vars->{'message'} = 'classification_created'; | |
f6c2d743 | 99 | $vars->{'classification'} = $classification; |
4c1a9a42 | 100 | $vars->{'classifications'} = [Bugzilla::Classification->get_all]; |
c87cca60 | 101 | $vars->{'token'} = issue_session_token('reclassify_classifications'); |
102 | LoadTemplate('reclassify'); | |
88d26275 | 103 | } |
104 | ||
105 | # | |
106 | # action='del' -> ask if user really wants to delete | |
107 | # | |
108 | # (next action would be 'delete') | |
109 | # | |
110 | ||
111 | if ($action eq 'del') { | |
88d26275 | 112 | |
4c1a9a42 | 113 | my $classification = Bugzilla::Classification->check($class_name); |
88d26275 | 114 | |
ab199bf5 | 115 | if ($classification->id == 1) { |
116 | ThrowUserError("classification_not_deletable"); | |
117 | } | |
88d26275 | 118 | |
ab199bf5 | 119 | if ($classification->product_count()) { |
120 | ThrowUserError("classification_has_products"); | |
121 | } | |
88d26275 | 122 | |
4587cba8 | 123 | $vars->{'classification'} = $classification; |
93815fc7 | 124 | $vars->{'token'} = issue_session_token('delete_classification'); |
88d26275 | 125 | |
126 | LoadTemplate($action); | |
127 | } | |
128 | ||
129 | # | |
130 | # action='delete' -> really delete the classification | |
131 | # | |
132 | ||
133 | if ($action eq 'delete') { | |
93815fc7 | 134 | check_token_data($token, 'delete_classification'); |
88d26275 | 135 | |
4c1a9a42 | 136 | my $classification = Bugzilla::Classification->check($class_name); |
f6c2d743 | 137 | $classification->remove_from_db; |
138 | delete_token($token); | |
ab199bf5 | 139 | |
c87cca60 | 140 | $vars->{'message'} = 'classification_deleted'; |
f6c2d743 | 141 | $vars->{'classification'} = $classification; |
c87cca60 | 142 | LoadTemplate('select'); |
88d26275 | 143 | } |
144 | ||
145 | # | |
146 | # action='edit' -> present the edit classifications from | |
147 | # | |
148 | # (next action would be 'update') | |
149 | # | |
150 | ||
151 | if ($action eq 'edit') { | |
4c1a9a42 | 152 | my $classification = Bugzilla::Classification->check($class_name); |
88d26275 | 153 | |
4587cba8 | 154 | $vars->{'classification'} = $classification; |
93815fc7 | 155 | $vars->{'token'} = issue_session_token('edit_classification'); |
88d26275 | 156 | |
157 | LoadTemplate($action); | |
158 | } | |
159 | ||
160 | # | |
161 | # action='update' -> update the classification | |
162 | # | |
163 | ||
164 | if ($action eq 'update') { | |
93815fc7 | 165 | check_token_data($token, 'edit_classification'); |
88d26275 | 166 | |
ab199bf5 | 167 | my $class_old_name = trim($cgi->param('classificationold') || ''); |
f6c2d743 | 168 | my $classification = Bugzilla::Classification->check($class_old_name); |
88d26275 | 169 | |
1521f85b OV |
170 | $classification->set_all({ |
171 | name => $class_name, | |
172 | description => scalar $cgi->param('description'), | |
173 | sortkey => scalar $cgi->param('sortkey'), | |
174 | }); | |
aee162e1 | 175 | |
f6c2d743 | 176 | my $changes = $classification->update; |
177 | delete_token($token); | |
88d26275 | 178 | |
c87cca60 | 179 | $vars->{'message'} = 'classification_updated'; |
f6c2d743 | 180 | $vars->{'classification'} = $classification; |
181 | $vars->{'changes'} = $changes; | |
c87cca60 | 182 | LoadTemplate('select'); |
88d26275 | 183 | } |
184 | ||
185 | # | |
186 | # action='reclassify' -> reclassify products for the classification | |
187 | # | |
188 | ||
189 | if ($action eq 'reclassify') { | |
4c1a9a42 | 190 | my $classification = Bugzilla::Classification->check($class_name); |
c5464b5b | 191 | |
ab199bf5 | 192 | my $sth = $dbh->prepare("UPDATE products SET classification_id = ? |
193 | WHERE name = ?"); | |
c5464b5b | 194 | my @names; |
88d26275 | 195 | |
88d26275 | 196 | if (defined $cgi->param('add_products')) { |
93815fc7 | 197 | check_token_data($token, 'reclassify_classifications'); |
88d26275 | 198 | if (defined $cgi->param('prodlist')) { |
199 | foreach my $prod ($cgi->param("prodlist")) { | |
200 | trick_taint($prod); | |
ab199bf5 | 201 | $sth->execute($classification->id, $prod); |
c5464b5b | 202 | push @names, $prod; |
88d26275 | 203 | } |
204 | } | |
93815fc7 | 205 | delete_token($token); |
88d26275 | 206 | } elsif (defined $cgi->param('remove_products')) { |
93815fc7 | 207 | check_token_data($token, 'reclassify_classifications'); |
88d26275 | 208 | if (defined $cgi->param('myprodlist')) { |
209 | foreach my $prod ($cgi->param("myprodlist")) { | |
210 | trick_taint($prod); | |
c5464b5b BJ |
211 | $sth->execute(1, $prod); |
212 | push @names, $prod; | |
88d26275 | 213 | } |
214 | } | |
93815fc7 | 215 | delete_token($token); |
88d26275 | 216 | } |
217 | ||
4c1a9a42 | 218 | $vars->{'classifications'} = [Bugzilla::Classification->get_all]; |
4587cba8 | 219 | $vars->{'classification'} = $classification; |
93815fc7 | 220 | $vars->{'token'} = issue_session_token('reclassify_classifications'); |
88d26275 | 221 | |
c5464b5b BJ |
222 | foreach my $name (@names) { |
223 | Bugzilla->memcached->clear({ table => 'products', name => $name }); | |
224 | } | |
3ef245b2 | 225 | Bugzilla->memcached->clear_config(); |
c5464b5b | 226 | |
88d26275 | 227 | LoadTemplate($action); |
228 | } | |
229 | ||
230 | # | |
231 | # No valid action found | |
232 | # | |
233 | ||
c558f9f5 | 234 | ThrowUserError('unknown_action', {action => $action}); |