[Rt-commit] r2180 - in rt/branches/PLATANO-EXPERIMENTAL-CSS: . lib/RT lib/t/regression

jesse at bestpractical.com jesse at bestpractical.com
Wed Feb 2 11:26:42 EST 2005


Author: jesse
Date: Wed Feb  2 11:26:41 2005
New Revision: 2180

Added:
   rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/t/regression/15cf_single_values_are_single.t
Modified:
   rt/branches/PLATANO-EXPERIMENTAL-CSS/   (props changed)
   rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/CustomField_Overlay.pm
   rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/Record.pm
Log:
 r4017 at hualien:  jesse | 2005-02-02T15:40:38.080093Z
  r3909 at hualien:  jesse | 2005-01-22T15:42:46.936767Z
  Bullet-proofing for custom fields with a set but limited number of values
 


Modified: rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/CustomField_Overlay.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/CustomField_Overlay.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/CustomField_Overlay.pm	Wed Feb  2 11:26:41 2005
@@ -894,39 +894,84 @@
 
 =head2 AddValueForObject HASH
 
-Adds a custom field value for a ticket. Takes a param hash of Object and Content
+Adds a custom field value for a record object of some kind. 
+Takes a param hash of 
 
-=cut
-
-sub AddValueForObject {
-	my $self = shift;
-	my %args = ( Object => undef,
-                 Content => undef,
-		 LargeContent => undef,
-		 ContentType => undef,
-		     @_ );
-	my $obj = $args{'Object'} or return;
+Required:
 
+    Object
+    Content
 
-    unless ($self->CurrentUserHasRight('ModifyCustomField')) {
-        return (0, $self->loc('Permission Denied'));
-    }
+Optional:
 
+    LargeContent
+    ContentType
 
+=cut
 
-	my $newval = RT::ObjectCustomFieldValue->new($self->CurrentUser);
-	my $val = $newval->Create(ObjectType => ref($obj),
-	                    ObjectId => $obj->Id,
-                            Content => $args{'Content'},
-                            LargeContent => $args{'LargeContent'},
-                            ContentType => $args{'ContentType'},
-                            CustomField => $self->Id);
+sub AddValueForObject {
+    my $self = shift;
+    my %args = (
+        Object       => undef,
+        Content      => undef,
+        LargeContent => undef,
+        ContentType  => undef,
+        @_
+    );
+    my $obj = $args{'Object'} or return;
+
+    unless ( $self->CurrentUserHasRight('ModifyCustomField') ) {
+        return ( 0, $self->loc('Permission Denied') );
+    }
+
+    $RT::Handle->BeginTransaction;
+
+    my $current_values = $self->ValuesForObject($obj);
+
+    if ( $self->MaxValues ) {
+        my $extra_values = ( $current_values->Count + 1 ) - $self->MaxValues;
+
+        # (The +1 is for the new value we're adding)
+
+        # If we have a set of current values and we've gone over the maximum
+        # allowed number of values, we'll need to delete some to make room.
+        # which former values are blown away is not guaranteed
+
+        while ($extra_values) {
+            my $extra_item = $current_values->Next;
+
+            unless ( $extra_item->id ) {
+                $RT::Logger->crit(
+"We were just asked to delete a custom fieldvalue that doesn't exist!"
+                );
+                $RT::Handle->Rollback();
+                return (undef);
+            }
+            $extra_item->Delete;
+            $extra_values--;
+
+        }
+    }
+    my $newval = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
+    my $val    = $newval->Create(
+        ObjectType   => ref($obj),
+        ObjectId     => $obj->Id,
+        Content      => $args{'Content'},
+        LargeContent => $args{'LargeContent'},
+        ContentType  => $args{'ContentType'},
+        CustomField  => $self->Id
+    );
+
+    unless ($val) {
+        $RT::Handle->Rollback();
+        return ($val);
+    }
 
-    return($val);
+    $RT::Handle->Commit();
+    return ($val);
 
 }
 
-
 # }}}
 
 # {{{ DeleteValueForObject

Modified: rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/Record.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/Record.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/RT/Record.pm	Wed Feb  2 11:26:41 2005
@@ -1528,17 +1528,17 @@
         return ( 0, $self->loc("Invalid value for custom field") );
     }
 
-    # If the custom field only accepts a single value, delete the existing
+    # If the custom field only accepts a certain # of values, delete the existing
     # value and record a "changed from foo to bar" transaction
-    if ( $cf->SingleValue ) {
+    unless ( $cf->UnlimitedValues) {
 
  # We need to whack any old values here.  In most cases, the custom field should
  # only have one value to delete.  In the pathalogical case, this custom field
  # used to be a multiple and we have many values to whack....
         my $cf_values = $values->Count;
 
-        if ( $cf_values > 1 ) {
-            my $i = 0;   #We want to delete all but the last one, so we can then
+        if ( $cf_values > $cf->MaxValues ) {
+            my $i = 0;   #We want to delete all but the max we can currently have , so we can then
                  # execute the same code to "change" the value from old to new
             while ( my $value = $values->Next ) {
                 $i++;
@@ -1574,13 +1574,7 @@
         );
 
         unless ($new_value_id) {
-            return (
-                0,
-                $self->loc(
-                    "Could not add new custom field value. [_1] ",,
-                    $value_msg
-                )
-            );
+            return ( 0, $self->loc( "Could not add new custom field value. [_1] ",, $value_msg));
         }
 
         my $new_value = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
@@ -1605,22 +1599,14 @@
         }
 
         if ( $old_value eq '' ) {
-            return ( 1,
-                $self->loc( "[_1] [_2] added", $cf->Name, $new_value->Content )
-            );
+            return ( 1, $self->loc( "[_1] [_2] added", $cf->Name, $new_value->Content ));
         }
         elsif ( $new_value->Content eq '' ) {
             return ( 1,
                 $self->loc( "[_1] [_2] deleted", $cf->Name, $old_value->Content ) );
         }
         else {
-            return (
-                1,
-                $self->loc(
-                    "[_1] [_2] changed to [_3]", $cf->Name,
-                    $old_content,                $new_value->Content
-                )
-            );
+            return ( 1, $self->loc( "[_1] [_2] changed to [_3]", $cf->Name, $old_content,                $new_value->Content));
         }
 
     }
@@ -1650,13 +1636,7 @@
                     $self->loc( "Couldn't create a transaction: [_1]", $Msg ) );
             }
         }
-        return (
-            1,
-            $self->loc(
-                "[_1] added as a value for [_2]",
-                $args{'Value'}, $cf->Name
-            )
-        );
+        return ( 1, $self->loc( "[_1] added as a value for [_2]", $args{'Value'}, $cf->Name));
     }
 
 }

Added: rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/t/regression/15cf_single_values_are_single.t
==============================================================================
--- (empty file)
+++ rt/branches/PLATANO-EXPERIMENTAL-CSS/lib/t/regression/15cf_single_values_are_single.t	Wed Feb  2 11:26:41 2005
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use Test::More qw/no_plan/;
+
+use RT;
+RT::LoadConfig();
+RT::Init();
+
+
+my $q = RT::Queue->new($RT::SystemUser);
+my ($id,$msg) =$q->Create(Name => "CF-Single-".$$);
+ok($id,$msg);
+
+my $cf = RT::CustomField->new($RT::SystemUser);
+($id,$msg) = $cf->Create(Name => 'Single-'.$$, Type => 'Select', MaxValues => '1', Queue => $q->id);
+ok($id,$msg);
+
+
+($id,$msg) =$cf->AddValue(Name => 'First');
+ok($id,$msg);
+
+($id,$msg) =$cf->AddValue(Name => 'Second');
+ok($id,$msg);
+
+
+my $t = RT::Ticket->new($RT::SystemUser);
+($id,undef,$msg) = $t->Create(Queue => $q->id,
+          Subject => 'CF Test');
+
+ok($id,$msg);
+is($t->CustomFieldValues($cf->id)->Count, 0, "No values yet");
+$t->AddCustomFieldValue(Field => $cf->id, Value => 'First');
+is($t->CustomFieldValues($cf->id)->Count, 1, "One now");
+
+$t->AddCustomFieldValue(Field => $cf->id, Value => 'Second');
+is($t->CustomFieldValues($cf->id)->Count, 1, "Still one");
+
+1;


More information about the Rt-commit mailing list