rt 4.2.13 ticket#13852
[freeside.git] / rt / lib / RT / ScripAction.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2016 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 DESCRIPTION
54
55 This module should never be called directly by client code. it's an
56 internal module which should only be accessed through exported APIs
57 in other modules.
58
59 =cut
60
61
62 package RT::ScripAction;
63
64 use strict;
65 use warnings;
66
67 use base 'RT::Record';
68
69
70 sub Table {'ScripActions'}
71
72 use RT::Template;
73
74 sub _Accessible  {
75     my $self = shift;
76     my %Cols = (
77         Name  => 'read',
78         Description => 'read',
79         ExecModule  => 'read',
80         Argument  => 'read',
81         Creator => 'read/auto',
82         Created => 'read/auto',
83         LastUpdatedBy => 'read/auto',
84         LastUpdated => 'read/auto'
85     );
86     return($self->SUPER::_Accessible(@_, %Cols));
87 }
88
89
90 =head1 METHODS
91
92 =head2 Create
93
94 Takes a hash. Creates a new Action entry.
95
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 sub Delete  {
105     my $self = shift;
106     return (0, "ScripAction->Delete not implemented");
107 }
108
109
110 =head2 Load IDENTIFIER
111
112 Loads an action by its Name.
113
114 Returns: Id, Error Message
115
116 =cut
117
118 sub Load  {
119     my $self = shift;
120     my $identifier = shift;
121
122     if (!$identifier) {
123         return wantarray ? (0, $self->loc('Input error')) : 0;
124     }
125
126     my ($ok, $msg);
127     if ($identifier !~ /\D/) {
128         ($ok, $msg) = $self->SUPER::Load($identifier);
129     }
130     else {
131         ($ok, $msg) = $self->LoadByCol('Name', $identifier);
132     }
133
134     if (@_) {
135         RT->Deprecated(
136             Arguments => "Template as second argument",
137             Remove    => "4.4",
138         );
139         $self->{'Template'} = shift;
140     }
141
142     return wantarray ? ($ok, $msg) : $ok;
143 }
144
145
146 =head2 LoadAction HASH
147
148 Takes a hash consisting of TicketObj and TransactionObj.  Loads an RT::Action:: module.
149
150 =cut
151
152 sub LoadAction  {
153     my $self = shift;
154     my %args = (
155         TransactionObj => undef,
156         TicketObj => undef,
157         ScripObj => undef,
158         @_
159     );
160
161     # XXX: this whole block goes with TemplateObj method
162     unless ( @_ && exists $args{'TemplateObj'} ) {
163         local $self->{_TicketObj} = $args{TicketObj};
164         $args{'TemplateObj'} = $self->TemplateObj;
165     }
166     else {
167         $self->{'TemplateObj'} = $args{'TemplateObj'};
168     }
169
170     $self->ExecModule =~ /^(\w+)$/;
171     my $module = $1;
172     my $type = "RT::Action::". $module;
173
174     $type->require or die "Require of $type action module failed.\n$@\n";
175
176     return $self->{'Action'} = $type->new(
177         %args,
178         Argument       => $self->Argument,
179         CurrentUser    => $self->CurrentUser,
180         ScripActionObj => $self,
181     );
182 }
183
184
185 =head2 TemplateObj
186
187 Return this action's template object.  Deprecated.
188
189 =cut
190
191 sub TemplateObj {
192     my $self = shift;
193     RT->Deprecated(
194         Remove => "4.4",
195     );
196
197     if ( !$self->{'TemplateObj'} ) {
198         return undef unless $self->{Template};
199         $self->{'TemplateObj'} = RT::Template->new( $self->CurrentUser );
200         $self->{'TemplateObj'}->Load( $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 sub Prepare  {
221     my $self = shift;
222     $self->{_Message_ID} = 0;
223     return $self->Action->Prepare( @_ );
224 }
225
226 sub Commit  {
227     my $self = shift;
228     return $self->Action->Commit( @_ );
229 }
230
231 sub Describe  {
232     my $self = shift;
233     return $self->Action->Describe( @_ );
234 }
235
236 =head2 Action
237
238 Return the actual RT::Action object for this scrip.
239
240 =cut
241
242 sub Action {
243     my $self = shift;
244     return $self->{'Action'};
245 }
246
247 =head2 id
248
249 Returns the current value of id.
250 (In the database, id is stored as int(11).)
251
252
253 =head2 Name
254
255 Returns the current value of Name.
256 (In the database, Name is stored as varchar(200).)
257
258 =head2 SetName VALUE
259
260 Set Name to VALUE.
261 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
262 (In the database, Name will be stored as a varchar(200).)
263
264
265 =head2 Description
266
267 Returns the current value of Description.
268 (In the database, Description is stored as varchar(255).)
269
270 =head2 SetDescription VALUE
271
272 Set Description to VALUE.
273 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
274 (In the database, Description will be stored as a varchar(255).)
275
276
277 =head2 ExecModule
278
279 Returns the current value of ExecModule.
280 (In the database, ExecModule is stored as varchar(60).)
281
282 =head2 SetExecModule VALUE
283
284 Set ExecModule to VALUE.
285 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
286 (In the database, ExecModule will be stored as a varchar(60).)
287
288
289 =head2 Argument
290
291 Returns the current value of Argument.
292 (In the database, Argument is stored as varbinary(255).)
293
294 =head2 SetArgument VALUE
295
296 Set Argument to VALUE.
297 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
298 (In the database, Argument will be stored as a varbinary(255).)
299
300
301 =head2 Creator
302
303 Returns the current value of Creator.
304 (In the database, Creator is stored as int(11).)
305
306 =head2 Created
307
308 Returns the current value of Created.
309 (In the database, Created is stored as datetime.)
310
311 =head2 LastUpdatedBy
312
313 Returns the current value of LastUpdatedBy.
314 (In the database, LastUpdatedBy is stored as int(11).)
315
316 =head2 LastUpdated
317
318 Returns the current value of LastUpdated.
319 (In the database, LastUpdated is stored as datetime.)
320
321 =cut
322
323
324 sub _CoreAccessible {
325     {
326
327         id =>
328                 {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
329         Name =>
330                 {read => 1, write => 1, sql_type => 12, length => 200,  is_blob => 0,  is_numeric => 0,  type => 'varchar(200)', default => ''},
331         Description =>
332                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
333         ExecModule =>
334                 {read => 1, write => 1, sql_type => 12, length => 60,  is_blob => 0,  is_numeric => 0,  type => 'varchar(60)', default => ''},
335         Argument =>
336                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varbinary(255)', default => ''},
337         Creator =>
338                 {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
339         Created =>
340                 {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
341         LastUpdatedBy =>
342                 {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
343         LastUpdated =>
344                 {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
345
346  }
347 };
348
349 sub PreInflate {
350     my $class = shift;
351     my ($importer, $uid, $data) = @_;
352
353     $class->SUPER::PreInflate( $importer, $uid, $data );
354
355     return not $importer->SkipBy( "Name", $class, $uid, $data );
356 }
357
358 sub __DependsOn {
359     my $self = shift;
360     my %args = (
361         Shredder => undef,
362         Dependencies => undef,
363         @_,
364     );
365     my $deps = $args{'Dependencies'};
366
367 # Scrips
368     my $objs = RT::Scrips->new( $self->CurrentUser );
369     $objs->Limit( FIELD => 'ScripAction', VALUE => $self->Id );
370     $deps->_PushDependencies(
371         BaseObject => $self,
372         Flags => RT::Shredder::Constants::DEPENDS_ON,
373         TargetObjects => $objs,
374         Shredder => $args{'Shredder'}
375     );
376
377     return $self->SUPER::__DependsOn( %args );
378 }
379
380 RT::Base->_ImportOverlays();
381
382 1;