import rt 3.6.4
[freeside.git] / rt / lib / RT / ObjectCustomFieldValue_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 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/copyleft/gpl.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 package RT::ObjectCustomFieldValue;
49
50 use strict;
51 no warnings qw(redefine);
52
53
54 sub Create {
55     my $self = shift;
56     my %args = (
57                 CustomField => '0',
58                 ObjectType => '',
59                 ObjectId => '0',
60                 Disabled => '0',
61                 Content => '',
62                 LargeContent => '',
63                 ContentType => '',
64                 ContentEncoding => '',
65
66           @_);
67
68    
69     if( $args{'Content'} && length($args{'Content'}) > 255 && !$args{'LargeContent'} ) {
70
71         $args{'LargeContent'} = $args{'Content'};
72         $args{'Content'} = '';
73         $args{'ContentType'} = 'text/plain';
74     }
75
76     ( $args{'ContentEncoding'}, $args{'LargeContent'} ) =
77       $self->_EncodeLOB( $args{'LargeContent'}, $args{'ContentType'} )
78       if ( $args{'LargeContent'} );
79
80     $self->SUPER::Create(
81                          CustomField => $args{'CustomField'},
82                          ObjectType => $args{'ObjectType'},
83                          ObjectId => $args{'ObjectId'},
84                          Disabled => $args{'Disabled'},
85                          Content => $args{'Content'},
86                          LargeContent => $args{'LargeContent'},
87                          ContentType => $args{'ContentType'},
88                          ContentEncoding => $args{'ContentEncoding'},
89 );
90
91
92
93 }
94
95
96 sub LargeContent {
97     my $self = shift;
98     $self->_DecodeLOB( $self->ContentType, $self->ContentEncoding,
99         $self->_Value( 'LargeContent', decode_utf8 => 0 ) );
100
101 }
102
103
104
105
106 =head2 LoadByTicketContentAndCustomField { Ticket => TICKET, CustomField => CUSTOMFIELD, Content => CONTENT }
107
108 Loads a custom field value by Ticket, Content and which CustomField it's tied to
109
110 =cut
111
112
113 sub LoadByTicketContentAndCustomField {
114     my $self = shift;
115     my %args = ( Ticket => undef,
116                 CustomField => undef,
117                 Content => undef,
118                 @_
119                 );
120
121
122     $self->LoadByCols( Content => $args{'Content'},
123                          CustomField => $args{'CustomField'},
124                          ObjectType => 'RT::Ticket',
125                          ObjectId => $args{'Ticket'},
126                          Disabled => 0
127                          );
128
129     
130 }
131
132 sub LoadByObjectContentAndCustomField {
133     my $self = shift;
134     my %args = ( Object => undef,
135                 CustomField => undef,
136                 Content => undef,
137                 @_
138                 );
139
140     my $obj = $args{'Object'} or return;
141
142     $self->LoadByCols( Content => $args{'Content'},
143                          CustomField => $args{'CustomField'},
144                          ObjectType => ref($obj),
145                          ObjectId => $obj->Id,
146                          Disabled => 0
147                          );
148
149 }
150
151
152 =head2 Content
153
154 Return this custom field's content. If there's no "regular"
155 content, try "LargeContent"
156
157 =cut
158
159
160 sub Content {
161     my $self = shift;
162     my $content = $self->SUPER::Content;
163     if (!$content && $self->ContentType eq 'text/plain') {
164        return $self->LargeContent(); 
165     } else {
166         return $content;
167     }
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->Load($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     $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.
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 =back
215
216 =cut
217
218 sub _FillInTemplateURL {
219
220     my $self = shift;
221
222     my $url = shift;
223
224     $url =~ s/__id__/@{[$self->ObjectId]}/g;
225     $url =~ s/__CustomField__/@{[$self->Content]}/g;
226
227     return $url;
228 }
229
230
231 =head2 ValueLinkURL
232
233 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for 
234 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
235 a LinkValueTo
236
237 =cut
238
239 sub LinkValueTo {
240     my $self = shift;
241     return $self->_FillInTemplateURL($self->CustomFieldObj->LinkValueTo);
242 }
243
244
245
246 =head2 ValueIncludeURL
247
248 Returns a filled in URL template for this ObjectCustomFieldValue, suitable for 
249 constructing a hyperlink in RT's webui. Returns undef if this custom field doesn't have
250 a IncludeContentForValue
251
252 =cut
253
254 sub IncludeContentForValue {
255     my $self = shift;
256     return $self->_FillInTemplateURL($self->CustomFieldObj->IncludeContentForValue);
257 }
258
259
260
261
262 1;