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