]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 530468: Create a read-only Bugzilla::Whine object for whine events and
authorEric Black <eblack@higherone.com>
Tue, 6 Jul 2010 00:16:04 +0000 (17:16 -0700)
committerMax Kanat-Alexander <mkanat@bugzilla.org>
Tue, 6 Jul 2010 00:16:04 +0000 (17:16 -0700)
have editwhines.cgi use it
r=mkanat, a=mkanat

Bugzilla/Whine.pm [new file with mode: 0644]
editwhines.cgi

diff --git a/Bugzilla/Whine.pm b/Bugzilla/Whine.pm
new file mode 100644 (file)
index 0000000..73b0802
--- /dev/null
@@ -0,0 +1,132 @@
+# -*- 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 Eric Black.
+# Portions created by the Initial Developer are Copyright (C) 2010 
+# Eric Black. All Rights Reserved.
+#
+# Contributor(s): Eric Black <black.eric@gmail.com>
+
+use strict;
+
+package Bugzilla::Whine;
+
+use base qw(Bugzilla::Object);
+
+use Bugzilla::Constants;
+use Bugzilla::Error;
+use Bugzilla::User;
+use Bugzilla::Util;
+use Bugzilla::Whine::Schedule;
+use Bugzilla::Whine::Query;
+
+#############
+# Constants #
+#############
+
+use constant DB_TABLE => 'whine_events';
+
+use constant DB_COLUMNS => qw(
+    id
+    owner_userid
+    subject
+    body
+    mailifnobugs
+);
+
+use constant LIST_ORDER => 'id';
+
+####################
+# Simple Accessors #
+####################
+sub subject         { return $_[0]->{'subject'};      }
+sub body            { return $_[0]->{'body'};         }
+sub mail_if_no_bugs { return $_[0]->{'mailifnobugs'}; }
+
+sub user {
+    my ($self) = @_;
+    return $self->{user} if defined $self->{user};
+    $self->{user} = new Bugzilla::User($self->{'owner_userid'});
+    return $self->{user};
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Bugzilla::Whine - A Whine event
+
+=head1 SYNOPSIS
+
+ use Bugzilla::Whine;
+
+ my $event = new Bugzilla::Whine($event_id);
+
+ my $subject      = $event->subject;
+ my $body         = $event->body;
+ my $mailifnobugs = $event->mail_if_no_bugs;
+ my $user         = $event->user;
+
+=head1 DESCRIPTION
+
+This module exists to represent a whine event that has been
+saved to the database.
+
+This is an implementation of L<Bugzilla::Object>, and so has all the
+same methods available as L<Bugzilla::Object>, in addition to what is
+documented below.
+
+=head1 METHODS
+
+=head2 Constructors
+
+=over
+
+=item C<new>
+
+Does not accept a bare C<name> argument. Instead, accepts only an id.
+
+See also: L<Bugzilla::Object/new>.
+
+=back
+
+
+=head2 Accessors
+
+These return data about the object, without modifying the object.
+
+=over
+
+=item C<subject>
+
+Returns the subject of the whine event.
+
+=item C<body>
+
+Returns the body of the whine event.
+
+=item C<mail_if_no_bugs>
+
+Returns a numeric 1(C<true>) or 0(C<false>) to represent whether this
+whine event object is supposed to be mailed even if there are no bugs
+returned by the query.
+
+=item C<user>
+
+Returns the L<Bugzilla::User> object for the owner of the L<Bugzilla::Whine>
+event.
+
+=back
index c59cf8be5e3f72dcffcbc2151782ed22a1849016..04e2b548fcedc5cfb6631b81cfdf795a13fddcea 100755 (executable)
@@ -38,6 +38,7 @@ use Bugzilla::Group;
 use Bugzilla::Token;
 use Bugzilla::Whine::Schedule;
 use Bugzilla::Whine::Query;
+use Bugzilla::Whine;
 
 # require the user to have logged in
 my $user = Bugzilla->login(LOGIN_REQUIRED);
@@ -55,10 +56,8 @@ my $userid   = $user->id;
 my $token    = $cgi->param('token');
 my $sth; # database statement handle
 
-# $events is a hash ref, keyed by event id, that stores the active user's
-# events.  It starts off with:
-#  'subject' - the subject line for the email message
-#  'body'    - the text to be sent at the top of the message
+# $events is a hash ref of Bugzilla::Whine objects keyed by event id,
+# that stores the active user's events.
 #
 # Eventually, it winds up with:
 #  'queries'  - array ref containing hashes of:
@@ -144,9 +143,9 @@ if ($cgi->param('update')) {
                 trick_taint($subject) if $subject;
                 trick_taint($body)    if $body;
 
-                if ( ($subject ne $events->{$eventid}->{'subject'})
-                  || ($mailifnobugs != $events->{$eventid}->{'mailifnobugs'})
-                  || ($body    ne $events->{$eventid}->{'body'}) ) {
+                if ( ($subject ne $events->{$eventid}->subject)
+                  || ($mailifnobugs != $events->{$eventid}->mail_if_no_bugs)
+                  || ($body    ne $events->{$eventid}->body) ) {
 
                     $sth = $dbh->prepare("UPDATE whine_events " .
                                          "SET subject=?, body=?, mailifnobugs=? " .
@@ -348,7 +347,6 @@ $events = get_events($userid);
 #
 # build the whine list by event id
 for my $event_id (keys %{$events}) {
-
     $events->{$event_id}->{'schedule'} = [];
     $events->{$event_id}->{'queries'} = [];
 
@@ -405,24 +403,13 @@ $vars->{'local_timezone'} = Bugzilla->local_timezone->short_name_for_datetime(Da
 $template->process("whine/schedule.html.tmpl", $vars)
   || ThrowTemplateError($template->error());
 
-# get_events takes a userid and returns a hash, keyed by event ID, containing
-# the subject and body of each event that user owns
+# get_events takes a userid and returns a hash of
+# Bugzilla::Whine objects keyed by event ID.
 sub get_events {
     my $userid = shift;
-    my $dbh = Bugzilla->dbh;
-    my $events = {};
-
-    my $sth = $dbh->prepare("SELECT DISTINCT id, subject, body, mailifnobugs " .
-                            "FROM whine_events " .
-                            "WHERE owner_userid=?");
-    $sth->execute($userid);
-    while (my ($ev, $sub, $bod, $mno) = $sth->fetchrow_array) {
-        $events->{$ev} = {
-            'subject' => $sub || '',
-            'body' => $bod || '',
-            'mailifnobugs' => $mno || 0,
-        };
-    }
-    return $events;
+    my $event_rows = Bugzilla::Whine->match({ owner_userid => $userid });
+    my %events = map { $_->{id} => $_ } @$event_rows;
+
+    return \%events;
 }