]> git.ipfire.org Git - thirdparty/bugzilla.git/blob - editkeywords.cgi
add a new hook: template_after_create (#60)
[thirdparty/bugzilla.git] / editkeywords.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::Util;
18 use Bugzilla::Error;
19 use Bugzilla::Keyword;
20 use Bugzilla::Token;
21
22 my $cgi = Bugzilla->cgi;
23 my $dbh = Bugzilla->dbh;
24 my $template = Bugzilla->template;
25 my $vars = {};
26
27 my $user = Bugzilla->login(LOGIN_REQUIRED);
28
29 print $cgi->header();
30
31 $user->in_group('editkeywords')
32 || ThrowUserError("auth_failure", {group => "editkeywords",
33 action => "edit",
34 object => "keywords"});
35
36 my $action = trim($cgi->param('action') || '');
37 my $key_id = $cgi->param('id');
38 my $token = $cgi->param('token');
39
40 $vars->{'action'} = $action;
41
42
43 if ($action eq "") {
44 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
45
46 $template->process("admin/keywords/list.html.tmpl", $vars)
47 || ThrowTemplateError($template->error());
48 exit;
49 }
50
51 if ($action eq 'add') {
52 $vars->{'token'} = issue_session_token('add_keyword');
53
54 $template->process("admin/keywords/create.html.tmpl", $vars)
55 || ThrowTemplateError($template->error());
56 exit;
57 }
58
59 #
60 # action='new' -> add keyword entered in the 'action=add' screen
61 #
62 if ($action eq 'new') {
63 check_token_data($token, 'add_keyword');
64 my $name = $cgi->param('name') || '';
65 my $desc = $cgi->param('description') || '';
66
67 my $keyword = Bugzilla::Keyword->create(
68 { name => $name, description => $desc });
69
70 delete_token($token);
71
72 $vars->{'message'} = 'keyword_created';
73 $vars->{'name'} = $keyword->name;
74 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
75
76 $template->process("admin/keywords/list.html.tmpl", $vars)
77 || ThrowTemplateError($template->error());
78 exit;
79 }
80
81
82 #
83 # action='edit' -> present the edit keywords from
84 #
85 # (next action would be 'update')
86 #
87
88 if ($action eq 'edit') {
89 my $keyword = new Bugzilla::Keyword($key_id)
90 || ThrowUserError('invalid_keyword_id', { id => $key_id });
91
92 $vars->{'keyword'} = $keyword;
93 $vars->{'token'} = issue_session_token('edit_keyword');
94
95 $template->process("admin/keywords/edit.html.tmpl", $vars)
96 || ThrowTemplateError($template->error());
97 exit;
98 }
99
100
101 #
102 # action='update' -> update the keyword
103 #
104
105 if ($action eq 'update') {
106 check_token_data($token, 'edit_keyword');
107 my $keyword = new Bugzilla::Keyword($key_id)
108 || ThrowUserError('invalid_keyword_id', { id => $key_id });
109
110 $keyword->set_all({
111 name => scalar $cgi->param('name'),
112 description => scalar $cgi->param('description'),
113 });
114 my $changes = $keyword->update();
115
116 delete_token($token);
117
118 $vars->{'message'} = 'keyword_updated';
119 $vars->{'keyword'} = $keyword;
120 $vars->{'changes'} = $changes;
121 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
122
123 $template->process("admin/keywords/list.html.tmpl", $vars)
124 || ThrowTemplateError($template->error());
125 exit;
126 }
127
128 if ($action eq 'del') {
129 my $keyword = new Bugzilla::Keyword($key_id)
130 || ThrowUserError('invalid_keyword_id', { id => $key_id });
131
132 $vars->{'keyword'} = $keyword;
133 $vars->{'token'} = issue_session_token('delete_keyword');
134
135 $template->process("admin/keywords/confirm-delete.html.tmpl", $vars)
136 || ThrowTemplateError($template->error());
137 exit;
138 }
139
140 if ($action eq 'delete') {
141 check_token_data($token, 'delete_keyword');
142 my $keyword = new Bugzilla::Keyword($key_id)
143 || ThrowUserError('invalid_keyword_id', { id => $key_id });
144
145 $keyword->remove_from_db();
146
147 delete_token($token);
148
149 $vars->{'message'} = 'keyword_deleted';
150 $vars->{'keyword'} = $keyword;
151 $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
152
153 $template->process("admin/keywords/list.html.tmpl", $vars)
154 || ThrowTemplateError($template->error());
155 exit;
156 }
157
158 ThrowUserError('unknown_action', {action => $action});