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;
179 return undef unless $self->CustomFieldObj->CurrentUserHasRight('SeeCustomField');
181 if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
182 return $self->LargeContent;
190 Returns the object this value applies to
196 my $Object = $self->__Value('ObjectType')->new( $self->CurrentUser );
197 $Object->LoadById( $self->__Value('ObjectId') );
204 Disable this value. Used to remove "current" values from records while leaving them in the history.
211 return $self->SetDisabled(1);
214 =head2 _FillInTemplateURL URL
216 Takes a URL containing placeholders and returns the URL as filled in for this
217 ObjectCustomFieldValue. The values for the placeholders will be URI-escaped.
219 Available placeholders:
225 The id of the object in question.
227 =item __CustomField__
229 The value of this custom field for the object in question.
231 =item __WebDomain__, __WebPort__, __WebPath__, __WebBaseURL__ and __WebURL__
233 The value of the config option.
241 id => { value => sub { $_[0]->ObjectId }, escape => 1 },
242 CustomField => { value => sub { $_[0]->Content }, escape => 1 },
243 WebDomain => { value => sub { RT->Config->Get('WebDomain') } },
244 WebPort => { value => sub { RT->Config->Get('WebPort') } },
245 WebPath => { value => sub { RT->Config->Get('WebPath') } },
246 WebBaseURL => { value => sub { RT->Config->Get('WebBaseURL') } },
247 WebURL => { value => sub { RT->Config->Get('WebURL') } },
250 sub _FillInTemplateURL {
254 return undef unless defined $url && length $url;
256 # special case, whole value should be an URL
257 if ( $url =~ /^__CustomField__/ ) {
258 my $value = $self->Content;
259 # protect from potentially malicious URLs
260 if ( $value =~ /^\s*(?:javascript|data):/i ) {
261 my $object = $self->Object;
263 "Potentially dangerous URL type in custom field '". $self->CustomFieldObj->Name ."'"
264 ." on ". ref($object) ." #". $object->id
268 $url =~ s/^__CustomField__/$value/;
271 # default value, uri-escape
272 for my $key (keys %placeholders) {
273 $url =~ s{__${key}__}{
274 my $value = $placeholders{$key}{'value'}->( $self );
275 $value = '' if !defined($value);
276 RT::Interface::Web::EscapeURI(\$value) if $placeholders{$key}{'escape'};
287 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for
288 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
295 return $self->_FillInTemplateURL($self->CustomFieldObj->LinkValueTo);
300 =head2 ValueIncludeURL
302 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for
303 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
304 a IncludeContentForValue
308 sub IncludeContentForValue {
310 return $self->_FillInTemplateURL($self->CustomFieldObj->IncludeContentForValue);