rt 4.2.16
[freeside.git] / rt / lib / RT / CustomFieldValue.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2019 Best Practical Solutions, LLC
6 #                                          <sales@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 use strict;
50 use warnings;
51
52 package RT::CustomFieldValue;
53
54 no warnings qw/redefine/;
55
56
57 use base 'RT::Record';
58 use RT::CustomField;
59
60 sub Table {'CustomFieldValues'}
61
62
63 =head2 ValidateName
64
65 Override the default ValidateName method that stops custom field values
66 from being integers.
67
68 =cut
69
70 sub Create {
71     my $self = shift;
72     my %args = (
73         CustomField => 0,
74         Name        => '',
75         Description => '',
76         SortOrder   => 0,
77         Category    => '',
78         @_,
79     );
80
81     my $cf_id = ref $args{'CustomField'}? $args{'CustomField'}->id: $args{'CustomField'};
82
83     my $cf = RT::CustomField->new( $self->CurrentUser );
84     $cf->Load( $cf_id );
85     unless ( $cf->id ) {
86         return (0, $self->loc("Couldn't load Custom Field #[_1]", $cf_id));
87     }
88     unless ( $cf->CurrentUserHasRight('AdminCustomField') || $cf->CurrentUserHasRight('AdminCustomFieldValues') ) {
89         return (0, $self->loc('Permission Denied'));
90     }
91
92     my ($id, $msg) = $self->SUPER::Create(
93         CustomField => $cf_id,
94         map { $_ => $args{$_} } qw(Name Description SortOrder Category)
95     );
96     return ($id, $msg);
97 }
98
99 sub ValidateName {
100     return defined $_[1] && length $_[1];
101 };
102
103 sub _Set { 
104     my $self = shift; 
105
106     my $cf_id = $self->CustomField; 
107
108     my $cf = RT::CustomField->new( $self->CurrentUser ); 
109     $cf->Load( $cf_id ); 
110
111     unless ( $cf->id ) { 
112         return (0, $self->loc("Couldn't load Custom Field #[_1]", $cf_id)); 
113     } 
114
115     unless ($cf->CurrentUserHasRight('AdminCustomField') || $cf->CurrentUserHasRight('AdminCustomFieldValues')) { 
116         return (0, $self->loc('Permission Denied')); 
117     } 
118
119     return $self->SUPER::_Set( @_ ); 
120
121
122
123 =head2 id
124
125 Returns the current value of id. 
126 (In the database, id is stored as int(11).)
127
128
129 =cut
130
131
132 =head2 CustomField
133
134 Returns the current value of CustomField. 
135 (In the database, CustomField is stored as int(11).)
136
137
138
139 =head2 SetCustomField VALUE
140
141
142 Set CustomField to VALUE. 
143 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
144 (In the database, CustomField will be stored as a int(11).)
145
146
147 =head2 SetCustomFieldObj
148
149 Store the CustomField object which loaded this CustomFieldValue.
150 Passed down from the CustomFieldValues collection in AddRecord.
151
152 This object will be transparently returned from CustomFieldObj rather
153 than loading from the database.
154
155 =cut
156
157 sub SetCustomFieldObj {
158     my $self = shift;
159     return $self->{'custom_field'} = shift;
160 }
161
162 =head2 CustomFieldObj
163
164 If a CustomField object was stored using SetCustomFieldObj and it is
165 the same CustomField stored in the CustomField column, then the stored
166 CustomField object (likely passed down from CustomField->Values) will be returned.
167
168 Otherwise returns the CustomField Object which has the id returned by CustomField
169
170 =cut
171
172 sub CustomFieldObj {
173     my $self = shift;
174
175     return $self->{custom_field} if $self->{custom_field}
176         and $self->{custom_field}->id == $self->__Value('CustomField');
177
178     my $CustomField =  RT::CustomField->new($self->CurrentUser);
179     $CustomField->Load($self->__Value('CustomField'));
180     return($CustomField);
181 }
182
183 =head2 Name
184
185 Returns the current value of Name. 
186 (In the database, Name is stored as varchar(200).)
187
188
189
190 =head2 SetName VALUE
191
192
193 Set Name to VALUE. 
194 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
195 (In the database, Name will be stored as a varchar(200).)
196
197
198 =cut
199
200
201 =head2 Description
202
203 Returns the current value of Description. 
204 (In the database, Description is stored as varchar(255).)
205
206
207
208 =head2 SetDescription VALUE
209
210
211 Set Description to VALUE. 
212 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
213 (In the database, Description will be stored as a varchar(255).)
214
215
216 =cut
217
218
219 =head2 SortOrder
220
221 Returns the current value of SortOrder. 
222 (In the database, SortOrder is stored as int(11).)
223
224
225
226 =head2 SetSortOrder VALUE
227
228
229 Set SortOrder to VALUE. 
230 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
231 (In the database, SortOrder will be stored as a int(11).)
232
233
234 =cut
235
236
237 =head2 Category
238
239 Returns the current value of Category.
240 (In the database, Category is stored as varchar(255).)
241
242
243
244 =head2 SetCategory VALUE
245
246
247 Set Category to VALUE.
248 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
249 (In the database, Category will be stored as a varchar(255).)
250
251
252 =cut
253
254
255 =head2 Creator
256
257 Returns the current value of Creator. 
258 (In the database, Creator is stored as int(11).)
259
260
261 =cut
262
263
264 =head2 Created
265
266 Returns the current value of Created. 
267 (In the database, Created is stored as datetime.)
268
269
270 =cut
271
272
273 =head2 LastUpdatedBy
274
275 Returns the current value of LastUpdatedBy. 
276 (In the database, LastUpdatedBy is stored as int(11).)
277
278
279 =cut
280
281
282 =head2 LastUpdated
283
284 Returns the current value of LastUpdated. 
285 (In the database, LastUpdated is stored as datetime.)
286
287
288 =cut
289
290
291
292 sub _CoreAccessible {
293     {
294      
295         id =>
296         {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
297         CustomField => 
298         {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
299         Name => 
300         {read => 1, write => 1, sql_type => 12, length => 200,  is_blob => 0,  is_numeric => 0,  type => 'varchar(200)', default => ''},
301         Description => 
302         {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
303         SortOrder => 
304         {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
305         Category =>
306         {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
307         Creator => 
308         {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
309         Created => 
310         {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
311         LastUpdatedBy => 
312         {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
313         LastUpdated => 
314         {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
315
316  }
317 };
318
319
320 sub FindDependencies {
321     my $self = shift;
322     my ($walker, $deps) = @_;
323
324     $self->SUPER::FindDependencies($walker, $deps);
325
326     $deps->Add( out => $self->CustomFieldObj );
327 }
328
329 RT::Base->_ImportOverlays();
330
331 1;