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
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',
119 Overrides the collection to ensure that only scrips the user can see are
128 return unless $record->CurrentUserHasRight('ShowScrips');
129 return $self->SUPER::AddRecord( $record );
134 Run through the relevant scrips. Scrips will run in order based on
135 description. (Most common use case is to prepend a number to the description,
136 forcing the scrips to run in ascending alphanumerical order.)
143 my %args = ( TicketObj => undef,
145 Transaction => undef,
146 TransactionObj => undef,
151 $self->Prepare(%args);
158 Commit all of this object's prepared scrips
165 foreach my $scrip (@{$self->Prepared}) {
167 "Committing scrip #". $scrip->id
168 ." on txn #". $self->{'TransactionObj'}->id
169 ." of ticket #". $self->{'TicketObj'}->id
172 $scrip->Commit( TicketObj => $self->{'TicketObj'},
173 TransactionObj => $self->{'TransactionObj'} );
181 Only prepare the scrips, returning an array of the scrips we're interested in
182 in order of preparation, not execution
188 my %args = ( TicketObj => undef,
190 Transaction => undef,
191 TransactionObj => undef,
196 #We're really going to need a non-acled ticket for the scrips to work
197 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
198 Ticket => $args{'Ticket'},
199 TransactionObj => $args{'TransactionObj'},
200 Transaction => $args{'Transaction'} );
203 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
206 #Iterate through each script and check it's applicability.
207 while ( my $scrip = $self->Next() ) {
209 unless ( $scrip->IsApplicable(
210 TicketObj => $self->{'TicketObj'},
211 TransactionObj => $self->{'TransactionObj'}
213 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it isn't applicable");
217 #If it's applicable, prepare and commit it
218 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
219 TransactionObj => $self->{'TransactionObj'}
221 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it didn't Prepare");
224 push @{$self->{'prepared_scrips'}}, $scrip;
228 return (@{$self->Prepared});
234 Returns an arrayref of the scrips this object has prepared
241 return ($self->{'prepared_scrips'} || []);
244 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
246 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
247 relevant scrips. (Also to figure out which scrips apply)
254 sub _SetupSourceObjects {
260 Transaction => undef,
261 TransactionObj => undef,
265 if ( $args{'TicketObj'} ) {
266 # This loads a clean copy of the Ticket object to ensure that we
267 # don't accidentally escalate the privileges of the passed in
268 # ticket (this function can be invoked from the UI).
269 # We copy the TransactionBatch transactions so that Scrips
270 # running against the new Ticket will have access to them. We
271 # use RanTransactionBatch to guard against running
272 # TransactionBatch Scrips more than once.
273 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
274 $self->{'TicketObj'}->Load( $args{'TicketObj'}->Id );
275 if ( $args{'TicketObj'}->TransactionBatch ) {
276 # try to ensure that we won't infinite loop if something dies, triggering DESTROY while
277 # we have the _TransactionBatch objects;
278 $self->{'TicketObj'}->RanTransactionBatch(1);
279 $self->{'TicketObj'}->{'_TransactionBatch'} = $args{'TicketObj'}->{'_TransactionBatch'};
283 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
284 $self->{'TicketObj'}->Load( $args{'Ticket'} )
285 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}");
288 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
289 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
292 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
293 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
294 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}");
302 Find only the apropriate scrips for whatever we're doing now. Order them
303 by their description. (Most common use case is to prepend a number to the
304 description, forcing the scrips to display and run in ascending alphanumerical
317 $self->LimitToQueue( $self->{'TicketObj'}->QueueObj->Id )
318 ; #Limit it to $Ticket->QueueObj->Id
319 $self->LimitToGlobal();
322 $self->Limit( FIELD => "Stage", VALUE => $args{'Stage'} );
324 my $ConditionsAlias = $self->NewAlias('ScripConditions');
328 FIELD1 => 'ScripCondition',
329 ALIAS2 => $ConditionsAlias,
333 #We only want things where the scrip applies to this sort of transaction
334 # TransactionBatch stage can define list of transaction
335 foreach( split /\s*,\s*/, ($args{'Type'} || '') ) {
337 ALIAS => $ConditionsAlias,
338 FIELD => 'ApplicableTransTypes',
341 ENTRYAGGREGATOR => 'OR',
345 # Or where the scrip applies to any transaction
347 ALIAS => $ConditionsAlias,
348 FIELD => 'ApplicableTransTypes',
351 ENTRYAGGREGATOR => 'OR',
354 # Promise some kind of ordering
355 $self->OrderBy( FIELD => 'Description' );
357 # we call Count below, but later we always do search
358 # so just do search and get count from results
359 $self->_DoSearch if $self->{'must_redo_search'};
362 "Found ". $self->Count ." scrips for $args{'Stage'} stage"
363 ." with applicable type(s) $args{'Type'}"
364 ." for txn #".$self->{TransactionObj}->Id
365 ." on ticket #".$self->{TicketObj}->Id
374 Returns an empty new RT::Scrip item
380 return(RT::Scrip->new($self->CurrentUser));
382 RT::Base->_ImportOverlays();