import rt 2.0.14
[freeside.git] / rt / lib / RT / Watcher.pm
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
4
5 =head1 NAME
6
7   RT::Watcher - RT Watcher object
8
9 =head1 SYNOPSIS
10
11   use RT::Watcher;
12
13
14 =head1 DESCRIPTION
15
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.
18
19 =head1 METHODS
20
21 =begin testing
22
23 ok(require RT::TestHarness);
24 ok(require RT::Watcher);
25
26 =end testing
27
28 =cut
29
30 package RT::Watcher;
31 use RT::Record;
32 @ISA= qw(RT::Record);
33
34
35 # {{{ sub _Init 
36
37 sub _Init {
38   my $self = shift;
39   
40   $self->{'table'} = "Watchers";
41   return ($self->SUPER::_Init(@_));
42
43 }
44 # }}}
45
46 # {{{ sub Create 
47
48 =head2 Create PARAMHASH
49
50 Create a new watcher object with the following Attributes:
51
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. 
58
59 =cut
60
61 sub Create  {
62     my $self = shift;
63     my %args = (
64                 Owner => undef,
65                 Email => undef,
66                 Value => undef,
67                 Scope => undef,
68                 Type => undef,
69                 Quiet => 0,
70                 @_ # get the real argumentlist
71                );
72     
73     #Do we have someone this applies to?
74     unless (($args{'Owner'} =~ /^(\d+)$/) || ($args{'Email'} =~ /\@/)) {
75         return (0, "No user or email address specified");
76     }
77     
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'});
82         if ($User->id) {
83             $args{'Owner'} = $User->id;
84             delete $args{'Email'};
85         }
86     }
87     
88     
89     if ($args{'Type'} eq "Requestor" and $args{'Owner'} == 0) {
90         # Requestors *MUST* have an account
91         
92         my $Address = RT::CanonicalizeAddress($args{'Email'});
93         
94         my $NewUser = RT::User->new($RT::SystemUser);
95         my ($Val, $Message) =
96           $NewUser->Create(Name => $Address,
97                            EmailAddress => $Address,
98                            RealName => $Address,
99                            Password => undef,
100                            Privileged => 0,
101                            Comments => 'Autocreated on ticket submission'
102                           );
103         return (0, "Could not create watcher for requestor")
104           unless $Val;
105         if ($NewUser->id) {
106             $args{'Owner'} = $NewUser->id;
107             delete $args{'Email'};
108         }
109     }
110     
111     
112     
113     
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);
118
119     my $id = $self->SUPER::Create(%args);
120     if ($id) {
121         return (1,"Interest noted");
122     }
123     else {
124         return (0, "Error adding watcher");
125     }
126 }
127 # }}}
128
129 # {{{ sub Load 
130
131 =head2 Load ID
132   
133   Loads a watcher by the primary key of the watchers table ($Watcher->id)
134   
135 =cut
136
137 sub Load  {
138     my $self = shift;
139     my $identifier = shift;
140     
141     if ($identifier !~ /\D/) {
142         $self->SUPER::LoadById($identifier);
143     }
144     else {
145         return (0, "That's not a numerical id");
146     }
147 }
148
149 # }}}
150
151 # {{{ sub LoadByValue
152
153 =head2 LoadByValue PARAMHASH
154   
155 LoadByValue takes a parameter hash with the following attributes:
156
157   Email, Owner, Scope, Type, Value
158
159 The same rules enforced at create are enforced by Load.
160
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.
163
164 =cut
165
166 sub LoadByValue {
167     my $self = shift;
168     my %args = ( Email => undef, 
169                  Owner => undef,
170                  Scope => undef,
171                  Type => undef,
172                  Value => undef,
173                  @_);
174     
175     #TODO: all this code is being copied from Create. that\'s silly
176     
177     #Do we have someone this applies to?
178     unless (($args{'Owner'} =~ /^(\d*)$/) || ($args{'Email'} =~ /\@/)) {
179         return (0, "No user or email address specified");
180     }
181     
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'});
186         if ($User->id > 0) {
187             $args{'Owner'} = $User->id;
188             delete $args{'Email'};
189         }
190     }
191     
192     if ((defined ($args{'Type'})) and 
193         ($args{'Type'} !~ /^(Requestor|Cc|AdminCc)$/i)) {
194         return (0, "Invalid Type");
195     }
196     if ($args{'Owner'}) {
197         $self->LoadByCols( Type => $args{'Type'},
198                            Value => $args{'Value'},
199                            Owner => $args{'Owner'},
200                            Scope => $args{'Scope'},
201                          );
202     }
203     else {
204         $self->LoadByCols( Type => $args{'Type'},
205                            Email => $args{'Email'},
206                            Value => $args{'Value'},
207                            Scope => $args{'Scope'},
208                          );
209     }   
210     unless ($self->Id) {
211         return(0, "Couldn\'t find that watcher");
212     }
213     return (1, "Watcher loaded");
214 }
215
216 # }}}
217
218 # {{{ sub OwnerObj 
219
220 =head2 OwnerObj
221
222 Return an RT Owner Object for this Watcher, if we have one
223
224 =cut
225
226 sub OwnerObj  {
227     my $self = shift;
228     if (!defined $self->{'OwnerObj'}) {
229         require RT::User;
230         $self->{'OwnerObj'} = RT::User->new($self->CurrentUser);
231         if ($self->Owner) {
232             $self->{'OwnerObj'}->Load($self->Owner);
233         } else {
234             return $RT::Nobody->UserObj;
235         }
236     }
237     return ($self->{'OwnerObj'});
238 }
239 # }}}
240
241 # {{{ sub Email
242
243 =head2 Email
244
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
248 an RT::User object.
249
250 =cut
251
252 sub Email {
253     my $self = shift;
254     
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'));
258     }
259     elsif ($self->Owner) {
260         return ($self->OwnerObj->EmailAddress);
261     }
262     else {
263         return ("Data error");
264     }
265 }
266 # }}}
267   
268 # {{{ sub IsUser
269
270 =head2 IsUser
271
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
275
276 =cut
277
278 sub IsUser {
279     my $self = shift;
280     # if this watcher has an email address glued onto it,
281     # return undef
282
283     if (defined($self->__Value('Email'))) {
284         return undef;
285     }
286     else {
287         return 1;
288     }
289 }
290
291 # }}}
292
293 # {{{ sub _Accessible 
294 sub _Accessible  {
295   my $self = shift;
296   my %Cols = (
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'
307              );
308   return($self->SUPER::_Accessible(@_, %Cols));
309 }
310 # }}}
311
312 1;
313