From: Niclas Zimmermann Date: Fri, 19 Apr 2013 12:42:28 +0000 (+0200) Subject: Merge branch 'master' of github.com:kivitendo/kivitendo-erp X-Git-Tag: release-3.1.0beta1~486 X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/commitdiff_plain/b3f351139a745a2d01dd35e5a70f72e61b2c2adb?hp=693e58208df2667024aaf4ac92cb8f8e8d820b38 Merge branch 'master' of github.com:kivitendo/kivitendo-erp --- diff --git a/SL/Common.pm b/SL/Common.pm index 159609d30..ff6c7218a 100644 --- a/SL/Common.pm +++ b/SL/Common.pm @@ -46,6 +46,21 @@ sub tmpname { return "/tmp/kivitendo-tmp-" . unique_id(); } +sub truncate { + my ($text, %params) = @_; + + $params{at} //= 50; + $params{at} = 3 if 3 > $params{at}; + + $params{strip} //= ''; + + $text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x; + $text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?: newlines? | full )$/x; + + return $text if length($text) <= $params{at}; + return substr($text, 0, $params{at} - 3) . '...'; +} + sub retrieve_parts { $main::lxdebug->enter_sub(); @@ -576,3 +591,46 @@ sub check_params_x { } 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +Common - Common routines used in a lot of places. + +=head1 SYNOPSIS + + my $short_text = Common::truncate($long_text, at => 10); + +=head1 FUNCTIONS + +=over 4 + +=item C + +Truncates C<$text> at a position and insert an ellipsis if the text is +longer. The maximum number of characters to return is given with the +paramter C which defaults to 50. + +The optional parameter C can be used to remove unwanted line +feed/carriage return characters from the text before truncation. It +can be set to C<1> (only strip those at the end of C<$text>) or +C (replace consecutive line feed/carriage return characters in +the middle by a single space and remove tailing line feed/carriage +return characters). + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE, +Sven Schöling Es.schoeling@linet-services.deE + +=cut diff --git a/SL/DB/Object.pm b/SL/DB/Object.pm index 1c7c050ea..8b86e9711 100755 --- a/SL/DB/Object.pm +++ b/SL/DB/Object.pm @@ -134,12 +134,12 @@ sub save { my ($result, $exception); my $worker = sub { - SL::DB::Object::Hooks::run_hooks($self, 'before_save'); $exception = $EVAL_ERROR unless eval { + SL::DB::Object::Hooks::run_hooks($self, 'before_save'); $result = $self->SUPER::save(@args); + SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result); 1; }; - SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result); return $result; }; @@ -156,12 +156,12 @@ sub delete { my ($result, $exception); my $worker = sub { - SL::DB::Object::Hooks::run_hooks($self, 'before_delete'); $exception = $EVAL_ERROR unless eval { + SL::DB::Object::Hooks::run_hooks($self, 'before_delete'); $result = $self->SUPER::delete(@args); + SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result); 1; }; - SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result); return $result; }; diff --git a/SL/DB/Object/Hooks.pm b/SL/DB/Object/Hooks.pm index 3839286ba..371e4450e 100644 --- a/SL/DB/Object/Hooks.pm +++ b/SL/DB/Object/Hooks.pm @@ -44,9 +44,10 @@ sub run_hooks { foreach my $sub (@{ ( $hooks{$when} || { })->{ ref($object) } || [ ] }) { my $result = ref($sub) eq 'CODE' ? $sub->($object, @args) : $object->call_sub($sub, @args); - die SL::X::DBHookError->new(when => $when, - hook => (ref($sub) eq 'CODE' ? '' : $sub), - object => $object) + die SL::X::DBHookError->new(when => $when, + hook => (ref($sub) eq 'CODE' ? '' : $sub), + object => $object, + object_type => ref($object)) if !$result; } } diff --git a/SL/Presenter/Text.pm b/SL/Presenter/Text.pm index 637bff6b6..af3732309 100644 --- a/SL/Presenter/Text.pm +++ b/SL/Presenter/Text.pm @@ -12,12 +12,7 @@ use Carp; sub truncate { my ($self, $text, %params) = @_; - $params{at} ||= 50; - $params{at} = 3 if 3 > $params{at}; - $params{at} -= 3; - - return $text if length($text) < $params{at}; - return substr($text, 0, $params{at}) . '...'; + return Common::truncate($text, %params); } sub simple_format { @@ -77,15 +72,11 @@ it, e.g. C<2 PT 2 h> for the value C<18> and German. If the parameter C is trueish then C<---> is returned instead of the normal formatting if C<$value> equals 0. -=item C +=item C Returns the C<$text> truncated after a certain number of -characters. - -The number of characters to truncate at is determined by the parameter -C which defaults to 50. If the text is longer than C<$params{at}> -then it will be truncated and postfixed with '...'. Otherwise it will -be returned unmodified. +characters. See L for the actual implementation and +supported parameters. =item C diff --git a/SL/ReportGenerator.pm b/SL/ReportGenerator.pm index 40beabb92..d9edae200 100644 --- a/SL/ReportGenerator.pm +++ b/SL/ReportGenerator.pm @@ -349,7 +349,7 @@ sub prepare_html_content { $col->{CELL_ROWS} = [ ]; foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) { push @{ $col->{CELL_ROWS} }, { - 'data' => $self->html_format($col->{data}->[$i]), + 'data' => '' . $self->html_format($col->{data}->[$i]), 'link' => $col->{link}->[$i], }; } diff --git a/SL/X.pm b/SL/X.pm index 0f206949b..552e5ef29 100644 --- a/SL/X.pm +++ b/SL/X.pm @@ -5,6 +5,6 @@ use strict; use Exception::Lite qw(declareExceptionClass); declareExceptionClass('SL::X::FormError'); -declareExceptionClass('SL::X::DBHookError', [ '%s hook \'%s\' failed', qw(when hook object) ]); +declareExceptionClass('SL::X::DBHookError', [ '%s hook \'%s\' for object type \'%s\' failed', qw(when hook object_type object) ]); 1; diff --git a/css/kivitendo/main.css b/css/kivitendo/main.css index 4cad54f27..12db365e1 100644 --- a/css/kivitendo/main.css +++ b/css/kivitendo/main.css @@ -225,12 +225,12 @@ body.menu { border-width: 1px; background: #FFFFE0; } -.listrow1 { +.listrow1, .listrow:nth-child(odd) { background-color: #FFFFFF; color: black; vertical-align: top; } -.listrow0 { +.listrow0, .listrow:nth-child(even) { background-color: #FFFF99; color: black; vertical-align: top; diff --git a/css/lx-office-erp/main.css b/css/lx-office-erp/main.css index 379423249..1e200b043 100644 --- a/css/lx-office-erp/main.css +++ b/css/lx-office-erp/main.css @@ -257,8 +257,8 @@ div.admin { } -.listrow1 { background-color: rgb(208,207,201); color: black; vertical-align: top; } -.listrow0 { background-color: rgb(236,233,216); color: black; vertical-align: top; } +.listrow1, .listrow:nth-child(odd) { background-color: rgb(208,207,201); color: black; vertical-align: top; } +.listrow0, .listrow:nth-child(even) { background-color: rgb(236,233,216); color: black; vertical-align: top; } .listrowempty { background-color: rgb(255,255,255); color: black; vertical-align: top; } .redrow1 { background-color: rgb(250,167, 161); color: black; vertical-align: top; } diff --git a/t/common.t b/t/common.t new file mode 100644 index 000000000..b68cba201 --- /dev/null +++ b/t/common.t @@ -0,0 +1,49 @@ +use strict; + +use Test::More; + +use lib 't'; +use Support::TestSetup; + +Support::TestSetup::login(); + +use SL::Common; + +sub test_truncate { + is(Common::truncate('nothing to do', at => -1), '...', 'truncation length < 0: at least 3'); + is(Common::truncate('nothing to do', at => 0), '...', 'truncation length = 0: at least 3'); + is(Common::truncate('nothing to do', at => 1), '...', 'truncation length = 1: at least 3'); + is(Common::truncate('nothing to do', at => 2), '...', 'truncation length = 2: at least 3'); + is(Common::truncate('nothing to do', at => 3), '...', 'truncation length = 3: at least 3'); + is(Common::truncate('nothing to do', at => 4), 'n...', 'truncation length = 4'); + is(Common::truncate('nothing to do', at => 9), 'nothin...', 'text length equal to truncation + 4'); + is(Common::truncate('nothing to do', at => 10), 'nothing...', 'text length equal to truncation + 3'); + is(Common::truncate('nothing to do', at => 11), 'nothing ...', 'text length equal to truncation + 2'); + is(Common::truncate('nothing to do', at => 12), 'nothing t...', 'text length equal to truncation + 1'); + is(Common::truncate('nothing to do', at => 13), 'nothing to do', 'text length equal to truncation'); + is(Common::truncate('nothing to do', at => 14), 'nothing to do', 'text length equal to truncation - 1'); + is(Common::truncate('nothing to do', at => 15), 'nothing to do', 'text length equal to truncation - 2'); + is(Common::truncate('nothing to do', at => 16), 'nothing to do', 'text length equal to truncation - 3'); + is(Common::truncate('nothing to do', at => 200), 'nothing to do', 'text length smaller than truncation'); + + is(Common::truncate('012345678901234567890123456789012345678901234567890123456789'), '01234567890123456789012345678901234567890123456...', 'default truncation length of 50'); + + # Test stripping + is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 1), "nothing\n\rat\rall", 'strip = 1, at = 50'); + is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 1), "nothing\n\ra...", 'strip = 1, at = 13'); + + is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'full'), "nothing at all", 'strip = full, at = 50'); + is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'full'), "nothing at...", 'strip = full, at = 13'); + + is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'newlines'), "nothing at all", 'strip = newlines, at = 50'); + is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'newlines'), "nothing at...", 'strip = newlines, at = 13'); + + is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'newline'), "nothing at all", 'strip = newline, at = 50'); + is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'newline'), "nothing at...", 'strip = newline, at = 13'); +} + +test_truncate(); + +done_testing; + +1;