summaryrefslogtreecommitdiffstats
path: root/extensions/MyDashboard/Extension.pm
blob: 82c9954422055c2444ea85b106cfe861273b9a4d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# 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::MyDashboard;

use strict;

use base qw(Bugzilla::Extension);

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Search;
use Bugzilla::Util;
use Bugzilla::Status;
use Bugzilla::Field;
use Bugzilla::Search::Saved;

use Bugzilla::Extension::MyDashboard::Util qw(open_states closed_states
                                              quoted_open_states quoted_closed_states);
use Bugzilla::Extension::MyDashboard::TimeAgo qw(time_ago);

use DateTime;

our $VERSION = BUGZILLA_VERSION;

sub QUERY_DEFS {
    my $user = Bugzilla->user;

    my @query_defs = (
        {
            name        => 'assignedbugs',
            heading     => 'Assigned to You',
            description => 'The bug has been assigned to you and it is not resolved or closed yet.',
            params      => {
                'bug_status'        => ['__open__'],
                'emailassigned_to1' => 1,
                'emailtype1'        => 'exact',
                'email1'            => $user->login
            }
        },
        {
            name        => 'newbugs',
            heading     => 'New Reported by You',
            description => 'You reported the bug but nobody has accepted it yet.',
            params      => {
                'bug_status'     => ['NEW'],
                'emailreporter1' => 1,
                'emailtype1'     => 'exact',
                'email1'         => $user->login
            }
        },
        {
            name        => 'inprogressbugs',
            heading     => "In Progress Reported by You",
            description => 'You reported the bug, the developer accepted the bug and is hopefully working on it.',
            params      => {
                'bug_status'     => [ map { $_->name } grep($_->name ne 'NEW' && $_->name ne 'MODIFIED', open_states()) ],
                'emailreporter1' => 1,
                'emailtype1'     => 'exact',
                'email1'         => $user->login
            }
        },
        {
            name        => 'openccbugs',
            heading     => "You Are CC'd On",
            description => 'You are in the CC list of the bug, so you are watching it.',
            params      => {
                'bug_status' => ['__open__'],
                'emailcc1'   => 1,
                'emailtype1' => 'exact',
                'email1'     => $user->login
            }
        },
    );

    if (Bugzilla->params->{'useqacontact'}) {
        push(@query_defs, {
            name        => 'qacontactbugs',
            heading     => 'You Are QA Contact',
            description => 'You are the qa contact on this bug and it is not resolved or closed yet.',
            params      => {
                'bug_status'       => ['__open__'],
                'emailqa_contact1' => 1,
                'emailtype1'       => 'exact',
                'email1'           => $user->login
            }
        });
    }

    return @query_defs;
}

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

sub db_schema_abstract_schema {
    my ($self, $args) = @_;

    my $schema = $args->{schema};

    $schema->{'mydashboard'} = {
        FIELDS => [
            namedquery_id => {TYPE => 'INT3', NOTNULL => 1,
                              REFERENCES => {TABLE  => 'namedqueries',
                                             COLUMN => 'id',
                                             DELETE => 'CASCADE'}},
            user_id       => {TYPE => 'INT3', NOTNULL => 1,
                              REFERENCES => {TABLE  => 'profiles',
                                             COLUMN => 'userid',
                                             DELETE => 'CASCADE'}},
        ],
        INDEXES => [
            mydashboard_namedquery_id_idx => {FIELDS => [qw(namedquery_id user_id)],
                                              TYPE   => 'UNIQUE'},
            mydashboard_user_id_idx => ['user_id'],
        ],
    };
}

###########
# Objects #
###########

BEGIN {
    *Bugzilla::Search::Saved::in_mydashboard = \&_in_mydashboard;
}

sub _in_mydashboard {
    my ($self) = @_;
    my $dbh = Bugzilla->dbh;
    return $self->{'in_mydashboard'} if exists $self->{'in_mydashboard'};
    $self->{'in_mydashboard'} = $dbh->selectrow_array("
        SELECT 1 FROM mydashboard WHERE namedquery_id = ? AND user_id = ?",
        undef, $self->id, $self->user->id);
    return $self->{'in_mydashboard'};
}

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

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

    return if $page ne 'mydashboard.html';

    # If we're using bug groups to restrict bug entry, we need to know who the
    # user is right from the start.
    my $user = Bugzilla->login(LOGIN_REQUIRED);

    # Switch to shadow db since we are just reading information
    Bugzilla->switch_to_shadow_db();

    _standard_saved_queries($vars);
    _flags_requested($vars);

    $vars->{'severities'} = get_legal_field_values('bug_severity');
}

sub _standard_saved_queries {
    my ($vars) = @_;
    my $dbh  = Bugzilla->dbh;
    my $user = Bugzilla->user;

    # Default sort order
    my $order = ["changeddate desc", "bug_id"];

    # List of columns that we will be selecting. In the future this should be configurable
    # Share with buglist.cgi?
    my @select_columns = ('bug_id','product','bug_status','bug_severity','version', 'component','short_desc', 'changeddate');

    # Define the columns that can be selected in a query 
    my $columns = Bugzilla::Search::COLUMNS;

    # Weed out columns that don't actually exist and detaint along the way.
    @select_columns = grep($columns->{$_} && trick_taint($_), @select_columns);

    ### Standard query definitions
    my @query_defs = QUERY_DEFS;

    ### Saved query definitions 
    ### These are enabled through the userprefs.cgi UI

    if ($user->showmybugslink) {
        my $query = Bugzilla->params->{mybugstemplate};
        my $login = $user->login;
        $query =~ s/%userid%/$login/;
        $query =~ s/^buglist.cgi\?//;
        push(@query_defs, {
            name        => 'mybugs',
            heading     => "My Bugs",
            saved       => 1,
            params      => $query,
        });
    }

    foreach my $q (@{$user->queries}) {
        next if !$q->in_mydashboard;
        push(@query_defs, { name    => $q->name,
                            heading => $q->name,
                            saved   => 1,
                            params  => $q->url });
    }

    #my $date_now = DateTime->now(time_zone => Bugzilla->local_timezone);

    ### Collect the query results for display in the template

    my @results;
    foreach my $qdef (@query_defs) {
        my $params = new Bugzilla::CGI($qdef->{params});

        my $search = new Bugzilla::Search( fields => \@select_columns,
                                           params => scalar $params->Vars,
                                           order  => $order );
        my $query = $search->sql();

        my $sth = $dbh->prepare($query);
        $sth->execute();

        my $rows = $sth->fetchall_arrayref();

        my @bugs;
        foreach my $row (@$rows) {
            my $bug = {};
            foreach my $column (@select_columns) {
                $bug->{$column} = shift @$row;
                #if ($column eq 'changeddate') {
                #   my $date_then = datetime_from($bug->{$column});
                #   $bug->{'updated'} = time_ago($date_then, $date_now);
                #}
            }
            push(@bugs, $bug);
        }

        $qdef->{bugs}   = \@bugs;
        $qdef->{buffer} = $params->canonicalise_query();

        push(@results, $qdef);
    }

    $vars->{'results'} = \@results;
}

sub _flags_requested {
    my ($vars) = @_;
    my $user = Bugzilla->user;
    my $dbh  = Bugzilla->dbh;

    my $attach_join_clause = "flags.attach_id = attachments.attach_id";
    if (Bugzilla->params->{insidergroup} && !$user->in_group(Bugzilla->params->{insidergroup})) {
        $attach_join_clause .= " AND attachments.isprivate < 1";
    }

    my $query = 
    # Select columns describing each flag, the bug/attachment on which
    # it has been set, who set it, and of whom they are requesting it.
    " SELECT flags.id AS id, 
             flagtypes.name AS type,
             flags.status AS status,
             flags.bug_id AS bug_id, 
             bugs.short_desc AS bug_summary,
             flags.attach_id AS attach_id, 
             attachments.description AS attach_summary,
             requesters.realname AS requester, 
             requestees.realname AS requestee,
             " . $dbh->sql_date_format('flags.creation_date', '%Y:%m:%d') . " AS created
        FROM flags 
             LEFT JOIN attachments
                  ON ($attach_join_clause)
             INNER JOIN flagtypes
                  ON flags.type_id = flagtypes.id
             INNER JOIN bugs
                  ON flags.bug_id = bugs.bug_id
             LEFT JOIN profiles AS requesters
                  ON flags.setter_id = requesters.userid
             LEFT JOIN profiles AS requestees
                  ON flags.requestee_id  = requestees.userid
             LEFT JOIN bug_group_map AS bgmap
                  ON bgmap.bug_id = bugs.bug_id
             LEFT JOIN cc AS ccmap
                  ON ccmap.who = " . $user->id . "
                  AND ccmap.bug_id = bugs.bug_id ";

    # Limit query to pending requests and open bugs only
    $query .= " WHERE bugs.bug_status IN (" . join(',', quoted_open_states()) . ")
                      AND flags.status = '?' ";

    # Weed out bug the user does not have access to
    $query .= " AND ((bgmap.group_id IS NULL)
                     OR bgmap.group_id IN (" . $user->groups_as_string . ")
                     OR (ccmap.who IS NOT NULL AND cclist_accessible = 1) 
                     OR (bugs.reporter = " . $user->id . " AND bugs.reporter_accessible = 1) 
                     OR (bugs.assigned_to = " . $user->id .") ";
    if (Bugzilla->params->{useqacontact}) {
        $query .= " OR (bugs.qa_contact = " . $user->id . ") ";
    }
    $query .= ") ";

    # Order the records (within each group).
    my $group_order_by = " GROUP BY flags.bug_id ORDER BY flags.creation_date, flagtypes.name";

    my $requestee_list = $dbh->selectall_arrayref($query . 
                                                  " AND requestees.login_name = ? " . 
                                                  $group_order_by, 
                                                  { Slice => {} }, $user->login);
    $vars->{'requestee_list'} = $requestee_list; 
    my $requester_list = $dbh->selectall_arrayref($query . 
                                                  " AND requesters.login_name = ? " . 
                                                  $group_order_by, 
                                                  { Slice => {} }, $user->login);
    $vars->{'requester_list'} = $requester_list;
}

#########
# Hooks #
#########

sub user_preferences {
    my ($self, $args) = @_;
    my $tab = $args->{'current_tab'};
    return unless $tab eq 'saved-searches';

    my $save    = $args->{'save_changes'};
    my $handled = $args->{'handled'};
    my $vars    = $args->{'vars'};

    my $dbh    = Bugzilla->dbh;
    my $user   = Bugzilla->user;
    my $params = Bugzilla->input_params;

    if ($save) {
        my $sth_insert_fp = $dbh->prepare('INSERT INTO mydashboard 
                                           (namedquery_id, user_id)
                                           VALUES (?, ?)');
        my $sth_delete_fp = $dbh->prepare('DELETE FROM mydashboard
                                           WHERE namedquery_id = ?
                                           AND user_id = ?');
        foreach my $q (@{$user->queries}, @{$user->queries_available}) {
            if (defined $params->{'in_mydashboard_' . $q->id}) {
                $sth_insert_fp->execute($q->id, $q->user->id) if !$q->in_mydashboard;
            }
            else {
                $sth_delete_fp->execute($q->id, $q->user->id) if $q->in_mydashboard;
            }
        }
    }
}

sub webservice {
    my ($self, $args) = @_;
    my $dispatch = $args->{dispatch};
    $dispatch->{MyDashboard} = "Bugzilla::Extension::MyDashboard::WebService";
}

__PACKAGE__->NAME;