editversions.cgi 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 mozilla.org code.
  15. #
  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.
  20. #
  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>
  25. use strict;
  26. use lib qw(. lib);
  27. use Bugzilla;
  28. use Bugzilla::Constants;
  29. use Bugzilla::Util;
  30. use Bugzilla::Error;
  31. use Bugzilla::Version;
  32. use Bugzilla::Token;
  33. my $cgi = Bugzilla->cgi;
  34. my $dbh = Bugzilla->dbh;
  35. my $template = Bugzilla->template;
  36. my $vars = {};
  37. # There is only one section about versions in the documentation,
  38. # so all actions point to the same page.
  39. $vars->{'doc_section'} = 'versions.html';
  40. #
  41. # Preliminary checks:
  42. #
  43. my $user = Bugzilla->login(LOGIN_REQUIRED);
  44. print $cgi->header();
  45. $user->in_group('editcomponents')
  46. || scalar(@{$user->get_products_by_permission('editcomponents')})
  47. || ThrowUserError("auth_failure", {group => "editcomponents",
  48. action => "edit",
  49. object => "versions"});
  50. #
  51. # often used variables
  52. #
  53. my $product_name = trim($cgi->param('product') || '');
  54. my $version_name = trim($cgi->param('version') || '');
  55. my $action = trim($cgi->param('action') || '');
  56. my $showbugcounts = (defined $cgi->param('showbugcounts'));
  57. my $token = $cgi->param('token');
  58. #
  59. # product = '' -> Show nice list of products
  60. #
  61. unless ($product_name) {
  62. my $selectable_products = $user->get_selectable_products;
  63. # If the user has editcomponents privs for some products only,
  64. # we have to restrict the list of products to display.
  65. unless ($user->in_group('editcomponents')) {
  66. $selectable_products = $user->get_products_by_permission('editcomponents');
  67. }
  68. $vars->{'products'} = $selectable_products;
  69. $vars->{'showbugcounts'} = $showbugcounts;
  70. $template->process("admin/versions/select-product.html.tmpl", $vars)
  71. || ThrowTemplateError($template->error());
  72. exit;
  73. }
  74. my $product = $user->check_can_admin_product($product_name);
  75. #
  76. # action='' -> Show nice list of versions
  77. #
  78. unless ($action) {
  79. $vars->{'showbugcounts'} = $showbugcounts;
  80. $vars->{'product'} = $product;
  81. $template->process("admin/versions/list.html.tmpl", $vars)
  82. || ThrowTemplateError($template->error());
  83. exit;
  84. }
  85. #
  86. # action='add' -> present form for parameters for new version
  87. #
  88. # (next action will be 'new')
  89. #
  90. if ($action eq 'add') {
  91. $vars->{'token'} = issue_session_token('add_version');
  92. $vars->{'product'} = $product;
  93. $template->process("admin/versions/create.html.tmpl", $vars)
  94. || ThrowTemplateError($template->error());
  95. exit;
  96. }
  97. #
  98. # action='new' -> add version entered in the 'action=add' screen
  99. #
  100. if ($action eq 'new') {
  101. check_token_data($token, 'add_version');
  102. my $version = Bugzilla::Version::create($version_name, $product);
  103. delete_token($token);
  104. $vars->{'message'} = 'version_created';
  105. $vars->{'version'} = $version;
  106. $vars->{'product'} = $product;
  107. $template->process("admin/versions/list.html.tmpl", $vars)
  108. || ThrowTemplateError($template->error());
  109. exit;
  110. }
  111. #
  112. # action='del' -> ask if user really wants to delete
  113. #
  114. # (next action would be 'delete')
  115. #
  116. if ($action eq 'del') {
  117. my $version = Bugzilla::Version->check({ product => $product,
  118. name => $version_name });
  119. $vars->{'version'} = $version;
  120. $vars->{'product'} = $product;
  121. $vars->{'token'} = issue_session_token('delete_version');
  122. $template->process("admin/versions/confirm-delete.html.tmpl", $vars)
  123. || ThrowTemplateError($template->error());
  124. exit;
  125. }
  126. #
  127. # action='delete' -> really delete the version
  128. #
  129. if ($action eq 'delete') {
  130. check_token_data($token, 'delete_version');
  131. my $version = Bugzilla::Version->check({ product => $product,
  132. name => $version_name });
  133. $version->remove_from_db;
  134. delete_token($token);
  135. $vars->{'message'} = 'version_deleted';
  136. $vars->{'version'} = $version;
  137. $vars->{'product'} = $product;
  138. $vars->{'no_edit_version_link'} = 1;
  139. $template->process("admin/versions/list.html.tmpl", $vars)
  140. || ThrowTemplateError($template->error());
  141. exit;
  142. }
  143. #
  144. # action='edit' -> present the edit version form
  145. #
  146. # (next action would be 'update')
  147. #
  148. if ($action eq 'edit') {
  149. my $version = Bugzilla::Version->check({ product => $product,
  150. name => $version_name });
  151. $vars->{'version'} = $version;
  152. $vars->{'product'} = $product;
  153. $vars->{'token'} = issue_session_token('edit_version');
  154. $template->process("admin/versions/edit.html.tmpl", $vars)
  155. || ThrowTemplateError($template->error());
  156. exit;
  157. }
  158. #
  159. # action='update' -> update the version
  160. #
  161. if ($action eq 'update') {
  162. check_token_data($token, 'edit_version');
  163. my $version_old_name = trim($cgi->param('versionold') || '');
  164. my $version = Bugzilla::Version->check({ product => $product,
  165. name => $version_old_name });
  166. $dbh->bz_start_transaction();
  167. $vars->{'updated'} = $version->update($version_name, $product);
  168. $dbh->bz_commit_transaction();
  169. delete_token($token);
  170. $vars->{'message'} = 'version_updated';
  171. $vars->{'version'} = $version;
  172. $vars->{'product'} = $product;
  173. $template->process("admin/versions/list.html.tmpl", $vars)
  174. || ThrowTemplateError($template->error());
  175. exit;
  176. }
  177. #
  178. # No valid action found
  179. #
  180. ThrowUserError('no_valid_action', {'field' => "version"});