This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / rt / lib / RT / CustomFields_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
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
16 # from www.gnu.org.
17
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.
22
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
35
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
44
45 # END BPS TAGGED BLOCK }}}
46
47 =head1 NAME
48
49   RT::CustomFields - a collection of RT CustomField objects
50
51 =head1 SYNOPSIS
52
53   use RT::CustomFields;
54
55 =head1 DESCRIPTION
56
57 =head1 METHODS
58
59
60 =begin testing
61
62 ok (require RT::CustomFields);
63
64 =end testing
65
66 =cut
67
68
69 package RT::CustomFields;
70
71 use strict;
72 no warnings qw(redefine);
73 use DBIx::SearchBuilder::Unique;
74
75
76 sub _OCFAlias {
77     my $self = shift;
78     unless ($self->{_sql_ocfalias}) {
79
80         $self->{'_sql_ocfalias'} = $self->NewAlias('ObjectCustomFields');
81     $self->Join( ALIAS1 => 'main',
82                 FIELD1 => 'id',
83                 ALIAS2 => $self->_OCFAlias,
84                 FIELD2 => 'CustomField' );
85     }
86     return($self->{_sql_ocfalias});
87 }
88
89
90 # {{{ sub LimitToGlobalOrQueue 
91
92 =head2 LimitToGlobalOrQueue QUEUEID
93
94 Limits the set of custom fields found to global custom fields or those tied to the queue with ID QUEUEID 
95
96 =cut
97
98 sub LimitToGlobalOrQueue {
99     my $self = shift;
100     my $queue = shift;
101     $self->LimitToGlobalOrObjectId( $queue );
102     $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
103 }
104
105 # }}}
106
107 # {{{ sub LimitToQueue 
108
109 =head2 LimitToQueue QUEUEID
110
111 Takes a queue id (numerical) as its only argument. Makes sure that 
112 Scopes it pulls out apply to this queue (or another that you've selected with
113 another call to this method
114
115 =cut
116
117 sub LimitToQueue  {
118    my $self = shift;
119   my $queue = shift;
120  
121   $self->Limit (ALIAS => $self->_OCFAlias,
122                 ENTRYAGGREGATOR => 'OR',
123                 FIELD => 'ObjectId',
124                 VALUE => "$queue")
125       if defined $queue;
126   $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
127 }
128 # }}}
129
130 # {{{ sub LimitToGlobal
131
132 =head2 LimitToGlobal
133
134 Makes sure that 
135 Scopes it pulls out apply to all queues (or another that you've selected with
136 another call to this method or LimitToQueue
137
138 =cut
139
140
141 sub LimitToGlobal  {
142    my $self = shift;
143  
144   $self->Limit (ALIAS => $self->_OCFAlias,
145                 ENTRYAGGREGATOR => 'OR',
146                 FIELD => 'ObjectId',
147                 VALUE => 0);
148   $self->LimitToLookupType( 'RT::Queue-RT::Ticket' );
149 }
150 # }}}
151
152
153 # {{{ sub _DoSearch 
154
155 =head2 _DoSearch
156
157 A subclass of DBIx::SearchBuilder::_DoSearch that makes sure that 
158  _Disabled rows never get seen unless we're explicitly trying to see 
159 them.
160
161 =cut
162
163 sub _DoSearch {
164     my $self = shift;
165     
166     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
167     unless($self->{'find_disabled_rows'}) {
168         $self->LimitToEnabled();
169     }
170     
171     return($self->SUPER::_DoSearch(@_));
172     
173 }
174
175 # }}}
176
177 # {{{ sub Next 
178
179 =head2 Next
180
181 Returns the next custom field that this user can see.
182
183 =cut
184   
185 sub Next {
186     my $self = shift;
187     
188     
189     my $CF = $self->SUPER::Next();
190     if ((defined($CF)) and (ref($CF))) {
191
192         if ($CF->CurrentUserHasRight('SeeCustomField')) {
193             return($CF);
194         }
195         
196         #If the user doesn't have the right to show this queue
197         else {  
198             return($self->Next());
199         }
200     }
201     #if there never was any queue
202     else {
203         return(undef);
204     }   
205     
206 }
207 # }}}
208
209 sub LimitToLookupType  {
210     my $self = shift;
211     my $lookup = shift;
212  
213     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
214 }
215
216 sub LimitToChildType  {
217     my $self = shift;
218     my $lookup = shift;
219  
220     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
221     $self->Limit( FIELD => 'LookupType', ENDSWITH => "$lookup" );
222 }
223
224 sub LimitToParentType  {
225     my $self = shift;
226     my $lookup = shift;
227  
228     $self->Limit( FIELD => 'LookupType', VALUE => "$lookup" );
229     $self->Limit( FIELD => 'LookupType', STARTSWITH => "$lookup" );
230 }
231
232 sub LimitToGlobalOrObjectId {
233     my $self = shift;
234     my $global_only = 1;
235
236
237     foreach my $id (@_) {
238         $self->Limit( ALIAS           => $self->_OCFAlias,
239                     FIELD           => 'ObjectId',
240                     OPERATOR        => '=',
241                     VALUE           => $id || 0,
242                     ENTRYAGGREGATOR => 'OR' );
243         $global_only = 0 if $id;
244     }
245
246     $self->Limit( ALIAS           => $self->_OCFAlias,
247                  FIELD           => 'ObjectId',
248                  OPERATOR        => '=',
249                  VALUE           => 0,
250                  ENTRYAGGREGATOR => 'OR' ) unless $global_only;
251
252     $self->OrderByCols(
253         { ALIAS => $self->_OCFAlias, FIELD => 'ObjectId' },
254         { ALIAS => $self->_OCFAlias, FIELD => 'SortOrder' },
255     );
256     
257     # This doesn't work on postgres. 
258     #$self->OrderBy( ALIAS => $class_cfs , FIELD => "SortOrder", ORDER => 'ASC');
259
260 }
261   
262 1;
263