import of rt 3.0.9
[freeside.git] / rt / lib / RT / ScripAction_Overlay.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::ScripAction - RT Action object
27
28 =head1 SYNOPSIS
29
30   use RT::ScripAction;
31
32
33 =head1 DESCRIPTION
34
35 This module should never be called directly by client code. it's an internal module which
36 should only be accessed through exported APIs in other modules.
37
38
39 =begin testing
40
41 ok (require RT::ScripAction);
42
43 =end testing
44
45 =head1 METHODS
46
47 =cut
48
49 use strict;
50 no warnings qw(redefine);
51 use RT::Template;
52
53 # {{{  sub _Init 
54 sub _Init  {
55     my $self = shift; 
56     $self->{'table'} = "ScripActions";
57     return ($self->SUPER::_Init(@_));
58 }
59 # }}}
60
61 # {{{ sub _Accessible 
62 sub _Accessible  {
63     my $self = shift;
64     my %Cols = ( Name  => 'read',
65                  Description => 'read',
66                  ExecModule  => 'read',
67                  Argument  => 'read',
68                  Creator => 'read/auto',
69                  Created => 'read/auto',
70                  LastUpdatedBy => 'read/auto',
71                  LastUpdated => 'read/auto'
72        );
73     return($self->SUPER::_Accessible(@_, %Cols));
74 }
75 # }}}
76
77 # {{{ sub Create 
78 =head2 Create
79   
80  Takes a hash. Creates a new Action entry.
81  should be better documented.
82 =cut
83
84 sub Create  {
85     my $self = shift;
86     #TODO check these args and do smart things.
87     return($self->SUPER::Create(@_));
88 }
89 # }}}
90
91 # {{{ sub Delete 
92 sub Delete  {
93     my $self = shift;
94     
95     return (0, "ScripAction->Delete not implemented");
96 }
97 # }}}
98
99 # {{{ sub Load 
100 sub Load  {
101     my $self = shift;
102     my $identifier = shift;
103     
104     if (!$identifier) {
105         return (0, $self->loc('Input error'));
106     }       
107     
108     if ($identifier !~ /\D/) {
109         $self->SUPER::Load($identifier);
110     }
111     else {
112         $self->LoadByCol('Name', $identifier);
113         
114     }
115
116     if (@_) {
117         # Set the template Id to the passed in template    
118         my $template = shift;
119         
120         $self->{'Template'} = $template;
121     }
122     return ($self->loc('[_1] ScripAction loaded', $self->Id));
123 }
124 # }}}
125
126 # {{{ sub LoadAction 
127
128 =head2 LoadAction HASH
129
130   Takes a hash consisting of TicketObj and TransactionObj.  Loads an RT::Action:: module.
131
132 =cut
133
134 sub LoadAction  {
135     my $self = shift;
136     my %args = ( TransactionObj => undef,
137                  TicketObj => undef,
138                  @_ );
139
140     $self->{_TicketObj} = $args{TicketObj};
141     
142     #TODO: Put this in an eval  
143     $self->ExecModule =~ /^(\w+)$/;
144     my $module = $1;
145     my $type = "RT::Action::". $module;
146  
147     eval "require $type" || die "Require of $type failed.\n$@\n";
148     
149     $self->{'Action'}  = $type->new ( 'ScripActionObj' => $self, 
150                                       'TicketObj' => $args{'TicketObj'},
151                                       'ScripObj' => $args{'ScripObj'},
152                                       'TransactionObj' => $args{'TransactionObj'},
153                                       'TemplateObj' => $self->TemplateObj,
154                                       'Argument' => $self->Argument,
155                                     );
156 }
157 # }}}
158
159 # {{{ sub TemplateObj
160
161 =head2 TemplateObj
162
163 Return this action\'s template object
164
165 =cut
166
167 sub TemplateObj {
168     my $self = shift;
169     return undef unless $self->{Template};
170     if ( !$self->{'TemplateObj'} ) {
171         $self->{'TemplateObj'} = RT::Template->new( $self->CurrentUser );
172         $self->{'TemplateObj'}->LoadById( $self->{'Template'} );
173
174         if ( ( $self->{'TemplateObj'}->__Value('Queue') == 0 )
175             && $self->{'_TicketObj'} ) {
176             my $tmptemplate = RT::Template->new( $self->CurrentUser );
177             my ( $ok, $err ) = $tmptemplate->LoadQueueTemplate(
178                 Queue => $self->{'_TicketObj'}->QueueObj->id,
179                 Name  => $self->{'TemplateObj'}->Name);
180
181             if ( $tmptemplate->id ) {
182                 # found the queue-specific template with the same name
183                 $self->{'TemplateObj'} = $tmptemplate;
184             }
185         }
186
187     }
188
189     return ( $self->{'TemplateObj'} );
190 }
191 # }}}
192
193 # The following methods call the action object
194
195 # {{{ sub Prepare 
196
197 sub Prepare  {
198     my $self = shift;
199     return ($self->{'Action'}->Prepare());
200   
201 }
202 # }}}
203
204 # {{{ sub Commit 
205 sub Commit  {
206     my $self = shift;
207     return($self->{'Action'}->Commit());
208     
209     
210 }
211 # }}}
212
213 # {{{ sub Describe 
214 sub Describe  {
215     my $self = shift;
216     return ($self->{'Action'}->Describe());
217     
218 }
219 # }}}
220
221 # {{{ sub DESTROY
222 sub DESTROY {
223     my $self=shift;
224     $self->{'_TicketObj'} = undef;
225     $self->{'Action'} = undef;
226     $self->{'TemplateObj'} = undef;
227 }
228 # }}}
229
230
231 1;
232
233