import rt 3.8.7
[freeside.git] / rt / lib / RT / ScripAction_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 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/licenses/old-licenses/gpl-2.0.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
49 =head1 NAME
50
51   RT::ScripAction - RT Action object
52
53 =head1 SYNOPSIS
54
55   use RT::ScripAction;
56
57
58 =head1 DESCRIPTION
59
60 This module should never be called directly by client code. it's an internal module which
61 should only be accessed through exported APIs in other modules.
62
63
64
65 =head1 METHODS
66
67 =cut
68
69
70 package RT::ScripAction;
71
72 use strict;
73 no warnings qw(redefine);
74 use RT::Template;
75
76 # {{{ sub _Accessible 
77 sub _Accessible  {
78     my $self = shift;
79     my %Cols = ( Name  => 'read',
80                  Description => 'read',
81                  ExecModule  => 'read',
82                  Argument  => 'read',
83                  Creator => 'read/auto',
84                  Created => 'read/auto',
85                  LastUpdatedBy => 'read/auto',
86                  LastUpdated => 'read/auto'
87        );
88     return($self->SUPER::_Accessible(@_, %Cols));
89 }
90 # }}}
91
92 # {{{ sub Create 
93
94 =head2 Create
95
96 Takes a hash. Creates a new Action entry.  should be better
97 documented.
98
99 =cut
100
101 sub Create  {
102     my $self = shift;
103     #TODO check these args and do smart things.
104     return($self->SUPER::Create(@_));
105 }
106 # }}}
107
108 # {{{ sub Delete 
109 sub Delete  {
110     my $self = shift;
111     
112     return (0, "ScripAction->Delete not implemented");
113 }
114 # }}}
115
116 # {{{ sub Load 
117
118 =head2 Load IDENTIFIER
119
120 Loads an action by its Name.
121
122 Returns: Id, Error Message
123
124 =cut
125
126 sub Load  {
127     my $self = shift;
128     my $identifier = shift;
129     
130     if (!$identifier) {
131         return (0, $self->loc('Input error'));
132     }       
133     
134     if ($identifier !~ /\D/) {
135         $self->SUPER::Load($identifier);
136     }
137     else {
138         $self->LoadByCol('Name', $identifier);
139         
140     }
141
142     if (@_) {
143         # Set the template Id to the passed in template    
144         my $template = shift;
145         
146         $self->{'Template'} = $template;
147     }
148     return ($self->Id, ($self->loc('[_1] ScripAction loaded', $self->Id)));
149 }
150 # }}}
151
152 # {{{ sub LoadAction 
153
154 =head2 LoadAction HASH
155
156   Takes a hash consisting of TicketObj and TransactionObj.  Loads an RT::Action:: module.
157
158 =cut
159
160 sub LoadAction  {
161     my $self = shift;
162     my %args = ( TransactionObj => undef,
163                  TicketObj => undef,
164                  @_ );
165
166     $self->{_TicketObj} = $args{TicketObj};
167     
168     #TODO: Put this in an eval  
169     $self->ExecModule =~ /^(\w+)$/;
170     my $module = $1;
171     my $type = "RT::Action::". $module;
172  
173     eval "require $type" || die "Require of $type failed.\n$@\n";
174     
175     $self->{'Action'}  = $type->new ( Argument => $self->Argument,
176                                       CurrentUser => $self->CurrentUser,
177                                       ScripActionObj => $self, 
178                                       ScripObj => $args{'ScripObj'},
179                                       TemplateObj => $self->TemplateObj,
180                                       TicketObj => $args{'TicketObj'},
181                                       TransactionObj => $args{'TransactionObj'},
182                                     );
183 }
184 # }}}
185
186 # {{{ sub TemplateObj
187
188 =head2 TemplateObj
189
190 Return this action's template object
191
192 TODO: Why are we not using the Scrip's template object?
193
194
195 =cut
196
197 sub TemplateObj {
198     my $self = shift;
199     return undef unless $self->{Template};
200     if ( !$self->{'TemplateObj'} ) {
201         $self->{'TemplateObj'} = RT::Template->new( $self->CurrentUser );
202         $self->{'TemplateObj'}->LoadById( $self->{'Template'} );
203
204         if ( ( $self->{'TemplateObj'}->__Value('Queue') == 0 )
205             && $self->{'_TicketObj'} ) {
206             my $tmptemplate = RT::Template->new( $self->CurrentUser );
207             my ( $ok, $err ) = $tmptemplate->LoadQueueTemplate(
208                 Queue => $self->{'_TicketObj'}->QueueObj->id,
209                 Name  => $self->{'TemplateObj'}->Name);
210
211             if ( $tmptemplate->id ) {
212                 # found the queue-specific template with the same name
213                 $self->{'TemplateObj'} = $tmptemplate;
214             }
215         }
216
217     }
218
219     return ( $self->{'TemplateObj'} );
220 }
221 # }}}
222
223 # The following methods call the action object
224
225 # {{{ sub Prepare 
226
227 sub Prepare  {
228     my $self = shift;
229     $self->{_Message_ID} = 0;
230     return ($self->Action->Prepare());
231   
232 }
233 # }}}
234
235 # {{{ sub Commit 
236 sub Commit  {
237     my $self = shift;
238     return($self->Action->Commit());
239     
240     
241 }
242 # }}}
243
244 # {{{ sub Describe 
245 sub Describe  {
246     my $self = shift;
247     return ($self->Action->Describe());
248     
249 }
250 # }}}
251
252 =head2 Action
253
254 Return the actual RT::Action object for this scrip.
255
256 =cut
257
258 sub Action {
259     my $self = shift;
260     return ($self->{'Action'});
261 }
262
263 # {{{ sub DESTROY
264 sub DESTROY {
265     my $self=shift;
266     $self->{'_TicketObj'} = undef;
267     $self->{'Action'} = undef;
268     $self->{'TemplateObj'} = undef;
269 }
270 # }}}
271
272 =head2 TODO
273
274 Between this, RT::Scrip and RT::Action::*, we need to be able to get rid of a 
275 class. This just reeks of too much complexity -- jesse
276
277 =cut
278
279 1;
280
281