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
74 use base 'RT::SearchBuilder';
81 Takes a queue id (numerical) as its only argument. Makes sure that
82 Scopes it pulls out apply to this queue (or another that you've selected with
83 another call to this method
91 $self->Limit (ENTRYAGGREGATOR => 'OR',
102 Scopes it pulls out apply to all queues (or another that you've selected with
103 another call to this method or LimitToQueue
111 $self->Limit (ENTRYAGGREGATOR => 'OR',
121 Returns the next scrip that this user can see.
129 my $Scrip = $self->SUPER::Next();
130 if ((defined($Scrip)) and (ref($Scrip))) {
132 if ($Scrip->CurrentUserHasRight('ShowScrips')) {
136 #If the user doesn't have the right to show this scrip
138 return($self->Next());
141 #if there never was any scrip
150 Run through the relevant scrips. Scrips will run in order based on
151 description. (Most common use case is to prepend a number to the description,
152 forcing the scrips to run in ascending alphanumerical order.)
159 my %args = ( TicketObj => undef,
161 Transaction => undef,
162 TransactionObj => undef,
167 $self->Prepare(%args);
174 Commit all of this object's prepared scrips
181 foreach my $scrip (@{$self->Prepared}) {
183 "Committing scrip #". $scrip->id
184 ." on txn #". $self->{'TransactionObj'}->id
185 ." of ticket #". $self->{'TicketObj'}->id
188 $scrip->Commit( TicketObj => $self->{'TicketObj'},
189 TransactionObj => $self->{'TransactionObj'} );
197 Only prepare the scrips, returning an array of the scrips we're interested in
198 in order of preparation, not execution
204 my %args = ( TicketObj => undef,
206 Transaction => undef,
207 TransactionObj => undef,
212 #We're really going to need a non-acled ticket for the scrips to work
213 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
214 Ticket => $args{'Ticket'},
215 TransactionObj => $args{'TransactionObj'},
216 Transaction => $args{'Transaction'} );
219 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
222 #Iterate through each script and check it's applicability.
223 while ( my $scrip = $self->Next() ) {
225 unless ( $scrip->IsApplicable(
226 TicketObj => $self->{'TicketObj'},
227 TransactionObj => $self->{'TransactionObj'}
229 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it isn't applicable");
233 #If it's applicable, prepare and commit it
234 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
235 TransactionObj => $self->{'TransactionObj'}
237 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it didn't Prepare");
240 push @{$self->{'prepared_scrips'}}, $scrip;
244 return (@{$self->Prepared});
250 Returns an arrayref of the scrips this object has prepared
257 return ($self->{'prepared_scrips'} || []);
260 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
262 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
263 relevant scrips. (Also to figure out which scrips apply)
270 sub _SetupSourceObjects {
276 Transaction => undef,
277 TransactionObj => undef,
281 if ( $args{'TicketObj'} ) {
282 # This loads a clean copy of the Ticket object to ensure that we
283 # don't accidentally escalate the privileges of the passed in
284 # ticket (this function can be invoked from the UI).
285 # We copy the TransactionBatch transactions so that Scrips
286 # running against the new Ticket will have access to them. We
287 # use RanTransactionBatch to guard against running
288 # TransactionBatch Scrips more than once.
289 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
290 $self->{'TicketObj'}->Load( $args{'TicketObj'}->Id );
291 if ( $args{'TicketObj'}->TransactionBatch ) {
292 # try to ensure that we won't infinite loop if something dies, triggering DESTROY while
293 # we have the _TransactionBatch objects;
294 $self->{'TicketObj'}->RanTransactionBatch(1);
295 $self->{'TicketObj'}->{'_TransactionBatch'} = $args{'TicketObj'}->{'_TransactionBatch'};
299 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
300 $self->{'TicketObj'}->Load( $args{'Ticket'} )
301 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}");
304 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
305 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
308 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
309 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
310 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}");
318 Find only the apropriate scrips for whatever we're doing now. Order them
319 by their description. (Most common use case is to prepend a number to the
320 description, forcing the scrips to display and run in ascending alphanumerical
333 $self->LimitToQueue( $self->{'TicketObj'}->QueueObj->Id )
334 ; #Limit it to $Ticket->QueueObj->Id
335 $self->LimitToGlobal();
338 $self->Limit( FIELD => "Stage", VALUE => $args{'Stage'} );
340 my $ConditionsAlias = $self->NewAlias('ScripConditions');
344 FIELD1 => 'ScripCondition',
345 ALIAS2 => $ConditionsAlias,
349 #We only want things where the scrip applies to this sort of transaction
350 # TransactionBatch stage can define list of transaction
351 foreach( split /\s*,\s*/, ($args{'Type'} || '') ) {
353 ALIAS => $ConditionsAlias,
354 FIELD => 'ApplicableTransTypes',
357 ENTRYAGGREGATOR => 'OR',
361 # Or where the scrip applies to any transaction
363 ALIAS => $ConditionsAlias,
364 FIELD => 'ApplicableTransTypes',
367 ENTRYAGGREGATOR => 'OR',
370 # Promise some kind of ordering
371 $self->OrderBy( FIELD => 'Description' );
373 # we call Count below, but later we always do search
374 # so just do search and get count from results
375 $self->_DoSearch if $self->{'must_redo_search'};
378 "Found ". $self->Count ." scrips for $args{'Stage'} stage"
379 ." with applicable type(s) $args{'Type'}"
380 ." for txn #".$self->{TransactionObj}->Id
381 ." on ticket #".$self->{TicketObj}->Id
390 Returns an empty new RT::Scrip item
396 return(RT::Scrip->new($self->CurrentUser));
398 RT::Base->_ImportOverlays();