+=head3 MoveDown
+
+Moves custom field down. See </Sorting custom fields applications>.
+
+=cut
+
+sub MoveDown {
+ my $self = shift;
+
+ my $ocfs = RT::ObjectCustomFields->new( $self->CurrentUser );
+
+ my $oid = $self->ObjectId;
+ $ocfs->LimitToObjectId( $oid );
+ if ( $oid ) {
+ $ocfs->LimitToObjectId( 0 );
+ }
+
+ my $cf = $self->CustomFieldObj;
+ $ocfs->LimitToLookupType( $cf->LookupType );
+
+ $ocfs->Limit( FIELD => 'SortOrder', OPERATOR => '>', VALUE => $self->SortOrder );
+ $ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
+
+ my @below = ($ocfs->Next, $ocfs->Next);
+ unless ($below[0]) {
+ return (0, "Can not move down. It's already at the bottom");
+ }
+
+ my $new_sort_order;
+ if ( $below[0]->ObjectId == $self->ObjectId ) {
+ $new_sort_order = $below[0]->SortOrder;
+ my ($status, $msg) = $below[0]->SetSortOrder( $self->SortOrder );
+ unless ( $status ) {
+ return (0, "Couldn't move custom field");
+ }
+ }
+ elsif ( $below[1] && $below[0]->SortOrder + 1 == $below[1]->SortOrder ) {
+ my $move_ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
+ $move_ocfs->LimitToLookupType( $cf->LookupType );
+ $move_ocfs->Limit(
+ FIELD => 'SortOrder',
+ OPERATOR => '<=',
+ VALUE => $below[0]->SortOrder,
+ );
+ $move_ocfs->OrderByCols( { FIELD => 'SortOrder', ORDER => 'ASC' } );
+ while ( my $record = $move_ocfs->Next ) {
+ my ($status, $msg) = $record->SetSortOrder( $record->SortOrder - 1 );
+ unless ( $status ) {
+ return (0, "Couldn't move custom field");
+ }
+ }
+ $new_sort_order = $below[0]->SortOrder;
+ } else {
+ $new_sort_order = $below[0]->SortOrder + 1;
+ }
+
+ my ($status, $msg) = $self->SetSortOrder( $new_sort_order );
+ unless ( $status ) {
+ return (0, "Couldn't move custom field");
+ }
+
+ return (1,"Moved custom field down");
+}