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