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