[852] | 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 | |
---|
[1023] | 17 | Instanciate a new Attributes object. |
---|
| 18 | |
---|
[852] | 19 | =cut |
---|
| 20 | |
---|
| 21 | sub 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 | |
---|
| 49 | Return base handle |
---|
| 50 | |
---|
| 51 | =cut |
---|
| 52 | |
---|
[852] | 53 | sub base { $_[0]->{_base} } |
---|
[1023] | 54 | |
---|
| 55 | =head2 name |
---|
| 56 | |
---|
| 57 | Return attribute name |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
[852] | 61 | sub name { $_[0]->{_name} } |
---|
[1023] | 62 | |
---|
| 63 | =head2 otype |
---|
| 64 | |
---|
| 65 | Return object type for this attribute |
---|
| 66 | |
---|
| 67 | =cut |
---|
| 68 | |
---|
| 69 | |
---|
[852] | 70 | sub otype { $_[0]->{_otype} } |
---|
[1023] | 71 | |
---|
| 72 | =head2 mandatory |
---|
| 73 | |
---|
| 74 | Return true if attribute is mandatory |
---|
| 75 | |
---|
| 76 | =cut |
---|
| 77 | |
---|
[861] | 78 | sub mandatory { $_[0]->{mandatory} || 0 } |
---|
[1023] | 79 | |
---|
| 80 | =head2 object |
---|
| 81 | |
---|
| 82 | Return handle object if any |
---|
| 83 | |
---|
| 84 | =cut |
---|
| 85 | |
---|
[861] | 86 | sub object { $_[0]->{_object} } |
---|
[852] | 87 | |
---|
[861] | 88 | =head2 reference |
---|
| 89 | |
---|
| 90 | A object type this attribute refer to |
---|
| 91 | |
---|
| 92 | =cut |
---|
| 93 | |
---|
| 94 | sub 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 | |
---|
| 106 | Return internal name of attributes |
---|
| 107 | |
---|
| 108 | =cut |
---|
| 109 | |
---|
| 110 | sub iname { $_[0]->{iname} || $_[0]->name } |
---|
| 111 | |
---|
[1023] | 112 | =head2 label |
---|
| 113 | |
---|
| 114 | Return the label to display |
---|
| 115 | |
---|
| 116 | =cut |
---|
| 117 | |
---|
[852] | 118 | sub label { $_[0]->{label} || $_[0]->{_name} } |
---|
| 119 | |
---|
[1023] | 120 | =head2 has_values_list |
---|
| 121 | |
---|
| 122 | Return true if the attribute have a fixed list of accepted values |
---|
| 123 | |
---|
| 124 | =cut |
---|
| 125 | |
---|
[861] | 126 | sub 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 | |
---|
| 141 | Return possible value allowed by this attribute |
---|
| 142 | |
---|
| 143 | =cut |
---|
| 144 | |
---|
[852] | 145 | sub 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 | |
---|
| 166 | Return the well formated value according attribute |
---|
| 167 | |
---|
| 168 | =cut |
---|
| 169 | |
---|
[861] | 170 | sub 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 | |
---|
| 181 | Return well formated single value for insert into base |
---|
| 182 | |
---|
| 183 | =cut |
---|
| 184 | |
---|
[861] | 185 | sub 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] | 196 | Return true if attribute can be read |
---|
| 197 | |
---|
| 198 | =cut |
---|
| 199 | |
---|
[933] | 200 | sub 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 | |
---|
| 211 | Return true if the attribute cannot be write by design |
---|
| 212 | |
---|
| 213 | =cut |
---|
| 214 | |
---|
| 215 | sub 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 | |
---|
| 226 | Return true if attribute cannot be read according acls or attributes state |
---|
| 227 | |
---|
| 228 | =cut |
---|
| 229 | |
---|
[857] | 230 | sub 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 | |
---|
| 239 | Return true is access to C<$mode> is granted |
---|
| 240 | |
---|
| 241 | =cut |
---|
| 242 | |
---|
[861] | 243 | sub 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] | 255 | Return the way the fields must be show in GUI. |
---|
[1023] | 256 | For 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] | 264 | The 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 | |
---|
| 282 | The real value of the attribute must be show |
---|
| 283 | |
---|
[852] | 284 | =back |
---|
| 285 | |
---|
| 286 | =cut |
---|
| 287 | |
---|
[861] | 288 | sub real_form_type { $_[0]->{formtype} || 'TEXT' } |
---|
[852] | 289 | |
---|
[1023] | 290 | =head2 form_type |
---|
| 291 | |
---|
| 292 | Return the way the attribute must be displayed |
---|
| 293 | |
---|
| 294 | =cut |
---|
| 295 | |
---|
[861] | 296 | sub 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 | |
---|
| 305 | Return the form option C<$option> |
---|
| 306 | |
---|
| 307 | =cut |
---|
| 308 | |
---|
[861] | 309 | sub form_option { |
---|
| 310 | my ($self, $option) = @_; |
---|
| 311 | return $self->{formopts}{$option} |
---|
| 312 | } |
---|
| 313 | |
---|
[1023] | 314 | =head2 uniq |
---|
| 315 | |
---|
| 316 | Return true is attribute value must be uniq |
---|
| 317 | |
---|
| 318 | =cut |
---|
| 319 | |
---|
[852] | 320 | sub uniq { $_[0]->{uniq} || 0 } |
---|
| 321 | |
---|
[1023] | 322 | =head2 multiple |
---|
| 323 | |
---|
| 324 | Return true is attribute value can be set several times |
---|
| 325 | |
---|
| 326 | =cut |
---|
| 327 | |
---|
[852] | 328 | sub multiple { $_[0]->{multiple} || 0 } |
---|
| 329 | |
---|
[1023] | 330 | =head2 hidden |
---|
| 331 | |
---|
| 332 | Return true if attributes must not appear in list by can be query. |
---|
| 333 | |
---|
| 334 | =cut |
---|
| 335 | |
---|
[861] | 336 | sub hidden { $_[0]->{hide} || 0 } |
---|
| 337 | |
---|
[1023] | 338 | =head2 delayed |
---|
| 339 | |
---|
| 340 | Return true if attribute must be set after object creation during |
---|
| 341 | synchronisation |
---|
| 342 | |
---|
| 343 | =cut |
---|
| 344 | |
---|
[861] | 345 | sub delayed { $_[0]->{delayed} || 0 } |
---|
| 346 | |
---|
[936] | 347 | =head2 get($attr) |
---|
| 348 | |
---|
| 349 | Return the value for this attribute |
---|
| 350 | |
---|
| 351 | =cut |
---|
| 352 | |
---|
| 353 | sub 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 | |
---|
| 365 | Set attribute value to attached object |
---|
| 366 | |
---|
| 367 | =cut |
---|
| 368 | |
---|
[936] | 369 | sub 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 | |
---|
| 388 | Return default value for this attribute |
---|
| 389 | |
---|
| 390 | =cut |
---|
| 391 | |
---|
[959] | 392 | sub default_value { |
---|
| 393 | my ($self) = @_; |
---|
| 394 | return grep { $_ } (ref $self->{default} |
---|
| 395 | ? @{ $self->{default} } |
---|
| 396 | : $self->{default}); |
---|
| 397 | } |
---|
| 398 | |
---|
[852] | 399 | 1; |
---|
[1023] | 400 | |
---|
| 401 | __END__ |
---|
| 402 | |
---|
| 403 | =head1 SEE ALSO |
---|
| 404 | |
---|
| 405 | L<LATMOS::Accounts::Bases> |
---|
| 406 | |
---|
| 407 | =head1 AUTHOR |
---|
| 408 | |
---|
| 409 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 410 | |
---|
| 411 | =head1 COPYRIGHT AND LICENSE |
---|
| 412 | |
---|
| 413 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 414 | |
---|
| 415 | This library is free software; you can redistribute it and/or modify |
---|
| 416 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 417 | at your option, any later version of Perl 5 you may have available. |
---|
| 418 | |
---|
| 419 | =cut |
---|