Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / editworkflow.cgi
blob3c33c72ade8ff613be97ed5f8bde788541ae9b47
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 the Bugzilla Bug Tracking System.
16 # The Initial Developer of the Original Code is Frédéric Buclin.
17 # Portions created by Frédéric Buclin are Copyright (C) 2007
18 # Frédéric Buclin. All Rights Reserved.
20 # Contributor(s): Frédéric Buclin <LpSolit@gmail.com>
22 use strict;
24 use lib qw(. lib);
26 use Bugzilla;
27 use Bugzilla::Constants;
28 use Bugzilla::Error;
29 use Bugzilla::Token;
30 use Bugzilla::Status;
32 my $cgi = Bugzilla->cgi;
33 my $dbh = Bugzilla->dbh;
34 my $user = Bugzilla->login(LOGIN_REQUIRED);
36 print $cgi->header();
38 $user->in_group('admin')
39 || ThrowUserError('auth_failure', {group => 'admin',
40 action => 'modify',
41 object => 'workflow'});
43 my $action = $cgi->param('action') || 'edit';
44 my $token = $cgi->param('token');
46 sub get_workflow {
47 my $dbh = Bugzilla->dbh;
48 my $workflow = $dbh->selectall_arrayref('SELECT old_status, new_status, require_comment
49 FROM status_workflow');
50 my %workflow;
51 foreach my $row (@$workflow) {
52 my ($old, $new, $type) = @$row;
53 $workflow{$old || 0}{$new} = $type;
55 return \%workflow;
58 sub load_template {
59 my ($filename, $message) = @_;
60 my $template = Bugzilla->template;
61 my $vars = {};
63 $vars->{'statuses'} = [Bugzilla::Status->get_all];
64 $vars->{'workflow'} = get_workflow();
65 $vars->{'token'} = issue_session_token("workflow_$filename");
66 $vars->{'message'} = $message;
68 $template->process("admin/workflow/$filename.html.tmpl", $vars)
69 || ThrowTemplateError($template->error());
70 exit;
73 if ($action eq 'edit') {
74 load_template('edit');
76 elsif ($action eq 'update') {
77 check_token_data($token, 'workflow_edit');
78 my $statuses = [Bugzilla::Status->get_all];
79 my $workflow = get_workflow();
81 my $sth_insert = $dbh->prepare('INSERT INTO status_workflow (old_status, new_status)
82 VALUES (?, ?)');
83 my $sth_delete = $dbh->prepare('DELETE FROM status_workflow
84 WHERE old_status = ? AND new_status = ?');
85 my $sth_delnul = $dbh->prepare('DELETE FROM status_workflow
86 WHERE old_status IS NULL AND new_status = ?');
88 # Part 1: Initial bug statuses.
89 foreach my $new (@$statuses) {
90 if ($new->is_open && $cgi->param('w_0_' . $new->id)) {
91 $sth_insert->execute(undef, $new->id)
92 unless defined $workflow->{0}->{$new->id};
94 else {
95 $sth_delnul->execute($new->id);
99 # Part 2: Bug status changes.
100 foreach my $old (@$statuses) {
101 foreach my $new (@$statuses) {
102 next if $old->id == $new->id;
104 # All transitions to 'duplicate_or_move_bug_status' must be valid.
105 if ($cgi->param('w_' . $old->id . '_' . $new->id)
106 || ($new->name eq Bugzilla->params->{'duplicate_or_move_bug_status'}))
108 $sth_insert->execute($old->id, $new->id)
109 unless defined $workflow->{$old->id}->{$new->id};
111 else {
112 $sth_delete->execute($old->id, $new->id);
116 delete_token($token);
117 load_template('edit', 'workflow_updated');
119 elsif ($action eq 'edit_comment') {
120 load_template('comment');
122 elsif ($action eq 'update_comment') {
123 check_token_data($token, 'workflow_comment');
124 my $workflow = get_workflow();
126 my $sth_update = $dbh->prepare('UPDATE status_workflow SET require_comment = ?
127 WHERE old_status = ? AND new_status = ?');
128 my $sth_updnul = $dbh->prepare('UPDATE status_workflow SET require_comment = ?
129 WHERE old_status IS NULL AND new_status = ?');
131 foreach my $old (keys %$workflow) {
132 # Hashes cannot have undef as a key, so we use 0. But the DB
133 # must store undef, for referential integrity.
134 my $old_id_for_db = $old || undef;
135 foreach my $new (keys %{$workflow->{$old}}) {
136 my $comment_required = $cgi->param("c_${old}_$new") ? 1 : 0;
137 next if ($workflow->{$old}->{$new} == $comment_required);
138 if ($old_id_for_db) {
139 $sth_update->execute($comment_required, $old_id_for_db, $new);
141 else {
142 $sth_updnul->execute($comment_required, $new);
146 delete_token($token);
147 load_template('comment', 'workflow_updated');
149 else {
150 ThrowCodeError("action_unrecognized", {action => $action});