Removing all the package replacement tags and SVN keyword tags
[isso.git] / PrinterBaseElement.php
1 <?php
2 /*=====================================================================*
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-2007 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/PrinterElement.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)2002 - 2007, Blue Static
39 * @package ISSO
40 *
41 */
42 class BSPrinterBaseElement extends BSPrinterElement
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 /**
98 * If the type is either checkbox, radio, or option then this will
99 * set the selected/checked attribute
100 *
101 * @param boolean Active?
102 */
103 public function setActive($active)
104 {
105 if (!in_array($this->type, array('checkbox', 'radio', 'option')))
106 {
107 throw new Exception('BSPrinterBaseElement::setActive() can only be used on elements of type checkbox, radio, or option');
108 }
109 $this->active = $active;
110 }
111
112 // ###################################################################
113 /**
114 * Sets the JavaScript onclick action
115 *
116 * @param string onClick attribute value
117 */
118 public function setOnClick($onClick)
119 {
120 $this->onClick = $onClick;
121 }
122
123 // ###################################################################
124 /**
125 * Sets the accesskey attribute value
126 *
127 * @param string Access key
128 */
129 public function setAccessKey($accessKey)
130 {
131 $this->accessKey = $accessKey;
132 }
133
134 // ###################################################################
135 /**
136 * Returns the type
137 *
138 * @return string Element type
139 */
140 public function getType()
141 {
142 return $this->type;
143 }
144
145 // ###################################################################
146 /**
147 * Returns the name of the element
148 *
149 * @return string Name
150 */
151 public function getName()
152 {
153 return $this->name;
154 }
155
156 // ###################################################################
157 /**
158 * Sets the name of the element
159 *
160 * @param string A new name
161 */
162 public function setName($name)
163 {
164 $this->name = $name;
165 }
166
167 // ###################################################################
168 /**
169 * Returns the output HTML
170 *
171 * @return string Output HTML
172 */
173 public function paint()
174 {
175 $name = ' name="' . $this->name . '"';
176 $value = ' value="' . $this->value . '"';
177 $onclick = ($this->onClick ? ' onclick="' . $this->onClick . '"' : '');
178 $accesskey = ($this->accessKey ? ' accesskey="' . $this->accessKey . '"' : '');
179
180 $attrs = $name . $value . $onclick . $accesskey;
181
182 switch ($this->type)
183 {
184 case 'text':
185 case 'password':
186 case 'button':
187 case 'submit':
188 case 'reset':
189 return '<input type="' . $this->type . '"' . $attrs . (($this->type == 'text' OR $this->type == 'password') ? ' size="35" maxlength="255"' : ''). $this->_prepareStyle() . ' />';
190 break;
191
192 case 'hidden':
193 return '<input type="hidden"' . $name . $value . ' />';
194 break;
195
196 case 'checkbox':
197 case 'radio':
198 return '<input type="' . $this->type . '"' . $attrs . ($this->active ? ' checked="checked"' : '') . $this->_prepareStyle() . ' />';
199 break;
200
201 case 'upload':
202 return '<input type="file" size="35"' . $name . $this->_prepareStyle() . ' />';
203 break;
204
205 case 'option':
206 return '<option' . $value . ($this->active ? ' selected="selected"' : '') . $this->_prepareStyle() . '>' . $this->name . '</option>';
207 break;
208
209 case 'select':
210 return '<select' . $name . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</select>';
211 break;
212
213 case 'textarea':
214 if (!isset($this->style['height']) OR !isset($this->style['width']))
215 {
216 throw new Exception('BSPrinterBaseElement of type "textarea" require a "height" and "width" style attribute');
217 }
218
219 return '<textarea' . $name . ' cols="50" rows="2"' . $this->_prepareStyle() . $accesskey . '>' . $this->value . '</textarea>';
220 break;
221
222 default:
223 throw new Exception('Invalid PrinterBaseElement type "' . $this->type . '"');
224 break;
225 }
226 }
227 }
228
229 ?>