1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 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
70 no warnings qw(redefine);
72 # {{{ sub LimitToQueue
76 Takes a queue id (numerical) as its only argument. Makes sure that
77 Scopes it pulls out apply to this queue (or another that you've selected with
78 another call to this method
86 $self->Limit (ENTRYAGGREGATOR => 'OR',
94 # {{{ sub LimitToGlobal
99 Scopes it pulls out apply to all queues (or another that you've selected with
100 another call to this method or LimitToQueue
108 $self->Limit (ENTRYAGGREGATOR => 'OR',
119 return(new RT::Scrip($self->CurrentUser));
127 Returns the next scrip that this user can see.
135 my $Scrip = $self->SUPER::Next();
136 if ((defined($Scrip)) and (ref($Scrip))) {
138 if ($Scrip->CurrentUserHasRight('ShowScrips')) {
142 #If the user doesn't have the right to show this scrip
144 return($self->Next());
147 #if there never was any scrip
157 Run through the relevant scrips. Scrips will run in order based on
158 description. (Most common use case is to prepend a number to the description,
159 forcing the scrips to run in ascending alphanumerical order.)
166 my %args = ( TicketObj => undef,
168 Transaction => undef,
169 TransactionObj => undef,
174 $self->Prepare(%args);
181 Commit all of this object's prepared scrips
189 foreach my $scrip (@{$self->Prepared}) {
191 "Committing scrip #". $scrip->id
192 ." on txn #". $self->{'TransactionObj'}->id
193 ." of ticket #". $self->{'TicketObj'}->id
196 $scrip->Commit( TicketObj => $self->{'TicketObj'},
197 TransactionObj => $self->{'TransactionObj'} );
204 Only prepare the scrips, returning an array of the scrips we're interested in
205 in order of preparation, not execution
211 my %args = ( TicketObj => undef,
213 Transaction => undef,
214 TransactionObj => undef,
219 #We're really going to need a non-acled ticket for the scrips to work
220 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
221 Ticket => $args{'Ticket'},
222 TransactionObj => $args{'TransactionObj'},
223 Transaction => $args{'Transaction'} );
226 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
229 #Iterate through each script and check it's applicability.
230 while ( my $scrip = $self->Next() ) {
232 unless ( $scrip->IsApplicable(
233 TicketObj => $self->{'TicketObj'},
234 TransactionObj => $self->{'TransactionObj'}
236 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it isn't applicable");
240 #If it's applicable, prepare and commit it
241 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
242 TransactionObj => $self->{'TransactionObj'}
244 $RT::Logger->debug("Skipping Scrip #".$scrip->Id." because it didn't Prepare");
247 push @{$self->{'prepared_scrips'}}, $scrip;
251 return (@{$self->Prepared});
257 Returns an arrayref of the scrips this object has prepared
264 return ($self->{'prepared_scrips'} || []);
268 # {{{ sup _SetupSourceObjects
270 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
272 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
273 relevant scrips. (Also to figure out which scrips apply)
280 sub _SetupSourceObjects {
286 Transaction => undef,
287 TransactionObj => undef,
291 if ( $args{'TicketObj'} ) {
292 # clone the ticket here as we need to change CurrentUser
293 $self->{'TicketObj'} = bless { %{$args{'TicketObj'} } }, 'RT::Ticket';
294 $self->{'TicketObj'}->CurrentUser( $self->CurrentUser );
297 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
298 $self->{'TicketObj'}->Load( $args{'Ticket'} )
299 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}");
302 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
303 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
306 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
307 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
308 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}");
314 # {{{ sub _FindScrips;
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