summaryrefslogtreecommitdiffstats
path: root/extensions/GuidedBugEntry/Extension.pm
blob: 5665e18aef92a536f41a00800db794674dfd699a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::GuidedBugEntry;
use strict;
use base qw(Bugzilla::Extension);

use Bugzilla::Token;
use Bugzilla::Error;
use Bugzilla::Status;
use Bugzilla::Util 'url_quote';
use Bugzilla::UserAgent;

our $VERSION = '1';

sub enter_bug_start {
    my ($self, $args) = @_;
    my $vars = $args->{vars};
    my $template = Bugzilla->template;
    my $cgi = Bugzilla->cgi;
    my $user = Bugzilla->user;

    # hack for skipping old guided code when enabled
    $vars->{'disable_guided'} = 1;

    # force guided format for new users
    my $format = $cgi->param('format') || '';
    if (
        $format eq 'guided' ||
        (
            $format eq '' &&
            !$user->in_group('canconfirm')
        )
    ) {
        # skip the first step if a product is provided
        if ($cgi->param('product')) {
            print $cgi->redirect('enter_bug.cgi?format=guided#h=dupes' .
                '|' . url_quote($cgi->param('product')) .
                '|' . url_quote($cgi->param('component') || '')
                );
            exit;
        }

        $self->_init_vars($vars);
        print $cgi->header();
        $template->process('guided/guided.html.tmpl', $vars)
          || ThrowTemplateError($template->error());
        exit;
    }

    # we use the __default__ format to bypass the guided entry
    # it isn't understood upstream, so remove it once a product
    # has been selected.
    if (
        ($cgi->param('format') && $cgi->param('format') eq "__default__")
        && ($cgi->param('product') && $cgi->param('product') ne '')
    ) {
        $cgi->delete('format');
    }
}

sub _init_vars {
    my ($self, $vars) = @_;
    my $user = Bugzilla->user;

    my @enterable_products = @{$user->get_enterable_products};
    ThrowUserError('no_products') unless scalar(@enterable_products);

    my @classifications = ({object => undef, products => \@enterable_products});

    my $class;
    foreach my $product (@enterable_products) {
        $class->{$product->classification_id}->{'object'} ||=
            new Bugzilla::Classification($product->classification_id);
        push(@{$class->{$product->classification_id}->{'products'}}, $product);
    }
    @classifications =
        sort {
            $a->{'object'}->sortkey <=> $b->{'object'}->sortkey
            || lc($a->{'object'}->name) cmp lc($b->{'object'}->name)
        } (values %$class);
    $vars->{'classifications'} = \@classifications;

    my @open_states = BUG_STATE_OPEN();
    $vars->{'open_states'} = \@open_states;

    $vars->{'token'} = issue_session_token('create_bug');

    $vars->{'platform'} = detect_platform();
    $vars->{'op_sys'} = detect_op_sys();

    eval 'use Bugzilla::Extension::BMO::Data';
    $vars->{'BMO'} = $@ ? 0 : 1;
}

sub page_before_template {
    my ($self, $args) = @_;
    my $page = $args->{'page_id'};
    my $vars = $args->{'vars'};

    return unless $page eq 'guided_products.js';

    # import product -> security group mappings from the BMO ext

    our %product_sec_groups;
    eval q#use Bugzilla::Extension::BMO::Data '%product_sec_groups'#;
    return if $@;

    $vars->{'products'} = \%product_sec_groups;
}

__PACKAGE__->NAME;