]> git.ipfire.org Git - thirdparty/bugzilla.git/blame - editclassifications.cgi
add a new hook: template_after_create (#60)
[thirdparty/bugzilla.git] / editclassifications.cgi
CommitLineData
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 10use 5.10.1;
88d26275 11use strict;
9f3d18d4
FB
12use warnings;
13
415e32d4 14use lib qw(. lib);
88d26275 15
16use Bugzilla;
17use Bugzilla::Constants;
c1d16e42 18use Bugzilla::Util;
19use Bugzilla::Error;
ab199bf5 20use Bugzilla::Classification;
93815fc7 21use Bugzilla::Token;
88d26275 22
88d26275 23my $dbh = Bugzilla->dbh;
f1625214 24my $cgi = Bugzilla->cgi;
c1d16e42 25my $template = Bugzilla->template;
f1625214 26local our $vars = {};
88d26275 27
dddc17ec 28sub 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 49my $user = Bugzilla->login(LOGIN_REQUIRED);
88d26275 50
51print $cgi->header();
52
e15dcf48 53$user->in_group('editclassifications')
a1d58085 54 || ThrowUserError("auth_failure", {group => "editclassifications",
55 action => "edit",
56 object => "classifications"});
57
b1f4cf8b 58ThrowUserError("auth_classification_not_enabled")
59 unless Bugzilla->params->{"useclassification"};
88d26275 60
61#
62# often used variables
63#
ab199bf5 64my $action = trim($cgi->param('action') || '');
65my $class_name = trim($cgi->param('classification') || '');
93815fc7 66my $token = $cgi->param('token');
67
88d26275 68#
69# action='' -> Show nice list of classifications
70#
c87cca60 71LoadTemplate('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
79if ($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
88if ($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
111if ($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
133if ($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
151if ($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
164if ($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
189if ($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 234ThrowUserError('unknown_action', {action => $action});