1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2019 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
49 package RT::ObjectCustomFieldValues;
55 use base 'RT::SearchBuilder';
57 use RT::ObjectCustomFieldValue;
59 # Set up the OCFV cache for faster comparison on add/update
63 sub Table { 'ObjectCustomFieldValues'}
68 # By default, order by SortOrder
78 return ( $self->SUPER::_Init(@_) );
83 Cleans out and reinitializes the OCFV cache
91 # {{{ sub LimitToCustomField
93 =head2 LimitToCustomField FIELD
95 Limits the returned set to values for the custom field with Id FIELD
99 sub LimitToCustomField {
103 FIELD => 'CustomField',
110 =head2 LimitToObject OBJECT
112 Limits the returned set to values for the given OBJECT
120 FIELD => 'ObjectType',
121 VALUE => ref($object),
125 VALUE => $object->Id,
131 =head2 HasEntry CONTENT LARGE_CONTENT
133 If this collection has an entry with content that eq CONTENT and large content
134 that eq LARGE_CONTENT then returns the entry, otherwise returns undef.
142 my $large_content = shift;
143 return undef unless defined $value && length $value;
145 my $first = $self->First;
146 return undef unless $first; # No entries to check
148 # Key should be the same for all values of the same ocfv
149 my $ocfv_key = $first->GetOCFVCacheKey;
151 # This cache relieves performance issues when adding large numbers of values
152 # to a CF since each add compares against the full list each time.
154 unless ( $_OCFV_CACHE->{$ocfv_key} ) {
155 # Load the cache with existing values
156 foreach my $item ( @{$self->ItemsArrayRef} ) {
157 push @{$_OCFV_CACHE->{$ocfv_key}}, {
158 'ObjectId' => $item->Id,
159 'CustomFieldObj' => $item->CustomFieldObj,
160 'Content' => $item->_Value('Content'),
161 'LargeContent' => $item->LargeContent };
167 foreach my $item ( @{$_OCFV_CACHE->{$ocfv_key}} ) {
168 my $cf = $item->{'CustomFieldObj'};
169 my $args = $canon_value{ $cf->Type };
171 $args = { Content => $value, LargeContent => $large_content };
172 my ($ok, $msg) = $cf->_CanonicalizeValue( $args );
174 $canon_value{ $cf->Type } = $args;
177 if ( $cf->Type eq 'Select' ) {
178 # select is case insensitive
179 $item_id = $item->{'ObjectId'} if lc $item->{'Content'} eq lc $args->{Content};
182 if ( ($item->{'Content'} // '') eq $args->{Content} ) {
183 if ( defined $item->{'LargeContent'} ) {
184 $item_id = $item->{'ObjectId'}
185 if defined $args->{LargeContent}
186 && $item->{'LargeContent'} eq $args->{LargeContent};
189 $item_id = $item->{'ObjectId'} unless defined $args->{LargeContent};
191 } elsif ( $item->{'LargeContent'} && $args->{Content} ) {
192 $item_id = $item->{'ObjectId'} if ($item->{'LargeContent'} eq $args->{Content});
199 my $ocfv = RT::ObjectCustomFieldValue->new( $self->CurrentUser );
200 my ($ret, $msg) = $ocfv->Load($item_id);
201 RT::Logger->error("Unable to load object custom field value from id: $item_id $msg")
213 # unless we really want to find disabled rows,
214 # make sure we're only finding enabled ones.
215 unless ( $self->{'find_expired_rows'} ) {
216 $self->LimitToEnabled();
219 return $self->SUPER::_DoSearch(@_);
225 # unless we really want to find disabled rows,
226 # make sure we're only finding enabled ones.
227 unless ( $self->{'find_expired_rows'} ) {
228 $self->LimitToEnabled();
231 return $self->SUPER::_DoCount(@_);
234 RT::Base->_ImportOverlays();
236 # Clear the OCVF cache on exit to release connected RT::Ticket objects.
238 # Without this, there could be warnings generated like "Too late to safely run
239 # transaction-batch scrips...". You can test this by commenting it out and running
240 # some cf tests, e.g. perl -Ilib t/customfields/enter_one.t
241 END { ClearOCFVCache(); }