summaryrefslogtreecommitdiffstats
path: root/extensions/FlagDefaultRequestee/Extension.pm
blob: 0ab599cd351ac78b477179c8c2695a24cef923d0 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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::FlagDefaultRequestee;

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

use Bugzilla::Error;
use Bugzilla::FlagType;
use Bugzilla::User;

use Bugzilla::Extension::FlagDefaultRequestee::Constants;

our $VERSION = '1';

################
# Installation #
################

sub install_update_db {
    my $dbh = Bugzilla->dbh;
    if (!$dbh->bz_column_info('flagtypes', 'default_requestee')) {
        $dbh->bz_add_column('flagtypes', 'default_requestee', {
            TYPE => 'INT3', NOTNULL => 0,
            REFERENCES => { TABLE  => 'profiles',
                            COLUMN => 'userid',
                            DELETE => 'SET NULL' }
        });
    }
}

#############
# Templates #
#############

sub template_before_process {
    my ($self, $args) = @_;
    my ($vars, $file) = @$args{qw(vars file)};
    my $dbh = Bugzilla->dbh;

    return unless Bugzilla->user->id;

    return unless grep { $_ eq $file } FLAGTYPE_TEMPLATES;

    my $flag_types = [];
    if (exists $vars->{bug} || exists $vars->{attachment}) {
        my $bug;
        if (exists $vars->{bug}) {
            $bug = $vars->{'bug'};
        } elsif (exists $vars->{'attachment'}) {
            $bug = $vars->{'attachment'}->{bug};
        }

        $flag_types = Bugzilla::FlagType::match({
            'target_type'  => ($file =~ /^bug/ ? 'bug' : 'attachment'),
            'product_id'   => $bug->product_id,
            'component_id' => $bug->component_id,
            'bug_id'       => $bug->id,
            'active_or_has_flags' => $bug->id,
        });

        $vars->{flag_currently_requested} ||= {};
        foreach my $type (@$flag_types) {
            my $flags = Bugzilla::Flag->match({
                type_id => $type->id, 
                bug_id  => $bug->id, 
                status  => '?'
            });
            map { $vars->{flag_currently_requested}->{$_->id} = 1 } @$flags;
        }
    }
    elsif ($file =~ /^bug\/create/ && exists $vars->{product}) {
        my $bug_flags        = $vars->{product}->flag_types->{bug};
        my $attachment_flags = $vars->{product}->flag_types->{attachment};
        $flag_types          = [ map { $_ } (@$bug_flags, @$attachment_flags) ];
    }

    return if !@$flag_types;

    $vars->{flag_default_requestees}  ||= {};
    foreach my $type (@$flag_types) {
        next if !$type->default_requestee;
        $vars->{flag_default_requestees}->{$type->id} = $type->default_requestee->login;
    }
}

#########
# Admin #
#########

sub flagtype_end_of_create {
    my ($self, $args) = @_;
    _set_default_requestee($args->{type});
}

sub flagtype_end_of_update {
    my ($self, $args) = @_;
    _set_default_requestee($args->{type});
}

sub _set_default_requestee {
    my $type  = shift;
    my $input = Bugzilla->input_params;
    my $dbh   = Bugzilla->dbh;

    my $requestee_login = $input->{'default_requestee'};

    my $requestee_id = undef;
    if ($requestee_login) {
        if ($type->name eq 'review') {
            ThrowUserError("flag_default_requestee_review");
        }
        my $requestee = Bugzilla::User->check($requestee_login);
        $requestee_id = $requestee->id;
    }

    $dbh->do("UPDATE flagtypes SET default_requestee = ? WHERE id = ?",
             undef, $requestee_id, $type->id);
    Bugzilla->memcached->clear({ table => 'flagtypes', id => $type->id });
}

##################
# Object Methods #
##################

BEGIN {
    *Bugzilla::FlagType::default_requestee = \&_default_requestee;
}

sub _default_requestee {
    my ($self) = @_;
    my $dbh = Bugzilla->dbh;
    return $self->{default_requestee} if exists $self->{default_requestee};
    my $requestee_id = $dbh->selectrow_array("SELECT default_requestee
                                                FROM flagtypes
                                               WHERE id = ?", 
                                             undef, $self->id);
    $self->{default_requestee} = $requestee_id 
                                 ? Bugzilla::User->new($requestee_id)
                                 : undef;
    return $self->{default_requestee};
}

__PACKAGE__->NAME;