* Don't autovifivy @fields array entries.
[interchange.git] / code / UserTag / formel.tag
1 # Copyright 2002-2007 Interchange Development Group and others
2 # Copyright 2002-2005 Stefan Hornburg (racke@linuxia.de)
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.  See the LICENSE file for details.
8
9 # $Id: formel.tag,v 1.19 2007-08-01 10:52:44 kwalsh Exp $
10
11 UserTag formel Order   label name type size
12 UserTag formel addAttr
13 UserTag formel Version $Revision: 1.19 $
14 UserTag formel Routine <<EOF
15 sub {
16         my ($label, $name, $type, $size, $opt) = @_;
17         my ($labelhtml, $elhtml, $fmt);
18         my $checkfor = $opt->{'checkfor'} || $name;
19         my $sizestr = '';
20         my $labelproc;
21
22         $labelproc = sub {
23                 my ($label, $keep) = @_;
24                 my ($error);
25
26                 if ($opt->{cause}) {
27                         if ($error = $Tag->error({name => $checkfor, keep => 1})) {
28                                 $label .= $Tag->error({name => $checkfor, keep => $keep, 
29                                                 text => $opt->{cause}});
30                         }
31                 }
32                 else {
33                         $error = $Tag->error({name => $checkfor, keep => $keep});
34                 }
35
36                 if ($error) {
37                         if ($opt->{signal}) {
38                                 sprintf($opt->{signal}, $label);
39                         }
40                         else {
41                                 my $contrast = $::Variable->{CSS_CONTRAST} || 'mv_contrast';
42                                 qq{<span class="$contrast">$label</span>};
43                         }       
44                 }
45                 else {      
46                         $label;
47                 }
48         };
49
50         # set defaults
51         $type = 'text' unless $type;
52
53         for ('cause', 'format', 'order', 'reset', 'signal', 'size') {
54                 next if $opt->{$_};
55                 if ($::Values->{"mv_formel_$_"}) {
56                         $opt->{$_} = $::Values->{"mv_formel_$_"};
57                 }   
58         }
59
60         if ($opt->{'format'}) {
61                 $fmt = $opt->{'format'};
62         }
63         else {
64                 $fmt = '%s %s %s';
65         }
66
67         if ($opt->{'size'}) {
68                 if ($type eq 'textarea') {
69                         my ($cols, $rows) = split (/\s*[,x\s]\s*/, $opt->{'size'});
70                         $sizestr = qq{ rows="$rows" cols="$cols"};
71                 }
72                 else {
73                         $sizestr = qq{ size="$opt->{size}"};
74                 }
75         }
76
77         if ($opt->{'maxlength'}) {
78                 $sizestr .= qq{ maxlength="$opt->{maxlength}"};
79         }
80
81         if ($type eq 'radio' || $type eq 'checkbox') {          
82                 my ($rlabel, $rvalue, $select, @vals);
83                 
84                 if ($type eq 'checkbox') {
85                         @vals = split(/\0/, $::Values->{$name});
86                 }
87
88                 for my $button (split (/\s*,\s*/, $opt->{choices})) {
89                         $select = '';
90                         if ($button =~ /^(.*?)=(.*)$/) {
91                                 $rvalue = $1;
92                                 $rlabel = $2;
93                         }
94                         else {
95                                 $rvalue = $rlabel = $button;
96                         }
97
98                         if ($type eq 'checkbox') {
99                                 # multiple values possible for checkboxes
100                                 for my $val (@vals) {
101                                         if ($val eq $rvalue) {
102                                                 $select = 'checked';
103                                                 last;
104                                         }
105                                 }
106                         } elsif ($::Values->{$name} eq $rvalue) {
107                                 $select = ' checked';
108                         }
109
110                         $rlabel = &$labelproc($rlabel, 1);
111
112                         $elhtml .= qq{<input type="$type" name="$name" value="${rvalue}"$select $Vend::Xtrailer> $rlabel};
113                 }
114                 # delete error implicitly
115                 $labelhtml = &$labelproc($label);
116                 return sprintf ($fmt, $labelhtml, $elhtml);
117         }
118
119         $labelhtml = &$labelproc($label) if $label || $type ne 'display';
120
121         if ($type eq 'select') {
122                 my ($rlabel, $rvalue, $select);
123
124                 for my $option (split (/\s*,\s*/, $opt->{choices})) {
125                         $select = '';
126                         if ($option =~ /^(.*?)=(.*)$/) {
127                                 $rvalue = $1;
128                                 $rlabel = $2;
129                         }
130                         else {
131                                 $rvalue = $rlabel = $option;
132                         }
133
134                         if ($::Values->{$name} eq $rvalue) {
135                                 $select = ' selected="selected"';
136                         }
137                         if ($rvalue eq $rlabel) {       
138                                 $elhtml .= qq{<option $select>$rlabel</option>};
139                         }
140                         else {
141                                 $elhtml .= qq{<option value="$rvalue"$select>$rlabel</option>};
142                         }
143                 }
144                 return sprintf ($fmt, $labelhtml, 
145                                 qq{<select name="$name">$elhtml</select>});
146         }
147
148         if ($type eq 'display') {
149                 if ($label) {
150                         # use provided label
151                         $elhtml = $Tag->display($opt->{table} || 'products', $name, '', 
152                                         {value => $Values->{$name}});
153                 }
154                 else {
155                         # use dummy template to retrieve label from metadata
156                         $elhtml = $Tag->display($opt->{table} || 'products', $name, '', 
157                                         {value => $Values->{$name}, 
158                                         template => join(" \0", '$LABEL$', '$WIDGET$')});
159                         ($label, $elhtml) = split(/\s\0/, $elhtml);
160                         $labelhtml = &$labelproc($label);
161                 }
162         } elsif ($opt->{reset}) {
163                 if ($type eq 'textarea') {
164                         $elhtml = qq{<textarea name="${name}"$sizestr></textarea>};
165                 }
166                 else {
167                         $elhtml = qq{<input type="$type" name="${name}"$sizestr $Vend::Xtrailer>};
168                 }
169         }
170         else {
171                 if ($type eq 'textarea') {
172                         $elhtml = qq{<textarea name="${name}"$sizestr>$::Values->{$name}</textarea>};
173
174                 }
175                 elsif ($type eq 'text' || $type eq 'password' || $type !~ /\S/) {
176                         $elhtml = qq{<input type="$type" name="$name" value="$::Values->{$name}"$sizestr $Vend::Xtrailer>};
177                 }
178                 else {
179                         # pass type directly to display tag
180                         if ($opt->{order}) {
181                                 $fmt = sprintf($fmt, '$WIDGET$', '$LABEL$', $opt->{help});
182                         } else {
183                                 $fmt = sprintf($fmt, '$LABEL$', '$WIDGET$', $opt->{help});
184                         }
185
186                         return $Tag->display({name => $name,
187                                                    type => $type,
188                                                    label => $label,
189                                                    value => $Values->{$name},
190                                                    template => $fmt});
191                 }
192         }
193
194         if ($opt->{order}) {
195                 # display form element first
196                 sprintf ($fmt, $elhtml, $labelhtml, $opt->{help});
197         }
198         else {
199                 # display label first
200                 sprintf ($fmt, $labelhtml, $elhtml, $opt->{help});
201         }
202 }
203 EOF