import torrus 1.0.9
[freeside.git] / rt / lib / RT / ObjectCustomFieldValue_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
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
16 # from www.gnu.org.
17
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.
22
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.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
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.)
37
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.
46
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::ObjectCustomFieldValue;
50
51 use strict;
52 use warnings;
53 use RT::Interface::Web;
54
55 no warnings qw(redefine);
56
57 sub Create {
58     my $self = shift;
59     my %args = (
60         CustomField     => 0,
61         ObjectType      => '',
62         ObjectId        => 0,
63         Disabled        => 0,
64         Content         => '',
65         LargeContent    => undef,
66         ContentType     => '',
67         ContentEncoding => '',
68         @_,
69     );
70
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");
74         }
75         else {
76             $args{'LargeContent'} = $args{'Content'};
77             $args{'Content'} = '';
78             $args{'ContentType'} ||= 'text/plain';
79         }
80     }
81
82     ( $args{'ContentEncoding'}, $args{'LargeContent'} ) =
83         $self->_EncodeLOB( $args{'LargeContent'}, $args{'ContentType'} )
84             if defined $args{'LargeContent'};
85
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'},
95     );
96 }
97
98
99 sub LargeContent {
100     my $self = shift;
101     return $self->_DecodeLOB(
102         $self->ContentType,
103         $self->ContentEncoding,
104         $self->_Value( 'LargeContent', decode_utf8 => 0 )
105     );
106 }
107
108 =head2 LoadByTicketContentAndCustomField { Ticket => TICKET, CustomField => CUSTOMFIELD, Content => CONTENT }
109
110 Loads a custom field value by Ticket, Content and which CustomField it's tied to
111
112 =cut
113
114
115 sub LoadByTicketContentAndCustomField {
116     my $self = shift;
117     my %args = (
118         Ticket => undef,
119         CustomField => undef,
120         Content => undef,
121         @_
122     );
123
124     return $self->LoadByCols(
125         Content => $args{'Content'},
126         CustomField => $args{'CustomField'},
127         ObjectType => 'RT::Ticket',
128         ObjectId => $args{'Ticket'},
129         Disabled => 0
130     );
131 }
132
133 sub LoadByObjectContentAndCustomField {
134     my $self = shift;
135     my %args = (
136         Object => undef,
137         CustomField => undef,
138         Content => undef,
139         @_
140     );
141
142     my $obj = $args{'Object'} or return;
143
144     return $self->LoadByCols(
145         Content => $args{'Content'},
146         CustomField => $args{'CustomField'},
147         ObjectType => ref($obj),
148         ObjectId => $obj->Id,
149         Disabled => 0
150     );
151 }
152
153 =head2 CustomFieldObj
154
155 Returns the CustomField Object which has the id returned by CustomField
156
157 =cut
158
159 sub CustomFieldObj {
160     my $self = shift;
161     my $CustomField = RT::CustomField->new( $self->CurrentUser );
162     $CustomField->SetContextObject( $self->Object );
163     $CustomField->Load( $self->__Value('CustomField') );
164     return $CustomField;
165 }
166
167
168 =head2 Content
169
170 Return this custom field's content. If there's no "regular"
171 content, try "LargeContent"
172
173 =cut
174
175 sub Content {
176     my $self = shift;
177     my $content = $self->SUPER::Content;
178     if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
179         return $self->LargeContent;
180     } else {
181         return $content;
182     }
183 }
184
185 =head2 Object
186
187 Returns the object this value applies to
188
189 =cut
190
191 sub Object {
192     my $self  = shift;
193     my $Object = $self->__Value('ObjectType')->new( $self->CurrentUser );
194     $Object->LoadById( $self->__Value('ObjectId') );
195     return $Object;
196 }
197
198
199 =head2 Delete
200
201 Disable this value. Used to remove "current" values from records while leaving them in the history.
202
203 =cut
204
205
206 sub Delete {
207     my $self = shift;
208     return $self->SetDisabled(1);
209 }
210
211 =head2 _FillInTemplateURL URL
212
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.
215
216 Available placeholders:
217
218 =over
219
220 =item __id__
221
222 The id of the object in question.
223
224 =item __CustomField__
225
226 The value of this custom field for the object in question.
227
228 =item __WebDomain__, __WebPort__, __WebPath__, __WebBaseURL__ and __WebURL__
229
230 The value of the config option.
231
232 =back
233
234 =cut
235
236 {
237 my %placeholders = (
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') } },
245 );
246
247 sub _FillInTemplateURL {
248     my $self = shift;
249     my $url = shift;
250
251     return undef unless defined $url && length $url;
252
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;
259             $RT::Logger->error(
260                 "Dangerouse value with JavaScript in custom field '". $self->CustomFieldObj->Name ."'"
261                 ." on ". ref($object) ." #". $object->id
262             );
263             return undef;
264         }
265         $url =~ s/^__CustomField__/$value/;
266     }
267
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'};
274             $value
275         }gxe;
276     }
277
278     return $url;
279 } }
280
281
282 =head2 ValueLinkURL
283
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
286 a LinkValueTo
287
288 =cut
289
290 sub LinkValueTo {
291     my $self = shift;
292     return $self->_FillInTemplateURL($self->CustomFieldObj->LinkValueTo);
293 }
294
295
296
297 =head2 ValueIncludeURL
298
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
302
303 =cut
304
305 sub IncludeContentForValue {
306     my $self = shift;
307     return $self->_FillInTemplateURL($self->CustomFieldObj->IncludeContentForValue);
308 }
309
310 1;