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:
40: if (substr($htmlName, -2) === '[]') {
41: $arr = array();
42: foreach (is_array($data) ? $data : array() as $v) {
43: $arr[] = $v = static::sanitize($type, $v);
44: if ($v === NULL) {
45: return array();
46: }
47: }
48: return $arr;
49: } else {
50: return static::sanitize($type, $data);
51: }
52: }
53:
54:
55: private static function sanitize($type, $value)
56: {
57: if ($type === Form::DATA_TEXT) {
58: return is_scalar($value) ? Strings::normalizeNewLines($value) : NULL;
59:
60: } elseif ($type === Form::DATA_LINE) {
61: return is_scalar($value) ? Strings::trim(strtr($value, "\r\n", ' ')) : NULL;
62:
63: } elseif ($type === Form::DATA_FILE) {
64: return $value instanceof Nette\Http\FileUpload ? $value : NULL;
65:
66: } else {
67: throw new Nette\InvalidArgumentException('Unknown data type');
68: }
69: }
70:
71:
72: 73: 74: 75:
76: public static function generateHtmlName($id)
77: {
78: $name = str_replace(Nette\ComponentModel\IComponent::NAME_SEPARATOR, '][', $id, $count);
79: if ($count) {
80: $name = substr_replace($name, '', strpos($name, ']'), 1) . ']';
81: }
82: if (is_numeric($name) || in_array($name, self::$unsafeNames)) {
83: $name = '_' . $name;
84: }
85: return $name;
86: }
87:
88:
89: 90: 91:
92: public static function createInputList(array $items, array $inputAttrs = NULL, array $labelAttrs = NULL, $wrapper = NULL)
93: {
94: list($inputAttrs, $inputTag) = self::prepareAttrs($inputAttrs, 'input');
95: list($labelAttrs, $labelTag) = self::prepareAttrs($labelAttrs, 'label');
96: $res = '';
97: $input = Html::el();
98: $label = Html::el();
99: list($wrapper, $wrapperEnd) = $wrapper instanceof Html ? array($wrapper->startTag(), $wrapper->endTag()) : array((string) $wrapper, '');
100:
101: foreach ($items as $value => $caption) {
102: foreach ($inputAttrs as $k => $v) {
103: $input->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
104: }
105: foreach ($labelAttrs as $k => $v) {
106: $label->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
107: }
108: $input->value = $value;
109: $res .= ($res === '' && $wrapperEnd === '' ? '' : $wrapper)
110: . $labelTag . $label->attributes() . '>'
111: . $inputTag . $input->attributes() . (Html::$xhtml ? ' />' : '>')
112: . ($caption instanceof Html ? $caption : htmlspecialchars($caption))
113: . '</label>'
114: . $wrapperEnd;
115: }
116: return $res;
117: }
118:
119:
120: 121: 122:
123: public static function createSelectBox(array $items, array $optionAttrs = NULL)
124: {
125: list($optionAttrs, $optionTag) = self::prepareAttrs($optionAttrs, 'option');
126: $option = Html::el();
127: $res = $tmp = '';
128: foreach ($items as $group => $subitems) {
129: if (is_array($subitems)) {
130: $res .= Html::el('optgroup')->label($group)->startTag();
131: $tmp = '</optgroup>';
132: } else {
133: $subitems = array($group => $subitems);
134: }
135: foreach ($subitems as $value => $caption) {
136: $option->value = $value;
137: foreach ($optionAttrs as $k => $v) {
138: $option->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
139: }
140: if ($caption instanceof Html) {
141: $caption = clone $caption;
142: $res .= $caption->setName('option')->addAttributes($option->attrs);
143: } else {
144: $res .= $optionTag . $option->attributes() . '>'
145: . htmlspecialchars($caption)
146: . '</option>';
147: }
148: }
149: $res .= $tmp;
150: $tmp = '';
151: }
152: return Html::el('select')->setHtml($res);
153: }
154:
155:
156: private static function prepareAttrs($attrs, $name)
157: {
158: $dynamic = array();
159: foreach ((array) $attrs as $k => $v) {
160: $p = str_split($k, strlen($k) - 1);
161: if ($p[1] === '?' || $p[1] === ':') {
162: unset($attrs[$k], $attrs[$p[0]]);
163: if ($p[1] === '?') {
164: $dynamic[$p[0]] = array_fill_keys((array) $v, TRUE);
165: } elseif (is_array($v) && $v) {
166: $dynamic[$p[0]] = $v;
167: } else {
168: $attrs[$p[0]] = $v;
169: }
170: }
171: }
172: return array($dynamic, '<' . $name . Html::el(NULL, $attrs)->attributes());
173: }
174:
175: }
176: