1 | package LATMOS::Accounts::Bases::Attributes; |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::Bases::Attributes - Object to handle attribute schema |
---|
12 | |
---|
13 | =head1 FUNCTIONS |
---|
14 | |
---|
15 | =head2 new($attributes, $base_or_object, $maybe_otype) |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | sub new { |
---|
20 | my ($class, $attributes, $base_or_object, $maybe_otype) = @_; |
---|
21 | |
---|
22 | my ($object, $base, $otype) = |
---|
23 | $base_or_object->isa('LATMOS::Accounts::Bases::Objects') |
---|
24 | ? ($base_or_object, $base_or_object->base, $base_or_object->type) |
---|
25 | : (undef, $base_or_object, $maybe_otype); |
---|
26 | |
---|
27 | if (ref $attributes) { |
---|
28 | $attributes->{_base} = $base; |
---|
29 | $attributes->{_object} = $object; |
---|
30 | $attributes->{_name} = $attributes->{name}; |
---|
31 | $attributes->{_otype} = $otype; |
---|
32 | return bless($attributes, $class); |
---|
33 | } else { |
---|
34 | my $attr_info = $base->get_attr_schema($otype, $attributes) or return; |
---|
35 | |
---|
36 | $attr_info->{_base} = $base; |
---|
37 | $attr_info->{_object} = $object; |
---|
38 | $attr_info->{_name} = $attributes; |
---|
39 | $attr_info->{_otype} = $otype; |
---|
40 | |
---|
41 | return bless($attr_info, $class); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | sub base { $_[0]->{_base} } |
---|
46 | sub name { $_[0]->{_name} } |
---|
47 | sub otype { $_[0]->{_otype} } |
---|
48 | sub mandatory { $_[0]->{mandatory} || 0 } |
---|
49 | sub object { $_[0]->{_object} } |
---|
50 | |
---|
51 | =head2 reference |
---|
52 | |
---|
53 | A object type this attribute refer to |
---|
54 | |
---|
55 | =cut |
---|
56 | |
---|
57 | sub reference { |
---|
58 | my ($self) = @_; |
---|
59 | if ($self->{reference} && |
---|
60 | $self->base->is_supported_object($self->{reference})) { |
---|
61 | return $self->{reference}; |
---|
62 | } else { |
---|
63 | return; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | =head2 |
---|
68 | |
---|
69 | Return internal name of attributes |
---|
70 | |
---|
71 | =cut |
---|
72 | |
---|
73 | sub iname { $_[0]->{iname} || $_[0]->name } |
---|
74 | |
---|
75 | sub label { $_[0]->{label} || $_[0]->{_name} } |
---|
76 | |
---|
77 | sub has_values_list { |
---|
78 | my ($self) = @_; |
---|
79 | if ($self->base->obj_attr_allowed_values( |
---|
80 | $self->otype, |
---|
81 | $self->name) || |
---|
82 | $self->{can_values} || |
---|
83 | $self->reference) { |
---|
84 | return 1; |
---|
85 | } else { |
---|
86 | return 0; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | sub can_values { |
---|
91 | my ($self) = @_; |
---|
92 | if (my @values = $self->base->obj_attr_allowed_values( |
---|
93 | $self->otype, |
---|
94 | $self->name)) { |
---|
95 | return @values; |
---|
96 | } elsif ($self->{can_values}) { |
---|
97 | if (ref $self->{can_values} eq 'ARRAY') { |
---|
98 | return @{$self->{can_values}}; |
---|
99 | } elsif (ref $self->{can_values} eq 'CODE') { |
---|
100 | $self->{can_values}->($self, $self->object); |
---|
101 | } else { |
---|
102 | return; |
---|
103 | } |
---|
104 | } elsif (my $ref = $self->reference) { |
---|
105 | return $self->base->list_objects($ref); |
---|
106 | } else { return } |
---|
107 | } |
---|
108 | |
---|
109 | sub display { |
---|
110 | my ($self, $value) = @_; |
---|
111 | if ($self->{display}) { |
---|
112 | return $self->{display}->($self, $value); |
---|
113 | } else { |
---|
114 | return $value; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | sub input { |
---|
119 | my ($self, $value) = @_; |
---|
120 | if ($self->{input}) { |
---|
121 | return $self->{input}->($value); |
---|
122 | } else { |
---|
123 | return $value; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | sub ro { |
---|
128 | my ($self) = @_; |
---|
129 | if (ref $self->{ro} eq 'CODE') { |
---|
130 | return $self->{ro}->($self->object) || 0; |
---|
131 | } else { |
---|
132 | return $_[0]->{ro} || 0 |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | sub readable { |
---|
137 | my ($self) = @_; |
---|
138 | if (ref $self->{readable} eq 'CODE') { |
---|
139 | return $self->{readable}->($self->object) || 0; |
---|
140 | } else { |
---|
141 | return defined($_[0]->{readable}) ? $_[0]->{readable} : 1; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | sub readonly { |
---|
146 | my ($self) = @_; |
---|
147 | return 1 if ($self->ro); |
---|
148 | |
---|
149 | return ! $self->check_acl('w'); |
---|
150 | } |
---|
151 | |
---|
152 | sub check_acl { |
---|
153 | my ($self, $mode) = @_; |
---|
154 | |
---|
155 | return 1 if ($self->{_noacl}); |
---|
156 | |
---|
157 | return $self->base->check_acl($self->object |
---|
158 | ? ($self->object, $self->name, $mode) |
---|
159 | : ($self->otype, '@CREATE', $mode)); |
---|
160 | } |
---|
161 | |
---|
162 | =head2 form_type |
---|
163 | |
---|
164 | Return the way the fields must be show in GUI. |
---|
165 | For each type option maybe given by from_option |
---|
166 | |
---|
167 | =head3 LABEL |
---|
168 | |
---|
169 | =over 4 |
---|
170 | |
---|
171 | =item length |
---|
172 | |
---|
173 | The length to use to show the attribute |
---|
174 | |
---|
175 | =back |
---|
176 | |
---|
177 | =head3 TEXT |
---|
178 | |
---|
179 | =head3 TEXTAREA |
---|
180 | |
---|
181 | =head3 DATE |
---|
182 | |
---|
183 | =head3 LIST |
---|
184 | |
---|
185 | =head3 CHECKBOX |
---|
186 | |
---|
187 | =over 4 |
---|
188 | |
---|
189 | =item rawvalue |
---|
190 | |
---|
191 | The real value of the attribute must be show |
---|
192 | |
---|
193 | =back |
---|
194 | |
---|
195 | =cut |
---|
196 | |
---|
197 | sub real_form_type { $_[0]->{formtype} || 'TEXT' } |
---|
198 | |
---|
199 | sub form_type { |
---|
200 | $_[0]->readonly ? 'LABEL' : |
---|
201 | $_[0]->{formtype} ? $_[0]->{formtype} : |
---|
202 | $_[0]->has_values_list ? 'LIST' : |
---|
203 | $_[0]->real_form_type |
---|
204 | } |
---|
205 | |
---|
206 | sub form_option { |
---|
207 | my ($self, $option) = @_; |
---|
208 | return $self->{formopts}{$option} |
---|
209 | } |
---|
210 | |
---|
211 | sub uniq { $_[0]->{uniq} || 0 } |
---|
212 | |
---|
213 | sub multiple { $_[0]->{multiple} || 0 } |
---|
214 | |
---|
215 | sub hidden { $_[0]->{hide} || 0 } |
---|
216 | |
---|
217 | sub delayed { $_[0]->{delayed} || 0 } |
---|
218 | |
---|
219 | =head2 get($attr) |
---|
220 | |
---|
221 | Return the value for this attribute |
---|
222 | |
---|
223 | =cut |
---|
224 | |
---|
225 | sub get { |
---|
226 | my ($self) = @_; |
---|
227 | |
---|
228 | if (ref $self->{get} eq 'CODE') { |
---|
229 | return $self->{get}->($self); |
---|
230 | } else { |
---|
231 | return $self->object->get_field($self->iname); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | sub set { |
---|
236 | my ($self, $values) = @_; |
---|
237 | |
---|
238 | if (ref $self->{set} eq 'CODE') { |
---|
239 | return $self->{set}->($self, |
---|
240 | ref $values |
---|
241 | ? [ map { $self->input($_) } @$values ] |
---|
242 | : $values ? $self->input($values) : undef); |
---|
243 | } else { |
---|
244 | return $self->object->set_fields( |
---|
245 | $self->iname, |
---|
246 | ref $values |
---|
247 | ? [ map { $self->input($_) } @$values ] |
---|
248 | : $values ? $self->input($values) : undef); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | sub default_value { |
---|
253 | my ($self) = @_; |
---|
254 | return grep { $_ } (ref $self->{default} |
---|
255 | ? @{ $self->{default} } |
---|
256 | : $self->{default}); |
---|
257 | } |
---|
258 | |
---|
259 | 1; |
---|