]> git.ipfire.org Git - thirdparty/bugzilla.git/blob - editfields.cgi
add a new hook: template_after_create (#60)
[thirdparty/bugzilla.git] / editfields.cgi
1 #!/usr/bin/perl -T
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/.
5 #
6 # This Source Code Form is "Incompatible With Secondary Licenses", as
7 # defined by the Mozilla Public License, v. 2.0.
8
9 use 5.10.1;
10 use strict;
11 use warnings;
12
13 use lib qw(. lib);
14
15 use Bugzilla;
16 use Bugzilla::Constants;
17 use Bugzilla::Error;
18 use Bugzilla::Util;
19 use Bugzilla::Field;
20 use Bugzilla::Token;
21
22 my $cgi = Bugzilla->cgi;
23 my $template = Bugzilla->template;
24 my $vars = {};
25
26 # Make sure the user is logged in and is an administrator.
27 my $user = Bugzilla->login(LOGIN_REQUIRED);
28 $user->in_group('admin')
29 || ThrowUserError('auth_failure', {group => 'admin',
30 action => 'edit',
31 object => 'custom_fields'});
32
33 my $action = trim($cgi->param('action') || '');
34 my $token = $cgi->param('token');
35
36 print $cgi->header();
37
38 # List all existing custom fields if no action is given.
39 if (!$action) {
40 $template->process('admin/custom_fields/list.html.tmpl', $vars)
41 || ThrowTemplateError($template->error());
42 }
43 # Interface to add a new custom field.
44 elsif ($action eq 'add') {
45 $vars->{'token'} = issue_session_token('add_field');
46
47 $template->process('admin/custom_fields/create.html.tmpl', $vars)
48 || ThrowTemplateError($template->error());
49 }
50 elsif ($action eq 'new') {
51 check_token_data($token, 'add_field');
52 $vars->{'field'} = Bugzilla::Field->create({
53 name => scalar $cgi->param('name'),
54 description => scalar $cgi->param('desc'),
55 long_desc => scalar $cgi->param('long_desc'),
56 type => scalar $cgi->param('type'),
57 sortkey => scalar $cgi->param('sortkey'),
58 mailhead => scalar $cgi->param('new_bugmail'),
59 enter_bug => scalar $cgi->param('enter_bug'),
60 obsolete => scalar $cgi->param('obsolete'),
61 custom => 1,
62 buglist => 1,
63 visibility_field_id => scalar $cgi->param('visibility_field_id'),
64 visibility_values => [ $cgi->param('visibility_values') ],
65 value_field_id => scalar $cgi->param('value_field_id'),
66 reverse_desc => scalar $cgi->param('reverse_desc'),
67 is_mandatory => scalar $cgi->param('is_mandatory'),
68 });
69
70 delete_token($token);
71
72 $vars->{'message'} = 'custom_field_created';
73
74 $template->process('admin/custom_fields/list.html.tmpl', $vars)
75 || ThrowTemplateError($template->error());
76 }
77 elsif ($action eq 'edit') {
78 my $name = $cgi->param('name') || ThrowUserError('field_missing_name');
79 # Custom field names must start with "cf_".
80 if ($name !~ /^cf_/) {
81 $name = 'cf_' . $name;
82 }
83 my $field = new Bugzilla::Field({'name' => $name});
84 $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
85
86 $vars->{'field'} = $field;
87 $vars->{'token'} = issue_session_token('edit_field');
88
89 $template->process('admin/custom_fields/edit.html.tmpl', $vars)
90 || ThrowTemplateError($template->error());
91 }
92 elsif ($action eq 'update') {
93 check_token_data($token, 'edit_field');
94 my $name = $cgi->param('name');
95
96 # Validate fields.
97 $name || ThrowUserError('field_missing_name');
98 # Custom field names must start with "cf_".
99 if ($name !~ /^cf_/) {
100 $name = 'cf_' . $name;
101 }
102 my $field = new Bugzilla::Field({'name' => $name});
103 $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
104
105 $field->set_description($cgi->param('desc'));
106 $field->set_long_desc($cgi->param('long_desc'));
107 $field->set_sortkey($cgi->param('sortkey'));
108 $field->set_in_new_bugmail($cgi->param('new_bugmail'));
109 $field->set_enter_bug($cgi->param('enter_bug'));
110 $field->set_obsolete($cgi->param('obsolete'));
111 $field->set_is_mandatory($cgi->param('is_mandatory'));
112 $field->set_visibility_field($cgi->param('visibility_field_id'));
113 $field->set_visibility_values([ $cgi->param('visibility_values') ]);
114 $field->set_value_field($cgi->param('value_field_id'));
115 $field->set_reverse_desc($cgi->param('reverse_desc'));
116 $field->update();
117
118 delete_token($token);
119
120 $vars->{'field'} = $field;
121 $vars->{'message'} = 'custom_field_updated';
122
123 $template->process('admin/custom_fields/list.html.tmpl', $vars)
124 || ThrowTemplateError($template->error());
125 }
126 elsif ($action eq 'del') {
127 my $name = $cgi->param('name');
128
129 # Validate field.
130 $name || ThrowUserError('field_missing_name');
131 # Custom field names must start with "cf_".
132 if ($name !~ /^cf_/) {
133 $name = 'cf_' . $name;
134 }
135 my $field = new Bugzilla::Field({'name' => $name});
136 $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
137
138 $vars->{'field'} = $field;
139 $vars->{'token'} = issue_session_token('delete_field');
140
141 $template->process('admin/custom_fields/confirm-delete.html.tmpl', $vars)
142 || ThrowTemplateError($template->error());
143 }
144 elsif ($action eq 'delete') {
145 check_token_data($token, 'delete_field');
146 my $name = $cgi->param('name');
147
148 # Validate fields.
149 $name || ThrowUserError('field_missing_name');
150 # Custom field names must start with "cf_".
151 if ($name !~ /^cf_/) {
152 $name = 'cf_' . $name;
153 }
154 my $field = new Bugzilla::Field({'name' => $name});
155 $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
156
157 # Calling remove_from_db will check if field can be deleted.
158 # If the field cannot be deleted, it will throw an error.
159 $field->remove_from_db();
160
161 $vars->{'field'} = $field;
162 $vars->{'message'} = 'custom_field_deleted';
163
164 delete_token($token);
165
166 $template->process('admin/custom_fields/list.html.tmpl', $vars)
167 || ThrowTemplateError($template->error());
168 }
169 else {
170 ThrowUserError('unknown_action', {action => $action});
171 }