1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
51 RT::Scrips - a collection of RT Scrip objects
72 use base 'RT::SearchBuilder';
82 $self->{'with_disabled_column'} = 1;
84 return ( $self->SUPER::_Init(@_) );
89 Takes a queue id (numerical) as its only argument. Makes sure that
90 Scopes it pulls out apply to this queue (or another that you've selected with
91 another call to this method
98 return unless defined $queue;
100 my $alias = RT::ObjectScrips->new( $self->CurrentUser )
101 ->JoinTargetToThis( $self );
113 Scopes it pulls out apply to all queues (or another that you've selected with
114 another call to this method or LimitToQueue
121 return $self->LimitToQueue(0);
126 return RT::ObjectScrips->new( $self->CurrentUser )
127 ->LimitTargetToAdded( $self => @_ );
130 sub LimitToNotAdded {
132 return RT::ObjectScrips->new( $self->CurrentUser )
133 ->LimitTargetToNotAdded( $self => @_ );
138 my %args = @_%2? (Stage => @_) : @_;
139 return unless defined $args{'Stage'};
141 my $alias = RT::ObjectScrips->new( $self->CurrentUser )
142 ->JoinTargetToThis( $self, %args );
146 VALUE => $args{'Stage'},
150 =head2 LimitByTemplate
152 Takes a L<RT::Template> object and limits scrips to those that
157 sub LimitByTemplate {
159 my $template = shift;
161 $self->Limit( FIELD => 'Template', VALUE => $template->Name );
163 if ( $template->Queue ) {
164 # if template is local then we are interested in global and
165 # queue specific scrips
166 $self->LimitToQueue( $template->Queue );
167 $self->LimitToGlobal;
169 else { # template is global
171 # if every queue has a custom version then there
172 # is no scrip that uses the template
174 my $queues = RT::Queues->new( RT->SystemUser );
175 my $alias = $queues->Join(
179 TABLE2 => 'Templates',
186 VALUE => $template->Name,
194 return $self->Limit( FIELD => 'id', VALUE => 0 )
195 unless $queues->Count;
198 # otherwise it's either a global scrip or application to
199 # a queue with custom version of the template.
200 my $os_alias = RT::ObjectScrips->new( $self->CurrentUser )
201 ->JoinTargetToThis( $self );
202 my $tmpl_alias = $self->Join(
205 FIELD1 => 'ObjectId',
206 TABLE2 => 'Templates',
210 LEFTJOIN => $tmpl_alias, ALIAS => $tmpl_alias, FIELD => 'Name', VALUE => $template->Name,
213 LEFTJOIN => $tmpl_alias, ALIAS => $tmpl_alias, FIELD => 'Queue', OPERATOR => '!=', VALUE => 0,
216 $self->_OpenParen('UsedBy');
217 $self->Limit( SUBCLAUSE => 'UsedBy', ALIAS => $os_alias, FIELD => 'ObjectId', VALUE => 0 );
219 SUBCLAUSE => 'UsedBy',
220 ALIAS => $tmpl_alias,
225 $self->_CloseParen('UsedBy');
231 my $order = shift || 'ASC';
232 $self->OrderByCols( {
233 ALIAS => RT::ObjectScrips->new( $self->CurrentUser )
234 ->JoinTargetToThis( $self => @_ )
236 FIELD => 'SortOrder',
243 Overrides the collection to ensure that only scrips the user can see are
252 return unless $record->CurrentUserHasRight('ShowScrips');
253 return $self->SUPER::AddRecord( $record );
258 Run through the relevant scrips. Scrips will run in order based on
259 description. (Most common use case is to prepend a number to the description,
260 forcing the scrips to run in ascending alphanumerical order.)
267 my %args = ( TicketObj => undef,
269 Transaction => undef,
270 TransactionObj => undef,
275 $self->Prepare(%args);
282 Commit all of this object's prepared scrips
289 foreach my $scrip (@{$self->Prepared}) {
291 "Committing scrip #". $scrip->id
292 ." on txn #". $self->{'TransactionObj'}->id
293 ." of ticket #". $self->{'TicketObj'}->id
296 $scrip->Commit( TicketObj => $self->{'TicketObj'},
297 TransactionObj => $self->{'TransactionObj'} );
305 Only prepare the scrips, returning an array of the scrips we're interested in
306 in order of preparation, not execution
312 my %args = ( TicketObj => undef,
314 Transaction => undef,
315 TransactionObj => undef,
320 #We're really going to need a non-acled ticket for the scrips to work
321 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
322 Ticket => $args{'Ticket'},
323 TransactionObj => $args{'TransactionObj'},
324 Transaction => $args{'Transaction'} );
327 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
330 #Iterate through each script and check it's applicability.
331 while ( my $scrip = $self->Next() ) {
333 unless ( $scrip->IsApplicable(
334 TicketObj => $self->{'TicketObj'},
335 TransactionObj => $self->{'TransactionObj'}
337 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it isn't applicable");
341 #If it's applicable, prepare and commit it
342 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
343 TransactionObj => $self->{'TransactionObj'}
345 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it didn't Prepare");
348 push @{$self->{'prepared_scrips'}}, $scrip;
352 return (@{$self->Prepared});
358 Returns an arrayref of the scrips this object has prepared
365 return ($self->{'prepared_scrips'} || []);
368 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
370 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
371 relevant scrips. (Also to figure out which scrips apply)
378 sub _SetupSourceObjects {
384 Transaction => undef,
385 TransactionObj => undef,
389 if ( $args{'TicketObj'} ) {
390 # This loads a clean copy of the Ticket object to ensure that we
391 # don't accidentally escalate the privileges of the passed in
392 # ticket (this function can be invoked from the UI).
393 # We copy the TransactionBatch transactions so that Scrips
394 # running against the new Ticket will have access to them. We
395 # use RanTransactionBatch to guard against running
396 # TransactionBatch Scrips more than once.
397 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
398 $self->{'TicketObj'}->Load( $args{'TicketObj'}->Id );
399 if ( $args{'TicketObj'}->TransactionBatch ) {
400 # try to ensure that we won't infinite loop if something dies, triggering DESTROY while
401 # we have the _TransactionBatch objects;
402 $self->{'TicketObj'}->RanTransactionBatch(1);
403 $self->{'TicketObj'}->{'_TransactionBatch'} = $args{'TicketObj'}->{'_TransactionBatch'};
407 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
408 $self->{'TicketObj'}->Load( $args{'Ticket'} )
409 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}");
412 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
413 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
416 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
417 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
418 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}");
426 Find only the appropriate scrips for whatever we're doing now. Order
427 them by the SortOrder field from the ObjectScrips table.
439 $self->LimitToQueue( $self->{'TicketObj'}->QueueObj->Id );
440 $self->LimitToGlobal;
441 $self->LimitByStage( $args{'Stage'} );
443 my $ConditionsAlias = $self->Join(
445 FIELD1 => 'ScripCondition',
446 TABLE2 => 'ScripConditions',
450 #We only want things where the scrip applies to this sort of transaction
451 # TransactionBatch stage can define list of transaction
452 foreach( split /\s*,\s*/, ($args{'Type'} || '') ) {
454 ALIAS => $ConditionsAlias,
455 FIELD => 'ApplicableTransTypes',
458 ENTRYAGGREGATOR => 'OR',
462 # Or where the scrip applies to any transaction
464 ALIAS => $ConditionsAlias,
465 FIELD => 'ApplicableTransTypes',
468 ENTRYAGGREGATOR => 'OR',
471 $self->ApplySortOrder;
473 # we call Count below, but later we always do search
474 # so just do search and get count from results
475 $self->_DoSearch if $self->{'must_redo_search'};
478 "Found ". $self->Count ." scrips for $args{'Stage'} stage"
479 ." with applicable type(s) $args{'Type'}"
480 ." for txn #".$self->{TransactionObj}->Id
481 ." on ticket #".$self->{TicketObj}->Id
485 RT::Base->_ImportOverlays();