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