-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathHelpers.php
226 lines (199 loc) · 6.08 KB
/
Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Forms;
use Nette;
use Nette\Utils\Strings;
use Nette\Utils\Html;
/**
* Forms helpers.
*/
class Helpers extends Nette\Object
{
private static $unsafeNames = array(
'attributes', 'children', 'elements', 'focus', 'length', 'reset', 'style', 'submit', 'onsubmit', 'form',
'presenter', 'action',
);
/**
* Extracts and sanitizes submitted form data for single control.
* @param array submitted data
* @param string control HTML name
* @param string type Form::DATA_TEXT, DATA_LINE, DATA_FILE, DATA_KEYS
* @return string|string[]
* @internal
*/
public static function extractHttpData(array $data, $htmlName, $type)
{
$name = explode('[', str_replace(array('[]', ']', '.'), array('', '', '_'), $htmlName));
$data = Nette\Utils\Arrays::get($data, $name, NULL);
$itype = $type & ~Form::DATA_KEYS;
if (substr($htmlName, -2) === '[]') {
if (!is_array($data)) {
return array();
}
foreach ($data as $k => $v) {
$data[$k] = $v = static::sanitize($itype, $v);
if ($v === NULL) {
unset($data[$k]);
}
}
if ($type & Form::DATA_KEYS) {
return $data;
}
return array_values($data);
} else {
return static::sanitize($itype, $data);
}
}
private static function sanitize($type, $value)
{
if ($type === Form::DATA_TEXT) {
return is_scalar($value) ? Strings::normalizeNewLines($value) : NULL;
} elseif ($type === Form::DATA_LINE) {
return is_scalar($value) ? Strings::trim(strtr($value, "\r\n", ' ')) : NULL;
} elseif ($type === Form::DATA_FILE) {
return $value instanceof Nette\Http\FileUpload ? $value : NULL;
} else {
throw new Nette\InvalidArgumentException('Unknown data type');
}
}
/**
* Converts control name to HTML name.
* @return string
*/
public static function generateHtmlName($id)
{
$name = str_replace(Nette\ComponentModel\IComponent::NAME_SEPARATOR, '][', $id, $count);
if ($count) {
$name = substr_replace($name, '', strpos($name, ']'), 1) . ']';
}
if (is_numeric($name) || in_array($name, self::$unsafeNames, TRUE)) {
$name = '_' . $name;
}
return $name;
}
/**
* @return array
*/
public static function exportRules(Rules $rules)
{
$payload = array();
foreach ($rules as $rule) {
if (!is_string($op = $rule->validator)) {
if (!Nette\Utils\Callback::isStatic($op)) {
continue;
}
$op = Nette\Utils\Callback::toString($op);
}
if ($rule->branch) {
$item = array(
'op' => ($rule->isNegative ? '~' : '') . $op,
'rules' => static::exportRules($rule->branch),
'control' => $rule->control->getHtmlName(),
);
if ($rule->branch->getToggles()) {
$item['toggle'] = $rule->branch->getToggles();
} elseif (!$item['rules']) {
continue;
}
} else {
$item = array('op' => ($rule->isNegative ? '~' : '') . $op, 'msg' => Validator::formatMessage($rule, FALSE));
}
if (is_array($rule->arg)) {
$item['arg'] = array();
foreach ($rule->arg as $key => $value) {
$item['arg'][$key] = $value instanceof IControl ? array('control' => $value->getHtmlName()) : $value;
}
} elseif ($rule->arg !== NULL) {
$item['arg'] = $rule->arg instanceof IControl ? array('control' => $rule->arg->getHtmlName()) : $rule->arg;
}
$payload[] = $item;
}
return $payload;
}
/**
* @return string
*/
public static function createInputList(array $items, array $inputAttrs = NULL, array $labelAttrs = NULL, $wrapper = NULL)
{
list($inputAttrs, $inputTag) = self::prepareAttrs($inputAttrs, 'input');
list($labelAttrs, $labelTag) = self::prepareAttrs($labelAttrs, 'label');
$res = '';
$input = Html::el();
$label = Html::el();
list($wrapper, $wrapperEnd) = $wrapper instanceof Html ? array($wrapper->startTag(), $wrapper->endTag()) : array((string) $wrapper, '');
foreach ($items as $value => $caption) {
foreach ($inputAttrs as $k => $v) {
$input->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
}
foreach ($labelAttrs as $k => $v) {
$label->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
}
$input->value = $value;
$res .= ($res === '' && $wrapperEnd === '' ? '' : $wrapper)
. $labelTag . $label->attributes() . '>'
. $inputTag . $input->attributes() . (Html::$xhtml ? ' />' : '>')
. ($caption instanceof Html ? $caption : htmlspecialchars($caption, ENT_NOQUOTES, 'UTF-8'))
. '</label>'
. $wrapperEnd;
}
return $res;
}
/**
* @return Html
*/
public static function createSelectBox(array $items, array $optionAttrs = NULL, $selected = NULL)
{
if ($selected !== NULL) {
$optionAttrs['selected?'] = $selected;
}
list($optionAttrs, $optionTag) = self::prepareAttrs($optionAttrs, 'option');
$option = Html::el();
$res = $tmp = '';
foreach ($items as $group => $subitems) {
if (is_array($subitems)) {
$res .= Html::el('optgroup')->label($group)->startTag();
$tmp = '</optgroup>';
} else {
$subitems = array($group => $subitems);
}
foreach ($subitems as $value => $caption) {
$option->value = $value;
foreach ($optionAttrs as $k => $v) {
$option->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
}
if ($caption instanceof Html) {
$caption = clone $caption;
$res .= $caption->setName('option')->addAttributes($option->attrs);
} else {
$res .= $optionTag . $option->attributes() . '>'
. htmlspecialchars($caption, ENT_NOQUOTES, 'UTF-8')
. '</option>';
}
}
$res .= $tmp;
$tmp = '';
}
return Html::el('select')->setHtml($res);
}
private static function prepareAttrs($attrs, $name)
{
$dynamic = array();
foreach ((array) $attrs as $k => $v) {
$p = str_split($k, strlen($k) - 1);
if ($p[1] === '?' || $p[1] === ':') {
unset($attrs[$k], $attrs[$p[0]]);
if ($p[1] === '?') {
$dynamic[$p[0]] = array_fill_keys((array) $v, TRUE);
} elseif (is_array($v) && $v) {
$dynamic[$p[0]] = $v;
} else {
$attrs[$p[0]] = $v;
}
}
}
return array($dynamic, '<' . $name . Html::el(NULL, $attrs)->attributes());
}
}