[rt-users] CustomField Bulk Updates

Todd Chapman rt at chaka.net
Tue Nov 30 10:43:54 EST 2004


Here is a patch against 3.2.2. Please test before using for
anything important.

-Todd

Index: html/Ticket/Elements/EditCustomField
===================================================================
--- html/Ticket/Elements/EditCustomField        (revision 1949)
+++ html/Ticket/Elements/EditCustomField        (working copy)
@@ -68,8 +68,8 @@
 <input type="hidden" name="<%$NamePrefix%><%$CustomField->Id%>-Values-Magic" value="1">
 <textarea cols=<%$Cols%> rows=<%$Rows%> name="<%$NamePrefix%><%$CustomField->Id%>-Values"><%$content%></textarea>
 % } elsif ($CustomField->Type =~ /^Select/) {
-      <input type="hidden" name="<%$NamePrefix%><%$CustomField->Id%>-Values-Magic" value="1">
-      <select name="<%$NamePrefix%><%$CustomField->Id%>-Values"
+      <input type="hidden" name="<%$NamePrefix%><%$CustomField->Id%>-Values<% $NameSuffix ? "-$NameSuffix" : ""%>-Ma
gic" value="1">
+      <select name="<%$NamePrefix%><%$CustomField->Id%>-Values<% $NameSuffix ? "-$NameSuffix" : ""%>"
         size="<%$Rows%>"
         <%$CustomField->Type eq 'SelectMultiple' && 'MULTIPLE'%>>
 % my $CustomFieldValues = $CustomField->Values();
@@ -90,6 +90,7 @@
 $TicketObj => undef
 $CustomField => undef
 $NamePrefix => undef
+$NameSuffix => undef
 $Rows => 5
 $Cols=> 15
 $Default => undef
Index: html/Search/Bulk.html
===================================================================
--- html/Search/Bulk.html       (revision 1949)
+++ html/Search/Bulk.html       (working copy)
@@ -171,11 +171,21 @@
 %     }
 <%$pref%> <b><% $cf->Name %></b><br>
 <% $cf->FriendlyType %>
+%     if ($cf->Type =~ /SelectSingle/) {
+<br><&|/l&>Don't update</&><input type=checkbox name="<%$cf->Id%>-DoNotUpdate" CHECKED>
+%     }
 </td>
 <td>
-% if ($cf->Type ne "FreeformMultiple") {
+% if ($cf->Type eq "SelectMultiple") {
+Add Values
+<& /Ticket/Elements/EditCustomField, CustomField => $cf, NameSuffix => 'AddValue' &>
+Delete Values
+<& /Ticket/Elements/EditCustomField, CustomField => $cf, NameSuffix => 'DeleteValues' &>
+% }
+% elsif ($cf->Type ne "FreeformMultiple") {
 <& /Ticket/Elements/EditCustomField, CustomField => $cf &>
-% } else {
+% }
+% else {
 Add Values<br>
 <textarea cols=15 rows=3 name="<%$cf->Id%>-Values"></textarea>
 </td><td>
Index: lib/RT/Interface/Web.pm
===================================================================
--- lib/RT/Interface/Web.pm     (revision 1949)
+++ lib/RT/Interface/Web.pm     (working copy)
@@ -294,8 +294,18 @@
     );
     foreach my $arg (%ARGS) {
         if ($arg =~ /^CustomField-(\d+)(.*?)$/) {
+            my $cfid = $1;
+
             next if ($arg =~ /-Magic$/);
-            $create_args{"CustomField-".$1} = $ARGS{"$arg"};
+            my $cf = new RT::CustomField( $RT::SystemUser );
+            $cf->Load($cfid);
+
+            if ($cf->Type eq 'FreeformMultiple') {
+                $ARGS{$arg} =~ s/\r\n/\n/g;
+                $ARGS{$arg} = [split('\n', $ARGS{$arg})];
+            }
+
+            $create_args{"CustomField-".$cfid} = $ARGS{"$arg"};
         }
     }

@@ -1006,7 +1016,8 @@
         }
     }

-    $ARGSRef->{'Status'} ||= $TicketObj->Status;
+    #$ARGSRef->{'Status'} ||= $TicketObj->Status;
+    delete $ARGSRef->{'Status'} unless ($ARGSRef->{'Status'});

     my @results = UpdateRecordObject(
         AttributesRef => \@attribs,
@@ -1053,50 +1064,55 @@
     my %custom_fields_to_mod;
     foreach my $arg ( keys %{$ARGSRef} ) {
         if ( $arg =~ /^Ticket-(\d+)-CustomField-(\d+)-/ ) {
+            my ($tick, $cf) = ($1, $2);
+            next if $ARGSRef->{"Ticket-$tick-CustomField-$cf-DoNotUpdate"};

+
             # For each of those tickets, find out what custom fields we want to work with.
-            $custom_fields_to_mod{$1}{$2} = 1;
+            $custom_fields_to_mod{$tick}{$cf} = 1;
         }
     }

     # For each of those tickets
     foreach my $tick ( keys %custom_fields_to_mod ) {
         my $Ticket = $args{'TicketObj'};
-       if (!$Ticket or $Ticket->id != $tick) {
-           $Ticket = RT::Ticket->new( $session{'CurrentUser'} );
-           $Ticket->Load($tick);
-       }
+        if (!$Ticket or $Ticket->id != $tick) {
+            $Ticket = RT::Ticket->new( $session{'CurrentUser'} );
+            $Ticket->Load($tick);
+        }

-        # For each custom field
+        # For each custom field
         foreach my $cf ( keys %{ $custom_fields_to_mod{$tick} } ) {

-           my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
-           $CustomFieldObj->LoadById($cf);
+            my $CustomFieldObj = RT::CustomField->new($session{'CurrentUser'});
+            $CustomFieldObj->LoadById($cf);

             foreach my $arg ( keys %{$ARGSRef} ) {
                 # since http won't pass in a form element with a null value, we need
                 # to fake it
-                if ($arg =~ /^(.*?)-Values-Magic$/ ) {
+                if ($arg =~ /^(.*?)-Values(.*)-Magic$/ ) {
                     # We don't care about the magic, if there's really a values element;
-                    next if (exists $ARGSRef->{$1.'-Values'}) ;
+                    next if (exists $ARGSRef->{$1.'-Values'.$2}) ;

-                    $arg = $1."-Values";
-                    $ARGSRef->{$1."-Values"} = undef;
-
+                    $arg = $1."-Values".$2;
+                    $ARGSRef->{$1."-Values".$2} = undef;
+
                 }
                 next unless ( $arg =~ /^Ticket-$tick-CustomField-$cf-/ );
                 my @values =
-                  ( ref( $ARGSRef->{$arg} ) eq 'ARRAY' )
+                  ( ref( $ARGSRef->{$arg} ) eq 'ARRAY' )
                   ? @{ $ARGSRef->{$arg} }
                   : split /\n/, $ARGSRef->{$arg} ;

-               #for poor windows boxen that pass in "\r\n"
-               local $/ = "\r";
-               chomp @values;
+                #for poor windows boxen that pass in "\r\n"
+                local $/ = "\r";
+                chomp @values;

                 if ( ( $arg =~ /-AddValue$/ ) || ( $arg =~ /-Value$/ ) ) {
+                    my $cf_values = $Ticket->CustomFieldValues($cf);
                     foreach my $value (@values) {
                         next unless length($value);
+                        next if $cf_values->HasEntry($value);
                         my ( $val, $msg ) = $Ticket->AddCustomFieldValue(
                             Field => $cf,
                             Value => $value
@@ -1105,8 +1121,10 @@
                     }
                 }
                 elsif ( $arg =~ /-DeleteValues$/ ) {
+                    my $cf_values = $Ticket->CustomFieldValues($cf);
                     foreach my $value (@values) {
                         next unless length($value);
+                        next unless $cf_values->HasEntry($value);
                         my ( $val, $msg ) = $Ticket->DeleteCustomFieldValue(
                             Field => $cf,
                             Value => $value
@@ -1148,29 +1166,30 @@
                 elsif ( $arg =~ /-Values$/ ) {
                     my $cf_values = $Ticket->CustomFieldValues($cf);

-                   # keep everything up to the point of difference, delete the rest
-                   my $delete_flag;
-                   foreach my $old_cf (@{$cf_values->ItemsArrayRef}) {
-                       if (!$delete_flag and @values and $old_cf->Content eq $values[0]) {
-                           shift @values;
-                           next;
-                       }
+                    # keep everything up to the point of difference, delete the rest
+                    my $delete_flag;
+                    foreach my $old_cf (@{$cf_values->ItemsArrayRef}) {
+                        if (!$delete_flag and @values and $old_cf->Content eq $values[0]) {
+                            shift @values;
+                            next;
+                        }

-                       $delete_flag ||= 1;
-                       $old_cf->Delete;
-                   }
+                        $delete_flag ||= 1;
+                        $old_cf->Delete;
+                    }

-                   # now add/replace extra things, if any
-                   foreach my $value (@values) {
-                       my ( $val, $msg ) = $Ticket->AddCustomFieldValue(
-                           Field => $cf,
-                           Value => $value
-                       );
-                       push ( @results, $msg );
-                   }
-               }
+                    # now add/replace extra things, if any
+                    foreach my $value (@values) {
+                        my ( $val, $msg ) = $Ticket->AddCustomFieldValue(
+                            Field => $cf,
+                            Value => $value
+                        );
+                        push ( @results, $msg );
+                    }
+                }
                 else {
-                    push ( @results, "User asked for an unknown update type for custom field " . $cf->Name . " for t
icket " . $Ticket->id );
+                    push ( @results, "User asked for an unknown update type for custom field " . $CustomFieldObj->Na
m
+e . " for ticket " . $Ticket->id );
                 }
             }
         }




More information about the rt-users mailing list