2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
52 use lib "@LOCAL_LIB_PATH@";
53 use lib "@RT_LIB_PATH@";
57 RT->Config->Set('LogToScreen' => 'debug');
62 $RT::Handle->BeginTransaction();
65 my $CFs = RT::CustomFields->new( RT->SystemUser );
67 $CFs->Limit( FIELD => 'Type', VALUE => 'Select' );
70 while (my $cf = $CFs->Next ) {
71 next if $cf->BasedOnObj->Id;
74 my $values = $cf->Values;
75 while (my $value = $values->Next) {
76 next unless defined $value->Category and length $value->Category;
77 push @categories, $value->Category unless grep {$_ eq $value->Category} @categories;
78 $mapping{$value->Name} = $value->Category;
80 next unless @categories;
83 print "Found CF '@{[$cf->Name]}' with categories:\n";
84 print " $_\n" for @categories;
86 print "Split this CF's categories into a hierarchical custom field (Y/n)? ";
88 next if $dothis =~ /n/i;
90 print "Enter name of CF to create as category ('@{[$cf->Name]} category'): ";
93 $newname = $cf->Name . " category" unless length $newname;
95 # bump the CF's sort oder up by one
96 $cf->SetSortOrder( ($cf->SortOrder || 0) + 1 );
98 # ..and add a new CF before it
99 my $new = RT::CustomField->new( RT->SystemUser );
100 my ($id, $msg) = $new->Create(
104 LookupType => $cf->LookupType,
105 SortOrder => $cf->SortOrder - 1,
107 die "Can't create custom field '$newname': $msg" unless $id;
109 # Set the CF to be based on what we just made
110 $cf->SetBasedOn( $new->Id );
112 # Apply it to all of the same things
114 my $ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
115 $ocfs->LimitToCustomField( $cf->Id );
116 while (my $ocf = $ocfs->Next) {
117 my $newocf = RT::ObjectCustomField->new( RT->SystemUser );
118 ($id, $msg) = $newocf->Create(
119 SortOrder => $ocf->SortOrder,
120 CustomField => $new->Id,
121 ObjectId => $ocf->ObjectId,
123 die "Can't create ObjectCustomField: $msg" unless $id;
127 # Copy over all of the rights
129 my $acl = RT::ACL->new( RT->SystemUser );
130 $acl->LimitToObject( $cf );
131 while (my $ace = $acl->Next) {
132 my $newace = RT::ACE->new( RT->SystemUser );
133 ($id, $msg) = $newace->Create(
134 PrincipalId => $ace->PrincipalId,
135 PrincipalType => $ace->PrincipalType,
136 RightName => $ace->RightName,
139 die "Can't assign rights: $msg" unless $id;
143 # Add values for all of the categories
144 for my $i (0..$#categories) {
145 ($id, $msg) = $new->AddValue(
146 Name => $categories[$i],
149 die "Can't create custom field value: $msg" unless $id;
152 # Grovel through all ObjectCustomFieldValues, and add the
153 # appropriate category
155 my $ocfvs = RT::ObjectCustomFieldValues->new( RT->SystemUser );
156 $ocfvs->LimitToCustomField( $cf->Id );
157 while (my $ocfv = $ocfvs->Next) {
158 next unless exists $mapping{$ocfv->Content};
159 my $newocfv = RT::ObjectCustomFieldValue->new( RT->SystemUser );
160 ($id, $msg) = $newocfv->Create(
161 CustomField => $new->Id,
162 ObjectType => $ocfv->ObjectType,
163 ObjectId => $ocfv->ObjectId,
164 Content => $mapping{$ocfv->Content},
171 print "No custom fields with categories found\n" unless $seen;