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