RSS

(root)/bugzilla/trunk : /post_bug.cgi (revision 8182)

To get this branch, use:
bzr branch /bugzilla/trunk
Line Revision Contents
1 2107
#!/usr/bin/perl -wT
2 8075
# This Source Code Form is subject to the terms of the Mozilla Public
3
# License, v. 2.0. If a copy of the MPL was not distributed with this
4
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
#
6
# This Source Code Form is "Incompatible With Secondary Licenses", as
7
# defined by the Mozilla Public License, v. 2.0.
8 1
9 47
use strict;
10 5304
use lib qw(. lib);
11 1243
12 2161
use Bugzilla;
13 4232
use Bugzilla::Attachment;
14 4620
use Bugzilla::BugMail;
15 1957
use Bugzilla::Constants;
16 3599
use Bugzilla::Util;
17 4296
use Bugzilla::Error;
18 2493
use Bugzilla::Bug;
19 1830
use Bugzilla::User;
20 3537
use Bugzilla::Field;
21 5464
use Bugzilla::Hook;
22 4048
use Bugzilla::Product;
23 4341
use Bugzilla::Component;
24 4078
use Bugzilla::Keyword;
25 4199
use Bugzilla::Token;
26 4496
use Bugzilla::Flag;
27 1830
28 2537
my $user = Bugzilla->login(LOGIN_REQUIRED);
29 3762
30 2161
my $cgi = Bugzilla->cgi;
31 3112
my $dbh = Bugzilla->dbh;
32 3762
my $template = Bugzilla->template;
33
my $vars = {};
34 3112
35 4036
######################################################################
36
# Main Script
37
######################################################################
38
39 5327
# redirect to enter_bug if no field is passed.
40 7855
unless ($cgi->param()) {
41
    print $cgi->redirect(correct_urlbase() . 'enter_bug.cgi');
42
    exit;
43
}
44 5327
45 4199
# Detect if the user already used the same form to submit a bug
46
my $token = trim($cgi->param('token'));
47 8011
check_token_data($token, 'create_bug', 'index.cgi');
48 4199
49 1830
# do a match on the fields if applicable
50 6943
Bugzilla::User::match_field ({
51 1830
    'cc'            => { 'type' => 'multi'  },
52
    'assigned_to'   => { 'type' => 'single' },
53 3154
    'qa_contact'    => { 'type' => 'single' },
54 1830
});
55 1397
56 3314
if (defined $cgi->param('maketemplate')) {
57 4943
    $vars->{'url'} = $cgi->canonicalise_query('token');
58 3868
    $vars->{'short_desc'} = $cgi->param('short_desc');
59 1
    
60 2161
    print $cgi->header();
61 1424
    $template->process("bug/create/make-template.html.tmpl", $vars)
62
      || ThrowTemplateError($template->error());
63 47
    exit;
64 1
}
65
66 47
umask 0;
67
68 4561
# The format of the initial comment can be structured by adding fields to the
69
# enter_bug template and then referencing them in the comment template.
70
my $comment;
71
my $format = $template->get_format("bug/create/comment",
72
                                   scalar($cgi->param('format')), "txt");
73
$template->process($format->{'template'}, $vars, \$comment)
74
    || ThrowTemplateError($template->error());
75
76 4541
# Include custom fields editable on bug creation.
77 5493
my @custom_bug_fields = grep {$_->type != FIELD_TYPE_MULTI_SELECT && $_->enter_bug}
78
                             Bugzilla->active_custom_fields;
79 4541
80 4638
# Undefined custom fields are ignored to ensure they will get their default
81
# value (e.g. "---" for custom single select fields).
82
my @bug_fields = grep { defined $cgi->param($_->name) } @custom_bug_fields;
83
@bug_fields = map { $_->name } @bug_fields;
84 4541
85 4522
push(@bug_fields, qw(
86
    product
87
    component
88
89
    assigned_to
90
    qa_contact
91
92
    alias
93 4552
    blocked
94 7555
    comment_is_private
95 4522
    bug_file_loc
96
    bug_severity
97
    bug_status
98 4552
    dependson
99 4546
    keywords
100 4522
    short_desc
101
    op_sys
102
    priority
103
    rep_platform
104
    version
105
    target_milestone
106
    status_whiteboard
107
108
    estimated_time
109
    deadline
110
));
111
my %bug_params;
112
foreach my $field (@bug_fields) {
113
    $bug_params{$field} = $cgi->param($field);
114
}
115 7918
foreach my $field (qw(cc groups)) {
116
    next if !$cgi->should_set($field);
117
    $bug_params{$field} = [$cgi->param($field)];
118
}
119
$bug_params{'comment'} = $comment;
120 3487
121 5493
my @multi_selects = grep {$_->type == FIELD_TYPE_MULTI_SELECT && $_->enter_bug}
122
                         Bugzilla->active_custom_fields;
123
124 5239
foreach my $field (@multi_selects) {
125 7918
    next if !$cgi->should_set($field->name);
126 5239
    $bug_params{$field->name} = [$cgi->param($field->name)];
127
}
128
129 4522
my $bug = Bugzilla::Bug->create(\%bug_params);
130 1376
131 8011
# Get the bug ID back and delete the token used to create this bug.
132 4522
my $id = $bug->bug_id;
133 8011
delete_token($token);
134
135 6820
# We do this directly from the DB because $bug->creation_ts has the seconds
136
# formatted out of it (which should be fixed some day).
137
my $timestamp = $dbh->selectrow_array(
138
    'SELECT creation_ts FROM bugs WHERE bug_id = ?', undef, $id);
139 1376
140 4561
# Set Version cookie, but only if the user actually selected
141
# a version on the page.
142
if (defined $cgi->param('version')) {
143
    $cgi->send_cookie(-name => "VERSION-" . $bug->product,
144
                      -value => $bug->version,
145
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
146 2966
}
147
148 4448
# We don't have to check if the user can see the bug, because a user filing
149
# a bug can always see it. You can't change reporter_accessible until
150
# after the bug is filed.
151 4379
152 4232
# Add an attachment if requested.
153 7383
if (defined($cgi->upload('data')) || $cgi->param('attach_text')) {
154 7555
    $cgi->param('isprivate', $cgi->param('comment_is_private'));
155 6553
156
    # Must be called before create() as it may alter $cgi->param('ispatch').
157
    my $content_type = Bugzilla::Attachment::get_content_type();
158
    my $attachment;
159
160
    # If the attachment cannot be successfully added to the bug,
161
    # we notify the user, but we don't interrupt the bug creation process.
162
    my $error_mode_cache = Bugzilla->error_mode;
163
    Bugzilla->error_mode(ERROR_MODE_DIE);
164
    eval {
165
        $attachment = Bugzilla::Attachment->create(
166
            {bug           => $bug,
167
             creation_ts   => $timestamp,
168 7383
             data          => scalar $cgi->param('attach_text') || $cgi->upload('data'),
169 6553
             description   => scalar $cgi->param('description'),
170 7422
             filename      => $cgi->param('attach_text') ? "file_$id.txt" : scalar $cgi->upload('data'),
171 6553
             ispatch       => scalar $cgi->param('ispatch'),
172
             isprivate     => scalar $cgi->param('isprivate'),
173
             mimetype      => $content_type,
174
            });
175
    };
176
    Bugzilla->error_mode($error_mode_cache);
177 4800
178 5009
    if ($attachment) {
179 6553
        # Set attachment flags.
180 6670
        my ($flags, $new_flags) = Bugzilla::Flag->extract_flags_from_cgi(
181
                                      $bug, $attachment, $vars, SKIP_REQUESTEE_ON_ERROR);
182
        $attachment->set_flags($flags, $new_flags);
183
        $attachment->update($timestamp);
184 6873
        my $comment = $bug->comments->[0];
185 7140
        $comment->set_all({ type => CMT_ATTACHMENT_CREATED, 
186
                            extra_data => $attachment->id });
187 6873
        $comment->update();
188 4800
    }
189
    else {
190
        $vars->{'message'} = 'attachment_creation_failed';
191
    }
192 4232
}
193
194 6553
# Set bug flags.
195 6670
my ($flags, $new_flags) = Bugzilla::Flag->extract_flags_from_cgi($bug, undef, $vars,
196
                                                             SKIP_REQUESTEE_ON_ERROR);
197
$bug->set_flags($flags, $new_flags);
198
$bug->update($timestamp);
199 4379
200 1476
$vars->{'id'} = $id;
201 1965
$vars->{'bug'} = $bug;
202
203 6854
Bugzilla::Hook::process('post_bug_after_creation', { vars => $vars });
204 5464
205 2258
ThrowCodeError("bug_error", { bug => $bug }) if $bug->error;
206 1965
207 7150
my $recipients = { changer => $user };
208 7113
my $bug_sent = Bugzilla::BugMail::Send($id, $recipients);
209
$bug_sent->{type} = 'created';
210
$bug_sent->{id}   = $id;
211
my @all_mail_results = ($bug_sent);
212
foreach my $dep (@{$bug->dependson || []}, @{$bug->blocked || []}) {
213
    my $dep_sent = Bugzilla::BugMail::Send($dep, $recipients);
214
    $dep_sent->{type} = 'dep';
215
    $dep_sent->{id}   = $dep;
216
    push(@all_mail_results, $dep_sent);
217
}
218
$vars->{sentmail} = \@all_mail_results;
219
220 7825
$format = $template->get_format("bug/create/created",
221
                                 scalar($cgi->param('created-format')),
222
                                 "html");
223 6885
print $cgi->header();
224 7825
$template->process($format->{'template'}, $vars)
225 6885
    || ThrowTemplateError($template->error());
226 4620
227
1;

Loggerhead 1.18.1 is a web-based interface for Bazaar branches