Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / editkeywords.cgi
blob5eabaedd5f20d7f4a0e0cc6947818e380f1f5a7a
1 #!/usr/bin/env perl -wT
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 # The Initial Developer of the Original Code is Terry Weissman.
17 # Portions created by Terry Weissman are
18 # Copyright (C) 2000 Terry Weissman. All
19 # Rights Reserved.
21 # Contributor(s): Terry Weissman <terry@mozilla.org>
23 use strict;
24 use lib qw(. lib);
26 use Bugzilla;
27 use Bugzilla::Constants;
28 use Bugzilla::Util;
29 use Bugzilla::Error;
30 use Bugzilla::Keyword;
31 use Bugzilla::Token;
33 my $cgi = Bugzilla->cgi;
34 my $dbh = Bugzilla->dbh;
35 my $template = Bugzilla->template;
36 my $vars = {};
39 # Preliminary checks:
42 my $user = Bugzilla->login(LOGIN_REQUIRED);
44 print $cgi->header();
46 $user->in_group('editkeywords')
47 || ThrowUserError("auth_failure", {group => "editkeywords",
48 action => "edit",
49 object => "keywords"});
51 my $action = trim($cgi->param('action') || '');
52 my $key_id = $cgi->param('id');
53 my $token = $cgi->param('token');
55 $vars->{'action'} = $action;
58 if ($action eq "") {
59 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
61 print $cgi->header();
62 $template->process("admin/keywords/list.html.tmpl", $vars)
63 || ThrowTemplateError($template->error());
65 exit;
69 if ($action eq 'add') {
70 $vars->{'token'} = issue_session_token('add_keyword');
72 print $cgi->header();
74 $template->process("admin/keywords/create.html.tmpl", $vars)
75 || ThrowTemplateError($template->error());
77 exit;
81 # action='new' -> add keyword entered in the 'action=add' screen
83 if ($action eq 'new') {
84 check_token_data($token, 'add_keyword');
85 my $name = $cgi->param('name') || '';
86 my $desc = $cgi->param('description') || '';
88 my $keyword = Bugzilla::Keyword->create(
89 { name => $name, description => $desc });
91 delete_token($token);
93 print $cgi->header();
95 $vars->{'message'} = 'keyword_created';
96 $vars->{'name'} = $keyword->name;
97 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
99 $template->process("admin/keywords/list.html.tmpl", $vars)
100 || ThrowTemplateError($template->error());
101 exit;
106 # action='edit' -> present the edit keywords from
108 # (next action would be 'update')
111 if ($action eq 'edit') {
112 my $keyword = new Bugzilla::Keyword($key_id)
113 || ThrowCodeError('invalid_keyword_id', { id => $key_id });
115 $vars->{'keyword'} = $keyword;
116 $vars->{'token'} = issue_session_token('edit_keyword');
118 print $cgi->header();
119 $template->process("admin/keywords/edit.html.tmpl", $vars)
120 || ThrowTemplateError($template->error());
121 exit;
126 # action='update' -> update the keyword
129 if ($action eq 'update') {
130 check_token_data($token, 'edit_keyword');
131 my $keyword = new Bugzilla::Keyword($key_id)
132 || ThrowCodeError('invalid_keyword_id', { id => $key_id });
134 $keyword->set_name($cgi->param('name'));
135 $keyword->set_description($cgi->param('description'));
136 my $changes = $keyword->update();
138 delete_token($token);
140 print $cgi->header();
142 $vars->{'message'} = 'keyword_updated';
143 $vars->{'keyword'} = $keyword;
144 $vars->{'changes'} = $changes;
145 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
147 $template->process("admin/keywords/list.html.tmpl", $vars)
148 || ThrowTemplateError($template->error());
149 exit;
152 if ($action eq 'del') {
153 my $keyword = new Bugzilla::Keyword($key_id)
154 || ThrowCodeError('invalid_keyword_id', { id => $key_id });
156 $vars->{'keyword'} = $keyword;
157 $vars->{'token'} = issue_session_token('delete_keyword');
159 print $cgi->header();
160 $template->process("admin/keywords/confirm-delete.html.tmpl", $vars)
161 || ThrowTemplateError($template->error());
162 exit;
165 if ($action eq 'delete') {
166 check_token_data($token, 'delete_keyword');
167 my $keyword = new Bugzilla::Keyword($key_id)
168 || ThrowCodeError('invalid_keyword_id', { id => $key_id });
170 $dbh->do('DELETE FROM keywords WHERE keywordid = ?', undef, $keyword->id);
171 $dbh->do('DELETE FROM keyworddefs WHERE id = ?', undef, $keyword->id);
173 delete_token($token);
175 print $cgi->header();
177 $vars->{'message'} = 'keyword_deleted';
178 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
180 $template->process("admin/keywords/list.html.tmpl", $vars)
181 || ThrowTemplateError($template->error());
182 exit;
185 ThrowCodeError("action_unrecognized", $vars);