1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 # <jesse@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/copyleft/gpl.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 }}}
50 RT::Scrips - a collection of RT Scrip objects
64 ok (require RT::Scrips);
74 no warnings qw(redefine);
76 # {{{ sub LimitToQueue
80 Takes a queue id (numerical) as its only argument. Makes sure that
81 Scopes it pulls out apply to this queue (or another that you've selected with
82 another call to this method
90 $self->Limit (ENTRYAGGREGATOR => 'OR',
98 # {{{ sub LimitToGlobal
103 Scopes it pulls out apply to all queues (or another that you've selected with
104 another call to this method or LimitToQueue
112 $self->Limit (ENTRYAGGREGATOR => 'OR',
123 return(new RT::Scrip($self->CurrentUser));
131 Returns the next scrip that this user can see.
139 my $Scrip = $self->SUPER::Next();
140 if ((defined($Scrip)) and (ref($Scrip))) {
142 if ($Scrip->CurrentUserHasRight('ShowScrips')) {
146 #If the user doesn't have the right to show this scrip
148 return($self->Next());
151 #if there never was any scrip
161 Run through the relevant scrips. Scrips will run in order based on
162 description. (Most common use case is to prepend a number to the description,
163 forcing the scrips to run in ascending alphanumerical order.)
170 my %args = ( TicketObj => undef,
172 Transaction => undef,
173 TransactionObj => undef,
178 $self->Prepare(%args);
185 Commit all of this object's prepared scrips
193 foreach my $scrip (@{$self->Prepared}) {
195 $scrip->Commit( TicketObj => $self->{'TicketObj'},
196 TransactionObj => $self->{'TransactionObj'} );
203 Only prepare the scrips, returning an array of the scrips we're interested in
204 in order of preparation, not execution
210 my %args = ( TicketObj => undef,
212 Transaction => undef,
213 TransactionObj => undef,
218 #We're really going to need a non-acled ticket for the scrips to work
219 $self->_SetupSourceObjects( TicketObj => $args{'TicketObj'},
220 Ticket => $args{'Ticket'},
221 TransactionObj => $args{'TransactionObj'},
222 Transaction => $args{'Transaction'} );
225 $self->_FindScrips( Stage => $args{'Stage'}, Type => $args{'Type'} );
228 #Iterate through each script and check it's applicability.
229 while ( my $scrip = $self->Next() ) {
232 unless ( $scrip->IsApplicable(
233 TicketObj => $self->{'TicketObj'},
234 TransactionObj => $self->{'TransactionObj'}
237 #If it's applicable, prepare and commit it
239 unless ( $scrip->Prepare( TicketObj => $self->{'TicketObj'},
240 TransactionObj => $self->{'TransactionObj'}
242 push @{$self->{'prepared_scrips'}}, $scrip;
246 return (@{$self->Prepared});
252 Returns an arrayref of the scrips this object has prepared
259 return ($self->{'prepared_scrips'} || []);
263 # {{{ sup _SetupSourceObjects
265 =head2 _SetupSourceObjects { TicketObj , Ticket, Transaction, TransactionObj }
267 Setup a ticket and transaction for this Scrip collection to work with as it runs through the
268 relevant scrips. (Also to figure out which scrips apply)
275 sub _SetupSourceObjects {
281 Transaction => undef,
282 TransactionObj => undef,
285 if ( ( $self->{'TicketObj'} = $args{'TicketObj'} ) ) {
286 $self->{'TicketObj'}->CurrentUser( $self->CurrentUser );
289 $self->{'TicketObj'} = RT::Ticket->new( $self->CurrentUser );
290 $self->{'TicketObj'}->Load( $args{'Ticket'} )
291 || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}\n");
294 if ( ( $self->{'TransactionObj'} = $args{'TransactionObj'} ) ) {
295 $self->{'TransactionObj'}->CurrentUser( $self->CurrentUser );
298 $self->{'TransactionObj'} = RT::Transaction->new( $self->CurrentUser );
299 $self->{'TransactionObj'}->Load( $args{'Transaction'} )
300 || $RT::Logger->err( "$self couldn't load transaction $args{'Transaction'}\n");
306 # {{{ sub _FindScrips;
310 Find only the apropriate scrips for whatever we're doing now. Order them
311 by their description. (Most common use case is to prepend a number to the
312 description, forcing the scrips to display and run in ascending alphanumerical
325 $self->LimitToQueue( $self->{'TicketObj'}->QueueObj->Id )
326 ; #Limit it to $Ticket->QueueObj->Id
327 $self->LimitToGlobal();
330 $self->Limit( FIELD => "Stage", VALUE => $args{'Stage'} );
332 my $ConditionsAlias = $self->NewAlias('ScripConditions');
336 FIELD1 => 'ScripCondition',
337 ALIAS2 => $ConditionsAlias,
341 #We only want things where the scrip applies to this sort of transaction
342 # TransactionBatch stage can define list of transaction
343 foreach( split /\s*,\s*/, ($args{'Type'} || '') ) {
345 ALIAS => $ConditionsAlias,
346 FIELD => 'ApplicableTransTypes',
349 ENTRYAGGREGATOR => 'OR',
353 # Or where the scrip applies to any transaction
355 ALIAS => $ConditionsAlias,
356 FIELD => 'ApplicableTransTypes',
359 ENTRYAGGREGATOR => 'OR',
362 # Promise some kind of ordering
363 $self->OrderBy( FIELD => 'description' );
365 $RT::Logger->debug("Found ".$self->Count. " scrips");