import rt 2.0.14
[freeside.git] / rt / lib / RT / ObjectKeywords.pm
1 #$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Attic/ObjectKeywords.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2
3 package RT::ObjectKeywords;
4
5 use strict;
6 use vars qw( @ISA );
7
8 =head1 NAME
9
10         RT::ObjectKeywords - note warning
11
12 =head1 WARNING
13
14 This module should B<NEVER> be called directly by client code. its API is entirely through RT ticket or other objects which can have keywords assigned.
15
16 =head1 SYNOPSIS
17
18 =head1 DESCRIPTION
19
20 =begin testing
21
22 ok (require RT::TestHarness);
23 ok (require RT::ObjectKeywords);
24
25 =end testing
26
27 =cut
28
29 use RT::EasySearch;
30 use RT::ObjectKeyword;
31
32 @ISA = qw( RT::EasySearch );
33
34 # {{{ sub _Init
35 sub _Init {
36     my $self = shift;
37     $self->{'table'} = 'ObjectKeywords';
38     $self->{'primary_key'} = 'id';
39     return ($self->SUPER::_Init(@_));
40 }
41 # }}}
42
43 # {{{ sub NewItem
44 sub NewItem {
45     my $self = shift;
46     return (new RT::ObjectKeyword($self->CurrentUser));
47 }
48 # }}}
49
50 # {{{ sub LimitToKeywordSelect
51
52 =head2 LimitToKeywordSelect
53
54   Takes a B<RT::KeywordSelect> id or Nameas its single argument. limits the returned set of ObjectKeywords
55 to ObjectKeywords which apply to that ticket
56
57 =cut
58
59
60 sub LimitToKeywordSelect {
61     my $self = shift;
62     my $keywordselect = shift;
63     
64     if ($keywordselect =~ /^\d+$/) {
65
66         $self->Limit(FIELD => 'KeywordSelect',
67                      OPERATOR => '=',
68                      ENTRYAGGREGATOR => 'OR',
69                      VALUE => "$keywordselect");
70     }
71
72     #We're limiting by name. time to be klever
73     else {
74         my $ks = $self->NewAlias('KeywordSelects');
75         $self->Join(ALIAS1 => $ks, FIELD1 => 'id',
76                     ALIAS2 => 'main', FIELD2 => 'KeywordSelect');
77         
78         $self->Limit( ALIAS => "$ks",
79                        FIELD => 'Name',
80                        VALUE => "$keywordselect",
81                        OPERATOR => "=",
82                        ENTRYAGGREGATOR => "OR");
83
84         $self->Limit ( ALIAS => "$ks",
85                        FIELD => 'ObjectType',
86                        VALUE => 'Ticket',
87                        OPERATOR => '=',
88                      );
89
90         $self->Limit ( ALIAS => "$ks",
91                        FIELD => 'ObjectField',
92                        VALUE => 'Queue',
93                        OPERATOR => '=',
94                      );
95
96
97         # TODO +++ we need to be able to limit the returned
98         # keywordselects to ones that apply only to this queue
99         #       $self->Limit( ALIAS => "$ks",
100         #                      FIELD => 'ObjectValue',
101         #                      VALUE => $self->QueueObj->Id,
102         #                      OPERATOR => "=",
103         #                      ENTRYAGGREGATOR => "OR");        
104
105     }
106
107
108
109 }
110
111 # }}}
112
113 # {{{ LimitToTicket
114
115 =head2 LimitToTicket TICKET_ID
116
117   Takes an B<RT::Ticket> id as its single argument. limits the returned set of ObjectKeywords
118 to ObjectKeywords which apply to that ticket
119
120 =cut
121
122 sub LimitToTicket {
123     my $self = shift;
124     my $ticket = shift;
125     $self->Limit(FIELD => 'ObjectId',
126                  OPERATOR => '=',
127                  ENTRYAGGREGATOR => 'OR',
128                  VALUE => "$ticket");
129
130     $self->Limit(FIELD => 'ObjectType',
131                  OPERATOR => '=',
132                  ENTRYAGGREGATOR => 'OR',
133                  VALUE => "Ticket");
134     
135 }
136
137 # }}}
138
139 # {{{ sub _DoSearch
140 #wrap around _DoSearch  so that we can build the hash of returned
141 #values 
142
143 sub _DoSearch {
144     my $self = shift;
145    # $RT::Logger->debug("Now in ".$self."->_DoSearch");
146     my $return = $self->SUPER::_DoSearch(@_);
147   #  $RT::Logger->debug("In $self ->_DoSearch. return from SUPER::_DoSearch was $return\n");
148     $self->_BuildHash();
149     return ($return);
150 }
151 # }}}
152
153 # {{{ sub _BuildHash
154 #Build a hash of this ACL's entries.
155 sub _BuildHash {
156     my $self = shift;
157
158     while (my $entry = $self->Next) {
159
160         my $hashkey = $entry->Keyword;
161         $self->{'as_hash'}->{"$hashkey"} =1;
162     }
163
164 }
165 # }}}
166
167 # {{{ HasEntry
168
169 =head2 HasEntry KEYWORD_ID
170   
171   Takes a keyword id and returns true if this ObjectKeywords object has an entry for that
172 keyword.  Returns undef otherwise.
173
174 =cut
175
176 sub HasEntry {
177
178     my $self = shift;
179     my $keyword = shift;
180
181
182     #if we haven't done the search yet, do it now.
183     $self->_DoSearch();
184     
185     #    $RT::Logger->debug("Now in ".$self."->HasEntry\n");
186     
187     
188     if ($self->{'as_hash'}->{ $keyword } == 1) {
189         return(1);
190     }
191     else {
192         return(undef);
193     }
194 }
195
196 # }}}
197
198 # {{{ sub RelativePaths
199
200 =head2 RelativePaths
201
202 # Return a (reference to a) list of KeywordRelativePaths
203
204 =cut
205
206 sub RelativePaths  {
207     my $self = shift;
208
209     my @list;
210
211     # Here $key is a RT::ObjectKeyword
212     while (my $key=$self->Next()) {
213         push(@list, $key->KeywordRelativePath);
214     }
215     return(\@list);
216 }
217 # }}}
218
219 # {{{ sub RelativePathsAsString
220
221 =head2 RelativePathsAsString
222
223 # Returns the RT::ObjectKeywords->RelativePaths as a comma seperated string
224
225 =cut
226
227 sub RelativePathsAsString {
228     my $self = shift;
229     return(join(", ",@{$self->KeywordRelativePaths}));
230 }
231 # }}}
232
233 1;
234