import of rt 3.0.4
[freeside.git] / rt / lib / RT / SearchBuilder.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::SearchBuilder - a baseclass for RT collection objects
27
28 =head1 SYNOPSIS
29
30 =head1 DESCRIPTION
31
32
33 =head1 METHODS
34
35
36 =begin testing
37
38 ok (require RT::SearchBuilder);
39
40 =end testing
41
42
43 =cut
44
45 package RT::SearchBuilder;
46
47 use RT::Base;
48 use DBIx::SearchBuilder;
49
50 use strict;
51 use vars qw(@ISA);
52 @ISA = qw(DBIx::SearchBuilder RT::Base);
53
54 # {{{ sub _Init 
55 sub _Init  {
56     my $self = shift;
57     
58     $self->{'user'} = shift;
59     unless(defined($self->CurrentUser)) {
60         use Carp;
61         Carp::confess("$self was created without a CurrentUser");
62         $RT::Logger->err("$self was created without a CurrentUser");
63         return(0);
64     }
65     $self->SUPER::_Init( 'Handle' => $RT::Handle);
66 }
67 # }}}
68
69 # {{{ sub LimitToEnabled
70
71 =head2 LimitToEnabled
72
73 Only find items that haven\'t been disabled
74
75 =cut
76
77 sub LimitToEnabled {
78     my $self = shift;
79     
80     $self->Limit( FIELD => 'Disabled',
81                   VALUE => '0',
82                   OPERATOR => '=' );
83 }
84 # }}}
85
86 # {{{ sub LimitToDisabled
87
88 =head2 LimitToDeleted
89
90 Only find items that have been deleted.
91
92 =cut
93
94 sub LimitToDeleted {
95     my $self = shift;
96     
97     $self->{'find_disabled_rows'} = 1;
98     $self->Limit( FIELD => 'Disabled',
99                   OPERATOR => '=',
100                   VALUE => '1'
101                 );
102 }
103 # }}}
104
105 # {{{ sub FindAllRows
106
107 =head2 FindAllRows
108
109 Find all matching rows, regardless of whether they are disabled or not
110
111 =cut
112
113 sub FindAllRows {
114   shift->{'find_disabled_rows'} = 1;
115 }
116
117 # {{{ sub Limit 
118
119 =head2 Limit PARAMHASH
120
121 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
122 making sure that by default lots of things don't do extra work trying to 
123 match lower(colname) agaist lc($val);
124
125 =cut
126
127 sub Limit {
128         my $self = shift;
129         my %args = ( CASESENSITIVE => 1,
130                      @_ );
131
132    return $self->SUPER::Limit(%args);
133 }
134
135 # }}}
136
137 # {{{ sub ItemsArrayRef
138
139 =item ItemsArrayRef
140
141 Return this object's ItemsArray.
142 If it has a SortOrder attribute, sort the array by SortOrder.
143 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
144 Otherwise, just give up and return it in the order it came from the db.
145
146
147 =begin testing
148
149 use_ok(RT::Queues);
150 ok(my $queues = RT::Queues->new($RT::SystemUser), 'Created a queues object');
151 ok( $queues->UnLimit(),'Unlimited the result set of the queues object');
152 my $items = $queues->ItemsArrayRef();
153 my @items = @{$items};
154
155 ok($queues->NewItem->_Accessible('Name','read'));
156 my @sorted = sort {lc($a->Name) cmp lc($b->Name)} @items;
157 ok (@sorted, "We have an array of queues, sorted". join(',',map {$_->Name} @sorted));
158
159 ok (@items, "We have an array of queues, raw". join(',',map {$_->Name} @items));
160 my @sorted_ids = map {$_->id } @sorted;
161 my @items_ids = map {$_->id } @items;
162
163 is ($#sorted, $#items);
164 is ($sorted[0]->Name, $items[0]->Name);
165 is ($sorted[-1]->Name, $items[-1]->Name);
166 is_deeply(\@items_ids, \@sorted_ids, "ItemsArrayRef sorts alphabetically by name");;
167
168
169 =end testing
170
171 =cut
172
173 sub ItemsArrayRef {
174     my $self = shift;
175     my @items;
176     
177     if ($self->NewItem()->_Accessible('SortOrder','read')) {
178         @items = sort { $a->SortOrder <=> $b->SortOrder } @{$self->SUPER::ItemsArrayRef()};
179     }
180     elsif ($self->NewItem()->_Accessible('Name','read')) {
181         @items = sort { lc($a->Name) cmp lc($b->Name) } @{$self->SUPER::ItemsArrayRef()};
182     }
183     else {
184         @items = @{$self->SUPER::ItemsArrayRef()};
185     }
186
187     return(\@items);
188
189 }
190
191 # }}}
192
193 eval "require RT::SearchBuilder_Vendor";
194 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Vendor.pm});
195 eval "require RT::SearchBuilder_Local";
196 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SearchBuilder_Local.pm});
197
198 1;
199
200