RSS

(root)/bugzilla/4.2 : /editvalues.cgi (revision 8010)

To get this branch, use:
bzr branch /bugzilla/4.2
Line Revision Contents
1 3135
#!/usr/bin/perl -wT
2
# The contents of this file are subject to the Mozilla Public
3
# License Version 1.1 (the "License"); you may not use this file
4
# except in compliance with the License. You may obtain a copy of
5
# the License at http://www.mozilla.org/MPL/
6
#
7
# Software distributed under the License is distributed on an "AS
8
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
# implied. See the License for the specific language governing
10
# rights and limitations under the License.
11
#
12
# The Original Code is the Bugzilla Bug Tracking System.
13
#
14 3434
# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
15 3756
#                 Frédéric Buclin <LpSolit@gmail.com>
16 3135
17
# This is a script to edit the values of fields that have drop-down
18
# or select boxes. It is largely a copy of editmilestones.cgi, but 
19
# with some cleanup.
20
21
use strict;
22 5304
use lib qw(. lib);
23 3135
24
use Bugzilla;
25
use Bugzilla::Util;
26
use Bugzilla::Error;
27
use Bugzilla::Constants;
28 4612
use Bugzilla::Token;
29 5068
use Bugzilla::Field;
30 6258
use Bugzilla::Field::Choice;
31 3135
32 6258
###############
33
# Subroutines #
34
###############
35
36
sub display_field_values {
37
    my $vars = shift;
38
    my $template = Bugzilla->template;
39
    $vars->{'values'} = $vars->{'field'}->legal_values;
40
    $template->process("admin/fieldvalues/list.html.tmpl", $vars)
41
      || ThrowTemplateError($template->error());
42
    exit;
43 3135
}
44
45
######################################################################
46
# Main Body Execution
47
######################################################################
48
49
# require the user to have logged in
50
Bugzilla->login(LOGIN_REQUIRED);
51
52
my $dbh      = Bugzilla->dbh;
53
my $cgi      = Bugzilla->cgi;
54
my $template = Bugzilla->template;
55 6258
my $vars = {};
56 3135
57 5322
# Replace this entry by separate entries in templates when
58
# the documentation about legal values becomes bigger.
59
$vars->{'doc_section'} = 'edit-values.html';
60
61 3135
print $cgi->header();
62
63 6153
Bugzilla->user->in_group('admin') ||
64 4579
    ThrowUserError('auth_failure', {group  => "admin",
65 3135
                                    action => "edit",
66 4578
                                    object => "field_values"});
67 3135
68
#
69
# often-used variables
70
#
71 6258
my $action = trim($cgi->param('action')  || '');
72
my $token  = $cgi->param('token');
73 4343
74 3135
#
75
# field = '' -> Show nice list of fields
76
#
77 6258
if (!$cgi->param('field')) {
78 7444
    my @field_list =
79
        @{ Bugzilla->fields({ is_select => 1, is_abnormal => 0 }) };
80 3135
81
    $vars->{'fields'} = \@field_list;
82 6258
    $template->process("admin/fieldvalues/select-field.html.tmpl", $vars)
83
      || ThrowTemplateError($template->error());
84
    exit;
85
}
86
87
# At this point, the field must be defined.
88
my $field = Bugzilla::Field->check($cgi->param('field'));
89 6955
if (!$field->is_select || $field->is_abnormal) {
90 6258
    ThrowUserError('fieldname_invalid', { field => $field });
91
}
92
$vars->{'field'} = $field;
93 3135
94 5441
#
95
# action='' -> Show nice list of values.
96
#
97 6258
display_field_values($vars) unless $action;
98 3135
99
#
100
# action='add' -> show form for adding new field value.
101
# (next action will be 'new')
102
#
103
if ($action eq 'add') {
104 4612
    $vars->{'token'} = issue_session_token('add_field_value');
105 6258
    $template->process("admin/fieldvalues/create.html.tmpl", $vars)
106 3135
      || ThrowTemplateError($template->error());
107
    exit;
108
}
109
110
#
111
# action='new' -> add field value entered in the 'action=add' screen
112
#
113
if ($action eq 'new') {
114 4612
    check_token_data($token, 'add_field_value');
115 3135
116 6312
    my $created_value = Bugzilla::Field::Choice->type($field)->create({
117
        value   => scalar $cgi->param('value'), 
118
        sortkey => scalar $cgi->param('sortkey'),
119
        is_open => scalar $cgi->param('is_open'),
120
        visibility_value_id => scalar $cgi->param('visibility_value_id'),
121
    });
122 3135
123 4612
    delete_token($token);
124
125 5441
    $vars->{'message'} = 'field_value_created';
126 6258
    $vars->{'value'} = $created_value;
127
    display_field_values($vars);
128 3135
}
129
130 6258
# After this, we always have a value
131
my $value = Bugzilla::Field::Choice->type($field)->check($cgi->param('value'));
132
$vars->{'value'} = $value;
133 3135
134
#
135
# action='del' -> ask if user really wants to delete
136
# (next action would be 'delete')
137
#
138
if ($action eq 'del') {
139 4343
    # If the value cannot be deleted, throw an error.
140 6258
    if ($value->is_static) {
141 4343
        ThrowUserError('fieldvalue_not_deletable', $vars);
142
    }
143 4612
    $vars->{'token'} = issue_session_token('delete_field_value');
144 4343
145 6258
    $template->process("admin/fieldvalues/confirm-delete.html.tmpl", $vars)
146 3135
      || ThrowTemplateError($template->error());
147
148
    exit;
149
}
150
151
152
#
153
# action='delete' -> really delete the field value
154
#
155
if ($action eq 'delete') {
156 4612
    check_token_data($token, 'delete_field_value');
157 6258
    $value->remove_from_db();
158 4612
    delete_token($token);
159 5441
    $vars->{'message'} = 'field_value_deleted';
160
    $vars->{'no_edit_link'} = 1;
161 6258
    display_field_values($vars);
162 3135
}
163
164
165
#
166
# action='edit' -> present the edit-value form
167
# (next action would be 'update')
168
#
169
if ($action eq 'edit') {
170 4612
    $vars->{'token'} = issue_session_token('edit_field_value');
171 4343
    $template->process("admin/fieldvalues/edit.html.tmpl", $vars)
172 3135
      || ThrowTemplateError($template->error());
173
174
    exit;
175
}
176
177
178
#
179
# action='update' -> update the field value
180
#
181
if ($action eq 'update') {
182 4612
    check_token_data($token, 'edit_field_value');
183 6258
    $vars->{'value_old'} = $value->name;
184 7170
    if ($cgi->should_set('is_active')) {
185
        $value->set_is_active($cgi->param('is_active'));
186
    }
187 6258
    $value->set_name($cgi->param('value_new'));
188
    $value->set_sortkey($cgi->param('sortkey'));
189 6312
    $value->set_visibility_value($cgi->param('visibility_value_id'));
190 6258
    $vars->{'changes'} = $value->update();
191 4612
    delete_token($token);
192 5441
    $vars->{'message'} = 'field_value_updated';
193 6258
    display_field_values($vars);
194 3135
}
195
196
# No valid action found
197 7188
ThrowUserError('unknown_action', {action => $action});

Loggerhead 1.18.1 is a web-based interface for Bazaar branches