637bff6b652f7b8188153aafff4a35d5bb50dbd1
[kivitendo-erp.git] / SL / Presenter / Text.pm
1 package SL::Presenter::Text;
2
3 use strict;
4
5 use parent qw(Exporter);
6
7 use Exporter qw(import);
8 our @EXPORT = qw(format_man_days simple_format truncate);
9
10 use Carp;
11
12 sub truncate {
13   my ($self, $text, %params) = @_;
14
15   $params{at}             ||= 50;
16   $params{at}               =  3 if 3 > $params{at};
17   $params{at}              -= 3;
18
19   return $text if length($text) < $params{at};
20   return substr($text, 0, $params{at}) . '...';
21 }
22
23 sub simple_format {
24   my ($self, $text, %params) = @_;
25
26   $text =  $::locale->quote_special_chars('HTML', $text || '');
27
28   $text =~ s{\r\n?}{\n}g;                    # \r\n and \r -> \n
29   $text =~ s{\n\n+}{</p>\n\n<p>}g;           # 2+ newline  -> paragraph
30   $text =~ s{([^\n]\n)(?=[^\n])}{$1<br />}g; # 1 newline   -> br
31
32   return '<p>' . $text;
33 }
34
35 sub format_man_days {
36   my ($self, $value, %params) = @_;
37
38   return '---' if $params{skip_zero} && !$value;
39
40   return $self->escape($::locale->text('#1 h', $::form->format_amount(\%::myconfig, $value, 2))) if 8.0 > $value;
41
42   $value     /= 8.0;
43   my $output  = $::locale->text('#1 MD', int($value));
44   my $rest    = ($value - int($value)) * 8.0;
45   $output    .= ' ' . $::locale->text('#1 h', $::form->format_amount(\%::myconfig, $rest)) if $rest > 0.0;
46
47   return $self->escape($output);
48 }
49
50 1;
51 __END__
52
53 =pod
54
55 =encoding utf8
56
57 =head1 NAME
58
59 SL::Presenter::Text - Presenter module for assorted text helpers
60
61 =head1 SYNOPSIS
62
63   my $long_text = "This is very, very long. Need shorter, surely.";
64   my $truncated = $::request->presenter->truncate($long_text, at => 10);
65   # Result: "This is..."
66
67 =head1 FUNCTIONS
68
69 =over 4
70
71 =item C<format_man_days $value, [%params]>
72
73 C<$value> is interpreted to mean a number of hours (for C<$value> < 8)
74 / man days (if >= 8). Returns a translated, human-readable version of
75 it, e.g. C<2 PT 2 h> for the value C<18> and German.
76
77 If the parameter C<skip_zero> is trueish then C<---> is returned
78 instead of the normal formatting if C<$value> equals 0.
79
80 =item C<truncate $text, [%params]>
81
82 Returns the C<$text> truncated after a certain number of
83 characters.
84
85 The number of characters to truncate at is determined by the parameter
86 C<at> which defaults to 50. If the text is longer than C<$params{at}>
87 then it will be truncated and postfixed with '...'. Otherwise it will
88 be returned unmodified.
89
90 =item C<simple_format $text>
91
92 Applies simple formatting rules to C<$text>: The text is put into
93 paragraph HTML tags. Two consecutive newlines are interpreted as a
94 paragraph change: they close the current paragraph tag and start a new
95 one. Single newlines are converted to line breaks. Carriage returns
96 are removed.
97
98 =back
99
100 =head1 BUGS
101
102 Nothing here yet.
103
104 =head1 AUTHOR
105
106 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
107
108 =cut