summaryrefslogtreecommitdiff
path: root/rt/lib/RT/ObjectKeywords.pm
blob: 5df996e37e0d5e836b7ab688641b3e6f55bbb2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Attic/ObjectKeywords.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $

package RT::ObjectKeywords;

use strict;
use vars qw( @ISA );

=head1 NAME

	RT::ObjectKeywords - note warning

=head1 WARNING

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.

=head1 SYNOPSIS

=head1 DESCRIPTION

=begin testing

ok (require RT::TestHarness);
ok (require RT::ObjectKeywords);

=end testing

=cut

use RT::EasySearch;
use RT::ObjectKeyword;

@ISA = qw( RT::EasySearch );

# {{{ sub _Init
sub _Init {
    my $self = shift;
    $self->{'table'} = 'ObjectKeywords';
    $self->{'primary_key'} = 'id';
    return ($self->SUPER::_Init(@_));
}
# }}}

# {{{ sub NewItem
sub NewItem {
    my $self = shift;
    return (new RT::ObjectKeyword($self->CurrentUser));
}
# }}}

# {{{ sub LimitToKeywordSelect

=head2 LimitToKeywordSelect

  Takes a B<RT::KeywordSelect> id or Nameas its single argument. limits the returned set of ObjectKeywords
to ObjectKeywords which apply to that ticket

=cut


sub LimitToKeywordSelect {
    my $self = shift;
    my $keywordselect = shift;
    
    if ($keywordselect =~ /^\d+$/) {

	$self->Limit(FIELD => 'KeywordSelect',
		     OPERATOR => '=',
		     ENTRYAGGREGATOR => 'OR',
		     VALUE => "$keywordselect");
    }

    #We're limiting by name. time to be klever
    else {
	my $ks = $self->NewAlias('KeywordSelects');
	$self->Join(ALIAS1 => $ks, FIELD1 => 'id',
		    ALIAS2 => 'main', FIELD2 => 'KeywordSelect');
	
	$self->Limit( ALIAS => "$ks",
		       FIELD => 'Name',
		       VALUE => "$keywordselect",
		       OPERATOR => "=",
		       ENTRYAGGREGATOR => "OR");

	$self->Limit ( ALIAS => "$ks",
		       FIELD => 'ObjectType',
		       VALUE => 'Ticket',
		       OPERATOR => '=',
		     );

	$self->Limit ( ALIAS => "$ks",
		       FIELD => 'ObjectField',
		       VALUE => 'Queue',
		       OPERATOR => '=',
		     );


	# TODO +++ we need to be able to limit the returned
	# keywordselects to ones that apply only to this queue
	#	$self->Limit( ALIAS => "$ks",
	#		       FIELD => 'ObjectValue',
	#		       VALUE => $self->QueueObj->Id,
	#		       OPERATOR => "=",
	#		       ENTRYAGGREGATOR => "OR");	

    }



}

# }}}

# {{{ LimitToTicket

=head2 LimitToTicket TICKET_ID

  Takes an B<RT::Ticket> id as its single argument. limits the returned set of ObjectKeywords
to ObjectKeywords which apply to that ticket

=cut

sub LimitToTicket {
    my $self = shift;
    my $ticket = shift;
    $self->Limit(FIELD => 'ObjectId',
		 OPERATOR => '=',
		 ENTRYAGGREGATOR => 'OR',
		 VALUE => "$ticket");

    $self->Limit(FIELD => 'ObjectType',
		 OPERATOR => '=',
		 ENTRYAGGREGATOR => 'OR',
		 VALUE => "Ticket");
    
}

# }}}

# {{{ sub _DoSearch
#wrap around _DoSearch  so that we can build the hash of returned
#values 

sub _DoSearch {
    my $self = shift;
   # $RT::Logger->debug("Now in ".$self."->_DoSearch");
    my $return = $self->SUPER::_DoSearch(@_);
  #  $RT::Logger->debug("In $self ->_DoSearch. return from SUPER::_DoSearch was $return\n");
    $self->_BuildHash();
    return ($return);
}
# }}}

# {{{ sub _BuildHash
#Build a hash of this ACL's entries.
sub _BuildHash {
    my $self = shift;

    while (my $entry = $self->Next) {

	my $hashkey = $entry->Keyword;
        $self->{'as_hash'}->{"$hashkey"} =1;
    }

}
# }}}

# {{{ HasEntry

=head2 HasEntry KEYWORD_ID
  
  Takes a keyword id and returns true if this ObjectKeywords object has an entry for that
keyword.  Returns undef otherwise.

=cut

sub HasEntry {

    my $self = shift;
    my $keyword = shift;


    #if we haven't done the search yet, do it now.
    $self->_DoSearch();
    
    #    $RT::Logger->debug("Now in ".$self."->HasEntry\n");
    
    
    if ($self->{'as_hash'}->{ $keyword } == 1) {
	return(1);
    }
    else {
	return(undef);
    }
}

# }}}

# {{{ sub RelativePaths

=head2 RelativePaths

# Return a (reference to a) list of KeywordRelativePaths

=cut

sub RelativePaths  {
    my $self = shift;

    my @list;

    # Here $key is a RT::ObjectKeyword
    while (my $key=$self->Next()) {
	push(@list, $key->KeywordRelativePath);
    }
    return(\@list);
}
# }}}

# {{{ sub RelativePathsAsString

=head2 RelativePathsAsString

# Returns the RT::ObjectKeywords->RelativePaths as a comma seperated string

=cut

sub RelativePathsAsString {
    my $self = shift;
    return(join(", ",@{$self->KeywordRelativePaths}));
}
# }}}

1;