query.cgi 12.6 KB
Newer Older
1
#!/usr/bin/perl -wT
2
# -*- Mode: perl; indent-tabs-mode: nil -*-
terry%netscape.com's avatar
terry%netscape.com committed
3
#
4 5 6 7 8 9 10 11 12 13
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
terry%netscape.com's avatar
terry%netscape.com committed
14
# The Original Code is the Bugzilla Bug Tracking System.
15
#
terry%netscape.com's avatar
terry%netscape.com committed
16
# The Initial Developer of the Original Code is Netscape Communications
17 18 19 20
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
terry%netscape.com's avatar
terry%netscape.com committed
21
# Contributor(s): Terry Weissman <terry@mozilla.org>
22
#                 David Gardiner <david.gardiner@unisa.edu.au>
23
#                 Matthias Radestock <matthias@sorted.org>
24
#                 Gervase Markham <gerv@gerv.net>
25
#                 Byron Jones <bugzilla@glob.com.au>
26
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
terry%netscape.com's avatar
terry%netscape.com committed
27

28
use strict;
29
use lib qw(. lib);
terry%netscape.com's avatar
terry%netscape.com committed
30

31
use Bugzilla;
32
use Bugzilla::Bug;
33
use Bugzilla::Constants;
34
use Bugzilla::Search;
35
use Bugzilla::User;
36
use Bugzilla::Util;
37
use Bugzilla::Error;
38
use Bugzilla::Product;
39
use Bugzilla::Keyword;
40
use Bugzilla::Field;
41
use Bugzilla::Install::Util qw(vers_cmp);
42

43
my $cgi = Bugzilla->cgi;
44
my $dbh = Bugzilla->dbh;
45 46
my $template = Bugzilla->template;
my $vars = {};
47
my $buffer = $cgi->query_string();
48

49
my $user = Bugzilla->login();
50
my $userid = $user->id;
51

52
if ($cgi->param('nukedefaultquery')) {
53
    if ($userid) {
54 55 56
        $dbh->do("DELETE FROM namedqueries" .
                 " WHERE userid = ? AND name = ?", 
                 undef, ($userid, DEFAULT_QUERY_NAME));
57
    }
58
    $buffer = "";
terry%netscape.com's avatar
terry%netscape.com committed
59 60
}

61 62 63
# We are done with changes committed to the DB.
$dbh = Bugzilla->switch_to_shadow_db;

64
my $userdefaultquery;
65
if ($userid) {
66 67 68 69
    $userdefaultquery = $dbh->selectrow_array(
        "SELECT query FROM namedqueries " .
         "WHERE userid = ? AND name = ?", 
         undef, ($userid, DEFAULT_QUERY_NAME));
terry%netscape.com's avatar
terry%netscape.com committed
70 71
}

72
local our %default;
73

74 75 76 77
# We pass the defaults as a hash of references to arrays. For those
# Items which are single-valued, the template should only reference [0]
# and ignore any multiple values.
sub PrefillForm {
78
    my ($buf) = (@_);
79
    my $cgi = Bugzilla->cgi;
80
    $buf = new Bugzilla::CGI($buf);
81
    my $foundone = 0;
82 83

    # Nothing must be undef, otherwise the template complains.
84
    my @list = ("bug_status", "resolution", "assigned_to",
85
                      "rep_platform", "priority", "bug_severity",
86
                      "classification", "product", "reporter", "op_sys",
87
                      "component", "version", "chfield", "chfieldfrom",
88
                      "chfieldto", "chfieldvalue", "target_milestone",
89 90
                      "email", "emailtype", "emailreporter",
                      "emailassigned_to", "emailcc", "emailqa_contact",
91
                      "emaillongdesc", "content",
92
                      "changedin", "short_desc", "short_desc_type",
93
                      "longdesc", "longdesc_type", "bug_file_loc",
94
                      "bug_file_loc_type", "status_whiteboard",
95
                      "status_whiteboard_type", "bug_id",
96
                      "bug_id_type", "keywords", "keywords_type",
97
                      "deadlinefrom", "deadlineto",
98
                      "x_axis_field", "y_axis_field", "z_axis_field",
99 100
                      "chart_format", "cumulate", "x_labels_vertical",
                      "category", "subcategory", "name", "newcategory",
101
                      "newsubcategory", "public", "frequency");
102 103 104 105 106 107
    # These fields can also have default values. And because there are
    # hooks in the advanced search page which let you add fields as
    # discrete forms, we also need to retain the operators.
    my @custom_fields = Bugzilla->active_custom_fields;
    push(@list, map { $_->name } @custom_fields);
    push(@list, map { $_->name . '_type'} @custom_fields);
108 109

    foreach my $name (@list) {
110
        $default{$name} = [];
111
    }
112
 
113 114 115
    # we won't prefill the boolean chart data from this query if
    # there are any being submitted via params
    my $prefillcharts = (grep(/^field-/, $cgi->param)) ? 0 : 1;
116 117
 
    # Iterate over the URL parameters
118 119 120
    foreach my $name ($buf->param()) {
        my @values = $buf->param($name);

121 122 123 124 125 126 127 128
        # If the name begins with the string 'field', 'type', 'value', or
        # 'negate', then it is part of the boolean charts. Because
        # these are built different than the rest of the form, we need
        # to store these as parameters. We also need to indicate that
        # we found something so the default query isn't added in if
        # all we have are boolean chart items.
        if ($name =~ m/^(?:field|type|value|negate)/) {
            $cgi->param(-name => $name, -value => $values[0]) if ($prefillcharts);
129 130
            $foundone = 1;
        }
131 132 133
        # If the name ends in a number (which it does for the fields which
        # are part of the email searching), we use the array
        # positions to show the defaults for that number field.
134
        elsif ($name =~ m/^(.+)(\d)$/ && defined($default{$1})) {
135
            $foundone = 1;
136
            $default{$1}->[$2] = $values[0];
terry%netscape.com's avatar
terry%netscape.com committed
137
        }
138
        elsif (exists $default{$name}) {
139
            $foundone = 1;
140 141 142
            push (@{$default{$name}}, @values);
        }
    }
143
    return $foundone;
terry%netscape.com's avatar
terry%netscape.com committed
144
}
145

146
if (!PrefillForm($buffer)) {
147 148 149
    # Ah-hah, there was no form stuff specified.  Do it again with the
    # default query.
    if ($userdefaultquery) {
150
        PrefillForm($userdefaultquery);
151
    } else {
152
        PrefillForm(Bugzilla->params->{"defaultquery"});
153 154
    }
}
155

156
if (!scalar(@{$default{'chfieldto'}}) || $default{'chfieldto'}->[0] eq "") {
157
    $default{'chfieldto'} = ["Now"];
terry%netscape.com's avatar
terry%netscape.com committed
158 159
}

160 161
# if using groups for entry, then we don't want people to see products they 
# don't have access to. Remove them from the list.
162 163
my @selectable_products = sort {lc($a->name) cmp lc($b->name)} 
                               @{$user->get_selectable_products};
164
Bugzilla::Product::preload(\@selectable_products);
165

166
# Create the component, version and milestone lists.
167 168 169 170 171 172 173 174
my %components;
my %versions;
my %milestones;

foreach my $product (@selectable_products) {
    $components{$_->name} = 1 foreach (@{$product->components});
    $versions{$_->name}   = 1 foreach (@{$product->versions});
    $milestones{$_->name} = 1 foreach (@{$product->milestones});
175 176
}

177
my @components = sort(keys %components);
178
my @versions = sort { vers_cmp (lc($a), lc($b)) } keys %versions;
179 180
my @milestones = sort(keys %milestones);

181
$vars->{'product'} = \@selectable_products;
182

183
# Create data structures representing each classification
184
if (Bugzilla->params->{'useclassification'}) {
185
    $vars->{'classification'} = $user->get_selectable_classifications;
186 187
}

188 189
# We use 'component_' because 'component' is a Template Toolkit reserved word.
$vars->{'component_'} = \@components;
terry%netscape.com's avatar
terry%netscape.com committed
190

191
$vars->{'version'} = \@versions;
192

193
if (Bugzilla->params->{'usetargetmilestone'}) {
194
    $vars->{'target_milestone'} = \@milestones;
195 196
}

197 198
my @chfields;

199
push @chfields, "[Bug creation]";
200 201 202

# This is what happens when you have variables whose definition depends
# on the DB schema, and then the underlying schema changes...
203
foreach my $val (editable_bug_fields()) {
204 205 206
    if ($val eq 'classification_id') {
        $val = 'classification';
    } elsif ($val eq 'product_id') {
207 208 209 210 211 212 213
        $val = 'product';
    } elsif ($val eq 'component_id') {
        $val = 'component';
    }
    push @chfields, $val;
}

214
if (Bugzilla->user->is_timetracker) {
215 216 217 218 219 220 221
    push @chfields, "work_time";
} else {
    @chfields = grep($_ ne "estimated_time", @chfields);
    @chfields = grep($_ ne "remaining_time", @chfields);
}
@chfields = (sort(@chfields));
$vars->{'chfield'} = \@chfields;
222 223 224 225 226 227
$vars->{'bug_status'} = Bugzilla::Field->new({name => 'bug_status'})->legal_values;
$vars->{'rep_platform'} = Bugzilla::Field->new({name => 'rep_platform'})->legal_values;
$vars->{'op_sys'} = Bugzilla::Field->new({name => 'op_sys'})->legal_values;
$vars->{'priority'} = Bugzilla::Field->new({name => 'priority'})->legal_values;
$vars->{'bug_severity'} = Bugzilla::Field->new({name => 'bug_severity'})->legal_values;
$vars->{'resolution'} = Bugzilla::Field->new({name => 'resolution'})->legal_values;
228 229

# Boolean charts
230
my @fields = @{ Bugzilla->fields({ obsolete => 0 }) };
231 232

# If we're not in the time-tracking group, exclude time-tracking fields.
233 234
if (!Bugzilla->user->is_timetracker) {
    foreach my $tt_field (TIMETRACKING_FIELDS) {
235 236 237 238 239 240
        @fields = grep($_->name ne $tt_field, @fields);
    }
}

@fields = sort {lc($a->description) cmp lc($b->description)} @fields;
unshift(@fields, { name => "noop", description => "---" });
241
$vars->{'fields'} = \@fields;
242

243 244 245
# Creating new charts - if the cmd-add value is there, we define the field
# value so the code sees it and creates the chart. It will attempt to select
# "xyzzy" as the default, and fail. This is the correct behaviour.
246
foreach my $cmd (grep(/^cmd-/, $cgi->param)) {
247
    if ($cmd =~ /^cmd-add(\d+)-(\d+)-(\d+)$/) {
248
        $cgi->param(-name => "field$1-$2-$3", -value => "xyzzy");
249 250
    }
}
251

252 253
if (!$cgi->param('field0-0-0')) {
    $cgi->param(-name => 'field0-0-0', -value => "xyzzy");
254 255
}

256 257 258 259
# Create data structure of boolean chart info. It's an array of arrays of
# arrays - with the inner arrays having three members - field, type and
# value.
my @charts;
260
for (my $chart = 0; $cgi->param("field$chart-0-0"); $chart++) {
261
    my @rows;
262
    for (my $row = 0; $cgi->param("field$chart-$row-0"); $row++) {
263
        my @cols;
264
        for (my $col = 0; $cgi->param("field$chart-$row-$col"); $col++) {
265 266 267 268
            my $value = $cgi->param("value$chart-$row-$col");
            if (!defined($value)) {
                $value = '';
            }
269
            push(@cols, { field => $cgi->param("field$chart-$row-$col"),
270
                          type => $cgi->param("type$chart-$row-$col") || 'noop',
271
                          value => $value });
272
        }
273
        push(@rows, \@cols);
274
    }
275
    push(@charts, {'rows' => \@rows, 'negate' => scalar($cgi->param("negate$chart")) });
276 277
}

278
$default{'charts'} = \@charts;
279

280
# Named queries
281
if ($userid) {
282 283
     $vars->{'namedqueries'} = $dbh->selectcol_arrayref(
           "SELECT name FROM namedqueries " .
284
            "WHERE userid = ? AND name != ? " .
285 286
         "ORDER BY name",
         undef, ($userid, DEFAULT_QUERY_NAME));
287
}
terry%netscape.com's avatar
terry%netscape.com committed
288

289 290 291
# Sort order
my $deforder;
my @orders = ('Bug Number', 'Importance', 'Assignee', 'Last Changed');
292

293
if ($cgi->cookie('LASTORDER')) {
294 295 296
    $deforder = "Reuse same sort as last time";
    unshift(@orders, $deforder);
}
297

298
if ($cgi->param('order')) { $deforder = $cgi->param('order') }
299

300 301
$vars->{'userdefaultquery'} = $userdefaultquery;
$vars->{'orders'} = \@orders;
302
$default{'order'} = [$deforder || 'Importance'];
terry%netscape.com's avatar
terry%netscape.com committed
303

304 305
if (($cgi->param('query_format') || $cgi->param('format') || "")
    eq "create-series") {
306 307 308 309
    require Bugzilla::Chart;
    $vars->{'category'} = Bugzilla::Chart::getVisibleSeries();
}

310 311 312 313 314 315 316
if ($cgi->param('format') && $cgi->param('format') =~ /^report-(table|graph)$/) {
    # Get legal custom fields for tabular and graphical reports.
    my @custom_fields_for_reports =
      grep { $_->type == FIELD_TYPE_SINGLE_SELECT } Bugzilla->active_custom_fields;
    $vars->{'custom_fields'} = \@custom_fields_for_reports;
}

317
$vars->{'known_name'} = $cgi->param('known_name');
318
$vars->{'columnlist'} = $cgi->param('columnlist');
319 320


321 322
# Add in the defaults.
$vars->{'default'} = \%default;
323

324 325 326
$vars->{'format'} = $cgi->param('format');
$vars->{'query_format'} = $cgi->param('query_format');

327
# Set default page to "specific" if none provided
328 329 330 331 332 333 334 335
if (!($cgi->param('query_format') || $cgi->param('format'))) {
    if (defined $cgi->cookie('DEFAULTFORMAT')) {
        $vars->{'format'} = $cgi->cookie('DEFAULTFORMAT');
    } else {
        $vars->{'format'} = 'specific';
    }
}

336 337
# Set cookie to current format as default, but only if the format
# one that we should remember.
338
if (defined($vars->{'format'}) && IsValidQueryType($vars->{'format'})) {
339 340 341 342
    $cgi->send_cookie(-name => 'DEFAULTFORMAT',
                      -value => $vars->{'format'},
                      -expires => "Fri, 01-Jan-2038 00:00:00 GMT");
}
343

344
# Generate and return the UI (HTML page) from the appropriate template.
345 346 347
# If we submit back to ourselves (for e.g. boolean charts), we need to
# preserve format information; hence query_format taking priority over
# format.
348 349 350
my $format = $template->get_format("search/search", 
                                   $vars->{'query_format'} || $vars->{'format'}, 
                                   scalar $cgi->param('ctype'));
351 352 353

print $cgi->header($format->{'ctype'});

354
$template->process($format->{'template'}, $vars)
355
  || ThrowTemplateError($template->error());