93de09bb26f322d6b1c379f4c921a9e13d8043ed
[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 new {
14   my $class   = shift;
15   my $context = shift;
16
17   return bless { }, $class;
18 }
19
20 sub name_to_id {
21   my $self =  shift;
22   my $name =  shift;
23
24   $name    =~ s/[^\w_]/_/g;
25   $name    =~ s/_+/_/g;
26
27   return $name;
28 }
29
30 sub attributes {
31   my $self    = shift;
32   my $options = shift || {};
33
34   my @result = ();
35   while (my ($name, $value) = each %{ $options }) {
36     next unless $name;
37     $value ||= '';
38     push @result, _H($name) . '="' . _H($value) . '"';
39   }
40
41   return @result ? ' ' . join(' ', @result) : '';
42 }
43
44 sub html_tag {
45   my $self       = shift;
46   my $tag        = shift;
47   my $content    = shift;
48   my $attributes = $self->attributes(shift || {});
49
50   return "<${tag}${attributes}/>" unless $content;
51   return "<${tag}${attributes}>${content}</${tag}>";
52 }
53
54 sub select_tag {
55   my $self              = shift;
56   my $name              = shift;
57   my $options_str       = shift;
58   my $attributes        = shift || {};
59
60   $attributes->{name}   = $name;
61   $attributes->{id}   ||= $self->name_to_id($name);
62
63   return $self->html_tag('select', $options_str, $attributes);
64 }
65
66 sub options_for_select {
67   my $self          = shift;
68   my $collection    = shift;
69   my $options       = shift || {};
70
71   my $value_key     = $options->{value} || 'id';
72   my $title_key     = $options->{title} || $value_key;
73
74   my @tags          = ();
75   if ($collection && (ref $collection eq 'ARRAY')) {
76     foreach my $element (@{ $collection }) {
77       my @result = !ref $element            ? ( $element,               $element               )
78                  :  ref $element eq 'ARRAY' ? ( $element->[0],          $element->[1]          )
79                  :  ref $element eq 'HASH'  ? ( $element->{$value_key}, $element->{$title_key} )
80                  :                            ( $element->$value_key,   $element->$title_key   );
81
82       my %attributes = ( value => $result[0] );
83       $attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || ''));
84
85       push @tags, $self->html_tag('option', _H($result[1]), \%attributes);
86     }
87   }
88
89   return join('', @tags);
90 }
91
92 1;
93
94 __END__
95
96 =head1 NAME
97
98 SL::Templates::Plugin::L -- Layouting / tag generation
99
100 =head1 SYNOPSIS
101
102 Usage from a template:
103
104   [% USE L %]
105
106   [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
107
108   [% L.select_tag('direction', L.options_for_select([ { direction => 'left',  display => 'To the left'  },
109                                                       { direction => 'right', display => 'To the right' } ],
110                                                     value => 'direction', title => 'display', default => 'right')) %]
111
112 =head1 DESCRIPTION
113
114 A module modeled a bit after Rails' ActionView helpers. Several small
115 functions that create HTML tags from various kinds of data sources.
116
117 =head1 FUNCTIONS
118
119 =head2 LOW-LEVEL FUNCTIONS
120
121 =over 4
122
123 =item C<name_to_id $name>
124
125 Converts a name to a HTML id by replacing various characters.
126
127 =item C<attributes \%items>
128
129 Creates a string from all elements in C<\%items> suitable for usage as
130 HTML tag attributes. Keys and values are HTML escaped even though keys
131 must not contain non-ASCII characters for browsers to accept them.
132
133 =item C<html_tag $tag_name, $content_string, \%attributes>
134
135 Creates an opening and closing HTML tag for C<$tag_name> and puts
136 C<$content_string> between the two. If C<$content_string> is undefined
137 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
138 are key/value pairs added to the opening tag.
139
140 C<$content_string> is not HTML escaped.
141
142 =back
143
144 =head2 HIGH-LEVEL FUNCTIONS
145
146 =over 4
147
148 =item C<select_tag $name, $options_string, \%attributes>
149
150 Creates a HTML 'select' tag named $name with the contents
151 $options_string and with arbitrary HTML attributes from
152 C<\%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
153
154 The $options_string is usually created by the C<options_for_select>
155 function.
156
157 =back
158
159 =head2 CONVERSION FUNCTIONS
160
161 =over 4
162
163 =item C<options_for_select \@collection, \%options>
164
165 Creates a string suitable for a HTML 'select' tag consisting of one
166 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
167 to use and the title to display are extracted from the elements in
168 C<\@collection>. Each element can be one of four things:
169
170 =over 12
171
172 =item 1. An array reference with at least two elements. The first element is
173 the value, the second element is its title.
174
175 =item 2. A scalar. The scalar is both the value and the title.
176
177 =item 3. A hash reference. In this case C<\%options> must contain
178 I<value> and I<title> keys that name the keys in the element to use
179 for the value and title respectively.
180
181 =item 4. A blessed reference. In this case C<\%options> must contain
182 I<value> and I<title> keys that name functions called on the blessed
183 reference whose return values are used as the value and title
184 respectively.
185
186 =back
187
188 For cases 3 and 4 C<$options{value}> defaults to C<id> and
189 C<$options{title}> defaults to C<$options{value}>.
190
191 =back
192
193 =head1 MODULE AUTHORS
194
195 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
196
197 L<http://linet-services.de>