Removed a globalization of $bugsys (wow, talk about directly ripping from Bugdar)
[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 * @return fluent interface
116 */
117 public function setActive($active)
118 {
119 if (!in_array($this->type, array('checkbox', 'radio', 'option')))
120 {
121 throw new Exception('BSPrinterElement::setActive() can only be used on elements of type checkbox, radio, or option');
122 }
123 $this->active = $active;
124 return $this;
125 }
126
127 // ###################################################################
128 /**
129 * Sets the JavaScript onclick action
130 *
131 * @param string onClick attribute value
132 *
133 * @return fluent interface
134 */
135 public function setOnClick($onClick)
136 {
137 $this->onClick = $onClick;
138 return $this;
139 }
140
141 // ###################################################################
142 /**
143 * Sets the accesskey attribute value
144 *
145 * @param string Access key
146 *
147 * @return fluent interface
148 */
149 public function setAccessKey($accessKey)
150 {
151 $this->accessKey = $accessKey;
152 return $this;
153 }
154
155 // ###################################################################
156 /**
157 * Returns the type
158 *
159 * @return string Element type
160 */
161 public function getType()
162 {
163 return $this->type;
164 }
165
166 // ###################################################################
167 /**
168 * Returns the name of the element
169 *
170 * @return string Name
171 */
172 public function getName()
173 {
174 return $this->name;
175 }
176
177 // ###################################################################
178 /**
179 * Sets the name of the element
180 *
181 * @param string A new name
182 *
183 * @return fluent interface
184 */
185 public function setName($name)
186 {
187 $this->name = $name;
188 return $this;
189 }
190
191 // ###################################################################
192 /**
193 * Returns the output HTML
194 *
195 * @return string Output HTML
196 */
197 public function paint()
198 {
199 $name = ' name="' . $this->name . '"';
200 $value = ' value="' . $this->value . '"';
201 $onclick = ($this->onClick ? ' onclick="' . $this->onClick . '"' : '');
202 $accesskey = ($this->accessKey ? ' accesskey="' . $this->accessKey . '"' : '');
203
204 $attrs = $name . $value . $onclick . $accesskey;
205
206 switch ($this->type)
207 {
208 case 'text':
209 case 'password':
210 case 'button':
211 case 'submit':
212 case 'reset':
213 return '<input type="' . $this->type . '"' . $attrs . (($this->type == 'text' || $this->type == 'password') ? ' size="35" maxlength="255"' : ''). $this->_prepareStyle() . ' />';
214 break;
215
216 case 'hidden':
217 return '<input type="hidden"' . $name . $value . ' />';
218 break;
219
220 case 'checkbox':
221 case 'radio':
222 return '<input type="' . $this->type . '"' . $attrs . ($this->active ? ' checked="checked"' : '') . $this->_prepareStyle() . ' />';
223 break;
224
225 case 'upload':
226 return '<input type="file" size="35"' . $name . $this->_prepareStyle() . ' />';
227 break;
228
229 case 'option':
230 return '<option' . $value . ($this->active ? ' selected="selected"' : '') . $this->_prepareStyle() . '>' . $this->name . '</option>';
231 break;
232
233 case 'select':
234 return '<select' . $name . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</select>';
235 break;
236
237 case 'textarea':
238 if (!isset($this->style['height']) || !isset($this->style['width']))
239 {
240 throw new Exception('BSPrinterElement of type "textarea" require a "height" and "width" style attribute');
241 }
242
243 return '<textarea' . $name . ' cols="50" rows="2"' . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</textarea>';
244 break;
245
246 default:
247 throw new Exception('Invalid BSPrinterElement type "' . $this->type . '"');
248 break;
249 }
250 }
251 }
252
253 ?>