editfields.cgi 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  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/
  8. #
  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.
  13. #
  14. # The Original Code is the Bugzilla Bug Tracking System.
  15. #
  16. # Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
  17. use strict;
  18. use lib qw(. lib);
  19. use Bugzilla;
  20. use Bugzilla::Constants;
  21. use Bugzilla::Error;
  22. use Bugzilla::Util;
  23. use Bugzilla::Field;
  24. use Bugzilla::Token;
  25. my $cgi = Bugzilla->cgi;
  26. my $template = Bugzilla->template;
  27. my $vars = {};
  28. # Make sure the user is logged in and is an administrator.
  29. my $user = Bugzilla->login(LOGIN_REQUIRED);
  30. $user->in_group('admin')
  31. || ThrowUserError('auth_failure', {group => 'admin',
  32. action => 'edit',
  33. object => 'custom_fields'});
  34. my $action = trim($cgi->param('action') || '');
  35. my $token = $cgi->param('token');
  36. print $cgi->header();
  37. # List all existing custom fields if no action is given.
  38. if (!$action) {
  39. $template->process('admin/custom_fields/list.html.tmpl', $vars)
  40. || ThrowTemplateError($template->error());
  41. }
  42. # Interface to add a new custom field.
  43. elsif ($action eq 'add') {
  44. $vars->{'token'} = issue_session_token('add_field');
  45. $template->process('admin/custom_fields/create.html.tmpl', $vars)
  46. || ThrowTemplateError($template->error());
  47. }
  48. elsif ($action eq 'new') {
  49. check_token_data($token, 'add_field');
  50. $vars->{'field'} = Bugzilla::Field->create({
  51. name => scalar $cgi->param('name'),
  52. description => scalar $cgi->param('desc'),
  53. type => scalar $cgi->param('type'),
  54. sortkey => scalar $cgi->param('sortkey'),
  55. mailhead => scalar $cgi->param('new_bugmail'),
  56. enter_bug => scalar $cgi->param('enter_bug'),
  57. obsolete => scalar $cgi->param('obsolete'),
  58. custom => 1,
  59. });
  60. delete_token($token);
  61. $vars->{'message'} = 'custom_field_created';
  62. $template->process('admin/custom_fields/list.html.tmpl', $vars)
  63. || ThrowTemplateError($template->error());
  64. }
  65. elsif ($action eq 'edit') {
  66. my $name = $cgi->param('name') || ThrowUserError('field_missing_name');
  67. # Custom field names must start with "cf_".
  68. if ($name !~ /^cf_/) {
  69. $name = 'cf_' . $name;
  70. }
  71. my $field = new Bugzilla::Field({'name' => $name});
  72. $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
  73. $vars->{'field'} = $field;
  74. $vars->{'token'} = issue_session_token('edit_field');
  75. $template->process('admin/custom_fields/edit.html.tmpl', $vars)
  76. || ThrowTemplateError($template->error());
  77. }
  78. elsif ($action eq 'update') {
  79. check_token_data($token, 'edit_field');
  80. my $name = $cgi->param('name');
  81. # Validate fields.
  82. $name || ThrowUserError('field_missing_name');
  83. # Custom field names must start with "cf_".
  84. if ($name !~ /^cf_/) {
  85. $name = 'cf_' . $name;
  86. }
  87. my $field = new Bugzilla::Field({'name' => $name});
  88. $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
  89. $field->set_description($cgi->param('desc'));
  90. $field->set_sortkey($cgi->param('sortkey'));
  91. $field->set_in_new_bugmail($cgi->param('new_bugmail'));
  92. $field->set_enter_bug($cgi->param('enter_bug'));
  93. $field->set_obsolete($cgi->param('obsolete'));
  94. $field->update();
  95. delete_token($token);
  96. $vars->{'field'} = $field;
  97. $vars->{'message'} = 'custom_field_updated';
  98. $template->process('admin/custom_fields/list.html.tmpl', $vars)
  99. || ThrowTemplateError($template->error());
  100. }
  101. elsif ($action eq 'del') {
  102. my $name = $cgi->param('name');
  103. # Validate field.
  104. $name || ThrowUserError('field_missing_name');
  105. # Custom field names must start with "cf_".
  106. if ($name !~ /^cf_/) {
  107. $name = 'cf_' . $name;
  108. }
  109. my $field = new Bugzilla::Field({'name' => $name});
  110. $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
  111. $vars->{'field'} = $field;
  112. $vars->{'token'} = issue_session_token('delete_field');
  113. $template->process('admin/custom_fields/confirm-delete.html.tmpl', $vars)
  114. || ThrowTemplateError($template->error());
  115. }
  116. elsif ($action eq 'delete') {
  117. check_token_data($token, 'delete_field');
  118. my $name = $cgi->param('name');
  119. # Validate fields.
  120. $name || ThrowUserError('field_missing_name');
  121. # Custom field names must start with "cf_".
  122. if ($name !~ /^cf_/) {
  123. $name = 'cf_' . $name;
  124. }
  125. my $field = new Bugzilla::Field({'name' => $name});
  126. $field || ThrowUserError('customfield_nonexistent', {'name' => $name});
  127. # Calling remove_from_db will check if field can be deleted.
  128. # If the field cannot be deleted, it will throw an error.
  129. $field->remove_from_db();
  130. $vars->{'field'} = $field;
  131. $vars->{'message'} = 'custom_field_deleted';
  132. delete_token($token);
  133. $template->process('admin/custom_fields/list.html.tmpl', $vars)
  134. || ThrowTemplateError($template->error());
  135. }
  136. else {
  137. ThrowUserError('no_valid_action', {'field' => 'custom_field'});
  138. }