editmilestones.cgi 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 Initial Developer of the Original Code is Matt Masson.
  15. # Portions created by Matt Masson are Copyright (C) 2000 Matt Masson.
  16. # All Rights Reserved.
  17. #
  18. # Contributors : Matt Masson <matthew@zeroknowledge.com>
  19. # Gavin Shelley <bugzilla@chimpychompy.org>
  20. # Frédéric Buclin <LpSolit@gmail.com>
  21. use strict;
  22. use lib qw(. lib);
  23. use Bugzilla;
  24. use Bugzilla::Constants;
  25. use Bugzilla::Util;
  26. use Bugzilla::Error;
  27. use Bugzilla::Milestone;
  28. use Bugzilla::Token;
  29. my $cgi = Bugzilla->cgi;
  30. my $dbh = Bugzilla->dbh;
  31. my $template = Bugzilla->template;
  32. my $vars = {};
  33. # There is only one section about milestones in the documentation,
  34. # so all actions point to the same page.
  35. $vars->{'doc_section'} = 'milestones.html';
  36. #
  37. # Preliminary checks:
  38. #
  39. my $user = Bugzilla->login(LOGIN_REQUIRED);
  40. print $cgi->header();
  41. $user->in_group('editcomponents')
  42. || scalar(@{$user->get_products_by_permission('editcomponents')})
  43. || ThrowUserError("auth_failure", {group => "editcomponents",
  44. action => "edit",
  45. object => "milestones"});
  46. #
  47. # often used variables
  48. #
  49. my $product_name = trim($cgi->param('product') || '');
  50. my $milestone_name = trim($cgi->param('milestone') || '');
  51. my $sortkey = trim($cgi->param('sortkey') || 0);
  52. my $action = trim($cgi->param('action') || '');
  53. my $showbugcounts = (defined $cgi->param('showbugcounts'));
  54. my $token = $cgi->param('token');
  55. #
  56. # product = '' -> Show nice list of products
  57. #
  58. unless ($product_name) {
  59. my $selectable_products = $user->get_selectable_products;
  60. # If the user has editcomponents privs for some products only,
  61. # we have to restrict the list of products to display.
  62. unless ($user->in_group('editcomponents')) {
  63. $selectable_products = $user->get_products_by_permission('editcomponents');
  64. }
  65. $vars->{'products'} = $selectable_products;
  66. $vars->{'showbugcounts'} = $showbugcounts;
  67. $template->process("admin/milestones/select-product.html.tmpl", $vars)
  68. || ThrowTemplateError($template->error());
  69. exit;
  70. }
  71. my $product = $user->check_can_admin_product($product_name);
  72. #
  73. # action='' -> Show nice list of milestones
  74. #
  75. unless ($action) {
  76. $vars->{'showbugcounts'} = $showbugcounts;
  77. $vars->{'product'} = $product;
  78. $template->process("admin/milestones/list.html.tmpl", $vars)
  79. || ThrowTemplateError($template->error());
  80. exit;
  81. }
  82. #
  83. # action='add' -> present form for parameters for new milestone
  84. #
  85. # (next action will be 'new')
  86. #
  87. if ($action eq 'add') {
  88. $vars->{'token'} = issue_session_token('add_milestone');
  89. $vars->{'product'} = $product;
  90. $template->process("admin/milestones/create.html.tmpl", $vars)
  91. || ThrowTemplateError($template->error());
  92. exit;
  93. }
  94. #
  95. # action='new' -> add milestone entered in the 'action=add' screen
  96. #
  97. if ($action eq 'new') {
  98. check_token_data($token, 'add_milestone');
  99. my $milestone = Bugzilla::Milestone->create({ name => $milestone_name,
  100. product => $product,
  101. sortkey => $sortkey });
  102. delete_token($token);
  103. $vars->{'message'} = 'milestone_created';
  104. $vars->{'milestone'} = $milestone;
  105. $vars->{'product'} = $product;
  106. $template->process("admin/milestones/list.html.tmpl", $vars)
  107. || ThrowTemplateError($template->error());
  108. exit;
  109. }
  110. #
  111. # action='del' -> ask if user really wants to delete
  112. #
  113. # (next action would be 'delete')
  114. #
  115. if ($action eq 'del') {
  116. my $milestone = Bugzilla::Milestone->check({ product => $product,
  117. name => $milestone_name });
  118. $vars->{'milestone'} = $milestone;
  119. $vars->{'product'} = $product;
  120. # The default milestone cannot be deleted.
  121. if ($product->default_milestone eq $milestone->name) {
  122. ThrowUserError("milestone_is_default", { milestone => $milestone });
  123. }
  124. $vars->{'token'} = issue_session_token('delete_milestone');
  125. $template->process("admin/milestones/confirm-delete.html.tmpl", $vars)
  126. || ThrowTemplateError($template->error());
  127. exit;
  128. }
  129. #
  130. # action='delete' -> really delete the milestone
  131. #
  132. if ($action eq 'delete') {
  133. check_token_data($token, 'delete_milestone');
  134. my $milestone = Bugzilla::Milestone->check({ product => $product,
  135. name => $milestone_name });
  136. $milestone->remove_from_db;
  137. delete_token($token);
  138. $vars->{'message'} = 'milestone_deleted';
  139. $vars->{'milestone'} = $milestone;
  140. $vars->{'product'} = $product;
  141. $vars->{'no_edit_milestone_link'} = 1;
  142. $template->process("admin/milestones/list.html.tmpl", $vars)
  143. || ThrowTemplateError($template->error());
  144. exit;
  145. }
  146. #
  147. # action='edit' -> present the edit milestone form
  148. #
  149. # (next action would be 'update')
  150. #
  151. if ($action eq 'edit') {
  152. my $milestone = Bugzilla::Milestone->check({ product => $product,
  153. name => $milestone_name });
  154. $vars->{'milestone'} = $milestone;
  155. $vars->{'product'} = $product;
  156. $vars->{'token'} = issue_session_token('edit_milestone');
  157. $template->process("admin/milestones/edit.html.tmpl", $vars)
  158. || ThrowTemplateError($template->error());
  159. exit;
  160. }
  161. #
  162. # action='update' -> update the milestone
  163. #
  164. if ($action eq 'update') {
  165. check_token_data($token, 'edit_milestone');
  166. my $milestone_old_name = trim($cgi->param('milestoneold') || '');
  167. my $milestone = Bugzilla::Milestone->check({ product => $product,
  168. name => $milestone_old_name });
  169. $milestone->set_name($milestone_name);
  170. $milestone->set_sortkey($sortkey);
  171. my $changes = $milestone->update();
  172. delete_token($token);
  173. $vars->{'message'} = 'milestone_updated';
  174. $vars->{'milestone'} = $milestone;
  175. $vars->{'product'} = $product;
  176. $vars->{'changes'} = $changes;
  177. $template->process("admin/milestones/list.html.tmpl", $vars)
  178. || ThrowTemplateError($template->error());
  179. exit;
  180. }
  181. #
  182. # No valid action found
  183. #
  184. ThrowUserError('no_valid_action', {'field' => "target_milestone"});