4f5632b9d2cf0240c3115dc807c86dc849b7fb20
[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 @tags          = ();
111   if ($collection && (ref $collection eq 'ARRAY')) {
112     foreach my $element (@{ $collection }) {
113       my @result = !ref $element            ? ( $element,               $element               )
114                  :  ref $element eq 'ARRAY' ? ( $element->[0],          $element->[1]          )
115                  :  ref $element eq 'HASH'  ? ( $element->{$value_key}, $element->{$title_key} )
116                  :                            ( $element->$value_key,   $element->$title_key   );
117
118       my %attributes = ( value => $result[0] );
119       $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result[0] || ''));
120
121       push @tags, $self->html_tag('option', _H($result[1]), %attributes);
122     }
123   }
124
125   return join('', @tags);
126 }
127
128 1;
129
130 __END__
131
132 =head1 NAME
133
134 SL::Templates::Plugin::L -- Layouting / tag generation
135
136 =head1 SYNOPSIS
137
138 Usage from a template:
139
140   [% USE L %]
141
142   [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
143
144   [% L.select_tag('direction', L.options_for_select([ { direction => 'left',  display => 'To the left'  },
145                                                       { direction => 'right', display => 'To the right' } ],
146                                                     value => 'direction', title => 'display', default => 'right')) %]
147
148 =head1 DESCRIPTION
149
150 A module modeled a bit after Rails' ActionView helpers. Several small
151 functions that create HTML tags from various kinds of data sources.
152
153 =head1 FUNCTIONS
154
155 =head2 LOW-LEVEL FUNCTIONS
156
157 =over 4
158
159 =item C<name_to_id $name>
160
161 Converts a name to a HTML id by replacing various characters.
162
163 =item C<attributes %items>
164
165 Creates a string from all elements in C<%items> suitable for usage as
166 HTML tag attributes. Keys and values are HTML escaped even though keys
167 must not contain non-ASCII characters for browsers to accept them.
168
169 =item C<html_tag $tag_name, $content_string, %attributes>
170
171 Creates an opening and closing HTML tag for C<$tag_name> and puts
172 C<$content_string> between the two. If C<$content_string> is undefined
173 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
174 are key/value pairs added to the opening tag.
175
176 C<$content_string> is not HTML escaped.
177
178 =back
179
180 =head2 HIGH-LEVEL FUNCTIONS
181
182 =over 4
183
184 =item C<select_tag $name, $options_string, %attributes>
185
186 Creates a HTML 'select' tag named C<$name> with the contents
187 C<$options_string> and with arbitrary HTML attributes from
188 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
189
190 The $options_string is usually created by the C<options_for_select>
191 function.
192
193 =item C<input_tag $name, $value, %attributes>
194
195 Creates a HTML 'input type=text' tag named C<$name> with the value
196 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
197 tag's C<id> defaults to C<name_to_id($name)>.
198
199 =item C<checkbox_tag $name, %attributes>
200
201 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
202 HTML attributes from C<%attributes>. The tag's C<id> defaults to
203 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
204
205 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
206 created with said C<label>. No attribute named C<label> is created in
207 that case.
208
209 =back
210
211 =head2 CONVERSION FUNCTIONS
212
213 =over 4
214
215 =item C<options_for_select \@collection, %options>
216
217 Creates a string suitable for a HTML 'select' tag consisting of one
218 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
219 to use and the title to display are extracted from the elements in
220 C<\@collection>. Each element can be one of four things:
221
222 =over 12
223
224 =item 1. An array reference with at least two elements. The first element is
225 the value, the second element is its title.
226
227 =item 2. A scalar. The scalar is both the value and the title.
228
229 =item 3. A hash reference. In this case C<%options> must contain
230 I<value> and I<title> keys that name the keys in the element to use
231 for the value and title respectively.
232
233 =item 4. A blessed reference. In this case C<%options> must contain
234 I<value> and I<title> keys that name functions called on the blessed
235 reference whose return values are used as the value and title
236 respectively.
237
238 =back
239
240 For cases 3 and 4 C<$options{value}> defaults to C<id> and
241 C<$options{title}> defaults to C<$options{value}>.
242
243 =back
244
245 =head1 MODULE AUTHORS
246
247 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
248
249 L<http://linet-services.de>