import rt 3.8.7
[freeside.git] / rt / lib / RT / CustomFieldValue_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 use warnings;
50 use strict;
51
52 package RT::CustomFieldValue;
53
54 no warnings qw/redefine/;
55
56
57 =head2 ValidateName
58
59 Override the default ValidateName method that stops custom field values
60 from being integers.
61
62 =cut
63
64 sub Create {
65     my $self = shift;
66     my %args = (
67         CustomField => 0,
68         Name        => '',
69         Description => '',
70         SortOrder   => 0,
71         Category    => '',
72         @_,
73     );
74
75     my $cf_id = ref $args{'CustomField'}? $args{'CustomField'}->id: $args{'CustomField'};
76
77     my $cf = RT::CustomField->new( $self->CurrentUser );
78     $cf->Load( $cf_id );
79     unless ( $cf->id ) {
80         return (0, $self->loc("Couldn't load Custom Field #[_1]", $cf_id));
81     }
82     unless ( $cf->CurrentUserHasRight('AdminCustomField') ) {
83         return (0, $self->loc('Permission Denied'));
84     }
85
86     my ($id, $msg) = $self->SUPER::Create(
87         CustomField => $cf_id,
88         map { $_ => $args{$_} } qw(Name Description SortOrder)
89     );
90     return ($id, $msg) unless $id;
91
92     if ( defined $args{'Category'} && length $args{'Category'} ) {
93         # $self would be loaded at this stage
94         my ($status, $msg) = $self->SetCategory( $args{'Category'} );
95         unless ( $status ) {
96             $RT::Logger->error("Couldn't set category: $msg");
97         }
98     }
99
100     return ($id, $msg);
101 }
102
103 =head2 Category
104
105 Returns the Category assigned to this Value
106 Returns udef if there is no Category
107
108 =cut
109
110 sub Category {
111     my $self = shift;
112     my $attr = $self->FirstAttribute('Category') or return undef;
113     return $attr->Content;
114 }
115
116 =head2 SetCategory Category
117
118 Takes a string Category and stores it as an attribute of this CustomFieldValue
119
120 =cut
121
122 sub SetCategory {
123     my $self = shift;
124     my $category = shift;
125     if ( defined $category && length $category ) {
126         return $self->SetAttribute(
127             Name    => 'Category',
128             Content => $category,
129         );
130     }
131     else {
132         my ($status, $msg) = $self->DeleteAttribute( 'Category' );
133         unless ( $status ) {
134             $RT::Logger->warning("Couldn't delete atribute: $msg");
135         }
136         # return true even if there was no category
137         return (1, $self->loc('Category unset'));
138     }
139 }
140
141 sub ValidateName {
142     return defined $_[1] && length $_[1];
143 };
144
145 =head2 DeleteCategory
146
147 Deletes the category associated with this value
148 Returns -1 if there is no Category
149
150 =cut
151
152 sub DeleteCategory {
153     my $self = shift;
154     my $attr = $self->FirstAttribute('Category') or return (-1,'No Category Set');
155     return $attr->Delete;
156 }
157
158 =head2 Delete
159
160 Make sure we delete our Category when we're deleted
161
162 =cut
163
164 sub Delete {
165     my $self = shift;
166
167     my ($result, $msg) = $self->DeleteCategory;
168
169     unless ($result) {
170         return ($result, $msg);
171     }
172
173     return $self->SUPER::Delete(@_);
174 }
175
176 1;