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