summaryrefslogtreecommitdiffstats
path: root/extensions/MyDashboard/Extension.pm
blob: 082f1c5623622d6c6ea6a041fdee61ec2059a0d1 (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
# 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::Saved;

use Bugzilla::Extension::MyDashboard::Queries qw(QUERY_DEFS);

our $VERSION = BUGZILLA_VERSION;

################
# 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, Bugzilla->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';

    # require user to be logged in for this page
    Bugzilla->login(LOGIN_REQUIRED);

    $vars->{queries} = [ QUERY_DEFS ];
}

#########
# 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}) {
            if (defined $params->{'in_mydashboard_' . $q->id}) {
                $sth_insert_fp->execute($q->id, $user->id) if !$q->in_mydashboard;
            }
            else {
                $sth_delete_fp->execute($q->id, $user->id) if $q->in_mydashboard;
            }
        }
    }
}

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

__PACKAGE__->NAME;