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