]> git.uio.no Git - usit-rt.git/blob - etc/upgrade/split-out-cf-categories
Master to 4.2.8
[usit-rt.git] / etc / upgrade / split-out-cf-categories
1 #!/usr/bin/perl
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
7 #                                          <sales@bestpractical.com>
8 #
9 # (Except where explicitly superseded by other copyright notices)
10 #
11 #
12 # LICENSE:
13 #
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
17 # from www.gnu.org.
18 #
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.
23 #
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.
29 #
30 #
31 # CONTRIBUTION SUBMISSION POLICY:
32 #
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.)
38 #
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.
47 #
48 # END BPS TAGGED BLOCK }}}
49 use strict;
50 use warnings;
51
52 use lib "local/lib";
53 use lib "lib";
54
55 use RT;
56 RT::LoadConfig();
57 RT->Config->Set('LogToSTDERR' => 'debug');
58 RT::Init();
59
60 $| = 1;
61
62 $RT::Handle->BeginTransaction();
63
64 use RT::CustomFields;
65 my $CFs = RT::CustomFields->new( RT->SystemUser );
66 $CFs->UnLimit;
67 $CFs->Limit( FIELD => 'Type', VALUE => 'Select' );
68
69 my $seen;
70 while (my $cf  = $CFs->Next ) {
71     next if $cf->BasedOnObj->Id;
72     my @categories;
73     my %mapping;
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;
79     }
80     next unless @categories;
81
82     $seen++;
83     print "Found CF '@{[$cf->Name]}' with categories:\n";
84     print "  $_\n" for @categories;
85
86     print "Split this CF's categories into a hierarchical custom field (Y/n)? ";
87     my $dothis = <>;
88     next if $dothis =~ /n/i;
89
90     print "Enter name of CF to create as category ('@{[$cf->Name]} category'): ";
91     my $newname = <>;
92     chomp $newname;
93     $newname = $cf->Name . " category" unless length $newname;
94
95     # bump the CF's sort oder up by one
96     $cf->SetSortOrder( ($cf->SortOrder || 0) + 1 );
97
98     # ..and add a new CF before it
99     my $new = RT::CustomField->new( RT->SystemUser );
100     my ($id, $msg) = $new->Create(
101         Name => $newname,
102         Type => 'Select',
103         MaxValues => 1,
104         LookupType => $cf->LookupType,
105         SortOrder => $cf->SortOrder - 1,
106     );
107     die "Can't create custom field '$newname': $msg" unless $id;
108
109     # Set the CF to be based on what we just made
110     $cf->SetBasedOn( $new->Id );
111
112     # Apply it to all of the same things
113     {
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,
122             );
123             die "Can't create ObjectCustomField: $msg" unless $id;
124         }
125     }
126
127     # Copy over all of the rights
128     {
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,
137                 Object => $new,
138             );
139             die "Can't assign rights: $msg" unless $id;
140         }
141     }
142
143     # Add values for all of the categories
144     for my $i (0..$#categories) {
145         ($id, $msg) = $new->AddValue(
146             Name => $categories[$i],
147             SortOrder => $i + 1,
148         );
149         die "Can't create custom field value: $msg" unless $id;
150     }
151
152     # Grovel through all ObjectCustomFieldValues, and add the
153     # appropriate category
154     {
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},
165             );
166         }
167     }
168 }
169
170 $RT::Handle->Commit;
171 print "No custom fields with categories found\n" unless $seen;