1 #$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Attic/KeywordSelect.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
3 package RT::KeywordSelect;
10 @ISA = qw(RT::Record);
16 RT::KeywordSelect - Manipulate an RT::KeywordSelect record
20 use RT::KeywordSelect;
22 my $keyword_select = RT::KeywordSelect->new($CurrentUser);
23 $keyword_select->Create(
25 ObjectType => 'Ticket',
29 my $keyword_select = RT::KeywordSelect->new($CurrentUser);
30 $keyword_select->Create(
33 ObjectType => 'Ticket',
34 ObjectField => 'Queue',
42 An B<RT::KeywordSelect> object is a link between a Keyword and a object
43 type (one of: Ticket), titled by the I<Name> field of the B<RT::Keyword> such
48 =item Object display will contain a field, titled with the I<Name> field and
49 showing any descendent keywords which are related to this object via the
50 B<RT::ObjectKeywords> table.
52 =item Object creation for this object will contain a field titled with the
53 I<Name> field and containing the descendents of the B<RT::Keyword> as
54 choices. If the I<Single> field of this B<RT::KeywordSelect> is true, each
55 object must be associated (via an B<RT::ObjectKeywords> record) to a single
56 descendent. If the I<Single> field is false, each object may be connect to
57 zero, one, or many descendents.
59 =item Searches for this object type will contain a selection field titled with
60 the I<Name> field and containing the descendents of the B<RT::Keyword> as
63 =item If I<ObjectField> is defined (one of: Queue), all of the above apply only
64 when the value of I<ObjectField> (Queue) in B<ObjectType> (Ticket) matches
72 ok (require RT::TestHarness);
73 ok (require RT::KeywordSelects);
86 =item new CURRENT_USER
88 Takes a single argument, an RT::CurrentUser object. Instantiates a new
89 (uncreated) RT::KeywordSelect object.
97 $self->{'table'} = "KeywordSelects";
98 $self->SUPER::_Init(@_);
102 # {{{ sub _Accessible
106 Name => 'read/write',
107 Keyword => 'read/write', # link to Keywords. Can be specified by id
108 Single => 'read/write', # bool (described below)
110 Depth => 'read/write', #- If non-zero, limits the descendents to this number of levels deep.
111 ObjectType => 'read/write', # currently only C<Ticket>
112 ObjectField => 'read/write', #optional, currently only C<Queue>
113 ObjectValue => 'read/write', #constrains KeywordSelect function to when B<ObjectType>.I<ObjectField> equals I<ObjectValue>
114 Disabled => 'read/write'
116 return($self->SUPER::_Accessible(@_, %Cols));
122 =head2 LoadByName( Name => [NAME], Queue => [QUEUE_ID])
123 . Takes a queue id and a keyword select name.
124 tries to load the keyword select for that queue. if that fails, it tries to load it
125 without a queue specified.
132 my %args = ( Name => undef,
136 if ($args{'Queue'}) {
137 #Try to get the keyword select for this queue
138 $self->LoadByCols( Name => $args{'Name'},
139 ObjectType => 'Ticket',
140 ObjectField => 'Queue',
141 ObjectValue => $args{'Queue'});
143 unless ($self->Id) { #if that failed to load an object
144 #Try to get the keyword select of that name that's global
145 $self->LoadByCols( Name => $args{'Name'},
146 ObjectType => 'Ticket',
147 ObjectField => 'Queue',
158 =item Create KEY => VALUE, ...
160 Takes a list of key/value pairs and creates a the object. Returns the id of
161 the newly created record, or false if there was an error.
165 Keyword - link to Keywords. Can be specified by id.
166 Name - A name for this KeywordSelect
167 Single - bool (described above)
168 Depth - If non-zero, limits the descendents to this number of levels deep.
169 ObjectType - currently only C<Ticket>
170 ObjectField - optional, currently only C<Queue>
171 ObjectValue - constrains KeywordSelect function to when B<ObjectType>.I<ObjectField> equals I<ObjectValue>
177 my %args = ( Keyword => undef,
182 ObjectField => undef,
183 ObjectValue => undef,
186 #If we're talking about a keyword select based on a ticket's 'Queue' field
187 if ( ($args{'ObjectField'} eq 'Queue') and
188 ($args{'ObjectType'} eq 'Ticket')) {
190 #If we're talking about a keywordselect for all queues
191 if ($args{'ObjectValue'} == 0) {
192 unless( $self->CurrentUserHasSystemRight('AdminKeywordSelects')) {
193 return (0, 'Permission Denied');
196 #otherwise, we're talking about a keywordselect for a specific queue
198 unless ($self->CurrentUserHasQueueRight( Right => 'AdminKeywordSelects',
199 Queue => $args{'ObjectValue'})) {
200 return (0, 'Permission Denied');
205 return (0, "Can't create a KeywordSelect for that object/field combo");
208 my $Keyword = new RT::Keyword($self->CurrentUser);
210 if ( $args{'Keyword'} && $args{'Keyword'} !~ /^\d+$/ ) {
211 $Keyword->LoadByPath($args{'Keyword'});
214 $Keyword->Load($args{'Keyword'});
217 unless ($Keyword->Id) {
218 $RT::Logger->debug("Keyword ".$args{'Keyword'} ." not found\n");
219 return(0, 'Keyword not found');
222 $args{'Name'} = $Keyword->Name if (!$args{'Name'});
224 my $val = $self->SUPER::Create( Name => $args{'Name'},
225 Keyword => $Keyword->Id,
226 Single => $args{'Single'},
227 Depth => $args{'Depth'},
228 ObjectType => $args{'ObjectType'},
229 ObjectField => $args{'ObjectField'},
230 ObjectValue => $args{'ObjectValue'});
232 return ($val, 'KeywordSelect Created');
235 return (0, 'System error. KeywordSelect not created');
246 return (0, 'Deleting this object would break referential integrity.');
252 # {{{ sub SetDisabled
254 =head2 Sub SetDisabled
256 Toggles the KeywordSelect's disabled flag.
265 unless ($self->CurrentUserHasRight('AdminKeywordSelects')) {
266 return (0, "Permission Denied");
268 return($self->_Set(Field => 'Disabled', Value => $value));
277 Returns the B<RT::Keyword> referenced by the I<Keyword> field.
284 my $Keyword = new RT::Keyword($self->CurrentUser);
285 $Keyword->Load( $self->Keyword ); #or ?
294 Returns the object (currently only RT::Queue) specified by ObjectField and ObjectValue.
300 if ( $self->ObjectField eq 'Queue' ) {
301 my $Queue = new RT::Queue($self->CurrentUser);
302 $Queue->Load( $self->ObjectValue );
305 $RT::Logger->error("$self trying to load an object value for a non-queue object");
314 # does an acl check, then passes off the call
318 unless ($self->CurrentUserHasRight('AdminKeywordSelects')) {
319 return (0, "Permission Denied");
322 return ($self->SUPER::_Set(@_));
329 # {{{ sub CurrentUserHasQueueRight
331 =head2 CurrentUserHasQueueRight ( Queue => QUEUEID, Right => RIGHTNANAME )
333 Check to see whether the current user has the specified right for the specified queue.
337 sub CurrentUserHasQueueRight {
339 my %args = (Queue => undef,
343 return ($self->HasRight( Right => $args{'Right'},
344 Principal => $self->CurrentUser->UserObj,
345 Queue => $args{'Queue'}));
350 # {{{ sub CurrentUserHasSystemRight
352 =head2 CurrentUserHasSystemRight RIGHTNAME
354 Check to see whether the current user has the specified right for the 'system' scope.
358 sub CurrentUserHasSystemRight {
361 $RT::Logger->debug("$self in hashsysright for right $right\n");
362 return ($self->HasRight( Right => $right,
364 Principal => $self->CurrentUser->UserObj));
369 # {{{ sub CurrentUserHasRight
371 =item CurrentUserHasRight RIGHT [QUEUEID]
373 Takes a rightname as a string. Can take a queue id as a second
374 optional parameter, which can be useful to a routine like create.
375 Helper menthod for HasRight. Presets Principal to CurrentUser then
380 sub CurrentUserHasRight {
383 return ($self->HasRight( Principal => $self->CurrentUser->UserObj,
394 Takes a param-hash consisting of "Right" and "Principal" Principal is
395 an RT::User object or an RT::CurrentUser object. "Right" is a textual
396 Right string that applies to KeywordSelects
402 my %args = ( Right => undef,
408 #If we're explicitly specifying a queue, as we need to do on create
409 if ($args{'Queue'}) {
410 return ($args{'Principal'}->HasQueueRight(Right => $args{'Right'},
411 Queue => $args{'Queue'}));
413 #else if we're specifying to check a system right
414 elsif ($args{'System'}) {
415 return( $args{'Principal'}->HasSystemRight( $args{'Right'} ));
418 #else if we 're using the object's queue
419 elsif (($self->__Value('ObjectField') eq 'Queue') and
420 ($self->__Value('ObjectValue') > 0 )) {
421 return ($args{'Principal'}->HasQueueRight(Right => $args{'Right'},
422 Queue => $self->__Value('ObjectValue') ));
425 #If the object is system scoped.
427 return( $args{'Principal'}->HasSystemRight( $args{'Right'} ));
437 Ivan Kohler <ivan-rt@420.am>, Jesse Vincent <jesse@fsck.com>
441 The ACL system for this object is more byzantine than it should be. reworking it eventually
442 would be a good thing.
446 L<RT::KeywordSelects>, L<RT::Keyword>, L<RT::Keywords>, L<RT::ObjectKeyword>,
447 L<RT::ObjectKeywords>, L<RT::Record>