1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Attic/Watcher.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2 # (c) 1996-2001 Jesse Vincent <jesse@fsck.com>
3 # This software is redistributable under the terms of the GNU GPL
7 RT::Watcher - RT Watcher object
16 This module should never be called directly by client code. it\'s an internal module which
17 should only be accessed through exported APIs in Ticket, Queue and other similar objects.
23 ok(require RT::TestHarness);
24 ok(require RT::Watcher);
40 $self->{'table'} = "Watchers";
41 return ($self->SUPER::_Init(@_));
48 =head2 Create PARAMHASH
50 Create a new watcher object with the following Attributes:
52 Scope: Ticket or Queue
53 Value: Ticket or queue id
54 Type: Requestor, Cc or AdminCc. Requestor is not supported for a scope of \'Queue\'
55 Email: The email address of the watcher. If the email address maps to an RT User, this is resolved
56 to an Owner object instead.
57 Owner: The RT user id of the \'owner\' of this watcher object.
70 @_ # get the real argumentlist
73 #Do we have someone this applies to?
74 unless (($args{'Owner'} =~ /^(\d+)$/) || ($args{'Email'} =~ /\@/)) {
75 return (0, "No user or email address specified");
78 #if we only have an email address, try to resolve it to an owner
79 if ($args{'Owner'} == 0) {
80 my $User = new RT::User($RT::SystemUser);
81 $User->LoadByEmail($args{'Email'});
83 $args{'Owner'} = $User->id;
84 delete $args{'Email'};
89 if ($args{'Type'} eq "Requestor" and $args{'Owner'} == 0) {
90 # Requestors *MUST* have an account
92 my $Address = RT::CanonicalizeAddress($args{'Email'});
94 my $NewUser = RT::User->new($RT::SystemUser);
96 $NewUser->Create(Name => $Address,
97 EmailAddress => $Address,
101 Comments => 'Autocreated on ticket submission'
103 return (0, "Could not create watcher for requestor")
106 $args{'Owner'} = $NewUser->id;
107 delete $args{'Email'};
114 #Make sure we\'ve got a valid type
115 #TODO --- move this to ValidateType
116 return (0, "Invalid Type")
117 unless ($args{'Type'} =~ /^(Requestor|Cc|AdminCc)$/i);
119 my $id = $self->SUPER::Create(%args);
121 return (1,"Interest noted");
124 return (0, "Error adding watcher");
133 Loads a watcher by the primary key of the watchers table ($Watcher->id)
139 my $identifier = shift;
141 if ($identifier !~ /\D/) {
142 $self->SUPER::LoadById($identifier);
145 return (0, "That's not a numerical id");
151 # {{{ sub LoadByValue
153 =head2 LoadByValue PARAMHASH
155 LoadByValue takes a parameter hash with the following attributes:
157 Email, Owner, Scope, Type, Value
159 The same rules enforced at create are enforced by Load.
161 Returns a tuple of (retval, msg). Retval is 1 on success and 0 on failure.
162 msg describes what happened in a human readable form.
168 my %args = ( Email => undef,
175 #TODO: all this code is being copied from Create. that\'s silly
177 #Do we have someone this applies to?
178 unless (($args{'Owner'} =~ /^(\d*)$/) || ($args{'Email'} =~ /\@/)) {
179 return (0, "No user or email address specified");
182 #if we only have an email address, try to resolve it to an owner
183 unless ($args{'Owner'}) {
184 my $User = new RT::User($RT::SystemUser);
185 $User->LoadByEmail($args{'Email'});
187 $args{'Owner'} = $User->id;
188 delete $args{'Email'};
192 if ((defined ($args{'Type'})) and
193 ($args{'Type'} !~ /^(Requestor|Cc|AdminCc)$/i)) {
194 return (0, "Invalid Type");
196 if ($args{'Owner'}) {
197 $self->LoadByCols( Type => $args{'Type'},
198 Value => $args{'Value'},
199 Owner => $args{'Owner'},
200 Scope => $args{'Scope'},
204 $self->LoadByCols( Type => $args{'Type'},
205 Email => $args{'Email'},
206 Value => $args{'Value'},
207 Scope => $args{'Scope'},
211 return(0, "Couldn\'t find that watcher");
213 return (1, "Watcher loaded");
222 Return an RT Owner Object for this Watcher, if we have one
228 if (!defined $self->{'OwnerObj'}) {
230 $self->{'OwnerObj'} = RT::User->new($self->CurrentUser);
232 $self->{'OwnerObj'}->Load($self->Owner);
234 return $RT::Nobody->UserObj;
237 return ($self->{'OwnerObj'});
245 This custom data accessor does the right thing and returns
246 the 'Email' attribute of this Watcher object. If that's undefined,
247 it returns the 'EmailAddress' attribute of its 'Owner' object, which is
255 # IF Email is defined, return that. Otherwise, return the Owner's email address
256 if (defined($self->__Value('Email'))) {
257 return ($self->__Value('Email'));
259 elsif ($self->Owner) {
260 return ($self->OwnerObj->EmailAddress);
263 return ("Data error");
272 Returns true if this watcher object is tied to a user object. (IE it
273 isn't sending to some other email address).
274 Otherwise, returns undef
280 # if this watcher has an email address glued onto it,
283 if (defined($self->__Value('Email'))) {
293 # {{{ sub _Accessible
297 Email => 'read/write',
298 Scope => 'read/write',
299 Value => 'read/write',
300 Type => 'read/write',
301 Quiet => 'read/write',
302 Owner => 'read/write',
303 Creator => 'read/auto',
304 Created => 'read/auto',
305 LastUpdatedBy => 'read/auto',
306 LastUpdated => 'read/auto'
308 return($self->SUPER::_Accessible(@_, %Cols));