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