stub für einen generische attrhelper.
[kivitendo-erp.git] / SL / DB / Helpers / AttrDate.pm
1 package SL::DB::Helpers::AttrDate;
2
3 use strict;
4
5 use Carp;
6 use English;
7
8 sub define {
9   my $package     = shift;
10   my $attribute   = shift;
11   my %params      = @_;
12
13   no strict 'refs';
14   *{ $package . '::' . $attribute . '_as_date' } = sub {
15     my ($self, $string) = @_;
16
17     if (@_ > 1) {
18       if ($string) {
19         my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
20         $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
21       } else {
22         $self->$attribute(undef);
23       }
24     }
25
26     return $self->$attribute
27       ? $::locale->reformat_date(
28           { dateformat => 'yy-mm-dd' },
29           $self->${attribute}->ymd,
30           $::myconfig{dateformat}
31         )
32       : undef;
33   };
34
35   return 1;
36 }
37
38 1;