import rt 3.8.7
[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
154 =head2 Content
155
156 Return this custom field's content. If there's no "regular"
157 content, try "LargeContent"
158
159 =cut
160
161 sub Content {
162     my $self = shift;
163     my $content = $self->SUPER::Content;
164     if ( !(defined $content && length $content) && $self->ContentType && $self->ContentType eq 'text/plain' ) {
165         return $self->LargeContent;
166     } else {
167         return $content;
168     }
169 }
170
171 =head2 Object
172
173 Returns the object this value applies to
174
175 =cut
176
177 sub Object {
178     my $self  = shift;
179     my $Object = $self->__Value('ObjectType')->new( $self->CurrentUser );
180     $Object->LoadById( $self->__Value('ObjectId') );
181     return $Object;
182 }
183
184
185 =head2 Delete
186
187 Disable this value. Used to remove "current" values from records while leaving them in the history.
188
189 =cut
190
191
192 sub Delete {
193     my $self = shift;
194     return $self->SetDisabled(1);
195 }
196
197 =head2 _FillInTemplateURL URL
198
199 Takes a URL containing placeholders and returns the URL as filled in for this 
200 ObjectCustomFieldValue. The values for the placeholders will be URI-escaped.
201
202 Available placeholders:
203
204 =over
205
206 =item __id__
207
208 The id of the object in question.
209
210 =item __CustomField__
211
212 The value of this custom field for the object in question.
213
214 =item __WebDomain__, __WebPort__, __WebPath__, __WebBaseURL__ and __WebURL__
215
216 The value of the config option.
217
218 =back
219
220 =cut
221
222 {
223 my %placeholders = (
224     id          => { value => sub { $_[0]->ObjectId }, escape => 1 },
225     CustomField => { value => sub { $_[0]->Content }, escape => 1 },
226     WebDomain   => { value => sub { RT->Config->Get('WebDomain') } },
227     WebPort     => { value => sub { RT->Config->Get('WebPort') } },
228     WebPath     => { value => sub { RT->Config->Get('WebPath') } },
229     WebBaseURL  => { value => sub { RT->Config->Get('WebBaseURL') } },
230     WebURL      => { value => sub { RT->Config->Get('WebURL') } },
231 );
232
233 sub _FillInTemplateURL {
234     my $self = shift;
235     my $url = shift;
236
237     # default value, uri-escape
238     for my $key (keys %placeholders) {
239         $url =~ s{__${key}__}{
240             my $value = $placeholders{$key}{'value'}->( $self );
241             $value = '' if !defined($value);
242             RT::Interface::Web::EscapeURI(\$value) if $placeholders{$key}{'escape'};
243             $value
244         }gxe;
245     }
246
247     return $url;
248 } }
249
250
251 =head2 ValueLinkURL
252
253 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for 
254 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
255 a LinkValueTo
256
257 =cut
258
259 sub LinkValueTo {
260     my $self = shift;
261     return $self->_FillInTemplateURL($self->CustomFieldObj->LinkValueTo);
262 }
263
264
265
266 =head2 ValueIncludeURL
267
268 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for 
269 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
270 a IncludeContentForValue
271
272 =cut
273
274 sub IncludeContentForValue {
275     my $self = shift;
276     return $self->_FillInTemplateURL($self->CustomFieldObj->IncludeContentForValue);
277 }
278
279 1;