Keyword.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # -*- Mode: perl; indent-tabs-mode: nil -*-
  2. #
  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/
  7. #
  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.
  12. #
  13. # The Original Code is the Bugzilla Bug Tracking System.
  14. #
  15. # Contributor(s): Max Kanat-Alexander <mkanat@bugzilla.org>
  16. use strict;
  17. package Bugzilla::Keyword;
  18. use base qw(Bugzilla::Object);
  19. use Bugzilla::Error;
  20. use Bugzilla::Util;
  21. ###############################
  22. #### Initialization ####
  23. ###############################
  24. use constant DB_COLUMNS => qw(
  25. keyworddefs.id
  26. keyworddefs.name
  27. keyworddefs.description
  28. );
  29. use constant DB_TABLE => 'keyworddefs';
  30. use constant REQUIRED_CREATE_FIELDS => qw(name description);
  31. use constant VALIDATORS => {
  32. name => \&_check_name,
  33. description => \&_check_description,
  34. };
  35. use constant UPDATE_COLUMNS => qw(
  36. name
  37. description
  38. );
  39. ###############################
  40. #### Accessors ######
  41. ###############################
  42. sub description { return $_[0]->{'description'}; }
  43. sub bug_count {
  44. my ($self) = @_;
  45. return $self->{'bug_count'} if defined $self->{'bug_count'};
  46. ($self->{'bug_count'}) =
  47. Bugzilla->dbh->selectrow_array(
  48. 'SELECT COUNT(*) FROM keywords WHERE keywordid = ?',
  49. undef, $self->id);
  50. return $self->{'bug_count'};
  51. }
  52. ###############################
  53. #### Mutators #####
  54. ###############################
  55. sub set_name { $_[0]->set('name', $_[1]); }
  56. sub set_description { $_[0]->set('description', $_[1]); }
  57. ###############################
  58. #### Subroutines ######
  59. ###############################
  60. sub keyword_count {
  61. my ($count) =
  62. Bugzilla->dbh->selectrow_array('SELECT COUNT(*) FROM keyworddefs');
  63. return $count;
  64. }
  65. sub get_all_with_bug_count {
  66. my $class = shift;
  67. my $dbh = Bugzilla->dbh;
  68. my $keywords =
  69. $dbh->selectall_arrayref('SELECT ' . join(', ', DB_COLUMNS) . ',
  70. COUNT(keywords.bug_id) AS bug_count
  71. FROM keyworddefs
  72. LEFT JOIN keywords
  73. ON keyworddefs.id = keywords.keywordid ' .
  74. $dbh->sql_group_by('keyworddefs.id',
  75. 'keyworddefs.name,
  76. keyworddefs.description') . '
  77. ORDER BY keyworddefs.name', {'Slice' => {}});
  78. if (!$keywords) {
  79. return [];
  80. }
  81. foreach my $keyword (@$keywords) {
  82. bless($keyword, $class);
  83. }
  84. return $keywords;
  85. }
  86. ###############################
  87. ### Validators ###
  88. ###############################
  89. sub _check_name {
  90. my ($self, $name) = @_;
  91. $name = trim($name);
  92. $name eq "" && ThrowUserError("keyword_blank_name");
  93. if ($name =~ /[\s,]/) {
  94. ThrowUserError("keyword_invalid_name");
  95. }
  96. # We only want to validate the non-existence of the name if
  97. # we're creating a new Keyword or actually renaming the keyword.
  98. if (!ref($self) || $self->name ne $name) {
  99. my $keyword = new Bugzilla::Keyword({ name => $name });
  100. ThrowUserError("keyword_already_exists", { name => $name }) if $keyword;
  101. }
  102. return $name;
  103. }
  104. sub _check_description {
  105. my ($self, $desc) = @_;
  106. $desc = trim($desc);
  107. $desc eq '' && ThrowUserError("keyword_blank_description");
  108. return $desc;
  109. }
  110. 1;
  111. __END__
  112. =head1 NAME
  113. Bugzilla::Keyword - A Keyword that can be added to a bug.
  114. =head1 SYNOPSIS
  115. use Bugzilla::Keyword;
  116. my $count = Bugzilla::Keyword::keyword_count;
  117. my $description = $keyword->description;
  118. my $keywords = Bugzilla::Keyword->get_all_with_bug_count();
  119. =head1 DESCRIPTION
  120. Bugzilla::Keyword represents a keyword that can be added to a bug.
  121. This implements all standard C<Bugzilla::Object> methods. See
  122. L<Bugzilla::Object> for more details.
  123. =head1 SUBROUTINES
  124. This is only a list of subroutines specific to C<Bugzilla::Keyword>.
  125. See L<Bugzilla::Object> for more subroutines that this object
  126. implements.
  127. =over
  128. =item C<keyword_count()>
  129. Description: A utility function to get the total number
  130. of keywords defined. Mostly used to see
  131. if there are any keywords defined at all.
  132. Params: none
  133. Returns: An integer, the count of keywords.
  134. =item C<get_all_with_bug_count()>
  135. Description: Returns all defined keywords. This is an efficient way
  136. to get the associated bug counts, as only one SQL query
  137. is executed with this method, instead of one per keyword
  138. when calling get_all and then bug_count.
  139. Params: none
  140. Returns: A reference to an array of Keyword objects, or an empty
  141. arrayref if there are no keywords.
  142. =back
  143. =cut