Series.pm 9.08 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 37 38 39 40 41 42 43 44 45
use Bugzilla::Util;
use Bugzilla::User;

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);

46 47
    my $arg_count = scalar(@_);
    
48 49 50 51 52
    # 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;

53 54 55
    # 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.
56
    if ($arg_count == 1) {
57
        if (ref($_[0])) {
58
            # We've been given a CGI object to create a new Series from.
59 60
            # This series may already exist - external code needs to check
            # before it calls writeToDatabase().
61
            $self->initFromCGI($_[0]);
62 63
        }
        else {
64 65
            # We've been given a series_id, which should represent an existing
            # Series.
66
            $retval = $self->initFromDatabase($_[0]);
67 68
        }
    }
69
    elsif ($arg_count >= 6 && $arg_count <= 8) {
70
        # We've been given a load of parameters to create a new Series from.
71 72
        # Currently, undef is always passed as the first parameter; this allows
        # you to call writeToDatabase() unconditionally. 
73
        $self->initFromParameters(@_);
74 75
    }
    else {
76
        die("Bad parameters passed in - invalid number of args: $arg_count");
77 78
    }

79
    return $retval;
80 81 82 83 84 85
}

sub initFromDatabase {
    my $self = shift;
    my $series_id = shift;
    
86 87
    detaint_natural($series_id) 
      || ThrowCodeError("invalid_series_id", { 'series_id' => $series_id });
88 89 90 91
    
    my $dbh = Bugzilla->dbh;
    my @series = $dbh->selectrow_array("SELECT series.series_id, cc1.name, " .
        "cc2.name, series.name, series.creator, series.frequency, " .
92
        "series.query, series.is_public " .
93 94
        "FROM series " .
        "LEFT JOIN series_categories AS cc1 " .
95
        "    ON series.category = cc1.id " .
96
        "LEFT JOIN series_categories AS cc2 " .
97 98 99 100 101 102 103 104
        "    ON series.subcategory = cc2.id " .
        "LEFT JOIN category_group_map AS cgm " .
        "    ON series.category = cgm.category_id " .
        "LEFT JOIN user_group_map AS ugm " .
        "    ON cgm.group_id = ugm.group_id " .
        "    AND ugm.user_id = " . Bugzilla->user->id .
        "    AND isbless = 0 " .
        "WHERE series.series_id = $series_id AND " .
105
        "(is_public = 1 OR creator = " . Bugzilla->user->id . " OR " .
106
        "(ugm.group_id IS NOT NULL)) " . 
107 108
        $dbh->sql_group_by('series.series_id', 'cc1.name, cc2.name, ' .
                           'series.name, series.creator, series.frequency, ' .
109
                           'series.query, series.is_public'));
110 111 112
    
    if (@series) {
        $self->initFromParameters(@series);
113
        return $self;
114 115
    }
    else {
116
        return undef;
117 118 119 120
    }
}

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

    ($self->{'series_id'}, $self->{'category'},  $self->{'subcategory'},
     $self->{'name'}, $self->{'creator'}, $self->{'frequency'},
126
     $self->{'query'}, $self->{'public'}) = @_;
127 128
}

129 130 131 132 133 134 135
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'})
136
          || ThrowCodeError("invalid_series_id", 
137 138 139 140 141
                               { 'series_id' => $self->{'series_id'} });
    }
    
    $self->{'category'} = $cgi->param('category')
      || $cgi->param('newcategory')
142
      || ThrowUserError("missing_category");
143 144 145

    $self->{'subcategory'} = $cgi->param('subcategory')
      || $cgi->param('newsubcategory')
146
      || ThrowUserError("missing_subcategory");
147 148

    $self->{'name'} = $cgi->param('name')
149
      || ThrowUserError("missing_name");
150 151 152 153 154

    $self->{'creator'} = Bugzilla->user->id;

    $self->{'frequency'} = $cgi->param('frequency');
    detaint_natural($self->{'frequency'})
155
      || ThrowUserError("missing_frequency");
156 157 158 159

    $self->{'query'} = $cgi->canonicalise_query("format", "ctype", "action",
                                        "category", "subcategory", "name",
                                        "frequency", "public", "query_format");
160
    trick_taint($self->{'query'});
161
                                        
162 163 164 165
    $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.
166
    $self->{'public'} = 0 unless Bugzilla->user->in_group('admin');
167 168 169
}

sub writeToDatabase {
170 171 172
    my $self = shift;

    my $dbh = Bugzilla->dbh;
173
    $dbh->bz_start_transaction();
174 175 176 177

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

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

        # Retrieve series_id
        $self->{'series_id'} = $dbh->selectrow_array("SELECT MAX(series_id) " .
                                                     "FROM series");
        $self->{'series_id'}
209
          || ThrowCodeError("missing_series_id", { 'series' => $self });
210 211
    }
    
212
    $dbh->bz_commit_transaction();
213 214
}

215
# Check whether a series with this name, category and subcategory exists in
216
# the DB and, if so, returns its series_id.
217 218 219 220 221 222 223 224
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'});
225
    my $series_id = $dbh->selectrow_array("SELECT series_id " .
226 227 228 229
                              "FROM series WHERE category = $category_id " .
                              "AND subcategory = $subcategory_id AND name = " .
                              $dbh->quote($self->{'name'}));
                              
230
    return($series_id);
231 232
}

233 234 235 236 237 238 239 240 241 242 243
# 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);

244
        $category_id = $dbh->selectrow_array("SELECT id " .
245 246
                                      "from series_categories " .
                                      "WHERE name =" . $dbh->quote($category));
247 248

        last if defined($category_id);
249 250 251 252 253 254 255 256 257

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

    return $category_id;
}

1;