1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
52 package RT::Record::AddAndSort;
53 use base 'RT::Record';
57 RT::Record::AddAndSort - base class for records that can be added and sorted
61 Base class for L<RT::ObjectCustomField> and L<RT::ObjectScrip> that unifies
62 application of L<RT::CustomField>s and L<RT::Scrip>s to various objects. Also,
63 deals with order of the records.
67 =head2 Meta information
69 =head3 CollectionClass
71 Returns class representing collection for this record class. Basicly adds 's'
72 at the end. Should be overriden if default doesn't work.
74 For example returns L<RT::ObjectCustomFields> when called on L<RT::ObjectCustomField>.
79 return (ref($_[0]) || $_[0]).'s';
84 Returns name of the field in the table where id of object we add is stored.
85 By default deletes everything up to '::Object' from class name.
86 This method allows to use friendlier argument names and methods.
88 For example returns 'Scrip' for L<RT::ObjectScrip>.
93 my $class = ref($_[0]) || $_[0];
94 $class =~ s/.*::Object// or return undef;
98 =head3 ObjectCollectionClass
100 Takes an object under L</TargetField> name and should return class
101 name representing collection the object can be added to.
103 Must be overriden by sub classes.
106 See L<RT::ObjectScrip/ObjectCollectionClass> and L<RT::ObjectCustomField/CollectionClass>.
110 sub ObjectCollectionClass { die "should be subclassed" }
116 Takes 'ObjectId' with id of an object we can be added to, object we can
117 add to under L</TargetField> name, Disabled and SortOrder.
119 This method doesn't create duplicates. If record already exists then it's not created, but
120 loaded instead. Note that nothing is updated if record exist.
122 If SortOrder is not defined then it's calculated to place new record last. If it's
123 provided then it's caller's duty to make sure it is correct value.
127 my $ocf = RT::ObjectCustomField->new( RT->SystemUser );
128 my ($id, $msg) = $ocf->Create( CustomField => 1, ObjectId => 0 );
130 See L</Add> which has more error checks. Also, L<RT::Scrip> and L<RT::CustomField>
131 have more appropriate methods that B<should be> prefered over calling this directly.
143 my $tfield = $self->TargetField;
145 my $target = $self->TargetObj( $args{ $tfield } );
146 unless ( $target->id ) {
147 $RT::Logger->error("Couldn't load ". ref($target) ." '$args{$tfield}'");
151 my $exist = $self->new($self->CurrentUser);
152 $exist->LoadByCols( ObjectId => $args{'ObjectId'}, $tfield => $target->id );
154 $self->Load( $exist->id );
158 unless ( defined $args{'SortOrder'} ) {
159 $args{'SortOrder'} = $self->NextSortOrder(
165 return $self->SUPER::Create(
167 $tfield => $target->id,
173 Helper method that wraps L</Create> and does more checks to make sure
174 result is consistent. Doesn't allow adding a record to an object if the
175 record is already global. Removes record from particular objects when
176 asked to add the record globally.
184 my $field = $self->TargetField;
186 my $tid = $args{ $field };
187 $tid = $tid->id if ref $tid;
188 $tid ||= $self->TargetObj->id;
190 my $oid = $args{'ObjectId'};
191 $oid = $oid->id if ref $oid;
194 if ( $self->IsAdded( $tid => $oid ) ) {
195 return ( 0, $self->loc("Is already added to the object") );
200 return (0, $self->loc("Couldn't add as it's global already") )
201 if $self->IsAdded( $tid => 0 );
204 $self->DeleteAll( $field => $tid );
207 return $self->Create(
208 %args, $field => $tid, ObjectId => $oid,
214 my ($tid, $oid) = @_;
215 my $record = $self->new( $self->CurrentUser );
216 $record->LoadByCols( $self->TargetField => $tid, ObjectId => $oid );
222 Returns collection with objects target of this record is added to.
223 Class of the collection depends on L</ObjectCollectionClass>.
224 See all L</NotAddedTo>.
226 For example returns L<RT::Queues> collection if the target is L<RT::Scrip>.
228 Returns empty collection if target is added globally.
235 my ($res, $alias) = $self->_AddedTo( @_ );
236 return $res unless $res;
241 OPERATOR => 'IS NOT',
250 Returns collection with objects target of this record is not added to.
251 Class of the collection depends on L</ObjectCollectionClass>.
254 Returns empty collection if target is added globally.
261 my ($res, $alias) = $self->_AddedTo( @_ );
262 return $res unless $res;
278 my $field = $self->TargetField;
279 my $target = $args{ $field } || $self->TargetObj;
281 my ($class) = $self->ObjectCollectionClass( $field => $target );
282 return undef unless $class;
284 my $res = $class->new( $self->CurrentUser );
286 # If target added to a Group, only display user-defined groups
287 $res->LimitToUserDefinedGroups if $class eq 'RT::Groups';
289 $res->OrderBy( FIELD => 'Name' );
290 my $alias = $res->Join(
294 TABLE2 => $self->Table,
295 FIELD2 => 'ObjectId',
301 VALUE => $target->id,
303 return ($res, $alias);
315 return $self->SUPER::Delete if $self->IsSortOrderShared;
317 # Move everything below us up
318 my $siblings = $self->Neighbors;
319 $siblings->Limit( FIELD => 'SortOrder', OPERATOR => '>=', VALUE => $self->SortOrder );
320 $siblings->OrderBy( FIELD => 'SortOrder', ORDER => 'ASC' );
321 foreach my $record ( @{ $siblings->ItemsArrayRef } ) {
322 $record->SetSortOrder($record->SortOrder - 1);
325 return $self->SUPER::Delete;
330 Helper method to delete all applications for one target (Scrip, CustomField, ...).
331 Target can be provided in arguments. If it's not then L</TargetObj> is used.
333 $object_scrip->DeleteAll;
335 $object_scrip->DeleteAll( Scrip => $scrip );
343 my $field = $self->TargetField;
345 my $id = $args{ $field };
346 $id = $id->id if ref $id;
347 $id ||= $self->TargetObj->id;
349 my $list = $self->CollectionClass->new( $self->CurrentUser );
350 $list->Limit( FIELD => $field, VALUE => $id );
351 $_->Delete foreach @{ $list->ItemsArrayRef };
360 sub MoveUp { return shift->Move( Up => @_ ) }
368 sub MoveDown { return shift->Move( Down => @_ ) }
372 Takes 'up' or 'down'. One method that implements L</MoveUp> and L</MoveDown>.
378 my $dir = lc(shift || 'up');
381 if ( $dir eq 'down' ) {
397 my $siblings = $self->Siblings;
398 $siblings->Limit( FIELD => 'SortOrder', OPERATOR => $meta{'next_op'}, VALUE => $self->SortOrder );
399 $siblings->OrderBy( FIELD => 'SortOrder', ORDER => $meta{'next_order'} );
401 my @next = ($siblings->Next, $siblings->Next);
403 return $dir eq 'down'
404 ? (0, "Can not move down. It's already at the bottom")
405 : (0, "Can not move up. It's already at the top")
409 my ($new_sort_order, $move);
411 unless ( $self->ObjectId ) {
412 # moving global, it can not share sort order, so just move it
413 # on place of next global and move everything in between one number
415 $new_sort_order = $next[0]->SortOrder;
416 $move = $self->Neighbors;
418 FIELD => 'SortOrder', OPERATOR => $meta{'next_op'}, VALUE => $self->SortOrder,
421 FIELD => 'SortOrder', OPERATOR => $meta{'prev_op'}, VALUE => $next[0]->SortOrder,
422 ENTRYAGGREGATOR => 'AND',
425 elsif ( $next[0]->ObjectId == $self->ObjectId ) {
426 # moving two locals, just swap them, they should follow 'so = so+/-1' rule
427 $new_sort_order = $next[0]->SortOrder;
431 # moving local behind global
432 unless ( $self->IsSortOrderShared ) {
433 # not shared SO allows us to swap
434 $new_sort_order = $next[0]->SortOrder;
438 # more records there and shared SO, we have to move everything
439 $new_sort_order = $next[0]->SortOrder;
440 $move = $self->Neighbors;
442 FIELD => 'SortOrder', OPERATOR => $meta{prev_op}, VALUE => $next[0]->SortOrder,
446 # shared SO and place after is free, so just jump
447 $new_sort_order = $next[0]->SortOrder + $meta{'diff'};
452 foreach my $record ( $move->isa('RT::Record')? ($move) : @{ $move->ItemsArrayRef } ) {
453 my ($status, $msg) = $record->SetSortOrder(
454 $record->SortOrder - $meta{'diff'}
456 return (0, "Couldn't move: $msg") unless $status;
460 my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
462 return (0, "Couldn't move: $msg");
468 =head2 Accessors, instrospection and traversing.
472 Returns target object of this record. Returns L<RT::Scrip> object for
481 my $method = $self->TargetField .'Obj';
482 return $self->$method( $id );
487 Returns next available SortOrder value in the L<neighborhood|/Neighbors>.
488 Pass arguments to L</Neighbors> and can take optional ObjectId argument,
489 calls ObjectId if it's not provided.
497 my $oid = $args{'ObjectId'};
498 $oid = $self->ObjectId unless defined $oid;
501 my $neighbors = $self->Neighbors( %args );
503 $neighbors->LimitToObjectId( $oid );
504 $neighbors->LimitToObjectId( 0 );
505 } elsif ( !$neighbors->_isLimited ) {
508 $neighbors->OrderBy( FIELD => 'SortOrder', ORDER => 'DESC' );
509 return 0 unless my $first = $neighbors->First;
510 return $first->SortOrder + 1;
513 =head3 IsSortOrderShared
515 Returns true if this record shares SortOrder value with a L<neighbor|/Neighbors>.
519 sub IsSortOrderShared {
521 return 0 unless $self->ObjectId;
523 my $neighbors = $self->Neighbors;
524 $neighbors->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => $self->id );
525 $neighbors->Limit( FIELD => 'SortOrder', VALUE => $self->SortOrder );
526 return $neighbors->Count;
529 =head2 Neighbors and Siblings
531 These two methods should only be understood by developers who wants
532 to implement new classes of records that can be added to other records
535 Main purpose is to maintain SortOrder values.
537 Let's take a look at custom fields. A custom field can be created for tickets,
538 queues, transactions, users... Custom fields created for tickets can
539 be added globally or to particular set of queues. Custom fields for
540 tickets are neighbors. Neighbor custom fields added to the same objects
541 are siblings. Custom fields added globally are sibling to all neighbors.
543 For scrips Stage defines neighborhood.
545 Let's look at the three scrips in create stage S1, S2 and S3, queues Q1 and Q2 and
552 Above table says that S2 is added globally, S1 is added to Q1 and executed
553 before S2 in this queue, also S1 is added to Q1, but exectued after S2 in this
554 queue, S3 is only added to Q2 and executed before S2 and S1.
556 Siblings are scrips added to an object including globally added or only
557 globally added. In our example there are three different collection
558 of siblings: (S2) - global, (S1, S2) for Q1, (S3, S2, S1) for Q2.
560 Sort order can be shared between neighbors, but can not be shared between siblings.
562 Here is what happens with sort order if we move S1@Q2 one position up:
574 Hopefuly it's enough to understand how it works.
576 Targets from different neighborhood can not be sorted against each other.
580 Returns collection of records of this class with all
581 neighbors. By default all possible targets are neighbors.
583 Takes the same arguments as L</Create> method. If arguments are not passed
584 then uses the current record.
586 See L</Neighbors and Siblings> for detailed description.
588 See L<RT::ObjectCustomField/Neighbors> for example.
594 return $self->CollectionClass->new( $self->CurrentUser );
599 Returns collection of records of this class with siblings.
601 Takes the same arguments as L</Neighbors>. Siblings is subset of L</Neighbors>.
609 my $oid = $args{'ObjectId'};
610 $oid = $self->ObjectId unless defined $oid;
613 my $res = $self->Neighbors( %args );
614 $res->LimitToObjectId( $oid );
615 $res->LimitToObjectId( 0 ) if $oid;
619 RT::Base->_ImportOverlays();