1use strict;
2use warnings;
3
4our @Initial = (
5    sub {
6        # make sure global CFs are not applied to local objects
7        my $ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
8        $ocfs->Limit( FIELD => 'ObjectId', OPERATOR => '!=', VALUE => 0 );
9        my $alias = $ocfs->Join(
10            FIELD1 => 'CustomField',
11            TABLE2 => 'ObjectCustomFields',
12            FIELD2 => 'CustomField',
13        );
14        $ocfs->Limit( ALIAS => $alias, FIELD => 'ObjectId', VALUE => 0 );
15        while ( my $ocf = $ocfs->Next ) {
16            $ocf->Delete;
17        }
18    },
19    sub {
20        # sort SortOrder
21        my $sth = RT->DatabaseHandle->dbh->prepare(
22            "SELECT cfs.LookupType, ocfs.id"
23            ." FROM ObjectCustomFields ocfs, CustomFields cfs"
24            ." WHERE cfs.id = ocfs.CustomField"
25            ." ORDER BY cfs.LookupType, ocfs.SortOrder, cfs.Name"
26        );
27        $sth->execute;
28
29        my ($i, $prev_type) = (0, '');
30        while ( my ($lt, $id) = $sth->fetchrow_array ) {
31            $i = 0 if $prev_type ne $lt;
32            my $ocf = RT::ObjectCustomField->new( RT->SystemUser );
33            $ocf->Load( $id );
34            my ($status, $msg) = $ocf->SetSortOrder( $i++ );
35            RT->Logger->warning("Couldn't set SortOrder: $msg")
36                unless $status;
37            $prev_type = $lt;
38        }
39    },
40);
41
42