1: <?php
2:
3: namespace Alpha\View\Widget;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Alpha\Util\Security\SecurityUtils;
7: use Alpha\Util\InputFilter;
8: use Alpha\Model\Type\Text;
9: use Alpha\Exception\IllegalArguementException;
10:
11: /**
12: * Text HTML input box custom widget.
13: *
14: * @since 1.0
15: *
16: * @author John Collins <dev@alphaframework.org>
17: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
18: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
19: * All rights reserved.
20: *
21: * <pre>
22: * Redistribution and use in source and binary forms, with or
23: * without modification, are permitted provided that the
24: * following conditions are met:
25: *
26: * * Redistributions of source code must retain the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer.
29: * * Redistributions in binary form must reproduce the above
30: * copyright notice, this list of conditions and the
31: * following disclaimer in the documentation and/or other
32: * materials provided with the distribution.
33: * * Neither the name of the Alpha Framework nor the names
34: * of its contributors may be used to endorse or promote
35: * products derived from this software without specific
36: * prior written permission.
37: *
38: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
43: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
49: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51: * </pre>
52: */
53: class TextBox
54: {
55: /**
56: * The text object that will be edited by this text box.
57: *
58: * @var Alpha\Model\Type\Text
59: *
60: * @since 1.0
61: */
62: public $textObject;
63:
64: /**
65: * The data label for the text object.
66: *
67: * @var string
68: *
69: * @since 1.0
70: */
71: public $label;
72:
73: /**
74: * The name of the HTML input box.
75: *
76: * @var string
77: *
78: * @since 1.0
79: */
80: public $name;
81:
82: /**
83: * The amount of rows to display by default.
84: *
85: * @var int
86: *
87: * @since 1.0
88: */
89: public $rows;
90:
91: /**
92: * An optional additional idenitfier to append to the id of the text box where many are on one page.
93: *
94: * @var int
95: *
96: * @since 1.0
97: */
98: public $identifier;
99:
100: /**
101: * The constructor.
102: *
103: * @param Alpha\Model\Type\Text $text The text object that will be edited by this text box.
104: * @param string $label The data label for the text object.
105: * @param string $name The name of the HTML input box.
106: * @param int $rows The display size (rows).
107: * @param int $identifier An additional idenitfier to append to the id of the text box.
108: *
109: * @since 1.0
110: *
111: * @throws Alpha\Exception\IllegalArguementException
112: */
113: public function __construct($text, $label, $name, $rows = 5, $identifier = 0)
114: {
115: $config = ConfigProvider::getInstance();
116:
117: if ($text instanceof Text) {
118: $this->textObject = $text;
119: } else {
120: throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
121: }
122:
123: $this->label = $label;
124: $this->rows = $rows;
125: $this->identifier = $identifier;
126:
127: if ($config->get('security.encrypt.http.fieldnames')) {
128: $this->name = base64_encode(SecurityUtils::encrypt($name));
129: } else {
130: $this->name = $name;
131: }
132: }
133:
134: /**
135: * Renders the HTML and javascript for the text box.
136: *
137: * @return string
138: *
139: * @since 1.0
140: */
141: public function render()
142: {
143: $config = ConfigProvider::getInstance();
144:
145: $html = '<div class="form-group">';
146: $html .= ' <label for="'.$this->name.'">'.$this->label.'</label>';
147:
148: $html .= '<textarea class="form-control" maxlength="'.$this->textObject->getSize().'" id="text_field_'.$this->name.'_'.$this->identifier.'" rows="'.$this->rows.'" name="'.$this->name.'">';
149:
150: if ($this->textObject->getAllowHTML()) {
151: $html .= InputFilter::decode($this->textObject->getValue(), true);
152: } else {
153: $html .= InputFilter::decode($this->textObject->getValue());
154: }
155:
156: $html .= '</textarea>';
157:
158: $html .= '</div>';
159:
160: if ($this->textObject->getRule() != '') {
161: $html .= '<input type="hidden" id="text_field_'.$this->name.'_'.$this->identifier.'_msg" value="'.$this->textObject->getHelper().'"/>';
162: $html .= '<input type="hidden" id="text_field_'.$this->name.'_'.$this->identifier.'_rule" value="'.$this->textObject->getRule().'"/>';
163: }
164:
165: return $html;
166: }
167:
168: /**
169: * Setter for text object.
170: *
171: * @param string $text
172: *
173: * @since 1.0
174: *
175: * @throws Alpha\Exception\IllegalArguementException
176: */
177: public function setTextObject($text)
178: {
179: if ($text instanceof Text) {
180: $this->text = $text;
181: } else {
182: throw new IllegalArguementException('Text object passed ['.var_export($text, true).'] is not a valid Text object!');
183: }
184: }
185:
186: /**
187: * Getter for text object.
188: *
189: * @return Alpha\Model\Type\Text
190: *
191: * @since 1.0
192: */
193: public function getTextObject()
194: {
195: return $this->textObject;
196: }
197: }
198: