source: branches/4.0/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Attributes.pm @ 1299

Last change on this file since 1299 was 1299, checked in by nanardon, 9 years ago

backport fix

  • Property svn:keywords set to Id
File size: 7.4 KB
RevLine 
[852]1package LATMOS::Accounts::Bases::Attributes;
2
3# $Id$
4
5use strict;
6use warnings;
7use 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
[1023]17Instanciate a new Attributes object.
18
[852]19=cut
20
21sub new {
22    my ($class, $attributes, $base_or_object, $maybe_otype) = @_;
23   
24    my ($object, $base, $otype) =
25    $base_or_object->isa('LATMOS::Accounts::Bases::Objects')
26        ? ($base_or_object, $base_or_object->base, $base_or_object->type)
27        : (undef, $base_or_object, $maybe_otype);
28
[949]29    if (ref $attributes) {
30        $attributes->{_base} = $base;
31        $attributes->{_object} = $object;
32        $attributes->{_name} = $attributes->{name};
33        $attributes->{_otype} = $otype;
34        return bless($attributes, $class);
35    } else {
36        my $attr_info = $base->get_attr_schema($otype, $attributes) or return;
[852]37
[949]38        $attr_info->{_base} = $base;
39        $attr_info->{_object} = $object;
40        $attr_info->{_name} = $attributes;
41        $attr_info->{_otype} = $otype;
[852]42
[949]43        return bless($attr_info, $class);
44    }
[852]45}
46
[1023]47=head2 base
48
49Return base handle
50
51=cut
52
[852]53sub base  { $_[0]->{_base}  }
[1023]54
55=head2 name
56
57Return attribute name
58
59=cut
60
[852]61sub name  { $_[0]->{_name}  }
[1023]62
63=head2 otype
64
65Return object type for this attribute
66
67=cut
68
69
[852]70sub otype { $_[0]->{_otype} }
[1023]71
72=head2 mandatory
73
74Return true if attribute is mandatory
75
76=cut
77
[861]78sub mandatory { $_[0]->{mandatory} || 0 }
[1023]79
80=head2 object
81
82Return handle object if any
83
84=cut
85
[861]86sub object { $_[0]->{_object} }
[852]87
[861]88=head2 reference
89
90A object type this attribute refer to
91
92=cut
93
94sub reference {
95    my ($self) = @_;
96    if ($self->{reference} &&
97        $self->base->is_supported_object($self->{reference})) {
98        return $self->{reference};
99    } else {
100        return;
101    }
102}
103
[1023]104=head2 iname
[857]105
106Return internal name of attributes
107
108=cut
109
110sub iname { $_[0]->{iname} || $_[0]->name }
111
[1023]112=head2 label
113
114Return the label to display
115
116=cut
117
[852]118sub label { $_[0]->{label} || $_[0]->{_name} }
119
[1023]120=head2 has_values_list
121
122Return true if the attribute have a fixed list of accepted values
123
124=cut
125
[861]126sub has_values_list {
127    my ($self) = @_;
128    if ($self->base->obj_attr_allowed_values(
129        $self->otype,
130        $self->name) ||
131        $self->{can_values} ||
132        $self->reference) {
133        return 1;
134    } else {
135        return 0;
136    }
137}
138
[1023]139=head2 can_values
140
141Return possible value allowed by this attribute
142
143=cut
144
[852]145sub can_values {
146    my ($self) = @_;
147    if (my @values = $self->base->obj_attr_allowed_values(
148            $self->otype,
149            $self->name)) {
150        return @values;
151    } elsif ($self->{can_values}) {
152        if (ref $self->{can_values} eq 'ARRAY') {
153            return @{$self->{can_values}};
154        } elsif (ref $self->{can_values} eq 'CODE') {
[861]155            $self->{can_values}->($self, $self->object);
[852]156        } else {
157            return;
158        }
[861]159    } elsif (my $ref = $self->reference) {
160        return $self->base->list_objects($ref);
[852]161    } else { return }
162}
163
[1023]164=head2 display ($value)
165
166Return the well formated value according attribute
167
168=cut
169
[861]170sub display {
171    my ($self, $value) = @_;
172    if ($self->{display}) {
173        return $self->{display}->($self, $value);
174    } else {
175        return $value;
176    }
177}
[857]178
[1023]179=head2 input ($value)
180
181Return well formated single value for insert into base
182
183=cut
184
[861]185sub input {
186    my ($self, $value) = @_;
187    if ($self->{input}) {
188        return $self->{input}->($value);
189    } else {
190        return $value;
191    }
192}
193
[1023]194=head2 readable
[861]195
[1023]196Return true if attribute can be read
197
198=cut
199
[933]200sub readable {
201    my ($self) = @_;
202    if (ref $self->{readable} eq 'CODE') {
203        return $self->{readable}->($self->object) || 0;
204    } else {
205        return defined($_[0]->{readable}) ? $_[0]->{readable} : 1;
206    }
207}
208
[1023]209=head2 ro
210
211Return true if the attribute cannot be write by design
212
213=cut
214
215sub ro {
216    my ($self) = @_;
217    if (ref $self->{ro} eq 'CODE') {
218        return $self->{ro}->($self->object) || 0;
219    } else {
220        return $_[0]->{ro} || 0 
221    }
222}
223
224=head2 readonly
225
226Return true if attribute cannot be read according acls or attributes state
227
228=cut
229
[857]230sub readonly { 
[852]231    my ($self) = @_;
[857]232    return 1 if ($self->ro);
[852]233   
[861]234    return ! $self->check_acl('w');
[852]235}
236
[1023]237=head2 check_acl ($mode)
238
239Return true is access to C<$mode> is granted
240
241=cut
242
[861]243sub check_acl {
244    my ($self, $mode) = @_;
[959]245
246    return 1 if ($self->{_noacl});
247
[861]248    return $self->base->check_acl($self->object
249        ? ($self->object, $self->name, $mode)
250        : ($self->otype, '@CREATE', $mode));
251}
252
[1023]253=head2 real_form_type
[852]254
[861]255Return the way the fields must be show in GUI.
[1023]256For each type option maybe given by form_option
[852]257
[861]258=head3 LABEL
259
[852]260=over 4
261
[861]262=item length
[852]263
[861]264The length to use to show the attribute
[852]265
[861]266=back
[852]267
[861]268=head3 TEXT
[852]269
[861]270=head3 TEXTAREA
[852]271
[861]272=head3 DATE
273
274=head3 LIST
275
276=head3 CHECKBOX
277
278=over 4
279
280=item rawvalue
281
282The real value of the attribute must be show
283
[852]284=back
285
286=cut
287
[861]288sub real_form_type { $_[0]->{formtype} || 'TEXT' }
[852]289
[1023]290=head2 form_type
291
292Return the way the attribute must be displayed
293
294=cut
295
[861]296sub form_type {
297    $_[0]->readonly ? 'LABEL' :
298    $_[0]->{formtype} ? $_[0]->{formtype} :
299    $_[0]->has_values_list ? 'LIST' :
300    $_[0]->real_form_type
301}
302
[1023]303=head2 form_option ($option)
304
305Return the form option C<$option>
306
307=cut
308
[861]309sub form_option {
310    my ($self, $option) = @_;
311    return $self->{formopts}{$option}
312}
313
[1023]314=head2 uniq
315
316Return true is attribute value must be uniq
317
318=cut
319
[852]320sub uniq { $_[0]->{uniq} || 0 }
321
[1023]322=head2 multiple
323
324Return true is attribute value can be set several times
325
326=cut
327
[852]328sub multiple { $_[0]->{multiple} || 0 }
329
[1023]330=head2 hidden
331
332Return true if attributes must not appear in list by can be query.
333
334=cut
335
[861]336sub hidden { $_[0]->{hide} || 0 }
337
[1023]338=head2 delayed
339
340Return true if attribute must be set after object creation during
341synchronisation
342
343=cut
344
[861]345sub delayed { $_[0]->{delayed} || 0 }
346
[936]347=head2 get($attr)
348
349Return the value for this attribute
350
351=cut
352
353sub get {
354    my ($self) = @_;
355
356    if (ref $self->{get} eq 'CODE') {
357        return $self->{get}->($self);
358    } else {
359        return $self->object->get_field($self->iname);
360    }
361}
362
[1023]363=head2 set ($values)
364
365Set attribute value to attached object
366
367=cut
368
[936]369sub set {
370    my ($self, $values) = @_;
371
372    if (ref $self->{set} eq 'CODE') {
[959]373        return $self->{set}->($self,
374            ref $values
375            ? [ map { $self->input($_) } @$values ]
376            : $values ? $self->input($values) : undef);
[936]377    } else {
[950]378        return $self->object->set_fields(
379            $self->iname,
380            ref $values
381                ? [ map { $self->input($_) } @$values ]
[959]382                : $values ? $self->input($values) : undef);
[936]383    }
384}
385
[1023]386=head2 default_value
387
388Return default value for this attribute
389
390=cut
391
[959]392sub default_value {
393    my ($self) = @_;
394    return grep { $_ } (ref $self->{default}
395        ? @{ $self->{default} }
396        : $self->{default});
397}
398
[1299]399=head2 monitored
400
401Return true if the attribute is monitored
402
403=cut
404
405sub monitored {
406    my ($self) = @_;
407
408    if ($self->iname ne $self->name) {
409        my $attr = $self->base->attribute($self->otype, $self->iname) or return;
410        return $attr->monitored;
411    } else {
412        return $self->{monitored} || 0;
413    }
414}
415
[852]4161;
[1023]417
418__END__
419
420=head1 SEE ALSO
421
422L<LATMOS::Accounts::Bases>
423
424=head1 AUTHOR
425
426Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
427
428=head1 COPYRIGHT AND LICENSE
429
430Copyright (C) 2012 CNRS SA/CETP/LATMOS
431
432This library is free software; you can redistribute it and/or modify
433it under the same terms as Perl itself, either Perl version 5.10.0 or,
434at your option, any later version of Perl 5 you may have available.
435
436=cut
Note: See TracBrowser for help on using the repository browser.