1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2013 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',
245 Returns the next scrip that this user can see.
253 my $Scrip = $self->SUPER::Next();
254 if ((defined($Scrip)) and (ref($Scrip))) {
256 if ($Scrip->CurrentUserHasRight('ShowScrips')) {
260 #If the user doesn't have the right to show this scrip
262 return($self->Next());
265 #if there never was any scrip
274 Run through the relevant scrips. Scrips will run in order based on
275 description. (Most common use case is to prepend a number to the description,
276 forcing the scrips to run in ascending alphanumerical order.)
283 my %args = ( TicketObj => undef,
285 Transaction => undef,
286 TransactionObj => undef,
291 $self->Prepare(%args);
298 Commit all of this object's prepared scrips
305 foreach my $scrip (@{$self->Prepared}) {
307 "Committing scrip #". $scrip->id
308 ." on txn #". $self->{'TransactionObj'}->id
309 ." of ticket #". $self->{'TicketObj'}->id
312 $scrip->Commit( TicketObj => $self->{'TicketObj'},
313 TransactionObj => $self->{'TransactionObj'} );
321 Only prepare the scrips, returning an array of the scrips we're interested in
322 in order of preparation, not execution
328 my %args = ( TicketObj => undef,
330 Transaction => undef,
331 TransactionObj => undef,
336 #We're really going to need a non-acled ticket for the scrips to work
337 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
338 Ticket => $args{'Ticket'},
339 TransactionObj => $args{'TransactionObj'},
340 Transaction => $args{'Transaction'} );
343 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
346 #Iterate through each script and check it's applicability.
347 while ( my $scrip = $self->Next() ) {
349 unless ( $scrip->IsApplicable(
350 TicketObj => $self->{'TicketObj'},
351 TransactionObj => $self->{'TransactionObj'}
353 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it isn't applicable");
357 #If it's applicable, prepare and commit it
358 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
359 TransactionObj => $self->{'TransactionObj'}
361 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it didn't Prepare");
364 push @{$self->{'prepared_scrips'}}, $scrip;
368 return (@{$self->Prepared});
374 Returns an arrayref of the scrips this object has prepared
381 return ($self->{'prepared_scrips'} || []);
384 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
386 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
387 relevant scrips. (Also to figure out which scrips apply)
394 sub _SetupSourceObjects {
400 Transaction => undef,
401 TransactionObj => undef,
405 if ( $args{'TicketObj'} ) {
406 # This loads a clean copy of the Ticket object to ensure that we
407 # don't accidentally escalate the privileges of the passed in
408 # ticket (this function can be invoked from the UI).
409 # We copy the TransactionBatch transactions so that Scrips
410 # running against the new Ticket will have access to them. We
411 # use RanTransactionBatch to guard against running
412 # TransactionBatch Scrips more than once.
413 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
414 $self->{'TicketObj'}->Load( $args{'TicketObj'}->Id );
415 if ( $args{'TicketObj'}->TransactionBatch ) {
416 # try to ensure that we won't infinite loop if something dies, triggering DESTROY while
417 # we have the _TransactionBatch objects;
418 $self->{'TicketObj'}->RanTransactionBatch(1);
419 $self->{'TicketObj'}->{'_TransactionBatch'} = $args{'TicketObj'}->{'_TransactionBatch'};
423 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
424 $self->{'TicketObj'}->Load( $args{'Ticket'} )
425 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}");
428 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
429 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
432 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
433 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
434 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}");
442 Find only the apropriate scrips for whatever we're doing now. Order them
443 by their description. (Most common use case is to prepend a number to the
444 description, forcing the scrips to display and run in ascending alphanumerical
457 $self->LimitToQueue( $self->{'TicketObj'}->QueueObj->Id );
458 $self->LimitToGlobal;
459 $self->LimitByStage( $args{'Stage'} );
461 my $ConditionsAlias = $self->Join(
463 FIELD1 => 'ScripCondition',
464 TABLE2 => 'ScripConditions',
468 #We only want things where the scrip applies to this sort of transaction
469 # TransactionBatch stage can define list of transaction
470 foreach( split /\s*,\s*/, ($args{'Type'} || '') ) {
472 ALIAS => $ConditionsAlias,
473 FIELD => 'ApplicableTransTypes',
476 ENTRYAGGREGATOR => 'OR',
480 # Or where the scrip applies to any transaction
482 ALIAS => $ConditionsAlias,
483 FIELD => 'ApplicableTransTypes',
486 ENTRYAGGREGATOR => 'OR',
489 $self->ApplySortOrder;
491 # we call Count below, but later we always do search
492 # so just do search and get count from results
493 $self->_DoSearch if $self->{'must_redo_search'};
496 "Found ". $self->Count ." scrips for $args{'Stage'} stage"
497 ." with applicable type(s) $args{'Type'}"
498 ." for txn #".$self->{TransactionObj}->Id
499 ." on ticket #".$self->{TicketObj}->Id
503 RT::Base->_ImportOverlays();