1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace App\Lib\Forms\Controls;
13:
14: use Nette\Utils\Html;
15:
16: class Helpers extends \Nette\Object {
17:
18: 19: 20: 21:
22: public static function createStateList(array $items, array $inputAttrs = NULL, array $labelAttrs = NULL, $wrapper = NULL) {
23: list($inputAttrs, $inputTag) = self::prepareAttrs($inputAttrs, 'input');
24: list($labelAttrs, $labelTag) = self::prepareAttrs($labelAttrs, 'label');
25: $res = '';
26:
27: $input = Html::el();
28: $label = Html::el();
29: list($wrapper, $wrapperEnd) = $wrapper instanceof Html ? array($wrapper->startTag(), $wrapper->endTag()) : array((string) $wrapper, '');
30:
31: $frame = Html::el('div class="radioset"');
32: foreach ($items as $value => $caption) {
33: foreach ($inputAttrs as $k => $v) {
34: $input->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
35: }
36: foreach ($labelAttrs as $k => $v) {
37: $label->attrs[$k] = isset($v[$value]) ? $v[$value] : NULL;
38: }
39: $input->value = $value;
40: $res .= ($res === '' && $wrapperEnd === '' ? '' : $wrapper)
41: . $inputTag . $input->attributes() . " id =\"$value\"" . (Html::$xhtml ? ' />' : '>')
42: . $labelTag . $label->attributes() . " for =\"$value\"" .'>'
43: . ($caption instanceof Html ? $caption : htmlspecialchars($caption))
44: . '</label>'
45: . $wrapperEnd;
46: }
47: return $frame->add($res);
48: }
49:
50: private static function prepareAttrs($attrs, $name)
51: {
52: $dynamic = array();
53: foreach ((array) $attrs as $k => $v) {
54: $p = str_split($k, strlen($k) - 1);
55: if ($p[1] === '?' || $p[1] === ':') {
56: unset($attrs[$k], $attrs[$p[0]]);
57: if ($p[1] === '?') {
58: $dynamic[$p[0]] = array_fill_keys((array) $v, TRUE);
59: } elseif (is_array($v) && $v) {
60: $dynamic[$p[0]] = $v;
61: } else {
62: $attrs[$p[0]] = $v;
63: }
64: }
65: }
66: return array($dynamic, '<' . $name . Html::el(NULL, $attrs)->attributes());
67: }
68: }
69: