1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 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::ObjectCustomFieldValue;
53 use RT::Interface::Web;
55 no warnings qw(redefine);
65 LargeContent => undef,
67 ContentEncoding => '',
71 if ( defined $args{'Content'} && length( Encode::encode_utf8($args{'Content'}) ) > 255 ) {
72 if ( defined $args{'LargeContent'} && length $args{'LargeContent'} ) {
73 $RT::Logger->error("Content is longer than 255 bytes and LargeContent specified");
76 $args{'LargeContent'} = $args{'Content'};
77 $args{'Content'} = '';
78 $args{'ContentType'} ||= 'text/plain';
82 ( $args{'ContentEncoding'}, $args{'LargeContent'} ) =
83 $self->_EncodeLOB( $args{'LargeContent'}, $args{'ContentType'} )
84 if defined $args{'LargeContent'};
86 return $self->SUPER::Create(
87 CustomField => $args{'CustomField'},
88 ObjectType => $args{'ObjectType'},
89 ObjectId => $args{'ObjectId'},
90 Disabled => $args{'Disabled'},
91 Content => $args{'Content'},
92 LargeContent => $args{'LargeContent'},
93 ContentType => $args{'ContentType'},
94 ContentEncoding => $args{'ContentEncoding'},
101 return $self->_DecodeLOB(
103 $self->ContentEncoding,
104 $self->_Value( 'LargeContent', decode_utf8 => 0 )
108 =head2 LoadByTicketContentAndCustomField { Ticket => TICKET, CustomField => CUSTOMFIELD, Content => CONTENT }
110 Loads a custom field value by Ticket, Content and which CustomField it's tied to
115 sub LoadByTicketContentAndCustomField {
119 CustomField => undef,
124 return $self->LoadByCols(
125 Content => $args{'Content'},
126 CustomField => $args{'CustomField'},
127 ObjectType => 'RT::Ticket',
128 ObjectId => $args{'Ticket'},
133 sub LoadByObjectContentAndCustomField {
137 CustomField => undef,
142 my $obj = $args{'Object'} or return;
144 return $self->LoadByCols(
145 Content => $args{'Content'},
146 CustomField => $args{'CustomField'},
147 ObjectType => ref($obj),
148 ObjectId => $obj->Id,
153 =head2 CustomFieldObj
155 Returns the CustomField Object which has the id returned by CustomField
161 my $CustomField = RT::CustomField->new( $self->CurrentUser );
162 $CustomField->SetContextObject( $self->Object );
163 $CustomField->Load( $self->__Value('CustomField') );
170 Return this custom field's content. If there's no "regular"
171 content, try "LargeContent"
177 my $content = $self->SUPER::Content;
178 if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
179 return $self->LargeContent;
187 Returns the object this value applies to
193 my $Object = $self->__Value('ObjectType')->new( $self->CurrentUser );
194 $Object->LoadById( $self->__Value('ObjectId') );
201 Disable this value. Used to remove "current" values from records while leaving them in the history.
208 return $self->SetDisabled(1);
211 =head2 _FillInTemplateURL URL
213 Takes a URL containing placeholders and returns the URL as filled in for this
214 ObjectCustomFieldValue. The values for the placeholders will be URI-escaped.
216 Available placeholders:
222 The id of the object in question.
224 =item __CustomField__
226 The value of this custom field for the object in question.
228 =item __WebDomain__, __WebPort__, __WebPath__, __WebBaseURL__ and __WebURL__
230 The value of the config option.
238 id => { value => sub { $_[0]->ObjectId }, escape => 1 },
239 CustomField => { value => sub { $_[0]->Content }, escape => 1 },
240 WebDomain => { value => sub { RT->Config->Get('WebDomain') } },
241 WebPort => { value => sub { RT->Config->Get('WebPort') } },
242 WebPath => { value => sub { RT->Config->Get('WebPath') } },
243 WebBaseURL => { value => sub { RT->Config->Get('WebBaseURL') } },
244 WebURL => { value => sub { RT->Config->Get('WebURL') } },
247 sub _FillInTemplateURL {
251 return undef unless defined $url && length $url;
253 # special case, whole value should be an URL
254 if ( $url =~ /^__CustomField__/ ) {
255 my $value = $self->Content;
256 # protect from javascript: URLs
257 if ( $value =~ /^\s*javascript:/i ) {
258 my $object = $self->Object;
260 "Dangerouse value with JavaScript in custom field '". $self->CustomFieldObj->Name ."'"
261 ." on ". ref($object) ." #". $object->id
265 $url =~ s/^__CustomField__/$value/;
268 # default value, uri-escape
269 for my $key (keys %placeholders) {
270 $url =~ s{__${key}__}{
271 my $value = $placeholders{$key}{'value'}->( $self );
272 $value = '' if !defined($value);
273 RT::Interface::Web::EscapeURI(\$value) if $placeholders{$key}{'escape'};
284 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for
285 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
292 return $self->_FillInTemplateURL($self->CustomFieldObj->LinkValueTo);
297 =head2 ValueIncludeURL
299 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for
300 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
301 a IncludeContentForValue
305 sub IncludeContentForValue {
307 return $self->_FillInTemplateURL($self->CustomFieldObj->IncludeContentForValue);