editkeywords.cgi 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 the Bugzilla Bug Tracking System.
  15. #
  16. # The Initial Developer of the Original Code is Terry Weissman.
  17. # Portions created by Terry Weissman are
  18. # Copyright (C) 2000 Terry Weissman. All
  19. # Rights Reserved.
  20. #
  21. # Contributor(s): Terry Weissman <terry@mozilla.org>
  22. use strict;
  23. use lib qw(. lib);
  24. use Bugzilla;
  25. use Bugzilla::Constants;
  26. use Bugzilla::Util;
  27. use Bugzilla::Error;
  28. use Bugzilla::Keyword;
  29. use Bugzilla::Token;
  30. my $cgi = Bugzilla->cgi;
  31. my $dbh = Bugzilla->dbh;
  32. my $template = Bugzilla->template;
  33. my $vars = {};
  34. #
  35. # Preliminary checks:
  36. #
  37. my $user = Bugzilla->login(LOGIN_REQUIRED);
  38. print $cgi->header();
  39. $user->in_group('editkeywords')
  40. || ThrowUserError("auth_failure", {group => "editkeywords",
  41. action => "edit",
  42. object => "keywords"});
  43. my $action = trim($cgi->param('action') || '');
  44. my $key_id = $cgi->param('id');
  45. my $token = $cgi->param('token');
  46. $vars->{'action'} = $action;
  47. if ($action eq "") {
  48. $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
  49. print $cgi->header();
  50. $template->process("admin/keywords/list.html.tmpl", $vars)
  51. || ThrowTemplateError($template->error());
  52. exit;
  53. }
  54. if ($action eq 'add') {
  55. $vars->{'token'} = issue_session_token('add_keyword');
  56. print $cgi->header();
  57. $template->process("admin/keywords/create.html.tmpl", $vars)
  58. || ThrowTemplateError($template->error());
  59. exit;
  60. }
  61. #
  62. # action='new' -> add keyword entered in the 'action=add' screen
  63. #
  64. if ($action eq 'new') {
  65. check_token_data($token, 'add_keyword');
  66. my $name = $cgi->param('name') || '';
  67. my $desc = $cgi->param('description') || '';
  68. my $keyword = Bugzilla::Keyword->create(
  69. { name => $name, description => $desc });
  70. delete_token($token);
  71. print $cgi->header();
  72. $vars->{'message'} = 'keyword_created';
  73. $vars->{'name'} = $keyword->name;
  74. $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
  75. $template->process("admin/keywords/list.html.tmpl", $vars)
  76. || ThrowTemplateError($template->error());
  77. exit;
  78. }
  79. #
  80. # action='edit' -> present the edit keywords from
  81. #
  82. # (next action would be 'update')
  83. #
  84. if ($action eq 'edit') {
  85. my $keyword = new Bugzilla::Keyword($key_id)
  86. || ThrowCodeError('invalid_keyword_id', { id => $key_id });
  87. $vars->{'keyword'} = $keyword;
  88. $vars->{'token'} = issue_session_token('edit_keyword');
  89. print $cgi->header();
  90. $template->process("admin/keywords/edit.html.tmpl", $vars)
  91. || ThrowTemplateError($template->error());
  92. exit;
  93. }
  94. #
  95. # action='update' -> update the keyword
  96. #
  97. if ($action eq 'update') {
  98. check_token_data($token, 'edit_keyword');
  99. my $keyword = new Bugzilla::Keyword($key_id)
  100. || ThrowCodeError('invalid_keyword_id', { id => $key_id });
  101. $keyword->set_name($cgi->param('name'));
  102. $keyword->set_description($cgi->param('description'));
  103. my $changes = $keyword->update();
  104. delete_token($token);
  105. print $cgi->header();
  106. $vars->{'message'} = 'keyword_updated';
  107. $vars->{'keyword'} = $keyword;
  108. $vars->{'changes'} = $changes;
  109. $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
  110. $template->process("admin/keywords/list.html.tmpl", $vars)
  111. || ThrowTemplateError($template->error());
  112. exit;
  113. }
  114. if ($action eq 'del') {
  115. my $keyword = new Bugzilla::Keyword($key_id)
  116. || ThrowCodeError('invalid_keyword_id', { id => $key_id });
  117. $vars->{'keyword'} = $keyword;
  118. $vars->{'token'} = issue_session_token('delete_keyword');
  119. print $cgi->header();
  120. $template->process("admin/keywords/confirm-delete.html.tmpl", $vars)
  121. || ThrowTemplateError($template->error());
  122. exit;
  123. }
  124. if ($action eq 'delete') {
  125. check_token_data($token, 'delete_keyword');
  126. my $keyword = new Bugzilla::Keyword($key_id)
  127. || ThrowCodeError('invalid_keyword_id', { id => $key_id });
  128. $dbh->do('DELETE FROM keywords WHERE keywordid = ?', undef, $keyword->id);
  129. $dbh->do('DELETE FROM keyworddefs WHERE id = ?', undef, $keyword->id);
  130. delete_token($token);
  131. print $cgi->header();
  132. $vars->{'message'} = 'keyword_deleted';
  133. $vars->{'keywords'} = Bugzilla::Keyword->get_all_with_bug_count();
  134. $template->process("admin/keywords/list.html.tmpl", $vars)
  135. || ThrowTemplateError($template->error());
  136. exit;
  137. }
  138. ThrowCodeError("action_unrecognized", $vars);