rt 4.0.23
[freeside.git] / rt / lib / RT / ScripAction.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@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 use warnings;
74
75 use base 'RT::Record';
76
77 sub Table {'ScripActions'}
78
79
80
81 use RT::Template;
82
83 sub _Accessible  {
84     my $self = shift;
85     my %Cols = ( Name  => 'read',
86                  Description => 'read',
87                  ExecModule  => 'read',
88                  Argument  => 'read',
89                  Creator => 'read/auto',
90                  Created => 'read/auto',
91                  LastUpdatedBy => 'read/auto',
92                  LastUpdated => 'read/auto'
93        );
94     return($self->SUPER::_Accessible(@_, %Cols));
95 }
96
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 sub Delete  {
112     my $self = shift;
113     
114     return (0, "ScripAction->Delete not implemented");
115 }
116
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     my ($ok, $msg);
135     if ($identifier !~ /\D/) {
136         ($ok, $msg) = $self->SUPER::Load($identifier);
137     }
138     else {
139         ($ok, $msg) = $self->LoadByCol('Name', $identifier);
140         
141     }
142
143     if (@_) {
144         # Set the template Id to the passed in template    
145         my $template = shift;
146         
147         $self->{'Template'} = $template;
148     }
149
150     return ($ok, $msg);
151 }
152
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 =head2 TemplateObj
187
188 Return this action's template object
189
190 TODO: Why are we not using the Scrip's template object?
191
192
193 =cut
194
195 sub TemplateObj {
196     my $self = shift;
197     return undef unless $self->{Template};
198     if ( !$self->{'TemplateObj'} ) {
199         $self->{'TemplateObj'} = RT::Template->new( $self->CurrentUser );
200         $self->{'TemplateObj'}->LoadById( $self->{'Template'} );
201
202         if ( ( $self->{'TemplateObj'}->__Value('Queue') == 0 )
203             && $self->{'_TicketObj'} ) {
204             my $tmptemplate = RT::Template->new( $self->CurrentUser );
205             my ( $ok, $err ) = $tmptemplate->LoadQueueTemplate(
206                 Queue => $self->{'_TicketObj'}->QueueObj->id,
207                 Name  => $self->{'TemplateObj'}->Name);
208
209             if ( $tmptemplate->id ) {
210                 # found the queue-specific template with the same name
211                 $self->{'TemplateObj'} = $tmptemplate;
212             }
213         }
214
215     }
216
217     return ( $self->{'TemplateObj'} );
218 }
219
220 # The following methods call the action object
221
222
223 sub Prepare  {
224     my $self = shift;
225     $self->{_Message_ID} = 0;
226     return ($self->Action->Prepare());
227   
228 }
229
230 sub Commit  {
231     my $self = shift;
232     return($self->Action->Commit());
233     
234     
235 }
236
237 sub Describe  {
238     my $self = shift;
239     return ($self->Action->Describe());
240     
241 }
242
243 =head2 Action
244
245 Return the actual RT::Action object for this scrip.
246
247 =cut
248
249 sub Action {
250     my $self = shift;
251     return ($self->{'Action'});
252 }
253
254 sub DESTROY {
255     my $self=shift;
256     $self->{'_TicketObj'} = undef;
257     $self->{'Action'} = undef;
258     $self->{'TemplateObj'} = undef;
259 }
260
261 =head2 TODO
262
263 Between this, RT::Scrip and RT::Action::*, we need to be able to get rid of a 
264 class. This just reeks of too much complexity -- jesse
265
266 =cut
267
268
269
270
271 =head2 id
272
273 Returns the current value of id.
274 (In the database, id is stored as int(11).)
275
276
277 =cut
278
279
280 =head2 Name
281
282 Returns the current value of Name.
283 (In the database, Name is stored as varchar(200).)
284
285
286
287 =head2 SetName VALUE
288
289
290 Set Name to VALUE.
291 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
292 (In the database, Name will be stored as a varchar(200).)
293
294
295 =cut
296
297
298 =head2 Description
299
300 Returns the current value of Description.
301 (In the database, Description is stored as varchar(255).)
302
303
304
305 =head2 SetDescription VALUE
306
307
308 Set Description to VALUE.
309 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
310 (In the database, Description will be stored as a varchar(255).)
311
312
313 =cut
314
315
316 =head2 ExecModule
317
318 Returns the current value of ExecModule.
319 (In the database, ExecModule is stored as varchar(60).)
320
321
322
323 =head2 SetExecModule VALUE
324
325
326 Set ExecModule to VALUE.
327 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
328 (In the database, ExecModule will be stored as a varchar(60).)
329
330
331 =cut
332
333
334 =head2 Argument
335
336 Returns the current value of Argument.
337 (In the database, Argument is stored as varbinary(255).)
338
339
340
341 =head2 SetArgument VALUE
342
343
344 Set Argument to VALUE.
345 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
346 (In the database, Argument will be stored as a varbinary(255).)
347
348
349 =cut
350
351
352 =head2 Creator
353
354 Returns the current value of Creator.
355 (In the database, Creator is stored as int(11).)
356
357
358 =cut
359
360
361 =head2 Created
362
363 Returns the current value of Created.
364 (In the database, Created is stored as datetime.)
365
366
367 =cut
368
369
370 =head2 LastUpdatedBy
371
372 Returns the current value of LastUpdatedBy.
373 (In the database, LastUpdatedBy is stored as int(11).)
374
375
376 =cut
377
378
379 =head2 LastUpdated
380
381 Returns the current value of LastUpdated.
382 (In the database, LastUpdated is stored as datetime.)
383
384
385 =cut
386
387
388
389 sub _CoreAccessible {
390     {
391
392         id =>
393                 {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
394         Name =>
395                 {read => 1, write => 1, sql_type => 12, length => 200,  is_blob => 0,  is_numeric => 0,  type => 'varchar(200)', default => ''},
396         Description =>
397                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
398         ExecModule =>
399                 {read => 1, write => 1, sql_type => 12, length => 60,  is_blob => 0,  is_numeric => 0,  type => 'varchar(60)', default => ''},
400         Argument =>
401                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varbinary(255)', default => ''},
402         Creator =>
403                 {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
404         Created =>
405                 {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
406         LastUpdatedBy =>
407                 {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
408         LastUpdated =>
409                 {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
410
411  }
412 };
413
414 RT::Base->_ImportOverlays();
415
416 1;