import rt 3.6.6
[freeside.git] / rt / lib / RT / SavedSearches.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 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., 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.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
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.)
37
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.
46
47 # END BPS TAGGED BLOCK }}}
48 =head1 NAME
49
50   RT::SavedSearches - a pseudo-collection for SavedSearch objects.
51
52 =head1 SYNOPSIS
53
54   use RT::SavedSearch
55
56 =head1 DESCRIPTION
57
58   SavedSearches is an object consisting of a number of SavedSearch objects.
59   It works more or less like a DBIx::SearchBuilder collection, although it
60   is not.
61
62 =head1 METHODS
63
64 =begin testing
65
66 use_ok(RT::SavedSearches);
67
68 # The real tests are in lib/t/20savedsearch.t
69
70 =end testing
71
72 =cut
73
74 package RT::SavedSearches;
75
76 use RT::Base;
77 use RT::SavedSearch;
78
79 use strict;
80 use vars qw/@ISA/;
81 @ISA = qw/RT::Base/;
82
83 sub new  {
84     my $proto = shift;
85     my $class = ref($proto) || $proto;
86     my $self  = {};
87     bless ($self, $class);
88     $self->CurrentUser(@_);
89     $self->{'idx'} = 0;
90     $self->{'objects'} = [];
91     return $self;
92 }
93
94 =head2 LimitToPrivacy
95
96 Takes two argumets: a privacy string, of the format "<class>-<id>", as
97 produced by RT::SavedSearch::Privacy(); and a type string, as produced
98 by RT::SavedSearch::Type().  The SavedSearches object will load the
99 searches belonging to that user or group that are of the type
100 specified.  If no type is specified, all the searches belonging to the
101 user/group will be loaded.  Repeated calls to the same object should DTRT.
102
103 =cut
104
105 sub LimitToPrivacy {
106     my $self = shift;
107     my $privacy = shift;
108     my $type = shift;
109
110     my $object = $self->_GetObject($privacy);
111
112     if ($object) {
113         $self->{'objects'} = [];
114         my @search_atts = $object->Attributes->Named('SavedSearch');
115         foreach my $att (@search_atts) {
116             my $search = RT::SavedSearch->new($self->CurrentUser);
117             $search->Load($privacy, $att->Id);
118             next if $type && $search->Type ne $type;
119             push(@{$self->{'objects'}}, $search);
120         }
121     } else {
122         $RT::Logger->error("Could not load object $privacy");
123     }
124 }
125
126 ### Accessor methods
127
128 =head2 Next
129
130 Returns the next object in the collection.
131
132 =cut
133
134 sub Next {
135     my $self = shift;
136     my $search = $self->{'objects'}->[$self->{'idx'}];
137     if ($search) {
138         $self->{'idx'}++;
139     } else {
140         # We have run out of objects; reset the counter.
141         $self->{'idx'} = 0;
142     }
143     return $search;
144 }
145
146 =head2 Count
147
148 Returns the number of search objects found.
149
150 =cut
151
152 sub Count {
153     my $self = shift;
154     return scalar @{$self->{'objects'}};
155 }
156
157 ### Internal methods
158
159 # _GetObject: helper routine to load the correct object whose parameters
160 #  have been passed.
161
162 sub _GetObject {
163     my $self = shift;
164     my $privacy = shift;
165
166     return RT::SavedSearch->new($self->CurrentUser)->_GetObject($privacy);
167 }
168
169 ### Internal methods
170
171 # _PrivacyObjects: returns a list of objects that can be used to load saved searches from.
172
173 sub _PrivacyObjects {
174     my $self        = shift;
175     my $CurrentUser = $self->CurrentUser;
176
177     my $groups = RT::Groups->new($CurrentUser);
178     $groups->LimitToUserDefinedGroups;
179     $groups->WithMember( PrincipalId => $CurrentUser->Id,
180                          Recursively => 1 );
181
182     return ( $CurrentUser->UserObj, @{ $groups->ItemsArrayRef() } );
183 }
184
185 eval "require RT::SavedSearches_Vendor";
186 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearches_Vendor.pm});
187 eval "require RT::SavedSearches_Local";
188 die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearches_Local.pm});
189
190 1;