692c396635bb23e2e079236173fe86aa589c5194
[kivitendo-erp.git] / SL / Template / Plugin / L.pm
1 package SL::Template::Plugin::L;
2
3 use base qw( Template::Plugin );
4 use Template::Plugin;
5
6 use strict;
7
8 sub _H {
9   my $string = shift;
10   return $::locale->quote_special_chars('HTML', $string);
11 }
12
13 sub _hashify {
14   return (@_ && (ref($_[0]) eq 'HASH')) ? %{ $_[0] } : @_;
15 }
16
17 sub new {
18   my $class   = shift;
19   my $context = shift;
20
21   return bless { }, $class;
22 }
23
24 sub name_to_id {
25   my $self =  shift;
26   my $name =  shift;
27
28   $name    =~ s/[^\w_]/_/g;
29   $name    =~ s/_+/_/g;
30
31   return $name;
32 }
33
34 sub attributes {
35   my $self    = shift;
36   my %options = _hashify(@_);
37
38   my @result = ();
39   while (my ($name, $value) = each %options) {
40     next unless $name;
41     $value ||= '';
42     push @result, _H($name) . '="' . _H($value) . '"';
43   }
44
45   return @result ? ' ' . join(' ', @result) : '';
46 }
47
48 sub html_tag {
49   my $self       = shift;
50   my $tag        = shift;
51   my $content    = shift;
52   my $attributes = $self->attributes(@_);
53
54   return "<${tag}${attributes}/>" unless $content;
55   return "<${tag}${attributes}>${content}</${tag}>";
56 }
57
58 sub select_tag {
59   my $self            = shift;
60   my $name            = shift;
61   my $options_str     = shift;
62   my %attributes      = _hashify(@_);
63
64   $attributes{id}   ||= $self->name_to_id($name);
65
66   return $self->html_tag('select', $options_str, %attributes, name => $name);
67 }
68
69 sub checkbox_tag {
70   my $self             = shift;
71   my $name             = shift;
72   my %attributes       = _hashify(@_);
73
74   $attributes{id}    ||= $self->name_to_id($name);
75   $attributes{value}   = 1 unless defined $attributes{value};
76   my $label            = delete $attributes{label};
77
78   if ($attributes{checked}) {
79     $attributes{checked} = 'checked';
80   } else {
81     delete $attributes{checked};
82   }
83
84   my $code  = $self->html_tag('input', undef,  %attributes, name => $name, type => 'checkbox');
85   $code    .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
86
87   return $code;
88 }
89
90 sub input_tag {
91   my $self            = shift;
92   my $name            = shift;
93   my $value           = shift;
94   my %attributes      = _hashify(@_);
95
96   $attributes{id}   ||= $self->name_to_id($name);
97   $attributes{type} ||= 'text';
98
99   return $self->html_tag('input', undef, %attributes, name => $name, value => $value);
100 }
101
102 sub options_for_select {
103   my $self          = shift;
104   my $collection    = shift;
105   my %options       = _hashify(@_);
106
107   my $value_key     = $options{value} || 'id';
108   my $title_key     = $options{title} || $value_key;
109
110   my @elements      = ();
111   push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
112
113   if ($collection && (ref $collection eq 'ARRAY')) {
114     foreach my $element (@{ $collection }) {
115       my @result = !ref $element            ? ( $element,               $element               )
116                  :  ref $element eq 'ARRAY' ? ( $element->[0],          $element->[1]          )
117                  :  ref $element eq 'HASH'  ? ( $element->{$value_key}, $element->{$title_key} )
118                  :                            ( $element->$value_key,   $element->$title_key   );
119
120       push @elements, \@result;
121     }
122   }
123
124   my $code = '';
125   foreach my $result (@elements) {
126     my %attributes = ( value => $result->[0] );
127     $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || ''));
128
129     $code .= $self->html_tag('option', _H($result->[1]), %attributes);
130   }
131
132   return $code;
133 }
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 SL::Templates::Plugin::L -- Layouting / tag generation
142
143 =head1 SYNOPSIS
144
145 Usage from a template:
146
147   [% USE L %]
148
149   [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
150
151   [% L.select_tag('direction', L.options_for_select([ { direction => 'left',  display => 'To the left'  },
152                                                       { direction => 'right', display => 'To the right' } ],
153                                                     value => 'direction', title => 'display', default => 'right')) %]
154
155 =head1 DESCRIPTION
156
157 A module modeled a bit after Rails' ActionView helpers. Several small
158 functions that create HTML tags from various kinds of data sources.
159
160 =head1 FUNCTIONS
161
162 =head2 LOW-LEVEL FUNCTIONS
163
164 =over 4
165
166 =item C<name_to_id $name>
167
168 Converts a name to a HTML id by replacing various characters.
169
170 =item C<attributes %items>
171
172 Creates a string from all elements in C<%items> suitable for usage as
173 HTML tag attributes. Keys and values are HTML escaped even though keys
174 must not contain non-ASCII characters for browsers to accept them.
175
176 =item C<html_tag $tag_name, $content_string, %attributes>
177
178 Creates an opening and closing HTML tag for C<$tag_name> and puts
179 C<$content_string> between the two. If C<$content_string> is undefined
180 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
181 are key/value pairs added to the opening tag.
182
183 C<$content_string> is not HTML escaped.
184
185 =back
186
187 =head2 HIGH-LEVEL FUNCTIONS
188
189 =over 4
190
191 =item C<select_tag $name, $options_string, %attributes>
192
193 Creates a HTML 'select' tag named C<$name> with the contents
194 C<$options_string> and with arbitrary HTML attributes from
195 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
196
197 The $options_string is usually created by the C<options_for_select>
198 function.
199
200 =item C<input_tag $name, $value, %attributes>
201
202 Creates a HTML 'input type=text' tag named C<$name> with the value
203 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
204 tag's C<id> defaults to C<name_to_id($name)>.
205
206 =item C<checkbox_tag $name, %attributes>
207
208 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
209 HTML attributes from C<%attributes>. The tag's C<id> defaults to
210 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
211
212 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
213 created with said C<label>. No attribute named C<label> is created in
214 that case.
215
216 =back
217
218 =head2 CONVERSION FUNCTIONS
219
220 =over 4
221
222 =item C<options_for_select \@collection, %options>
223
224 Creates a string suitable for a HTML 'select' tag consisting of one
225 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
226 to use and the title to display are extracted from the elements in
227 C<\@collection>. Each element can be one of four things:
228
229 =over 12
230
231 =item 1. An array reference with at least two elements. The first element is
232 the value, the second element is its title.
233
234 =item 2. A scalar. The scalar is both the value and the title.
235
236 =item 3. A hash reference. In this case C<%options> must contain
237 I<value> and I<title> keys that name the keys in the element to use
238 for the value and title respectively.
239
240 =item 4. A blessed reference. In this case C<%options> must contain
241 I<value> and I<title> keys that name functions called on the blessed
242 reference whose return values are used as the value and title
243 respectively.
244
245 =back
246
247 For cases 3 and 4 C<$options{value}> defaults to C<id> and
248 C<$options{title}> defaults to C<$options{value}>.
249
250 If the option C<with_empty> is set then an empty element (value
251 C<undef>) will be used as the first element. The title to display for
252 this element can be set with the option C<empty_title> and defaults to
253 an empty string.
254
255 =back
256
257 =head1 MODULE AUTHORS
258
259 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
260
261 L<http://linet-services.de>