Series.pm 9.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# 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.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s): Gervase Markham <gerv@gerv.net>
21
#                 Lance Larsh <lance.larsh@oracle.com>
22 23 24 25

use strict;

# This module implements a series - a set of data to be plotted on a chart.
26 27 28 29 30 31
#
# This Series is in the database if and only if self->{'series_id'} is defined. 
# Note that the series being in the database does not mean that the fields of 
# this object are the same as the DB entries, as the object may have been 
# altered.

32 33
package Bugzilla::Series;

34
use Bugzilla::Error;
35 36
use Bugzilla::Util;

37 38 39 40 41
# This is a hack so that we can re-use the rename_field_value
# code from Bugzilla::Search::Saved.
use constant DB_TABLE => 'series';
use constant ID_FIELD => 'series_id';

42 43 44 45 46 47 48 49
sub new {
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
  
    # Create a ref to an empty hash and bless it
    my $self = {};
    bless($self, $class);

50 51
    my $arg_count = scalar(@_);
    
52 53 54 55 56
    # new() can return undef if you pass in a series_id and the user doesn't 
    # have sufficient permissions. If you create a new series in this way,
    # you need to check for an undef return, and act appropriately.
    my $retval = $self;

57 58 59
    # There are three ways of creating Series objects. Two (CGI and Parameters)
    # are for use when creating a new series. One (Database) is for retrieving
    # information on existing series.
60
    if ($arg_count == 1) {
61
        if (ref($_[0])) {
62
            # We've been given a CGI object to create a new Series from.
63 64
            # This series may already exist - external code needs to check
            # before it calls writeToDatabase().
65
            $self->initFromCGI($_[0]);
66 67
        }
        else {
68 69
            # We've been given a series_id, which should represent an existing
            # Series.
70
            $retval = $self->initFromDatabase($_[0]);
71 72
        }
    }
73
    elsif ($arg_count >= 6 && $arg_count <= 8) {
74
        # We've been given a load of parameters to create a new Series from.
75
        # Currently, undef is always passed as the first parameter; this allows
76 77
        # you to call writeToDatabase() unconditionally.
        # XXX - You cannot set category_id and subcategory_id from here.
78
        $self->initFromParameters(@_);
79 80
    }
    else {
81
        die("Bad parameters passed in - invalid number of args: $arg_count");
82 83
    }

84
    return $retval;
85 86 87
}

sub initFromDatabase {
88 89 90 91
    my ($self, $series_id) = @_;
    my $dbh = Bugzilla->dbh;
    my $user = Bugzilla->user;

92 93
    detaint_natural($series_id) 
      || ThrowCodeError("invalid_series_id", { 'series_id' => $series_id });
94 95 96

    my $grouplist = $user->groups_as_string;

97 98
    my @series = $dbh->selectrow_array("SELECT series.series_id, cc1.name, " .
        "cc2.name, series.name, series.creator, series.frequency, " .
99
        "series.query, series.is_public, series.category, series.subcategory " .
100
        "FROM series " .
101
        "INNER JOIN series_categories AS cc1 " .
102
        "    ON series.category = cc1.id " .
103
        "INNER JOIN series_categories AS cc2 " .
104 105 106
        "    ON series.subcategory = cc2.id " .
        "LEFT JOIN category_group_map AS cgm " .
        "    ON series.category = cgm.category_id " .
107 108 109 110 111
        "    AND cgm.group_id NOT IN($grouplist) " .
        "WHERE series.series_id = ? " .
        "    AND (creator = ? OR (is_public = 1 AND cgm.category_id IS NULL))",
        undef, ($series_id, $user->id));

112 113
    if (@series) {
        $self->initFromParameters(@series);
114
        return $self;
115 116
    }
    else {
117
        return undef;
118 119 120 121
    }
}

sub initFromParameters {
122
    # Pass undef as the first parameter if you are creating a new series.
123 124 125
    my $self = shift;

    ($self->{'series_id'}, $self->{'category'},  $self->{'subcategory'},
126 127 128
     $self->{'name'}, $self->{'creator_id'}, $self->{'frequency'},
     $self->{'query'}, $self->{'public'}, $self->{'category_id'},
     $self->{'subcategory_id'}) = @_;
129 130 131 132

    # If the first parameter is undefined, check if this series already
    # exists and update it series_id accordingly
    $self->{'series_id'} ||= $self->existsInDatabase();
133 134
}

135 136 137 138 139 140 141
sub initFromCGI {
    my $self = shift;
    my $cgi = shift;

    $self->{'series_id'} = $cgi->param('series_id') || undef;
    if (defined($self->{'series_id'})) {
        detaint_natural($self->{'series_id'})
142
          || ThrowCodeError("invalid_series_id", 
143 144 145 146 147
                               { 'series_id' => $self->{'series_id'} });
    }
    
    $self->{'category'} = $cgi->param('category')
      || $cgi->param('newcategory')
148
      || ThrowUserError("missing_category");
149 150 151

    $self->{'subcategory'} = $cgi->param('subcategory')
      || $cgi->param('newsubcategory')
152
      || ThrowUserError("missing_subcategory");
153 154

    $self->{'name'} = $cgi->param('name')
155
      || ThrowUserError("missing_name");
156

157
    $self->{'creator_id'} = Bugzilla->user->id;
158 159 160

    $self->{'frequency'} = $cgi->param('frequency');
    detaint_natural($self->{'frequency'})
161
      || ThrowUserError("missing_frequency");
162 163 164 165

    $self->{'query'} = $cgi->canonicalise_query("format", "ctype", "action",
                                        "category", "subcategory", "name",
                                        "frequency", "public", "query_format");
166
    trick_taint($self->{'query'});
167
                                        
168 169 170 171
    $self->{'public'} = $cgi->param('public') ? 1 : 0;
    
    # Change 'admin' here and in series.html.tmpl, or remove the check
    # completely, if you want to change who can make series public.
172
    $self->{'public'} = 0 unless Bugzilla->user->in_group('admin');
173 174 175
}

sub writeToDatabase {
176 177 178
    my $self = shift;

    my $dbh = Bugzilla->dbh;
179
    $dbh->bz_start_transaction();
180 181 182 183

    my $category_id = getCategoryID($self->{'category'});
    my $subcategory_id = getCategoryID($self->{'subcategory'});

184 185 186 187 188 189 190
    my $exists;
    if ($self->{'series_id'}) { 
        $exists = 
            $dbh->selectrow_array("SELECT series_id FROM series
                                   WHERE series_id = $self->{'series_id'}");
    }
    
191
    # Is this already in the database?                              
192
    if ($exists) {
193 194 195 196
        # Update existing series
        my $dbh = Bugzilla->dbh;
        $dbh->do("UPDATE series SET " .
                 "category = ?, subcategory = ?," .
197
                 "name = ?, frequency = ?, is_public = ?  " .
198 199
                 "WHERE series_id = ?", undef,
                 $category_id, $subcategory_id, $self->{'name'},
200 201
                 $self->{'frequency'}, $self->{'public'}, 
                 $self->{'series_id'});
202 203 204 205
    }
    else {
        # Insert the new series into the series table
        $dbh->do("INSERT INTO series (creator, category, subcategory, " .
206
                 "name, frequency, query, is_public) VALUES " . 
207
                 "(?, ?, ?, ?, ?, ?, ?)", undef,
208
                 $self->{'creator_id'}, $category_id, $subcategory_id, $self->{'name'},
209
                 $self->{'frequency'}, $self->{'query'}, $self->{'public'});
210 211 212 213 214

        # Retrieve series_id
        $self->{'series_id'} = $dbh->selectrow_array("SELECT MAX(series_id) " .
                                                     "FROM series");
        $self->{'series_id'}
215
          || ThrowCodeError("missing_series_id", { 'series' => $self });
216 217
    }
    
218
    $dbh->bz_commit_transaction();
219 220
}

221
# Check whether a series with this name, category and subcategory exists in
222
# the DB and, if so, returns its series_id.
223 224 225 226 227 228 229 230
sub existsInDatabase {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    my $category_id = getCategoryID($self->{'category'});
    my $subcategory_id = getCategoryID($self->{'subcategory'});
    
    trick_taint($self->{'name'});
231
    my $series_id = $dbh->selectrow_array("SELECT series_id " .
232 233 234 235
                              "FROM series WHERE category = $category_id " .
                              "AND subcategory = $subcategory_id AND name = " .
                              $dbh->quote($self->{'name'}));
                              
236
    return($series_id);
237 238
}

239 240 241 242 243 244 245 246 247 248 249
# Get a category or subcategory IDs, creating the category if it doesn't exist.
sub getCategoryID {
    my ($category) = @_;
    my $category_id;
    my $dbh = Bugzilla->dbh;

    # This seems for the best idiom for "Do A. Then maybe do B and A again."
    while (1) {
        # We are quoting this to put it in the DB, so we can remove taint
        trick_taint($category);

250
        $category_id = $dbh->selectrow_array("SELECT id " .
251 252
                                      "from series_categories " .
                                      "WHERE name =" . $dbh->quote($category));
253 254

        last if defined($category_id);
255 256 257 258 259 260 261 262

        $dbh->do("INSERT INTO series_categories (name) " .
                 "VALUES (" . $dbh->quote($category) . ")");
    }

    return $category_id;
}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
##########
# Methods
##########
sub id   { return $_[0]->{'series_id'}; }
sub name { return $_[0]->{'name'}; }

sub creator {
    my $self = shift;

    if (!$self->{creator} && $self->{creator_id}) {
        require Bugzilla::User;
        $self->{creator} = new Bugzilla::User($self->{creator_id});
    }
    return $self->{creator};
}

sub remove_from_db {
    my $self = shift;
    my $dbh = Bugzilla->dbh;

    $dbh->do('DELETE FROM series WHERE series_id = ?', undef, $self->id);
}

286
1;