Revision 6737d960
Von Moritz Bunkus vor mehr als 15 Jahren hinzugefügt
SL/Form.pm | ||
---|---|---|
67 | 67 |
} |
68 | 68 |
} |
69 | 69 |
|
70 |
=item _store_value() |
|
71 |
|
|
72 |
parses a complex var name, and stores it in the form. |
|
73 |
|
|
74 |
syntax: |
|
75 |
$form->_store_value($key, $value); |
|
76 |
|
|
77 |
keys must start with a string, and can contain various tokens. |
|
78 |
supported key structures are: |
|
79 |
|
|
80 |
1. simple access |
|
81 |
simple key strings work as expected |
|
82 |
|
|
83 |
id => $form->{id} |
|
84 |
|
|
85 |
2. hash access. |
|
86 |
separating two keys by a dot (.) will result in a hash lookup for the inner value |
|
87 |
this is similar to the behaviour of java and templating mechanisms. |
|
88 |
|
|
89 |
filter.description => $form->{filter}->{description} |
|
90 |
|
|
91 |
3. array+hashref access |
|
92 |
|
|
93 |
adding brackets ([]) before the dot will cause the next hash to be put into an array. |
|
94 |
using [+] instead of [] will force a new array index. this is useful for recurring |
|
95 |
data structures like part lists. put a [+] into the first varname, and use [] on the |
|
96 |
following ones. |
|
97 |
|
|
98 |
repeating these names in your template: |
|
99 |
|
|
100 |
invoice.items[+].id |
|
101 |
invoice.items[].parts_id |
|
102 |
|
|
103 |
will result in: |
|
104 |
|
|
105 |
$form->{invoice}->{items}->[ |
|
106 |
{ |
|
107 |
id => ... |
|
108 |
parts_id => ... |
|
109 |
}, |
|
110 |
{ |
|
111 |
id => ... |
|
112 |
parts_id => ... |
|
113 |
} |
|
114 |
... |
|
115 |
] |
|
116 |
|
|
117 |
4. arrays |
|
118 |
|
|
119 |
using brackets at the end of a name will result in a pure array to be created. |
|
120 |
note that you mustn't use [+], which is reserved for array+hash access and will |
|
121 |
result in undefined behaviour in array context. |
|
122 |
|
|
123 |
filter.status[] => $form->{status}->[ val1, val2, ... ] |
|
124 |
|
|
125 |
=cut |
|
70 | 126 |
sub _store_value { |
71 | 127 |
$main::lxdebug->enter_sub(2); |
72 | 128 |
|
73 |
my $curr = shift;
|
|
129 |
my $self = shift;
|
|
74 | 130 |
my $key = shift; |
75 | 131 |
my $value = shift; |
76 | 132 |
|
77 |
while ($key =~ /\[\+?\]\.|\./) { |
|
78 |
substr($key, 0, $+[0]) = ''; |
|
133 |
my @tokens = split /((?:\[\+?\])?(?:\.|$))/, $key; |
|
79 | 134 |
|
80 |
if ($& eq '.') { |
|
81 |
$curr->{$`} ||= { }; |
|
82 |
$curr = $curr->{$`}; |
|
135 |
my $curr; |
|
83 | 136 |
|
84 |
} else { |
|
85 |
$curr->{$`} ||= [ ]; |
|
86 |
if (!scalar @{ $curr->{$`} } || $& eq '[+].') { |
|
87 |
push @{ $curr->{$`} }, { }; |
|
88 |
} |
|
137 |
if (scalar @tokens) { |
|
138 |
$curr = \ $self->{ shift @tokens }; |
|
139 |
} |
|
89 | 140 |
|
90 |
$curr = $curr->{$`}->[-1]; |
|
91 |
} |
|
141 |
while (@tokens) { |
|
142 |
my $sep = shift @tokens; |
|
143 |
my $key = shift @tokens; |
|
144 |
|
|
145 |
$curr = \ $$curr->[++$#$$curr], next if $sep eq '[]'; |
|
146 |
$curr = \ $$curr->[max 0, $#$$curr] if $sep eq '[].'; |
|
147 |
$curr = \ $$curr->[++$#$$curr] if $sep eq '[+].'; |
|
148 |
$curr = \ $$curr->{$key} |
|
92 | 149 |
} |
93 | 150 |
|
94 |
$curr->{$key} = $value;
|
|
151 |
$$curr = $value;
|
|
95 | 152 |
|
96 | 153 |
$main::lxdebug->leave_sub(2); |
97 | 154 |
|
98 |
return \$curr->{$key};
|
|
155 |
return $curr;
|
|
99 | 156 |
} |
100 | 157 |
|
101 | 158 |
sub _input_to_hash { |
102 | 159 |
$main::lxdebug->enter_sub(2); |
103 | 160 |
|
104 |
my $params = shift;
|
|
105 |
my $input = shift;
|
|
161 |
my $self = shift;
|
|
162 |
my $input = shift; |
|
106 | 163 |
|
107 |
my @pairs = split(/&/, $input);
|
|
164 |
my @pairs = split(/&/, $input); |
|
108 | 165 |
|
109 | 166 |
foreach (@pairs) { |
110 | 167 |
my ($key, $value) = split(/=/, $_, 2); |
111 |
_store_value($params, unescape(undef, $key), unescape(undef, $value));
|
|
168 |
$self->_store_value($self->unescape($key), $self->unescape($value)) if ($key);
|
|
112 | 169 |
} |
113 | 170 |
|
114 | 171 |
$main::lxdebug->leave_sub(2); |
... | ... | |
117 | 174 |
sub _request_to_hash { |
118 | 175 |
$main::lxdebug->enter_sub(2); |
119 | 176 |
|
120 |
my $params = shift;
|
|
121 |
my $input = shift;
|
|
177 |
my $self = shift;
|
|
178 |
my $input = shift; |
|
122 | 179 |
|
123 | 180 |
if (!$ENV{'CONTENT_TYPE'} |
124 | 181 |
|| ($ENV{'CONTENT_TYPE'} !~ /multipart\/form-data\s*;\s*boundary\s*=\s*(.+)$/)) { |
125 | 182 |
|
126 |
_input_to_hash($params, $input);
|
|
183 |
$self->_input_to_hash($input);
|
|
127 | 184 |
|
128 | 185 |
$main::lxdebug->leave_sub(2); |
129 | 186 |
return; |
... | ... | |
171 | 228 |
substr $line, $-[0], $+[0] - $-[0], ""; |
172 | 229 |
} |
173 | 230 |
|
174 |
$previous = _store_value($params, $name, '');
|
|
175 |
$params->{FILENAME} = $filename if ($filename);
|
|
231 |
$previous = $self->_store_value($name, '') if ($name);
|
|
232 |
$self->{FILENAME} = $filename if ($filename);
|
|
176 | 233 |
|
177 | 234 |
next; |
178 | 235 |
} |
... | ... | |
243 | 300 |
|
244 | 301 |
bless $self, $type; |
245 | 302 |
|
246 |
my $parameters = { }; |
|
247 |
_request_to_hash($parameters, $_); |
|
303 |
$self->_request_to_hash($_); |
|
248 | 304 |
|
249 | 305 |
my $db_charset = $main::dbcharset; |
250 | 306 |
$db_charset ||= Common::DEFAULT_CHARSET; |
251 | 307 |
|
252 |
if ($parameters->{INPUT_ENCODING} && (lc $parameters->{INPUT_ENCODING} ne $db_charset)) {
|
|
308 |
if ($self->{INPUT_ENCODING} && (lc $self->{INPUT_ENCODING} ne $db_charset)) {
|
|
253 | 309 |
require Text::Iconv; |
254 |
my $iconv = Text::Iconv->new($parameters->{INPUT_ENCODING}, $db_charset);
|
|
310 |
my $iconv = Text::Iconv->new($self->{INPUT_ENCODING}, $db_charset);
|
|
255 | 311 |
|
256 |
_recode_recursively($iconv, $parameters);
|
|
312 |
_recode_recursively($iconv, $self);
|
|
257 | 313 |
|
258 |
delete $parameters{INPUT_ENCODING};
|
|
314 |
delete $self{INPUT_ENCODING};
|
|
259 | 315 |
} |
260 | 316 |
|
261 |
map { $self->{$_} = $parameters->{$_}; } keys %{ $parameters }; |
|
262 |
|
|
263 | 317 |
$self->{action} = lc $self->{action}; |
264 | 318 |
$self->{action} =~ s/( |-|,|\#)/_/g; |
265 | 319 |
|
Auch abrufbar als: Unified diff
_store_value() aus Kundenprojekt übernommen.
1. Dokumentation der Funktion
2. Möglichkeit, 'variablenname[]' für einfache Arrays zu benutzen
3. Umstellung der anderen Funktionen, die _store_value() benutzen, auf Objekt-Aufrufsyntax.