Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / etc / upgrade / split-out-cf-categories.in
1 #!@PERL@
2 # BEGIN BPS TAGGED BLOCK {{{
3 #
4 # COPYRIGHT:
5 #
6 # This software is Copyright (c) 1996-2015 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 5.10.1;
50 use strict;
51 use warnings;
52
53 use lib "@LOCAL_LIB_PATH@";
54 use lib "@RT_LIB_PATH@";
55
56 use RT::Interface::CLI qw(Init);
57 Init();
58
59 $RT::Handle->BeginTransaction();
60
61 use RT::CustomFields;
62 my $CFs = RT::CustomFields->new( RT->SystemUser );
63 $CFs->UnLimit;
64 $CFs->Limit( FIELD => 'Type', VALUE => 'Select' );
65
66 my $seen;
67 while (my $cf  = $CFs->Next ) {
68     next if $cf->BasedOnObj->Id;
69     my @categories;
70     my %mapping;
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;
76     }
77     next unless @categories;
78
79     $seen++;
80     print "Found CF '@{[$cf->Name]}' with categories:\n";
81     print "  $_\n" for @categories;
82
83     print "Split this CF's categories into a hierarchical custom field (Y/n)? ";
84     my $dothis = <>;
85     next if $dothis =~ /n/i;
86
87     print "Enter name of CF to create as category ('@{[$cf->Name]} category'): ";
88     my $newname = <>;
89     chomp $newname;
90     $newname = $cf->Name . " category" unless length $newname;
91
92     # bump the CF's sort oder up by one
93     $cf->SetSortOrder( ($cf->SortOrder || 0) + 1 );
94
95     # ..and add a new CF before it
96     my $new = RT::CustomField->new( RT->SystemUser );
97     my ($id, $msg) = $new->Create(
98         Name => $newname,
99         Type => 'Select',
100         MaxValues => 1,
101         LookupType => $cf->LookupType,
102         SortOrder => $cf->SortOrder - 1,
103     );
104     die "Can't create custom field '$newname': $msg" unless $id;
105
106     # Set the CF to be based on what we just made
107     $cf->SetBasedOn( $new->Id );
108
109     # Apply it to all of the same things
110     {
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,
119             );
120             die "Can't create ObjectCustomField: $msg" unless $id;
121         }
122     }
123
124     # Copy over all of the rights
125     {
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,
134                 Object => $new,
135             );
136             die "Can't assign rights: $msg" unless $id;
137         }
138     }
139
140     # Add values for all of the categories
141     for my $i (0..$#categories) {
142         ($id, $msg) = $new->AddValue(
143             Name => $categories[$i],
144             SortOrder => $i + 1,
145         );
146         die "Can't create custom field value: $msg" unless $id;
147     }
148
149     # Grovel through all ObjectCustomFieldValues, and add the
150     # appropriate category
151     {
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},
162             );
163         }
164     }
165 }
166
167 $RT::Handle->Commit;
168 print "No custom fields with categories found\n" unless $seen;