import of rt 3.0.9
[freeside.git] / rt / lib / RT / Base.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 package RT::Base;
25 use Carp;
26 use Scalar::Util;
27
28 use strict;
29 use vars qw(@EXPORT);
30
31 @EXPORT=qw(loc CurrentUser);
32
33 =head1 FUNCTIONS
34
35
36
37 # {{{ sub CurrentUser 
38
39 =head2 CurrentUser
40
41 If called with an argument, sets the current user to that user object.
42 This will affect ACL decisions, etc.  
43 Returns the current user
44
45 =cut
46
47 sub CurrentUser {
48     my $self = shift;
49
50     if (@_) {
51         $self->{'original_user'} = $self->{'user'};
52         $self->{'user'} = shift;
53         Scalar::Util::weaken($self->{'user'}) if (ref($self->{'user'}) &&
54                                                     $self->{'user'} == $self );
55     }
56
57     unless ( ref( $self->{'user'}) ) {
58         $RT::Logger->err( "$self was created without a CurrentUser\n" . Carp::cluck() );
59         return (0);
60         die;
61     }
62     return ( $self->{'user'} );
63 }
64
65 # }}}
66
67 sub OriginalUser {
68     my $self = shift;
69
70     if (@_) {
71         $self->{'original_user'} = shift;
72         Scalar::Util::weaken($self->{'original_user'})
73             if (ref($self->{'original_user'}) && $self->{'original_user'} == $self );
74     }
75     return ( $self->{'original_user'} || $self->{'user'} );
76 }
77
78
79 =item loc LOC_STRING
80
81 l is a method which takes a loc string
82 to this object's CurrentUser->LanguageHandle for localization. 
83
84 you call it like this:
85
86     $self->loc("I have [quant,_1,concrete mixer].", 6);
87
88 In english, this would return:
89     I have 6 concrete mixers.
90
91
92 =cut
93
94 sub loc {
95     my $self = shift;
96     if (my $user = $self->OriginalUser) {
97         return $user->loc(@_);
98     }
99     else {
100         use Carp;
101         Carp::confess("No currentuser");
102         return ("Critical error:$self has no CurrentUser", $self);
103     }
104 }
105
106 eval "require RT::Base_Vendor";
107 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Base_Vendor.pm});
108 eval "require RT::Base_Local";
109 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Base_Local.pm});
110
111
112 1;