[Rt-commit] rt branch, 4.4/drop-cf-self-category-select, created. rt-4.2.5-208-gff8732c

? sunnavy sunnavy at bestpractical.com
Tue Oct 21 19:41:33 EDT 2014


The branch, 4.4/drop-cf-self-category-select has been created
        at  ff8732ca7b6764145a6a3c3fe1ed08c8ee063ee3 (commit)

- Log -----------------------------------------------------------------
commit ff8732ca7b6764145a6a3c3fe1ed08c8ee063ee3
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Oct 22 07:00:21 2014 +0800

    drop cf its own category select
    
    previously, if a select cf's "categories are based on" is empty, we will show
    an extra <select> to let user filter values by category, which is kinda
    misleading as the extra select is not a real cf, so it's not searchable.
    
    if user wants to use the category select, there is an upgrade step to turn
    categories into real cfs.
    
    Fixes: #30426

diff --git a/etc/upgrade/4.3.4/content b/etc/upgrade/4.3.4/content
new file mode 100644
index 0000000..adabf24
--- /dev/null
+++ b/etc/upgrade/4.3.4/content
@@ -0,0 +1,119 @@
+use strict;
+use warnings;
+
+our @Final = (
+    sub {
+        local $| = 1;
+
+        $RT::Handle->BeginTransaction();
+
+        use RT::CustomFields;
+        my $CFs = RT::CustomFields->new( RT->SystemUser );
+        $CFs->UnLimit;
+        $CFs->Limit( FIELD => 'Type', VALUE => 'Select' );
+
+        my $seen;
+        while (my $cf  = $CFs->Next ) {
+            next if $cf->BasedOnObj->Id;
+            my @categories;
+            my %mapping;
+            my $values = $cf->Values;
+            while (my $value = $values->Next) {
+                next unless defined $value->Category and length $value->Category;
+                push @categories, $value->Category unless grep {$_ eq $value->Category} @categories;
+                $mapping{$value->Name} = $value->Category;
+            }
+            next unless @categories;
+
+            $seen++;
+            print "Found CF '@{[$cf->Name]}' with categories:\n";
+            print "  $_\n" for @categories;
+
+            print "Split this CF's categories into a hierarchical custom field (Y/n)? ";
+            my $dothis = <>;
+            next if $dothis =~ /n/i;
+
+            print "Enter name of CF to create as category ('@{[$cf->Name]} category'): ";
+            my $newname = <>;
+            chomp $newname;
+            $newname = $cf->Name . " category" unless length $newname;
+
+            # bump the CF's sort oder up by one
+            $cf->SetSortOrder( ($cf->SortOrder || 0) + 1 );
+
+            # ..and add a new CF before it
+            my $new = RT::CustomField->new( RT->SystemUser );
+            my ($id, $msg) = $new->Create(
+                Name => $newname,
+                Type => 'Select',
+                MaxValues => 1,
+                LookupType => $cf->LookupType,
+                SortOrder => $cf->SortOrder - 1,
+            );
+            die "Can't create custom field '$newname': $msg" unless $id;
+
+            # Set the CF to be based on what we just made
+            $cf->SetBasedOn( $new->Id );
+
+            # Apply it to all of the same things
+            {
+                my $ocfs = RT::ObjectCustomFields->new( RT->SystemUser );
+                $ocfs->LimitToCustomField( $cf->Id );
+                while (my $ocf = $ocfs->Next) {
+                    my $newocf = RT::ObjectCustomField->new( RT->SystemUser );
+                    ($id, $msg) = $newocf->Create(
+                        SortOrder => $ocf->SortOrder,
+                        CustomField => $new->Id,
+                        ObjectId => $ocf->ObjectId,
+                    );
+                    die "Can't create ObjectCustomField: $msg" unless $id;
+                }
+            }
+
+            # Copy over all of the rights
+            {
+                my $acl = RT::ACL->new( RT->SystemUser );
+                $acl->LimitToObject( $cf );
+                while (my $ace = $acl->Next) {
+                    my $newace = RT::ACE->new( RT->SystemUser );
+                    ($id, $msg) = $newace->Create(
+                        PrincipalId => $ace->PrincipalId,
+                        PrincipalType => $ace->PrincipalType,
+                        RightName => $ace->RightName,
+                        Object => $new,
+                    );
+                    die "Can't assign rights: $msg" unless $id;
+                }
+            }
+
+            # Add values for all of the categories
+            for my $i (0..$#categories) {
+                ($id, $msg) = $new->AddValue(
+                    Name => $categories[$i],
+                    SortOrder => $i + 1,
+                );
+                die "Can't create custom field value: $msg" unless $id;
+            }
+
+            # Grovel through all ObjectCustomFieldValues, and add the
+            # appropriate category
+            {
+                my $ocfvs = RT::ObjectCustomFieldValues->new( RT->SystemUser );
+                $ocfvs->LimitToCustomField( $cf->Id );
+                while (my $ocfv = $ocfvs->Next) {
+                    next unless exists $mapping{$ocfv->Content};
+                    my $newocfv = RT::ObjectCustomFieldValue->new( RT->SystemUser );
+                    ($id, $msg) = $newocfv->Create(
+                        CustomField => $new->Id,
+                        ObjectType => $ocfv->ObjectType,
+                        ObjectId   => $ocfv->ObjectId,
+                        Content    => $mapping{$ocfv->Content},
+                    );
+                }
+            }
+        }
+
+        $RT::Handle->Commit;
+        print "No custom fields with categories found\n" unless $seen;
+    },
+);
diff --git a/share/html/Elements/EditCustomFieldSelect b/share/html/Elements/EditCustomFieldSelect
index e1c010b..0b9ebc5 100644
--- a/share/html/Elements/EditCustomFieldSelect
+++ b/share/html/Elements/EditCustomFieldSelect
@@ -51,16 +51,7 @@
 % my $selected = 0;
 % my @category;
 % my $out = $m->scomp('SELF:options', %ARGS, SelectedRef => \$selected, CategoryRef => \@category);
-% if (!$HideCategory and @category and not $CustomField->BasedOnObj->id) {
-%# XXX - Hide this select from w3m?
-  <select onchange="filter_cascade_by_id(<% $name |n,j %>, this.value)" name="<% $name %>-Category" class="CF-<%$CustomField->id%>-Edit">
-    <option value=""<% !$selected && qq[ selected="selected"] |n %>><&|/l&>-</&></option>
-%   foreach my $cat (@category) {
-%     my ($depth, $name) = @$cat;
-      <option value="<% $name %>"><% ' ' x $depth |n %><% $name %></option>
-%   }
-    </select><br />
-% } elsif ($CustomField->BasedOnObj->id) {
+% if ($CustomField->BasedOnObj->id) {
 
 <script type="text/javascript"><!--
 jQuery(  function () {
diff --git a/share/static/js/cascaded.js b/share/static/js/cascaded.js
index c5ea64a..70d6ef8 100644
--- a/share/static/js/cascaded.js
+++ b/share/static/js/cascaded.js
@@ -1,19 +1,18 @@
-function filter_cascade_by_id (id, vals, is_hierarchical) {
+function filter_cascade_by_id (id, vals) {
     var element = document.getElementById(id);
     if (!element) { return };
 
     if ( element.tagName == 'SELECT' ) {
         var complete_select = document.getElementById(id + "-Complete" );
-        return filter_cascade_select(element, complete_select, vals, is_hierarchical);
+        return filter_cascade_select(element, complete_select, vals );
     }
     else {
         if ( !( vals instanceof Array ) ) {
             vals = [vals];
         }
 
-        if ( is_hierarchical && (vals.length == 0 || (vals.length == 1 && vals[0] == '')) ) {
-            // no category, and the category is from a hierchical cf;
-            // leave it empty
+        if ( vals.length == 0 || (vals.length == 1 && vals[0] == '') ) {
+            // no category, leave it empty
             jQuery(element).find('div').hide();
         }
         else {
@@ -27,7 +26,7 @@ function filter_cascade_by_id (id, vals, is_hierarchical) {
     }
 }
 
-function filter_cascade_select (select, complete_select, vals, is_hierarchical) {
+function filter_cascade_select (select, complete_select, vals) {
     if ( !( vals instanceof Array ) ) {
         vals = [vals];
     }
@@ -45,13 +44,8 @@ function filter_cascade_select (select, complete_select, vals, is_hierarchical)
         var cloned_empty_label;
         for ( var j = 0; j < vals.length; j++ ) {
             var val = vals[j];
-            if ( val == '' && is_hierarchical ) {
-                // no category, and the category is from a hierchical cf;
-                // leave this set of options empty
-            } else if ( val == '' ) {
-                // no category, let's clone all node
-                jQuery(select).append(jQuery(complete_children).clone());
-                break;
+            if ( val == '' ) {
+                // no category, leave this set of options empty
             }
             else {
                 var labels_to_clone = {};
@@ -90,7 +84,7 @@ function filter_cascade_select (select, complete_select, vals, is_hierarchical)
 // for back compatibility
         for (i = 0; i < children.length; i++) {
             if (!children[i].label) { continue };
-            if ( val == '' && is_hierarchical ) {
+            if ( val == '' ) {
                 hide(children[i]);
                 continue;
             }

-----------------------------------------------------------------------


More information about the rt-commit mailing list