summaryrefslogtreecommitdiff
path: root/rt/lib/RT/Record.pm
blob: 5340f7de4d2253d3f20434253e70dbe841d8d16a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Record.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $

=head1 NAME

  RT::Record - Base class for RT record objects

=head1 SYNOPSIS


=head1 DESCRIPTION


=begin testing

ok (require RT::Record);

=end testing

=head1 METHODS

=cut


package RT::Record;
use DBIx::SearchBuilder::Record::Cachable;
use RT::Date;
use RT::User;

@ISA= qw(DBIx::SearchBuilder::Record::Cachable);

# {{{ sub _Init 

sub _Init  {
  my $self = shift;
  $self->_MyCurrentUser(@_);
  
}

# }}}

# {{{ _PrimaryKeys

=head2 _PrimaryKeys

The primary keys for RT classes is 'id'

=cut

sub _PrimaryKeys {
    my $self = shift;
    return(['id']);
}

# }}}

# {{{ sub _MyCurrentUser 

sub _MyCurrentUser  {
    my $self = shift;
  
    $self->CurrentUser(@_);
    if(!defined($self->CurrentUser)) {
	use Carp;
	Carp::cluck();
	$RT::Logger->err("$self was created without a CurrentUser\n"); 
      return(0);
    }
}

# }}}

# {{{ sub _Handle 
sub _Handle  {
  my $self = shift;
  return($RT::Handle);
}
# }}}

# {{{ sub Create 

sub Create  {
    my $self = shift;
    my $now = new RT::Date($self->CurrentUser);
    $now->Set(Format=> 'unix', Value => time);
    push @_, 'Created', $now->ISO()
      if ($self->_Accessible('Created', 'auto'));
    

    push @_, 'Creator', $self->{'user'}->id
      if $self->_Accessible('Creator', 'auto');
    
    push @_, 'LastUpdated', $now->ISO()
      if ($self->_Accessible('LastUpdated', 'auto'));

    push @_, 'LastUpdatedBy', $self->{'user'}->id
      if $self->_Accessible('LastUpdatedBy', 'auto');
    
    

   my $id = $self->SUPER::Create(@_);
    
    if ($id) {
	$self->Load($id);
    }
    
    return($id);
    
}

# }}}


# {{{ sub LoadByCols

=head2 LoadByCols

Override DBIx::SearchBuilder::LoadByCols to do case-insensitive loads if the 
DB is case sensitive

=cut

sub LoadByCols {
    my $self = shift;
    my %hash = (@_);

    # If this database is case sensitive we need to uncase objects for
    # explicit loading
    if ($self->_Handle->CaseSensitive) {
	 my %newhash;
	 foreach my $key (keys %hash) {
        # If we've been passed an empty value, we can't do the lookup. 
		# We don't need to explicitly downcase integers or an id.
		if ($key =~ '^id$' || $hash{$key} =~/^\d+$/ || !defined ($hash{$key}) ) {
			$newhash{$key} = $hash{$key};
		}
		else {
			$newhash{"lower(".$key.")"} = lc($hash{$key});	
		}
	 }
	$self->SUPER::LoadByCols(%newhash);
    }
    else {
	$self->SUPER::LoadByCols(%hash);
    }
}

# }}}


# {{{ Datehandling

# There is room for optimizations in most of those subs:

# {{{ LastUpdatedObj

sub LastUpdatedObj {
    my $self=shift;
    my $obj = new RT::Date($self->CurrentUser);
    
    $obj->Set(Format => 'sql', Value => $self->LastUpdated);
    return $obj;
}

# }}}

# {{{ CreatedObj

sub CreatedObj {
    my $self=shift;
    my $obj = new RT::Date($self->CurrentUser);
    
    $obj->Set(Format => 'sql', Value => $self->Created);

    
    return $obj;
}

# }}}

# {{{ AgeAsString
#
# TODO: This should be deprecated
#
sub AgeAsString {
    my $self=shift;
    return($self->CreatedObj->AgeAsString());
}
# }}}

# {{{ LastUpdatedAsString

# TODO this should be deprecated

sub LastUpdatedAsString {
    my $self=shift;
    if ($self->LastUpdated) {
	return ($self->LastUpdatedObj->AsString());
	  
    } else {
	return "never";
    }
}

# }}}

# {{{ CreatedAsString
#
# TODO This should be deprecated 
#
sub CreatedAsString {
    my $self = shift;
    return ($self->CreatedObj->AsString());
}
# }}}

# {{{ LongSinceUpdateAsString
#
# TODO This should be deprecated
#
sub LongSinceUpdateAsString {
    my $self=shift;
    if ($self->LastUpdated) {
      
        return ($self->LastUpdatedObj->AgeAsString());
	
    } else {
	return "never";
    }
}
# }}}

# }}} Datehandling


# {{{ sub _Set 
sub _Set  {
  my $self = shift;

  my %args = ( Field => undef,
	       Value => undef,
	       IsSQL => undef,
	       @_ );


  #if the user is trying to modify the record
  if ((!defined ($args{'Field'})) || (!defined ($args{'Value'}))) {
    $args{'Value'} = 0; 
   }

  $self->_SetLastUpdated();
  $self->SUPER::_Set(Field => $args{'Field'},
		     Value => $args{'Value'},
		     IsSQL => $args{'IsSQL'});
  
  
}
# }}}

# {{{ sub _SetLastUpdated

=head2 _SetLastUpdated

This routine updates the LastUpdated and LastUpdatedBy columns of the row in question
It takes no options. Arguably, this is a bug

=cut

sub _SetLastUpdated {
    my $self = shift;
    use RT::Date;
    my $now = new RT::Date($self->CurrentUser);
    $now->SetToNow();

    if ($self->_Accessible('LastUpdated','auto')) {
    	my ($msg, $val) = $self->__Set( Field => 'LastUpdated',
                                        Value => $now->ISO);
    }
    if ($self->_Accessible('LastUpdatedBy','auto')) {
        my ($msg, $val) = $self->__Set( Field => 'LastUpdatedBy', 
				        Value => $self->CurrentUser->id);
    }
}

# }}}

# {{{ sub CreatorObj 

=head2 CreatorObj

Returns an RT::User object with the RT account of the creator of this row

=cut

sub CreatorObj  {
  my $self = shift;
  unless (exists $self->{'CreatorObj'}) {
    
    $self->{'CreatorObj'} = RT::User->new($self->CurrentUser);
    $self->{'CreatorObj'}->Load($self->Creator);
  }
  return($self->{'CreatorObj'});
}
# }}}

# {{{ sub LastUpdatedByObj

=head2 LastUpdatedByObj

  Returns an RT::User object of the last user to touch this object

=cut

sub LastUpdatedByObj {
    my $self=shift;
    unless (exists $self->{LastUpdatedByObj}) {
	$self->{'LastUpdatedByObj'}=RT::User->new($self->CurrentUser);
	$self->{'LastUpdatedByObj'}->Load($self->LastUpdatedBy);
    }
    return $self->{'LastUpdatedByObj'};
}

# }}}

# {{{ sub CurrentUser 

=head2 CurrentUser

If called with an argument, sets the current user to that user object.
This will affect ACL decisions, etc.  
Returns the current user

=cut

sub CurrentUser  {
  my $self = shift;

  if (@_) {
    $self->{'user'} = shift;
  }
  return ($self->{'user'});
}
# }}}


1;