Fixing a documentation and error mistake that weren't fixed through our sesarch/repla...
[isso.git] / PrinterElement.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Base Printer Element (PrinterElement.php)
24 *
25 * @package ISSO
26 */
27
28 require_once(ISSO . '/PrinterAbstract.php');
29
30 /**
31 * Base Printer Element
32 *
33 * This class represents the lowest level of printer elements:
34 * buttons, input fields, etc. These cannot have children elements
35 * and are only attached to a parent.
36 *
37 * @author Blue Static
38 * @copyright Copyright (c)2005 - 2008, Blue Static
39 * @package ISSO
40 *
41 */
42 class BSPrinterElement extends BSPrinterAbstract
43 {
44 /**
45 * The name of the element
46 * @var string
47 */
48 private $name;
49
50 /**
51 * Type of element
52 * @var string
53 */
54 private $type;
55
56 /**
57 * The value of the element
58 * @var string
59 */
60 private $value;
61
62 /**
63 * The checked/selected attribute (checkbox, radio, and option only)
64 * @var boolean
65 */
66 private $active;
67
68 /**
69 * The JavaScript onClick attribute
70 * @var string
71 */
72 private $onClick;
73
74 /**
75 * Accesskey attribute
76 * @var string
77 */
78 private $accessKey;
79
80 /**
81 * Constructor
82 */
83 function __construct($type, $name, $value = '')
84 {
85 $this->type = $type;
86 $this->name = $name;
87 $this->value = $value;
88 $this->setCssClass('input');
89
90 if ($type == 'textarea')
91 {
92 $this->setStyle(array('width' => '100%', 'height' => '50px'));
93 }
94 }
95
96 /**
97 * Makes a new instance of the object in a static fashion
98 *
99 * @return object
100 */
101 public static function make()
102 {
103 $obj = new ReflectionClass(__CLASS__);
104 $args = func_get_args();
105 return $obj->newInstanceArgs($args);
106 }
107
108 // ###################################################################
109 /**
110 * If the type is either checkbox, radio, or option then this will
111 * set the selected/checked attribute
112 *
113 * @param boolean Active?
114 */
115 public function setActive($active)
116 {
117 if (!in_array($this->type, array('checkbox', 'radio', 'option')))
118 {
119 throw new Exception('BSPrinterElement::setActive() can only be used on elements of type checkbox, radio, or option');
120 }
121 $this->active = $active;
122 }
123
124 // ###################################################################
125 /**
126 * Sets the JavaScript onclick action
127 *
128 * @param string onClick attribute value
129 */
130 public function setOnClick($onClick)
131 {
132 $this->onClick = $onClick;
133 }
134
135 // ###################################################################
136 /**
137 * Sets the accesskey attribute value
138 *
139 * @param string Access key
140 */
141 public function setAccessKey($accessKey)
142 {
143 $this->accessKey = $accessKey;
144 }
145
146 // ###################################################################
147 /**
148 * Returns the type
149 *
150 * @return string Element type
151 */
152 public function getType()
153 {
154 return $this->type;
155 }
156
157 // ###################################################################
158 /**
159 * Returns the name of the element
160 *
161 * @return string Name
162 */
163 public function getName()
164 {
165 return $this->name;
166 }
167
168 // ###################################################################
169 /**
170 * Sets the name of the element
171 *
172 * @param string A new name
173 */
174 public function setName($name)
175 {
176 $this->name = $name;
177 }
178
179 // ###################################################################
180 /**
181 * Returns the output HTML
182 *
183 * @return string Output HTML
184 */
185 public function paint()
186 {
187 $name = ' name="' . $this->name . '"';
188 $value = ' value="' . $this->value . '"';
189 $onclick = ($this->onClick ? ' onclick="' . $this->onClick . '"' : '');
190 $accesskey = ($this->accessKey ? ' accesskey="' . $this->accessKey . '"' : '');
191
192 $attrs = $name . $value . $onclick . $accesskey;
193
194 switch ($this->type)
195 {
196 case 'text':
197 case 'password':
198 case 'button':
199 case 'submit':
200 case 'reset':
201 return '<input type="' . $this->type . '"' . $attrs . (($this->type == 'text' || $this->type == 'password') ? ' size="35" maxlength="255"' : ''). $this->_prepareStyle() . ' />';
202 break;
203
204 case 'hidden':
205 return '<input type="hidden"' . $name . $value . ' />';
206 break;
207
208 case 'checkbox':
209 case 'radio':
210 return '<input type="' . $this->type . '"' . $attrs . ($this->active ? ' checked="checked"' : '') . $this->_prepareStyle() . ' />';
211 break;
212
213 case 'upload':
214 return '<input type="file" size="35"' . $name . $this->_prepareStyle() . ' />';
215 break;
216
217 case 'option':
218 return '<option' . $value . ($this->active ? ' selected="selected"' : '') . $this->_prepareStyle() . '>' . $this->name . '</option>';
219 break;
220
221 case 'select':
222 return '<select' . $name . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</select>';
223 break;
224
225 case 'textarea':
226 if (!isset($this->style['height']) || !isset($this->style['width']))
227 {
228 throw new Exception('BSPrinterElement of type "textarea" require a "height" and "width" style attribute');
229 }
230
231 return '<textarea' . $name . ' cols="50" rows="2"' . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</textarea>';
232 break;
233
234 default:
235 throw new Exception('Invalid BSPrinterElement type "' . $this->type . '"');
236 break;
237 }
238 }
239 }
240
241 ?>