import rt 3.4.6
[freeside.git] / rt / lib / RT / ScripAction_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
35
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
44
45 # END BPS TAGGED BLOCK }}}
46
47 =head1 NAME
48
49   RT::ScripAction - RT Action object
50
51 =head1 SYNOPSIS
52
53   use RT::ScripAction;
54
55
56 =head1 DESCRIPTION
57
58 This module should never be called directly by client code. it's an internal module which
59 should only be accessed through exported APIs in other modules.
60
61
62 =begin testing
63
64 ok (require RT::ScripAction);
65
66 =end testing
67
68 =head1 METHODS
69
70 =cut
71
72
73 package RT::ScripAction;
74
75 use strict;
76 no warnings qw(redefine);
77 use RT::Template;
78
79 # {{{ sub _Accessible 
80 sub _Accessible  {
81     my $self = shift;
82     my %Cols = ( Name  => 'read',
83                  Description => 'read',
84                  ExecModule  => 'read',
85                  Argument  => 'read',
86                  Creator => 'read/auto',
87                  Created => 'read/auto',
88                  LastUpdatedBy => 'read/auto',
89                  LastUpdated => 'read/auto'
90        );
91     return($self->SUPER::_Accessible(@_, %Cols));
92 }
93 # }}}
94
95 # {{{ sub Create 
96
97 =head2 Create
98
99 Takes a hash. Creates a new Action entry.  should be better
100 documented.
101
102 =cut
103
104 sub Create  {
105     my $self = shift;
106     #TODO check these args and do smart things.
107     return($self->SUPER::Create(@_));
108 }
109 # }}}
110
111 # {{{ sub Delete 
112 sub Delete  {
113     my $self = shift;
114     
115     return (0, "ScripAction->Delete not implemented");
116 }
117 # }}}
118
119 # {{{ sub Load 
120
121 =head2 Load IDENTIFIER
122
123 Loads an action by its Name.
124
125 Returns: Id, Error Message
126
127 =cut
128
129 sub Load  {
130     my $self = shift;
131     my $identifier = shift;
132     
133     if (!$identifier) {
134         return (0, $self->loc('Input error'));
135     }       
136     
137     if ($identifier !~ /\D/) {
138         $self->SUPER::Load($identifier);
139     }
140     else {
141         $self->LoadByCol('Name', $identifier);
142         
143     }
144
145     if (@_) {
146         # Set the template Id to the passed in template    
147         my $template = shift;
148         
149         $self->{'Template'} = $template;
150     }
151     return ($self->Id, ($self->loc('[_1] ScripAction loaded', $self->Id)));
152 }
153 # }}}
154
155 # {{{ sub LoadAction 
156
157 =head2 LoadAction HASH
158
159   Takes a hash consisting of TicketObj and TransactionObj.  Loads an RT::Action:: module.
160
161 =cut
162
163 sub LoadAction  {
164     my $self = shift;
165     my %args = ( TransactionObj => undef,
166                  TicketObj => undef,
167                  @_ );
168
169     $self->{_TicketObj} = $args{TicketObj};
170     
171     #TODO: Put this in an eval  
172     $self->ExecModule =~ /^(\w+)$/;
173     my $module = $1;
174     my $type = "RT::Action::". $module;
175  
176     eval "require $type" || die "Require of $type failed.\n$@\n";
177     
178     $self->{'Action'}  = $type->new ( Argument => $self->Argument,
179                                       CurrentUser => $self->CurrentUser,
180                                       ScripActionObj => $self, 
181                                       ScripObj => $args{'ScripObj'},
182                                       TemplateObj => $self->TemplateObj,
183                                       TicketObj => $args{'TicketObj'},
184                                       TransactionObj => $args{'TransactionObj'},
185                                     );
186 }
187 # }}}
188
189 # {{{ sub TemplateObj
190
191 =head2 TemplateObj
192
193 Return this action's template object
194
195 TODO: Why are we not using the Scrip's template object?
196
197
198 =cut
199
200 sub TemplateObj {
201     my $self = shift;
202     return undef unless $self->{Template};
203     if ( !$self->{'TemplateObj'} ) {
204         $self->{'TemplateObj'} = RT::Template->new( $self->CurrentUser );
205         $self->{'TemplateObj'}->LoadById( $self->{'Template'} );
206
207         if ( ( $self->{'TemplateObj'}->__Value('Queue') == 0 )
208             && $self->{'_TicketObj'} ) {
209             my $tmptemplate = RT::Template->new( $self->CurrentUser );
210             my ( $ok, $err ) = $tmptemplate->LoadQueueTemplate(
211                 Queue => $self->{'_TicketObj'}->QueueObj->id,
212                 Name  => $self->{'TemplateObj'}->Name);
213
214             if ( $tmptemplate->id ) {
215                 # found the queue-specific template with the same name
216                 $self->{'TemplateObj'} = $tmptemplate;
217             }
218         }
219
220     }
221
222     return ( $self->{'TemplateObj'} );
223 }
224 # }}}
225
226 # The following methods call the action object
227
228 # {{{ sub Prepare 
229
230 sub Prepare  {
231     my $self = shift;
232     $self->{_Message_ID} = 0;
233     return ($self->Action->Prepare());
234   
235 }
236 # }}}
237
238 # {{{ sub Commit 
239 sub Commit  {
240     my $self = shift;
241     return($self->Action->Commit());
242     
243     
244 }
245 # }}}
246
247 # {{{ sub Describe 
248 sub Describe  {
249     my $self = shift;
250     return ($self->Action->Describe());
251     
252 }
253 # }}}
254
255 =head2 Action
256
257 Return the actual RT::Action object for this scrip.
258
259 =cut
260
261 sub Action {
262     my $self = shift;
263     return ($self->{'Action'});
264 }
265
266 # {{{ sub DESTROY
267 sub DESTROY {
268     my $self=shift;
269     $self->{'_TicketObj'} = undef;
270     $self->{'Action'} = undef;
271     $self->{'TemplateObj'} = undef;
272 }
273 # }}}
274
275 =head2 TODO
276
277 Between this, RT::Scrip and RT::Action::*, we need to be able to get rid of a 
278 class. This just reeks of too much complexity -- jesse
279
280 =cut
281
282 1;
283
284