Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / editversions.cgi
blob146f7f3d741d51155dc9ec74c86930ffa05fb7e2
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
19 # Rights Reserved.
21 # Contributor(s): Holger Schurig <holgerschurig@nikocity.de>
22 # Terry Weissman <terry@mozilla.org>
23 # Gavin Shelley <bugzilla@chimpychompy.org>
24 # Frédéric Buclin <LpSolit@gmail.com>
26 use strict;
27 use lib qw(. lib);
29 use Bugzilla;
30 use Bugzilla::Constants;
31 use Bugzilla::Util;
32 use Bugzilla::Error;
33 use Bugzilla::Version;
34 use Bugzilla::Token;
36 my $cgi = Bugzilla->cgi;
37 my $dbh = Bugzilla->dbh;
38 my $template = Bugzilla->template;
39 my $vars = {};
40 # There is only one section about versions in the documentation,
41 # so all actions point to the same page.
42 $vars->{'doc_section'} = 'versions.html';
45 # Preliminary checks:
48 my $user = Bugzilla->login(LOGIN_REQUIRED);
50 print $cgi->header();
52 $user->in_group('editcomponents')
53 || scalar(@{$user->get_products_by_permission('editcomponents')})
54 || ThrowUserError("auth_failure", {group => "editcomponents",
55 action => "edit",
56 object => "versions"});
59 # often used variables
61 my $product_name = trim($cgi->param('product') || '');
62 my $version_name = trim($cgi->param('version') || '');
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/versions/select-product.html.tmpl", $vars)
82 || ThrowTemplateError($template->error());
83 exit;
86 my $product = $user->check_can_admin_product($product_name);
89 # action='' -> Show nice list of versions
92 unless ($action) {
93 $vars->{'showbugcounts'} = $showbugcounts;
94 $vars->{'product'} = $product;
95 $template->process("admin/versions/list.html.tmpl", $vars)
96 || ThrowTemplateError($template->error());
98 exit;
102 # action='add' -> present form for parameters for new version
104 # (next action will be 'new')
107 if ($action eq 'add') {
108 $vars->{'token'} = issue_session_token('add_version');
109 $vars->{'product'} = $product;
110 $template->process("admin/versions/create.html.tmpl", $vars)
111 || ThrowTemplateError($template->error());
113 exit;
117 # action='new' -> add version entered in the 'action=add' screen
120 if ($action eq 'new') {
121 check_token_data($token, 'add_version');
122 my $version = Bugzilla::Version::create($version_name, $product);
123 delete_token($token);
125 $vars->{'message'} = 'version_created';
126 $vars->{'version'} = $version;
127 $vars->{'product'} = $product;
128 $template->process("admin/versions/list.html.tmpl", $vars)
129 || ThrowTemplateError($template->error());
131 exit;
135 # action='del' -> ask if user really wants to delete
137 # (next action would be 'delete')
140 if ($action eq 'del') {
141 my $version = Bugzilla::Version->check({ product => $product,
142 name => $version_name });
143 $vars->{'version'} = $version;
144 $vars->{'product'} = $product;
145 $vars->{'token'} = issue_session_token('delete_version');
146 $template->process("admin/versions/confirm-delete.html.tmpl", $vars)
147 || ThrowTemplateError($template->error());
149 exit;
153 # action='delete' -> really delete the version
156 if ($action eq 'delete') {
157 check_token_data($token, 'delete_version');
158 my $version = Bugzilla::Version->check({ product => $product,
159 name => $version_name });
160 $version->remove_from_db;
161 delete_token($token);
163 $vars->{'message'} = 'version_deleted';
164 $vars->{'version'} = $version;
165 $vars->{'product'} = $product;
166 $vars->{'no_edit_version_link'} = 1;
168 $template->process("admin/versions/list.html.tmpl", $vars)
169 || ThrowTemplateError($template->error());
171 exit;
175 # action='edit' -> present the edit version form
177 # (next action would be 'update')
180 if ($action eq 'edit') {
181 my $version = Bugzilla::Version->check({ product => $product,
182 name => $version_name });
183 $vars->{'version'} = $version;
184 $vars->{'product'} = $product;
185 $vars->{'token'} = issue_session_token('edit_version');
187 $template->process("admin/versions/edit.html.tmpl", $vars)
188 || ThrowTemplateError($template->error());
190 exit;
194 # action='update' -> update the version
197 if ($action eq 'update') {
198 check_token_data($token, 'edit_version');
199 my $version_old_name = trim($cgi->param('versionold') || '');
200 my $version = Bugzilla::Version->check({ product => $product,
201 name => $version_old_name });
203 $dbh->bz_start_transaction();
205 $vars->{'updated'} = $version->update($version_name, $product);
207 $dbh->bz_commit_transaction();
208 delete_token($token);
210 $vars->{'message'} = 'version_updated';
211 $vars->{'version'} = $version;
212 $vars->{'product'} = $product;
213 $template->process("admin/versions/list.html.tmpl", $vars)
214 || ThrowTemplateError($template->error());
216 exit;
220 # No valid action found
222 ThrowUserError('no_valid_action', {'field' => "version"});