import of rt 3.0.9
[freeside.git] / rt / lib / RT / Scrips_Overlay.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::Scrips - a collection of RT Scrip objects
27
28 =head1 SYNOPSIS
29
30   use RT::Scrips;
31
32 =head1 DESCRIPTION
33
34
35 =head1 METHODS
36
37
38 =begin testing
39
40 ok (require RT::Scrips);
41
42 =end testing
43
44 =cut
45
46 use strict;
47 no warnings qw(redefine);
48
49 # {{{ sub LimitToQueue 
50
51 =head2 LimitToQueue
52
53 Takes a queue id (numerical) as its only argument. Makes sure that 
54 Scopes it pulls out apply to this queue (or another that you've selected with
55 another call to this method
56
57 =cut
58
59 sub LimitToQueue  {
60    my $self = shift;
61   my $queue = shift;
62  
63   $self->Limit (ENTRYAGGREGATOR => 'OR',
64                 FIELD => 'Queue',
65                 VALUE => "$queue")
66       if defined $queue;
67   
68 }
69 # }}}
70
71 # {{{ sub LimitToGlobal
72
73 =head2 LimitToGlobal
74
75 Makes sure that 
76 Scopes it pulls out apply to all queues (or another that you've selected with
77 another call to this method or LimitToQueue
78
79 =cut
80
81
82 sub LimitToGlobal  {
83    my $self = shift;
84  
85   $self->Limit (ENTRYAGGREGATOR => 'OR',
86                 FIELD => 'Queue',
87                 VALUE => 0);
88   
89 }
90 # }}}
91
92 # {{{ sub NewItem 
93 sub NewItem  {
94   my $self = shift;
95   
96   return(new RT::Scrip($self->CurrentUser));
97 }
98 # }}}
99
100 # {{{ sub Next 
101
102 =head2 Next
103
104 Returns the next scrip that this user can see.
105
106 =cut
107   
108 sub Next {
109     my $self = shift;
110     
111     
112     my $Scrip = $self->SUPER::Next();
113     if ((defined($Scrip)) and (ref($Scrip))) {
114
115         if ($Scrip->CurrentUserHasRight('ShowScrips')) {
116             return($Scrip);
117         }
118         
119         #If the user doesn't have the right to show this scrip
120         else {  
121             return($self->Next());
122         }
123     }
124     #if there never was any scrip
125     else {
126         return(undef);
127     }   
128     
129 }
130 # }}}
131
132 sub Apply {
133     my ($self, %args) = @_;
134
135     #We're really going to need a non-acled ticket for the scrips to work
136     my ($TicketObj, $TransactionObj);
137
138     if ( ($TicketObj = $args{'TicketObj'}) ) {
139         $TicketObj->CurrentUser($self->CurrentUser);
140     }
141     else {
142         $TicketObj = RT::Ticket->new($self->CurrentUser);
143         $TicketObj->Load( $args{'Ticket'} )
144             || $RT::Logger->err("$self couldn't load ticket $args{'Ticket'}\n");
145     }
146
147     if ( ($TransactionObj = $args{'TransactionObj'}) ) {
148         $TransactionObj->CurrentUser($self->CurrentUser);
149     }
150     else {
151         $TransactionObj = RT::Transaction->new($self->CurrentUser);
152         $TransactionObj->Load( $args{'Transaction'} )
153             || $RT::Logger->err("$self couldn't load transaction $args{'Transaction'}\n");
154     }
155
156     # {{{ Deal with Scrips
157
158     $self->LimitToQueue( $TicketObj->QueueObj->Id )
159         ;                                  #Limit it to  $Ticket->QueueObj->Id
160     $self->LimitToGlobal()
161         unless $TicketObj->QueueObj->Disabled;    # or to "global"
162
163
164     $self->Limit(FIELD => "Stage", VALUE => $args{'Stage'});
165
166
167     my $ConditionsAlias = $self->NewAlias('ScripConditions');
168
169     $self->Join(
170         ALIAS1 => 'main',
171         FIELD1 => 'ScripCondition',
172         ALIAS2 => $ConditionsAlias,
173         FIELD2 => 'id'
174     );
175
176     #We only want things where the scrip applies to this sort of transaction
177     $self->Limit(
178         ALIAS           => $ConditionsAlias,
179         FIELD           => 'ApplicableTransTypes',
180         OPERATOR        => 'LIKE',
181         VALUE           => $args{'Type'},
182         ENTRYAGGREGATOR => 'OR',
183     ) if $args{'Type'};
184
185     # Or where the scrip applies to any transaction
186     $self->Limit(
187         ALIAS           => $ConditionsAlias,
188         FIELD           => 'ApplicableTransTypes',
189         OPERATOR        => 'LIKE',
190         VALUE           => "Any",
191         ENTRYAGGREGATOR => 'OR',
192     );
193
194     #Iterate through each script and check it's applicability.
195     while ( my $Scrip = $self->Next() ) {
196         $Scrip->Apply (TicketObj => $TicketObj,
197                         TransactionObj => $TransactionObj);
198     }
199
200     $TicketObj->CurrentUser( $TicketObj->OriginalUser );
201     $TransactionObj->CurrentUser( $TransactionObj->OriginalUser );
202
203     # }}}
204 }
205
206
207 1;
208