1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
package Bugzilla::Extension::TrackingFlags::Admin;
use strict;
use warnings;
use Bugzilla;
use Bugzilla::Component;
use Bugzilla::Error;
use Bugzilla::Group;
use Bugzilla::Product;
use Bugzilla::Util qw(trim detaint_natural);
use Bugzilla::Extension::TrackingFlags::Constants;
use Bugzilla::Extension::TrackingFlags::Flag;
use Bugzilla::Extension::TrackingFlags::Flag::Bug;
use Bugzilla::Extension::TrackingFlags::Flag::Value;
use Bugzilla::Extension::TrackingFlags::Flag::Visibility;
use JSON;
use Scalar::Util qw(blessed);
use base qw(Exporter);
our @EXPORT = qw(
admin_list
admin_edit
);
#
# page loading
#
sub admin_list {
my ($vars) = @_;
$vars->{show_bug_counts} = Bugzilla->input_params->{show_bug_counts};
$vars->{flags} = [ Bugzilla::Extension::TrackingFlags::Flag->get_all() ];
}
sub admin_edit {
my ($vars, $page) = @_;
my $input = Bugzilla->input_params;
$vars->{groups} = _groups_to_json();
$vars->{mode} = $input->{mode} || 'new';
$vars->{flag_id} = $input->{flag_id} || 0;
$vars->{tracking_flag_types} = FLAG_TYPES;
if ($input->{delete}) {
my $flag = Bugzilla::Extension::TrackingFlags::Flag->new($vars->{flag_id})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag', id => $vars->{flag_id} });
$flag->remove_from_db();
$vars->{message} = 'tracking_flag_deleted';
$vars->{flag} = $flag;
$vars->{flags} = [ Bugzilla::Extension::TrackingFlags::Flag->get_all() ];
print Bugzilla->cgi->header;
my $template = Bugzilla->template;
$template->process('pages/tracking_flags_admin_list.html.tmpl', $vars)
|| ThrowTemplateError($template->error());
exit;
} elsif ($input->{save}) {
# save
my ($flag, $values, $visibilities) = _load_from_input($input, $vars);
_validate($flag, $values, $visibilities);
my $flag_obj = _update_db($flag, $values, $visibilities);
$vars->{flag} = $flag_obj;
$vars->{values} = _flag_values_to_json($values);
$vars->{visibility} = _flag_visibility_to_json($visibilities);
if ($vars->{mode} eq 'new') {
$vars->{message} = 'tracking_flag_created';
} else {
$vars->{message} = 'tracking_flag_updated';
}
$vars->{mode} = 'edit';
} else {
# initial load
if ($vars->{mode} eq 'edit') {
# edit - straight load
my $flag = Bugzilla::Extension::TrackingFlags::Flag->new($vars->{flag_id})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag', id => $vars->{flag_id} });
$vars->{flag} = $flag;
$vars->{values} = _flag_values_to_json($flag->values);
$vars->{visibility} = _flag_visibility_to_json($flag->visibility);
$vars->{can_delete} = !$flag->bug_count;
} elsif ($vars->{mode} eq 'copy') {
# copy - load the source flag
$vars->{mode} = 'new';
my $flag = Bugzilla::Extension::TrackingFlags::Flag->new($input->{copy_from})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag', id => $vars->{copy_from} });
# increment the number at the end of the name and description
if ($flag->name =~ /^(\D+)(\d+)$/) {
$flag->set_name("$1" . ($2 + 1));
}
if ($flag->description =~ /^(\D+)(\d+)$/) {
$flag->set_description("$1" . ($2 + 1));
}
$flag->set_sortkey(_next_unique_sortkey($flag->sortkey));
$flag->set_type($flag->flag_type);
$flag->set_enter_bug($flag->enter_bug);
# always default new flags as active, even when copying an inactive one
$flag->set_is_active(1);
$vars->{flag} = $flag;
$vars->{values} = _flag_values_to_json($flag->values, 1);
$vars->{visibility} = _flag_visibility_to_json($flag->visibility, 1);
$vars->{can_delete} = 0;
} else {
$vars->{mode} = 'new';
$vars->{flag} = {
sortkey => 0,
enter_bug => 1,
is_active => 1,
};
$vars->{values} = _flag_values_to_json([
{
id => 0,
value => '---',
setter_group_id => '',
is_active => 1,
},
]);
$vars->{visibility} = '';
$vars->{can_delete} = 0;
}
}
}
sub _load_from_input {
my ($input, $vars) = @_;
# flag
my $flag = {
id => ($input->{mode} eq 'edit' ? $input->{flag_id} : 0),
name => trim($input->{flag_name} || ''),
description => trim($input->{flag_desc} || ''),
sortkey => $input->{flag_sort} || 0,
type => trim($input->{flag_type} || ''),
enter_bug => $input->{flag_enter_bug} ? 1 : 0,
is_active => $input->{flag_active} ? 1 : 0,
};
detaint_natural($flag->{id});
detaint_natural($flag->{sortkey});
detaint_natural($flag->{enter_bug});
detaint_natural($flag->{is_active});
# values
my $values = decode_json($input->{values} || '[]');
foreach my $value (@$values) {
$value->{value} = '' unless exists $value->{value} && defined $value->{value};
$value->{setter_group_id} = '' unless $value->{setter_group_id};
$value->{is_active} = $value->{is_active} ? 1 : 0;
}
# vibility
my $visibilities = decode_json($input->{visibility} || '[]');
foreach my $visibility (@$visibilities) {
$visibility->{product} = '' unless exists $visibility->{product} && defined $visibility->{product};
$visibility->{component} = '' unless exists $visibility->{component} && defined $visibility->{component};
}
return ($flag, $values, $visibilities);
}
sub _next_unique_sortkey {
my ($sortkey) = @_;
my %current;
foreach my $flag (Bugzilla::Extension::TrackingFlags::Flag->get_all()) {
$current{$flag->sortkey} = 1;
}
$sortkey += 5;
$sortkey += 5 while exists $current{$sortkey};
return $sortkey;
}
#
# validation
#
sub _validate {
my ($flag, $values, $visibilities) = @_;
# flag
my @missing;
push @missing, 'Field Name' if $flag->{name} eq '';
push @missing, 'Field Description' if $flag->{description} eq '';
push @missing, 'Field Sort Key' if $flag->{sortkey} eq '';
scalar(@missing)
&& ThrowUserError('tracking_flags_missing_mandatory', { fields => \@missing });
$flag->{name} =~ /^cf_/
|| ThrowUserError('tracking_flags_cf_prefix');
if ($flag->{id}) {
my $old_flag = Bugzilla::Extension::TrackingFlags::Flag->new($flag->{id})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag', id => $flag->{id} });
if ($flag->{name} ne $old_flag->name) {
Bugzilla::Field->new({ name => $flag->{name} })
&& ThrowUserError('field_already_exists', { field => { name => $flag->{name} }});
}
} else {
Bugzilla::Field->new({ name => $flag->{name} })
&& ThrowUserError('field_already_exists', { field => { name => $flag->{name} }});
}
# values
scalar(@$values)
|| ThrowUserError('tracking_flags_missing_values');
my %seen;
foreach my $value (@$values) {
my $v = $value->{value};
$v eq ''
&& ThrowUserError('tracking_flags_missing_value');
exists $seen{$v}
&& ThrowUserError('tracking_flags_duplicate_value', { value => $v });
$seen{$v} = 1;
push @missing, "Setter for $v" if !$value->{setter_group_id};
}
scalar(@missing)
&& ThrowUserError('tracking_flags_missing_mandatory', { fields => \@missing });
# visibility
scalar(@$visibilities)
|| ThrowUserError('tracking_flags_missing_visibility');
%seen = ();
foreach my $visibility (@$visibilities) {
my $name = $visibility->{product} . ':' . $visibility->{component};
exists $seen{$name}
&& ThrowUserError('tracking_flags_duplicate_visibility', { name => $name });
$visibility->{product_obj} = Bugzilla::Product->new({ name => $visibility->{product} })
|| ThrowCodeError('tracking_flags_invalid_product', { product => $visibility->{product} });
if ($visibility->{component} ne '') {
$visibility->{component_obj} = Bugzilla::Component->new({ product => $visibility->{product_obj},
name => $visibility->{component} })
|| ThrowCodeError('tracking_flags_invalid_component', { component => $visibility->{component} });
}
}
}
#
# database updating
#
sub _update_db {
my ($flag, $values, $visibilities) = @_;
my $dbh = Bugzilla->dbh;
$dbh->bz_start_transaction();
my $flag_obj = _update_db_flag($flag);
_update_db_values($flag_obj, $flag, $values);
_update_db_visibility($flag_obj, $flag, $visibilities);
$dbh->bz_commit_transaction();
return $flag_obj;
}
sub _update_db_flag {
my ($flag) = @_;
my $object_set = {
name => $flag->{name},
description => $flag->{description},
sortkey => $flag->{sortkey},
type => $flag->{type},
enter_bug => $flag->{enter_bug},
is_active => $flag->{is_active},
};
my $flag_obj;
if ($flag->{id}) {
# update existing flag
$flag_obj = Bugzilla::Extension::TrackingFlags::Flag->new($flag->{id})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag', id => $flag->{id} });
$flag_obj->set_all($object_set);
$flag_obj->update();
} else {
# create new flag
$flag_obj = Bugzilla::Extension::TrackingFlags::Flag->create($object_set);
}
return $flag_obj;
}
sub _update_db_values {
my ($flag_obj, $flag, $values) = @_;
# delete
foreach my $current_value (@{ $flag_obj->values }) {
if (!grep { $_->{id} == $current_value->id } @$values) {
$current_value->remove_from_db();
}
}
# add/update
my $sortkey = 0;
foreach my $value (@{ $values }) {
$sortkey += 10;
my $object_set = {
value => $value->{value},
setter_group_id => $value->{setter_group_id},
is_active => $value->{is_active},
sortkey => $sortkey,
};
if ($value->{id}) {
my $value_obj = Bugzilla::Extension::TrackingFlags::Flag::Value->new($value->{id})
|| ThrowCodeError('tracking_flags_invalid_item_id', { item => 'flag value', id => $flag->{id} });
my $old_value = $value_obj->value;
if ($object_set->{value} ne $old_value) {
$value_obj->set_all($object_set);
$value_obj->update();
Bugzilla::Extension::TrackingFlags::Flag::Bug->update_all_values({
value_obj => $value_obj,
old_value => $old_value,
new_value => $value_obj->value,
});
}
} else {
$object_set->{tracking_flag_id} = $flag_obj->flag_id;
Bugzilla::Extension::TrackingFlags::Flag::Value->create($object_set);
}
}
}
sub _update_db_visibility {
my ($flag_obj, $flag, $visibilities) = @_;
# delete
foreach my $current_visibility (@{ $flag_obj->visibility }) {
if (!grep { $_->{id} == $current_visibility->id } @$visibilities) {
$current_visibility->remove_from_db();
}
}
# add
foreach my $visibility (@{ $visibilities }) {
next if $visibility->{id};
Bugzilla::Extension::TrackingFlags::Flag::Visibility->create({
tracking_flag_id => $flag_obj->flag_id,
product_id => $visibility->{product_obj}->id,
component_id => $visibility->{component} ? $visibility->{component_obj}->id : undef,
});
}
}
#
# serialisation
#
sub _groups_to_json {
my @data;
foreach my $group (sort { $a->name cmp $b->name } Bugzilla::Group->get_all()) {
push @data, {
id => $group->id,
name => $group->name,
};
}
return encode_json(\@data);
}
sub _flag_values_to_json {
my ($values, $is_copy) = @_;
# setting is_copy will set the id's to zero, to force new values rather
# than editing existing ones
my @data;
foreach my $value (@$values) {
push @data, {
id => $is_copy ? 0 : $value->{id},
value => $value->{value},
setter_group_id => $value->{setter_group_id},
is_active => $value->{is_active} ? JSON::true : JSON::false,
};
}
return encode_json(\@data);
}
sub _flag_visibility_to_json {
my ($visibilities, $is_copy) = @_;
# setting is_copy will set the id's to zero, to force new visibilites
# rather than editing existing ones
my @data;
foreach my $visibility (@$visibilities) {
my $product = exists $visibility->{product_id}
? $visibility->product->name
: $visibility->{product};
my $component;
if (exists $visibility->{component_id} && $visibility->{component_id}) {
$component = $visibility->component->name;
} elsif (exists $visibility->{component}) {
$component = $visibility->{component};
} else {
$component = undef;
}
push @data, {
id => $is_copy ? 0 : $visibility->{id},
product => $product,
component => $component,
};
}
@data = sort {
lc($a->{product}) cmp lc($b->{product})
|| lc($a->{component}) cmp lc($b->{component})
} @data;
return encode_json(\@data);
}
1;
|