]> wagnertech.de Git - mfinanz.git/commitdiff
Merge branch 'master' of github.com:kivitendo/kivitendo-erp
authorNiclas Zimmermann <niclas@kivitendo-premium.de>
Fri, 19 Apr 2013 12:42:28 +0000 (14:42 +0200)
committerNiclas Zimmermann <niclas@kivitendo-premium.de>
Fri, 19 Apr 2013 12:42:28 +0000 (14:42 +0200)
SL/Common.pm
SL/DB/Object.pm
SL/DB/Object/Hooks.pm
SL/Presenter/Text.pm
SL/ReportGenerator.pm
SL/X.pm
css/kivitendo/main.css
css/lx-office-erp/main.css
t/common.t [new file with mode: 0644]

index 159609d30960382fc505aa0c9402dbe1ea5026ac..ff6c7218abb0f302d845093d41a5913674e69758 100644 (file)
@@ -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<truncate $text, %params>
+
+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<at> which defaults to 50.
+
+The optional parameter C<strip> 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<full> (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 E<lt>m.bunkus@linet-services.deE<gt>,
+Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
+
+=cut
index 1c7c050ea2d54924138c0f1b508fde76c62b0a35..8b86e97111a9942d2b0e422a30026d9557f571ff 100755 (executable)
@@ -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;
   };
index 3839286ba141f50a702cac9f346fda513970de80..371e4450e4198133ccdd7990664d37aa5c72b04a 100644 (file)
@@ -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' ? '<anonymous sub>' : $sub),
-                                object => $object)
+    die SL::X::DBHookError->new(when        => $when,
+                                hook        => (ref($sub) eq 'CODE' ? '<anonymous sub>' : $sub),
+                                object      => $object,
+                                object_type => ref($object))
       if !$result;
   }
 }
index 637bff6b652f7b8188153aafff4a35d5bb50dbd1..af3732309e777c149612062498851d10f6590bfd 100644 (file)
@@ -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<skip_zero> is trueish then C<---> is returned
 instead of the normal formatting if C<$value> equals 0.
 
-=item C<truncate $text, [%params]>
+=item C<truncate $text, %params>
 
 Returns the C<$text> truncated after a certain number of
-characters.
-
-The number of characters to truncate at is determined by the parameter
-C<at> 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<Common/truncate> for the actual implementation and
+supported parameters.
 
 =item C<simple_format $text>
 
index 40beabb92f1038fdeb7b66c57d435143ba16c283..d9edae200594e16d552cf4506d582bec894b1a96 100644 (file)
@@ -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 0f206949bb34f03e5f68f08e727985533c105bcd..552e5ef29a0e3976f4f58a6dc5227d2a0cd51469 100644 (file)
--- 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;
index 4cad54f27e350e21a08bfd1c8d3d37399b00f86f..12db365e1c3cd32e8b9a3ece96361f078680799f 100644 (file)
@@ -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;
index 379423249fcdce3039a691e2ec823e725893061c..1e200b0431ee5212db3d233649d3354a827ba50e 100644 (file)
@@ -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 (file)
index 0000000..b68cba2
--- /dev/null
@@ -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;