2
# -*- Mode: perl; indent-tabs-mode: nil -*-
4
# The contents of this file are subject to the Mozilla Public
5
# License Version 1.1 (the "License"); you may not use this file
6
# except in compliance with the License. You may obtain a copy of
7
# the License at http://www.mozilla.org/MPL/
9
# Software distributed under the License is distributed on an "AS
10
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
# implied. See the License for the specific language governing
12
# rights and limitations under the License.
14
# The Original Code is the Bugzilla Bug Tracking System.
16
# Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
22
use Bugzilla::Constants;
28
my $cgi = Bugzilla->cgi;
29
my $template = Bugzilla->template;
32
# Make sure the user is logged in and is an administrator.
33
my $user = Bugzilla->login(LOGIN_REQUIRED);
34
$user->in_group('admin')
35
|| ThrowUserError('auth_failure', {group => 'admin',
37
object => 'custom_fields'});
39
my $action = trim($cgi->param('action') || '');
40
my $token = $cgi->param('token');
44
# List all existing custom fields if no action is given.
46
$template->process('admin/custom_fields/list.html.tmpl', $vars)
47
|| ThrowTemplateError($template->error());
49
# Interface to add a new custom field.
50
elsif ($action eq 'add') {
51
$vars->{'token'} = issue_session_token('add_field');
53
$template->process('admin/custom_fields/create.html.tmpl', $vars)
54
|| ThrowTemplateError($template->error());
56
elsif ($action eq 'new') {
57
check_token_data($token, 'add_field');
59
$vars->{'field'} = Bugzilla::Field->create({
60
name => scalar $cgi->param('name'),
61
description => scalar $cgi->param('desc'),
62
type => scalar $cgi->param('type'),
63
sortkey => scalar $cgi->param('sortkey'),
64
mailhead => scalar $cgi->param('new_bugmail'),
65
enter_bug => scalar $cgi->param('enter_bug'),
66
obsolete => scalar $cgi->param('obsolete'),
72
$vars->{'message'} = 'custom_field_created';
74
$template->process('admin/custom_fields/list.html.tmpl', $vars)
75
|| ThrowTemplateError($template->error());
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;
83
my $field = new Bugzilla::Field({'name' => $name});
84
$field || ThrowUserError('customfield_nonexistent', {'name' => $name});
86
$vars->{'field'} = $field;
87
$vars->{'token'} = issue_session_token('edit_field');
89
$template->process('admin/custom_fields/edit.html.tmpl', $vars)
90
|| ThrowTemplateError($template->error());
92
elsif ($action eq 'update') {
93
check_token_data($token, 'edit_field');
94
my $name = $cgi->param('name');
97
$name || ThrowUserError('field_missing_name');
98
# Custom field names must start with "cf_".
99
if ($name !~ /^cf_/) {
100
$name = 'cf_' . $name;
102
my $field = new Bugzilla::Field({'name' => $name});
103
$field || ThrowUserError('customfield_nonexistent', {'name' => $name});
105
$field->set_description($cgi->param('desc'));
106
$field->set_sortkey($cgi->param('sortkey'));
107
$field->set_in_new_bugmail($cgi->param('new_bugmail'));
108
$field->set_enter_bug($cgi->param('enter_bug'));
109
$field->set_obsolete($cgi->param('obsolete'));
112
delete_token($token);
114
$vars->{'field'} = $field;
115
$vars->{'message'} = 'custom_field_updated';
117
$template->process('admin/custom_fields/list.html.tmpl', $vars)
118
|| ThrowTemplateError($template->error());
121
ThrowUserError('no_valid_action', {'field' => 'custom_field'});