1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class Horde_View_Helper_Form_InstanceTag_Form extends Horde_View_Helper_Form_InstanceTag_Base
25: {
26: public function toLabelTag($text, $options = array())
27: {
28: return $this->contentTag('label', $text, $options);
29: }
30:
31: public function toInputFieldTag($fieldType, $options = array())
32: {
33: if (!isset($options['size'])) {
34: $options['size'] = isset($options['maxlength'])
35: ? $options['maxlength']
36: : $this->_defaultFieldOptions['size'];
37: }
38: $options = array_merge($this->_defaultFieldOptions, $options);
39:
40: if ($fieldType == 'hidden') {
41: unset($options['size']);
42: }
43: $options['type'] = $fieldType;
44:
45: if ($fieldType != 'file') {
46: if (!isset($options['value'])) {
47: $options['value'] = $this->valueBeforeTypeCast($this->object());
48: }
49: }
50: $options = $this->addDefaultNameAndId($options);
51:
52: return $this->tag('input', $options);
53: }
54:
55: public function toRadioButtonTag($tagValue, $options = array())
56: {
57: $options = array_merge($this->_defaultRadioOptions, $options);
58: $options['type'] = 'radio';
59: $options['value'] = $tagValue;
60: if (isset($options['checked'])) {
61: $cv = $options['checked'];
62: unset($options['checked']);
63: $checked = ($cv == true || $cv == 'checked');
64: } else {
65: $checked = $this->isRadioButtonChecked($this->value($this->object()), $tagValue);
66: }
67: $options['checked'] = (boolean)$checked;
68:
69: $prettyTagValue = strval($tagValue);
70: $prettyTagValue = preg_replace('/\s/', '_', $prettyTagValue);
71: $prettyTagValue = preg_replace('/\W/', '', $prettyTagValue);
72: $prettyTagValue = strtolower($prettyTagValue);
73:
74: if (! isset($options['id'])) {
75: if (isset($this->autoIndex)) {
76: $options['id'] = "{$this->objectName}_{$this->autoIndex}_{$this->objectProperty}_$prettyTagValue";
77: } else {
78: $options['id'] = "{$this->objectName}_{$this->objectProperty}_$prettyTagValue";
79: }
80: }
81:
82: $options = $this->addDefaultNameAndId($options);
83:
84: return $this->tag('input', $options);
85: }
86:
87: public function toTextAreaTag($options = array())
88: {
89: $options = array_merge($this->_defaultTextAreaOptions, $options);
90: $options = $this->addDefaultNameAndId($options);
91:
92: if (isset($options['size'])) {
93: $size = $options['size'];
94: unset($options['size']);
95:
96: list($options['cols'], $options['rows']) = explode('x', $size);
97: }
98:
99: if (isset($options['value'])) {
100: $value = $options['value'];
101: unset($options['value']);
102: } else {
103: $value = $this->valueBeforeTypeCast($this->object(), $options);
104: }
105:
106: return $this->contentTag('textarea', htmlentities($value), $options);
107: }
108:
109: public function toCheckBoxTag($options = array(), $checkedValue = '1',
110: $uncheckedValue = '0')
111: {
112: $options['type'] = 'checkbox';
113: $options['value'] = $checkedValue;
114: if (isset($options['checked'])) {
115: $cv = $options['checked'];
116: unset($options['checked']);
117: $checked = ($cv == true || $cv == 'checked');
118: } else {
119: $checked = $this->isCheckBoxChecked($this->value($this->object()), $checkedValue);
120: }
121: $options['checked'] = (boolean)$checked;
122: $options = $this->addDefaultNameAndId($options);
123:
124:
125: $tags = $this->tag('input', array('name' => $options['name'],
126: 'type' => 'hidden',
127: 'value' => $uncheckedValue))
128: . $this->tag('input', $options);
129:
130: return $tags;
131: }
132:
133: protected function isCheckBoxChecked($value, $checkedValue)
134: {
135: switch (gettype($value)) {
136: case 'boolean':
137: return $value;
138: case 'NULL':
139: return false;
140: case 'integer':
141: return $value != 0;
142: case 'string':
143: return $value == $checkedValue;
144: default:
145: return intval($value) != 0;
146: }
147: }
148:
149: protected function isRadioButtonChecked($value, $checkedValue)
150: {
151: return (strval($value) == strval($checkedValue));
152: }
153: }
154: