1
# -*- Mode: perl; indent-tabs-mode: nil -*-
3
# The contents of this file are subject to the Mozilla Public
4
# License Version 1.1 (the "License"); you may not use this file
5
# except in compliance with the License. You may obtain a copy of
6
# the License at http://www.mozilla.org/MPL/
8
# Software distributed under the License is distributed on an "AS
9
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
# implied. See the License for the specific language governing
11
# rights and limitations under the License.
13
# The Original Code is the Bugzilla Bug Tracking System.
15
# Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
19
package Bugzilla::Keyword;
21
use base qw(Bugzilla::Object);
26
###############################
27
#### Initialization ####
28
###############################
30
use constant DB_COLUMNS => qw(
33
keyworddefs.description
36
use constant DB_TABLE => 'keyworddefs';
38
use constant REQUIRED_CREATE_FIELDS => qw(name description);
40
use constant VALIDATORS => {
41
name => \&_check_name,
42
description => \&_check_description,
45
use constant UPDATE_COLUMNS => qw(
50
###############################
52
###############################
54
sub description { return $_[0]->{'description'}; }
58
return $self->{'bug_count'} if defined $self->{'bug_count'};
59
($self->{'bug_count'}) =
60
Bugzilla->dbh->selectrow_array(
61
'SELECT COUNT(*) FROM keywords WHERE keywordid = ?',
63
return $self->{'bug_count'};
66
###############################
68
###############################
70
sub set_name { $_[0]->set('name', $_[1]); }
71
sub set_description { $_[0]->set('description', $_[1]); }
73
###############################
74
#### Subroutines ######
75
###############################
79
Bugzilla->dbh->selectrow_array('SELECT COUNT(*) FROM keyworddefs');
83
sub get_all_with_bug_count {
85
my $dbh = Bugzilla->dbh;
87
$dbh->selectall_arrayref('SELECT ' . join(', ', DB_COLUMNS) . ',
88
COUNT(keywords.bug_id) AS bug_count
91
ON keyworddefs.id = keywords.keywordid ' .
92
$dbh->sql_group_by('keyworddefs.id',
94
keyworddefs.description') . '
95
ORDER BY keyworddefs.name', {'Slice' => {}});
100
foreach my $keyword (@$keywords) {
101
bless($keyword, $class);
106
###############################
108
###############################
111
my ($self, $name) = @_;
114
$name eq "" && ThrowUserError("keyword_blank_name");
115
if ($name =~ /[\s,]/) {
116
ThrowUserError("keyword_invalid_name");
119
# We only want to validate the non-existence of the name if
120
# we're creating a new Keyword or actually renaming the keyword.
121
if (!ref($self) || $self->name ne $name) {
122
my $keyword = new Bugzilla::Keyword({ name => $name });
123
ThrowUserError("keyword_already_exists", { name => $name }) if $keyword;
129
sub _check_description {
130
my ($self, $desc) = @_;
132
$desc eq '' && ThrowUserError("keyword_blank_description");
142
Bugzilla::Keyword - A Keyword that can be added to a bug.
146
use Bugzilla::Keyword;
148
my $count = Bugzilla::Keyword::keyword_count;
150
my $description = $keyword->description;
152
my $keywords = Bugzilla::Keyword->get_all_with_bug_count();
156
Bugzilla::Keyword represents a keyword that can be added to a bug.
158
This implements all standard C<Bugzilla::Object> methods. See
159
L<Bugzilla::Object> for more details.
163
This is only a list of subroutines specific to C<Bugzilla::Keyword>.
164
See L<Bugzilla::Object> for more subroutines that this object
169
=item C<keyword_count()>
171
Description: A utility function to get the total number
172
of keywords defined. Mostly used to see
173
if there are any keywords defined at all.
175
Returns: An integer, the count of keywords.
177
=item C<get_all_with_bug_count()>
179
Description: Returns all defined keywords. This is an efficient way
180
to get the associated bug counts, as only one SQL query
181
is executed with this method, instead of one per keyword
182
when calling get_all and then bug_count.
184
Returns: A reference to an array of Keyword objects, or an empty
185
arrayref if there are no keywords.