2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2015 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 }}}
53 use lib "@LOCAL_LIB_PATH@";
54 use lib "@RT_LIB_PATH@";
56 use RT::Interface::CLI qw(Init);
59 $RT::Handle->BeginTransaction();
62 my $CFs = RT::CustomFields->new( RT->SystemUser );
64 $CFs->Limit( FIELD => 'Type', VALUE => 'Select' );
67 while (my $cf = $CFs->Next ) {
68 next if $cf->BasedOnObj->Id;
71 my $values = $cf->Values;
72 while (my $value = $values->Next) {
73 next unless defined $value->Category and length $value->Category;
74 push @categories, $value->Category unless grep {$_ eq $value->Category} @categories;
75 $mapping{$value->Name} = $value->Category;
77 next unless @categories;
80 print "Found CF '@{[$cf->Name]}' with categories:\n";
81 print " $_\n" for @categories;
83 print "Split this CF's categories into a hierarchical custom field (Y/n)? ";
85 next if $dothis =~ /n/i;
87 print "Enter name of CF to create as category ('@{[$cf->Name]} category'): ";
90 $newname = $cf->Name . " category" unless length $newname;
92 # bump the CF's sort oder up by one
93 $cf->SetSortOrder( ($cf->SortOrder || 0) + 1 );
95 # ..and add a new CF before it
96 my $new = RT::CustomField->new( RT->SystemUser );
97 my ($id, $msg) = $new->Create(
101 LookupType => $cf->LookupType,
102 SortOrder => $cf->SortOrder - 1,
104 die "Can't create custom field '$newname': $msg" unless $id;
106 # Set the CF to be based on what we just made
107 $cf->SetBasedOn( $new->Id );
109 # Apply it to all of the same things
111 my $ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
112 $ocfs->LimitToCustomField( $cf->Id );
113 while (my $ocf = $ocfs->Next) {
114 my $newocf = RT::ObjectCustomField->new( RT->SystemUser );
115 ($id, $msg) = $newocf->Create(
116 SortOrder => $ocf->SortOrder,
117 CustomField => $new->Id,
118 ObjectId => $ocf->ObjectId,
120 die "Can't create ObjectCustomField: $msg" unless $id;
124 # Copy over all of the rights
126 my $acl = RT::ACL->new( RT->SystemUser );
127 $acl->LimitToObject( $cf );
128 while (my $ace = $acl->Next) {
129 my $newace = RT::ACE->new( RT->SystemUser );
130 ($id, $msg) = $newace->Create(
131 PrincipalId => $ace->PrincipalId,
132 PrincipalType => $ace->PrincipalType,
133 RightName => $ace->RightName,
136 die "Can't assign rights: $msg" unless $id;
140 # Add values for all of the categories
141 for my $i (0..$#categories) {
142 ($id, $msg) = $new->AddValue(
143 Name => $categories[$i],
146 die "Can't create custom field value: $msg" unless $id;
149 # Grovel through all ObjectCustomFieldValues, and add the
150 # appropriate category
152 my $ocfvs = RT::ObjectCustomFieldValues->new( RT->SystemUser );
153 $ocfvs->LimitToCustomField( $cf->Id );
154 while (my $ocfv = $ocfvs->Next) {
155 next unless exists $mapping{$ocfv->Content};
156 my $newocfv = RT::ObjectCustomFieldValue->new( RT->SystemUser );
157 ($id, $msg) = $newocfv->Create(
158 CustomField => $new->Id,
159 ObjectType => $ocfv->ObjectType,
160 ObjectId => $ocfv->ObjectId,
161 Content => $mapping{$ocfv->Content},
168 print "No custom fields with categories found\n" unless $seen;