Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / ObjectCustomFieldValues.pm
index 486265e..527ce7b 100644 (file)
@@ -2,7 +2,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (Except where explicitly superseded by other copyright notices)
@@ -51,11 +51,10 @@ package RT::ObjectCustomFieldValues;
 use strict;
 use warnings;
 
+use base 'RT::SearchBuilder';
 
 use RT::ObjectCustomFieldValue;
 
-use base 'RT::SearchBuilder';
-
 sub Table { 'ObjectCustomFieldValues'}
 
 sub _Init {
@@ -63,12 +62,12 @@ sub _Init {
 
   # By default, order by SortOrder
   $self->OrderByCols(
-        { ALIAS => 'main',
-          FIELD => 'SortOrder',
-          ORDER => 'ASC' },
-        { ALIAS => 'main',
-          FIELD => 'id',
-          ORDER => 'ASC' },
+         { ALIAS => 'main',
+           FIELD => 'SortOrder',
+           ORDER => 'ASC' },
+         { ALIAS => 'main',
+           FIELD => 'id',
+           ORDER => 'ASC' },
      );
 
     return ( $self->SUPER::_Init(@_) );
@@ -115,10 +114,10 @@ sub LimitToObject {
 }
 
 
-=head2 HasEntry VALUE
+=head2 HasEntry CONTENT LARGE_CONTENT
 
-If this collection has an entry with content that eq VALUE then
-returns the entry, otherwise returns undef.
+If this collection has an entry with content that eq CONTENT and large content
+that eq LARGE_CONTENT then returns the entry, otherwise returns undef.
 
 =cut
 
@@ -126,11 +125,39 @@ returns the entry, otherwise returns undef.
 sub HasEntry {
     my $self = shift;
     my $value = shift;
+    my $large_content = shift;
     return undef unless defined $value && length $value;
 
+    my %canon_value;
     #TODO: this could cache and optimize a fair bit.
     foreach my $item ( @{$self->ItemsArrayRef} ) {
-        return $item if lc $item->Content eq lc $value;
+        my $cf = $item->CustomFieldObj;
+        my $args = $canon_value{ $cf->Type };
+        if ( !$args ) {
+            $args = { Content => $value, LargeContent => $large_content };
+            my ($ok, $msg) = $cf->_CanonicalizeValue( $args );
+            next unless $ok;
+            $canon_value{ $cf->Type } = $args;
+        }
+
+        if ( $cf->Type eq 'Select' ) {
+            # select is case insensitive
+            return $item if lc $item->Content eq lc $args->{Content};
+        }
+        else {
+            if ( ($item->_Value('Content') || '') eq $args->{Content} ) {
+                if ( defined $item->LargeContent ) {
+                    return $item
+                      if defined $args->{LargeContent}
+                      && $item->LargeContent eq $args->{LargeContent};
+                }
+                else {
+                    return $item unless defined $args->{LargeContent};
+                }
+            } elsif ( $item->LargeContent && $args->{Content} ) {
+                return $item if ($item->LargeContent eq $args->{Content});
+            }
+        }
     }
     return undef;
 }
@@ -159,17 +186,6 @@ sub _DoCount {
     return $self->SUPER::_DoCount(@_);
 }
 
-
-=head2 NewItem
-
-Returns an empty new RT::ObjectCustomFieldValue item
-
-=cut
-
-sub NewItem {
-    my $self = shift;
-    return(RT::ObjectCustomFieldValue->new($self->CurrentUser));
-}
 RT::Base->_ImportOverlays();
 
 1;