X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FHelper%2FAttr.pm;h=6f2dd55e264ee1c92e05b7a0bf60b819be17daeb;hb=f63af42d0b025b20a5836dad9da6c07388b5da7a;hp=463bfd654e5c3812b5e8d51e458172a03d42f1ae;hpb=762f8a97338181a9ecb01c20eed52215b470212d;p=kivitendo-erp.git diff --git a/SL/DB/Helper/Attr.pm b/SL/DB/Helper/Attr.pm index 463bfd654..6f2dd55e2 100644 --- a/SL/DB/Helper/Attr.pm +++ b/SL/DB/Helper/Attr.pm @@ -30,9 +30,11 @@ sub make { sub _make_by_type { my ($package, $name, $type) = @_; _as_number ($package, $name, places => -2) if $type =~ /numeric | real | float/xi; - _as_percent($package, $name, places => 0) if $type =~ /numeric | real | float/xi; + _as_percent($package, $name, places => 2) if $type =~ /numeric | real | float/xi; _as_number ($package, $name, places => 0) if $type =~ /int/xi; _as_date ($package, $name) if $type =~ /date | timestamp/xi; + _as_timestamp($package, $name) if $type =~ /timestamp/xi; + _as_bool_yn($package, $name) if $type =~ /bool/xi; } sub _as_number { @@ -92,7 +94,7 @@ sub _as_date { return $self->$attribute ? $::locale->reformat_date( { dateformat => 'yy-mm-dd' }, - ( $self->$attribute eq 'now' + ( ($self->$attribute eq 'now' || $self->$attribute eq 'now()') ? DateTime->now : $self->$attribute )->ymd, @@ -104,6 +106,49 @@ sub _as_date { return 1; } +sub _as_timestamp { + my $package = shift; + my $attribute = shift; + my %params = @_; + + my $accessor = sub { + my ($precision, $self, $string) = @_; + + $self->$attribute($string ? $::locale->parse_date_to_object($string) : undef) if @_ > 2; + + my $dt = $self->$attribute; + return undef unless $dt; + + $dt = DateTime->now if !ref($dt) && ($dt eq 'now'); + + return $::locale->format_date_object($dt, precision => $precision); + }; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_timestamp' } = sub { $accessor->('minute', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_s' } = sub { $accessor->('second', @_) }; + *{ $package . '::' . $attribute . '_as_timestamp_ms' } = sub { $accessor->('millisecond', @_) }; + + return 1; +} + +sub _as_bool_yn { + my ($package, $attribute, %params) = @_; + + no strict 'refs'; + *{ $package . '::' . $attribute . '_as_bool_yn' } = sub { + my ($self) = @_; + + if (@_ > 1) { + die 'not an accessor'; + } + + return !defined $self->$attribute ? '' + : $self->$attribute ? $::locale->text('Yes') + : $::locale->text('No'); + } +} + 1; @@ -111,6 +156,8 @@ sub _as_date { __END__ +=encoding utf-8 + =head1 NAME SL::DB::Helper::Attr - attribute helpers @@ -126,10 +173,19 @@ SL::DB::Helper::Attr - attribute helpers =head1 DESCRIPTION +Makes attribute helpers. + =head1 FUNCTIONS +see for yourself. + =head1 BUGS +None yet. + =head1 AUTHOR +Sven Schöling , +Moritz Bunkus + =cut