|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Abstract form elements |
|
4
|
|
|
* |
|
5
|
|
|
* Ntentan Framework |
|
6
|
|
|
* Copyright (c) 2008-2012 James Ekow Abaka Ainooson |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
9
|
|
|
* a copy of this software and associated documentation files (the |
|
10
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
11
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
12
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
13
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
14
|
|
|
* the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be |
|
17
|
|
|
* included in all copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
20
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
21
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
22
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
23
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
24
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
25
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
* @author James Ainooson <[email protected]> |
|
28
|
|
|
* @copyright Copyright 2010 James Ekow Abaka Ainooson |
|
29
|
|
|
* @license MIT |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
|
33
|
|
|
|
|
34
|
|
|
use ntentan\honam\engines\php\helpers\formHelper; |
|
35
|
|
|
use ntentan\honam\TemplateEngine; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The form element class. An element can be anything from the form |
|
39
|
|
|
* itself to the objects that are put in the form. Provides an |
|
40
|
|
|
* abstract render class that is used to output the HTML associated |
|
41
|
|
|
* with this form element. All visible elements of the form must be |
|
42
|
|
|
* subclasses of the element class. |
|
43
|
|
|
*/ |
|
44
|
|
|
abstract class Element |
|
45
|
|
|
{ |
|
46
|
|
|
const SCOPE_ELEMENT = ""; |
|
47
|
|
|
const SCOPE_WRAPPER = "_wrapper"; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The id of the form useful for CSS styling and DOM access. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $id; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The label of the form element. |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $label; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The description of the form element. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $description; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* An array of all the CSS classes associated with this element. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $classes = array(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* An array of all HTML attributes. These attributes are stored as |
|
79
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
|
80
|
|
|
* directly to the form element. |
|
81
|
|
|
* |
|
82
|
|
|
* @var array |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $attributes = array(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* An array of all HTML attributes. These attributes are stored as |
|
88
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
|
89
|
|
|
* directly to the wrapper which wraps the form element. |
|
90
|
|
|
* |
|
91
|
|
|
* @var array |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $wrapperAttributes = array(); |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* An array of all error messages associated with this element. |
|
97
|
|
|
* Error messages are setup during validation, when any element |
|
98
|
|
|
* fails its validation test. |
|
99
|
|
|
* |
|
100
|
|
|
* @var array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $errors = array(); |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* A boolean value which is set to true whenever there is an error |
|
106
|
|
|
* assiciated with the field element in one way or the other. |
|
107
|
|
|
*/ |
|
108
|
|
|
protected $error; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* The parent element which contains this element. |
|
112
|
|
|
* @var Element |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $parent = null; |
|
115
|
|
|
|
|
116
|
|
|
protected $name; |
|
117
|
|
|
|
|
118
|
|
|
protected $renderLabel = true; |
|
119
|
|
|
|
|
120
|
|
|
protected $templateRenderer; |
|
121
|
|
|
|
|
122
|
9 |
|
public function __construct($label="", $description="", $id="") |
|
123
|
|
|
{ |
|
124
|
9 |
|
$this->setLabel($label); |
|
125
|
9 |
|
$this->setDescription($description); |
|
126
|
9 |
|
$this->setId($id); |
|
127
|
9 |
|
} |
|
128
|
|
|
|
|
129
|
10 |
|
public function setId($id) |
|
130
|
|
|
{ |
|
131
|
10 |
|
$this->id = str_replace(".","_",$id); |
|
132
|
10 |
|
$this->setAttribute('id', $this->id); |
|
133
|
10 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
9 |
|
public function getId() |
|
137
|
|
|
{ |
|
138
|
9 |
|
return $this->id; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Public accessor for setting the name property of the field. |
|
143
|
|
|
* |
|
144
|
|
|
* @param $name The name to assign to the form element. |
|
145
|
|
|
* @return Element |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function setName($name) |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->name = $name; |
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Public accessor for getting the name property of the field. |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
9 |
|
public function getName() |
|
159
|
|
|
{ |
|
160
|
9 |
|
return $this->name; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
//! Sets the label which is attached to this element. |
|
164
|
9 |
|
public function setLabel($label) |
|
165
|
|
|
{ |
|
166
|
9 |
|
$this->label = $label; |
|
167
|
9 |
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
//! Gets the label which is attached to this element. |
|
171
|
9 |
|
public function getLabel() |
|
172
|
|
|
{ |
|
173
|
9 |
|
return $this->label; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Gets the description which is attached to this element. The description |
|
178
|
|
|
* is normally displayed under the element when rendering HTML. |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
9 |
|
public function getDescription() |
|
183
|
|
|
{ |
|
184
|
9 |
|
return $this->description; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Sets the description which is attached to this element. The description |
|
189
|
|
|
* is normally displayed under the element when rendering HTML. |
|
190
|
|
|
* |
|
191
|
|
|
* @return Element |
|
192
|
|
|
*/ |
|
193
|
9 |
|
public function setDescription($description) |
|
194
|
|
|
{ |
|
195
|
9 |
|
$this->description = $description; |
|
196
|
9 |
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Returns all the arrays associated with this document. |
|
201
|
|
|
* |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function getErrors() |
|
205
|
|
|
{ |
|
206
|
1 |
|
return $this->errors; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Returns the error flag for this element. |
|
210
|
9 |
|
public function hasError() |
|
211
|
|
|
{ |
|
212
|
9 |
|
return $this->error; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function getType() |
|
216
|
|
|
{ |
|
217
|
|
|
return __CLASS__; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Renders the form element by outputing the HTML associated with |
|
222
|
|
|
* the element. This method is abstract and it is implemented by |
|
223
|
|
|
* all the other classes which inherit the Element class. |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
abstract public function render(); |
|
228
|
|
|
|
|
229
|
9 |
|
public function __toString() |
|
|
|
|
|
|
230
|
|
|
{ |
|
231
|
9 |
|
return $this->templateRenderer->render("inline_layout_element.tpl.php", array('element' => $this)); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
//! Returns an array of all the CSS classes associated with this |
|
235
|
|
|
//! element. |
|
236
|
9 |
|
public function getCSSClasses() |
|
237
|
|
|
{ |
|
238
|
9 |
|
return implode(" ", $this->classes); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
//! Adds a css class to this element. |
|
242
|
1 |
|
public function addCSSClass($class) |
|
243
|
|
|
{ |
|
244
|
1 |
|
$this->classes[] = $class; |
|
245
|
1 |
|
return $this; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
//! Adds an attribute to the list of attributes of this element. |
|
249
|
|
|
//! This method internally creates a new Attribute object and appends |
|
250
|
|
|
//! it to the list of attributes. |
|
251
|
|
|
//! \see Attribute |
|
252
|
11 |
|
public function setAttribute($attribute,$value,$scope = Element::SCOPE_ELEMENT) |
|
253
|
|
|
{ |
|
254
|
|
|
// Force the setting of the attribute. |
|
255
|
11 |
|
if($scope == Element::SCOPE_ELEMENT) |
|
256
|
|
|
{ |
|
257
|
11 |
|
$this->attributes[$attribute] = $value; |
|
258
|
|
|
} |
|
259
|
|
|
else if($scope == Element::SCOPE_WRAPPER) |
|
260
|
|
|
{ |
|
261
|
|
|
$this->wrapperAttributes[$attribute] = $value; |
|
262
|
|
|
} |
|
263
|
11 |
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Returns an HTML representation of all the attributes. This method is |
|
268
|
|
|
* normally called when rendering the HTML for the element. |
|
269
|
|
|
*/ |
|
270
|
11 |
|
public function getAttributes($scope=Element::SCOPE_ELEMENT) |
|
271
|
|
|
{ |
|
272
|
11 |
|
$attributes = array(); |
|
273
|
11 |
|
switch($scope) |
|
274
|
|
|
{ |
|
275
|
11 |
|
case Element::SCOPE_ELEMENT: $attributes = $this->attributes; break; |
|
276
|
9 |
|
case Element::SCOPE_WRAPPER: $attributes = $this->wrapperAttributes; break; |
|
277
|
|
|
} |
|
278
|
11 |
|
return $attributes; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
9 |
|
public function getAttribute($attribute, $scope = Element::SCOPE_ELEMENT) |
|
|
|
|
|
|
282
|
|
|
{ |
|
283
|
9 |
|
$attributes = $this->getAttributes($scope); |
|
284
|
9 |
|
return @$attributes[$attribute]; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
4 |
|
public function setErrors($errors) |
|
288
|
|
|
{ |
|
289
|
4 |
|
$this->errors = $errors; |
|
290
|
4 |
|
$this->error = true; |
|
291
|
4 |
|
} |
|
292
|
|
|
|
|
293
|
9 |
|
public function getRenderLabel() |
|
294
|
|
|
{ |
|
295
|
9 |
|
return $this->renderLabel; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
1 |
|
public function setRenderLabel($renderLabel) |
|
299
|
|
|
{ |
|
300
|
1 |
|
$this->renderLabel = $renderLabel; |
|
301
|
1 |
|
} |
|
302
|
|
|
|
|
303
|
11 |
|
public function setTemplateRenderer($templateRenderer) |
|
304
|
|
|
{ |
|
305
|
11 |
|
$this->templateRenderer = $templateRenderer; |
|
306
|
11 |
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.