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 mozilla.org code.
16 # The Initial Developer of the Original Code is Holger
17 # Schurig. Portions created by Holger Schurig are
18 # Copyright (C) 1999 Holger Schurig. All
21 # Contributor(s): Holger Schurig <holgerschurig@nikocity.de>
22 # Terry Weissman <terry@mozilla.org>
23 # Frédéric Buclin <LpSolit@gmail.com>
24 # Akamai Technologies <bugzilla-dev@akamai.com>
30 use Bugzilla
::Constants
;
34 use Bugzilla
::Component
;
37 my $cgi = Bugzilla
->cgi;
38 my $template = Bugzilla
->template;
40 # There is only one section about components in the documentation,
41 # so all actions point to the same page.
42 $vars->{'doc_section'} = 'components.html';
48 my $user = Bugzilla
->login(LOGIN_REQUIRED
);
52 $user->in_group('editcomponents')
53 || scalar(@
{$user->get_products_by_permission('editcomponents')})
54 || ThrowUserError
("auth_failure", {group
=> "editcomponents",
56 object
=> "components"});
59 # often used variables
61 my $product_name = trim
($cgi->param('product') || '');
62 my $comp_name = trim
($cgi->param('component') || '');
63 my $action = trim
($cgi->param('action') || '');
64 my $showbugcounts = (defined $cgi->param('showbugcounts'));
65 my $token = $cgi->param('token');
68 # product = '' -> Show nice list of products
71 unless ($product_name) {
72 my $selectable_products = $user->get_selectable_products;
73 # If the user has editcomponents privs for some products only,
74 # we have to restrict the list of products to display.
75 unless ($user->in_group('editcomponents')) {
76 $selectable_products = $user->get_products_by_permission('editcomponents');
78 $vars->{'products'} = $selectable_products;
79 $vars->{'showbugcounts'} = $showbugcounts;
81 $template->process("admin/components/select-product.html.tmpl", $vars)
82 || ThrowTemplateError
($template->error());
86 my $product = $user->check_can_admin_product($product_name);
89 # action='' -> Show nice list of components
93 $vars->{'showbugcounts'} = $showbugcounts;
94 $vars->{'product'} = $product;
95 $template->process("admin/components/list.html.tmpl", $vars)
96 || ThrowTemplateError
($template->error());
101 # action='add' -> present form for parameters for new component
103 # (next action will be 'new')
106 if ($action eq 'add') {
107 $vars->{'token'} = issue_session_token
('add_component');
108 $vars->{'product'} = $product;
109 $template->process("admin/components/create.html.tmpl", $vars)
110 || ThrowTemplateError
($template->error());
115 # action='new' -> add component entered in the 'action=add' screen
118 if ($action eq 'new') {
119 check_token_data
($token, 'add_component');
120 # Do the user matching
121 Bugzilla
::User
::match_field
($cgi, {
122 'initialowner' => { 'type' => 'single' },
123 'initialqacontact' => { 'type' => 'single' },
124 'initialcc' => { 'type' => 'multi' },
127 my $default_assignee = trim
($cgi->param('initialowner') || '');
128 my $default_qa_contact = trim
($cgi->param('initialqacontact') || '');
129 my $description = trim
($cgi->param('description') || '');
130 my @initial_cc = $cgi->param('initialcc');
133 Bugzilla
::Component
->create({ name
=> $comp_name,
135 description
=> $description,
136 initialowner
=> $default_assignee,
137 initialqacontact
=> $default_qa_contact,
138 initial_cc
=> \
@initial_cc });
140 $vars->{'message'} = 'component_created';
141 $vars->{'comp'} = $component;
142 $vars->{'product'} = $product;
143 delete_token
($token);
145 $template->process("admin/components/list.html.tmpl", $vars)
146 || ThrowTemplateError
($template->error());
151 # action='del' -> ask if user really wants to delete
153 # (next action would be 'delete')
156 if ($action eq 'del') {
157 $vars->{'token'} = issue_session_token
('delete_component');
159 Bugzilla
::Component
->check({ product
=> $product, name
=> $comp_name });
160 $vars->{'product'} = $product;
162 $template->process("admin/components/confirm-delete.html.tmpl", $vars)
163 || ThrowTemplateError
($template->error());
168 # action='delete' -> really delete the component
171 if ($action eq 'delete') {
172 check_token_data
($token, 'delete_component');
174 Bugzilla
::Component
->check({ product
=> $product, name
=> $comp_name });
176 $component->remove_from_db;
178 $vars->{'message'} = 'component_deleted';
179 $vars->{'comp'} = $component;
180 $vars->{'product'} = $product;
181 $vars->{'no_edit_component_link'} = 1;
182 delete_token
($token);
184 $template->process("admin/components/list.html.tmpl", $vars)
185 || ThrowTemplateError
($template->error());
190 # action='edit' -> present the edit component form
192 # (next action would be 'update')
195 if ($action eq 'edit') {
196 $vars->{'token'} = issue_session_token
('edit_component');
198 Bugzilla
::Component
->check({ product
=> $product, name
=> $comp_name });
199 $vars->{'comp'} = $component;
201 $vars->{'initial_cc_names'} =
202 join(', ', map($_->login, @
{$component->initial_cc}));
204 $vars->{'product'} = $product;
206 $template->process("admin/components/edit.html.tmpl", $vars)
207 || ThrowTemplateError
($template->error());
212 # action='update' -> update the component
215 if ($action eq 'update') {
216 check_token_data
($token, 'edit_component');
217 # Do the user matching
218 Bugzilla
::User
::match_field
($cgi, {
219 'initialowner' => { 'type' => 'single' },
220 'initialqacontact' => { 'type' => 'single' },
221 'initialcc' => { 'type' => 'multi' },
224 my $comp_old_name = trim
($cgi->param('componentold') || '');
225 my $default_assignee = trim
($cgi->param('initialowner') || '');
226 my $default_qa_contact = trim
($cgi->param('initialqacontact') || '');
227 my $description = trim
($cgi->param('description') || '');
228 my @initial_cc = $cgi->param('initialcc');
231 Bugzilla
::Component
->check({ product
=> $product, name
=> $comp_old_name });
233 $component->set_name($comp_name);
234 $component->set_description($description);
235 $component->set_default_assignee($default_assignee);
236 $component->set_default_qa_contact($default_qa_contact);
237 $component->set_cc_list(\
@initial_cc);
238 my $changes = $component->update();
240 $vars->{'message'} = 'component_updated';
241 $vars->{'comp'} = $component;
242 $vars->{'product'} = $product;
243 $vars->{'changes'} = $changes;
244 delete_token
($token);
246 $template->process("admin/components/list.html.tmpl", $vars)
247 || ThrowTemplateError
($template->error());
252 # No valid action found
254 ThrowUserError
('no_valid_action', {'field' => "component"});