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\helpers\form; |
33
|
|
|
|
34
|
|
|
use ntentan\honam\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
|
|
|
protected $formId; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The id of the form useful for CSS styling and DOM access. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $id; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The label of the form element. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $label; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The description of the form element. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $description; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* An array of all the CSS classes associated with this element. |
74
|
|
|
* |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
protected $classes = array(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* An array of all HTML attributes. These attributes are stored as |
81
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
82
|
|
|
* directly to the form element. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $attributes = array(); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* An array of all HTML attributes. These attributes are stored as |
90
|
|
|
* objects of the Attribute class. Attributes in this array are applied |
91
|
|
|
* directly to the wrapper which wraps the form element. |
92
|
|
|
* |
93
|
|
|
* @var array |
94
|
|
|
*/ |
95
|
|
|
protected $wrapperAttributes = array(); |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* An array of all error messages associated with this element. |
99
|
|
|
* Error messages are setup during validation, when any element |
100
|
|
|
* fails its validation test. |
101
|
|
|
* |
102
|
|
|
* @var array |
103
|
|
|
*/ |
104
|
|
|
protected $errors = array(); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* A boolean value which is set to true whenever there is an error |
108
|
|
|
* assiciated with the field element in one way or the other. |
109
|
|
|
*/ |
110
|
|
|
protected $error; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* The parent element which contains this element. |
114
|
|
|
* @var Element |
115
|
|
|
*/ |
116
|
|
|
protected $parent = null; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* The name of the form field. This is what is to be outputed as |
120
|
|
|
* the HTML name attribute of the field. If name encryption is |
121
|
|
|
* enabled the outputed name to HTML is mangled by the encryption |
122
|
|
|
* algorithm. However internally the Field may still be referenced |
123
|
|
|
* bu the unmangled name. |
124
|
|
|
*/ |
125
|
|
|
public $name; |
126
|
|
|
|
127
|
|
|
protected $renderLabel = true; |
128
|
|
|
|
129
|
14 |
|
public function __construct($label="", $description="", $id="") |
130
|
|
|
{ |
131
|
14 |
|
$this->setLabel($label); |
132
|
14 |
|
$this->setDescription($description); |
133
|
14 |
|
$this->setId($id); |
134
|
14 |
|
} |
135
|
|
|
|
136
|
14 |
|
public function setId($id) |
137
|
|
|
{ |
138
|
14 |
|
$this->id = str_replace(".","_",$id); |
139
|
14 |
|
$this->setAttribute('id', $this->id); |
140
|
14 |
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
9 |
|
public function getId() |
144
|
|
|
{ |
145
|
9 |
|
return $this->id; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Public accessor for setting the name property of the field. |
150
|
|
|
* |
151
|
|
|
* @param $name The name to assign to the form element. |
152
|
|
|
*/ |
153
|
1 |
|
public function setName($name) |
154
|
|
|
{ |
155
|
1 |
|
$this->name = $name; |
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Public accessor for getting the name property of the field. |
161
|
|
|
* |
162
|
|
|
* @return The name of the form field. |
|
|
|
|
163
|
|
|
*/ |
164
|
9 |
|
public function getName() |
165
|
|
|
{ |
166
|
9 |
|
return $this->name; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
//! Sets the label which is attached to this element. |
170
|
14 |
|
public function setLabel($label) |
171
|
|
|
{ |
172
|
14 |
|
$this->label = $label; |
173
|
14 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
//! Gets the label which is attached to this element. |
177
|
9 |
|
public function getLabel() |
178
|
|
|
{ |
179
|
9 |
|
return $this->label; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Gets the description which is attached to this element. The description |
184
|
|
|
* is normally displayed under the element when rendering HTML. |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
9 |
|
public function getDescription() |
189
|
|
|
{ |
190
|
9 |
|
return $this->description; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sets the description which is attached to this element. The description |
195
|
|
|
* is normally displayed under the element when rendering HTML. |
196
|
|
|
* |
197
|
|
|
* @return string |
|
|
|
|
198
|
|
|
*/ |
199
|
14 |
|
public function setDescription($description) |
200
|
|
|
{ |
201
|
14 |
|
$this->description = $description; |
202
|
14 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns all the arrays associated with this document. |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
1 |
|
public function getErrors() |
211
|
|
|
{ |
212
|
1 |
|
return $this->errors; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// Returns the error flag for this element. |
216
|
9 |
|
public function hasError() |
217
|
|
|
{ |
218
|
9 |
|
return $this->error; |
219
|
|
|
} |
220
|
|
|
|
221
|
1 |
|
public function getType() |
222
|
|
|
{ |
223
|
1 |
|
return __CLASS__; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Renders the form element by outputing the HTML associated with |
228
|
|
|
* the element. This method is abstract and it is implemented by |
229
|
|
|
* all the other classes which inherit the Element class. |
230
|
|
|
*/ |
231
|
|
|
abstract public function render(); |
|
|
|
|
232
|
|
|
|
233
|
9 |
|
public function __toString() |
234
|
|
|
{ |
235
|
9 |
|
return TemplateEngine::render("inline_layout_element.tpl.php", array('element' => $this)); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
//! Returns an array of all the CSS classes associated with this |
239
|
|
|
//! element. |
240
|
9 |
|
public function getCSSClasses() |
241
|
|
|
{ |
242
|
9 |
|
return implode(" ", $this->classes); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
//! Adds a css class to this element. |
246
|
1 |
|
public function addCSSClass($class) |
247
|
|
|
{ |
248
|
1 |
|
$this->classes[] = $class; |
249
|
1 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
//! Adds an attribute to the list of attributes of this element. |
253
|
|
|
//! This method internally creates a new Attribute object and appends |
254
|
|
|
//! it to the list of attributes. |
255
|
|
|
//! \see Attribute |
256
|
14 |
|
public function setAttribute($attribute,$value,$scope = Element::SCOPE_ELEMENT) |
257
|
|
|
{ |
258
|
|
|
// Force the setting of the attribute. |
259
|
14 |
|
if($scope == Element::SCOPE_ELEMENT) |
260
|
|
|
{ |
261
|
14 |
|
$this->attributes[$attribute] = $value; |
262
|
|
|
} |
263
|
|
|
else if($scope == Element::SCOPE_WRAPPER) |
264
|
|
|
{ |
265
|
|
|
$this->wrapperAttributes[$attribute] = $value; |
266
|
|
|
} |
267
|
14 |
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns an HTML representation of all the attributes. This method is |
272
|
|
|
* normally called when rendering the HTML for the element. |
273
|
|
|
*/ |
274
|
12 |
|
public function getAttributes($scope=Element::SCOPE_ELEMENT) |
275
|
|
|
{ |
276
|
12 |
|
$attributes = array(); |
277
|
|
|
switch($scope) |
278
|
|
|
{ |
279
|
12 |
|
case Element::SCOPE_ELEMENT: $attributes = $this->attributes; break; |
|
|
|
|
280
|
9 |
|
case Element::SCOPE_WRAPPER: $attributes = $this->wrapperAttributes; break; |
|
|
|
|
281
|
|
|
} |
282
|
12 |
|
return $attributes; |
283
|
|
|
} |
284
|
|
|
|
285
|
9 |
|
public function getAttribute($attribute, $scope = Element::SCOPE_ELEMENT) |
|
|
|
|
286
|
|
|
{ |
287
|
9 |
|
$attributes = $this->getAttributes($scope); |
288
|
9 |
|
return @$attributes[$attribute]; |
289
|
|
|
} |
290
|
|
|
|
291
|
4 |
|
public function setErrors($errors) |
292
|
|
|
{ |
293
|
4 |
|
$this->errors = $errors; |
294
|
4 |
|
$this->error = true; |
295
|
4 |
|
} |
296
|
|
|
|
297
|
9 |
|
public function getRenderLabel() |
298
|
|
|
{ |
299
|
9 |
|
return $this->renderLabel; |
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
public function setRenderLabel($renderLabel) |
303
|
|
|
{ |
304
|
1 |
|
$this->renderLabel = $renderLabel; |
305
|
1 |
|
} |
306
|
|
|
|
307
|
9 |
|
public function isContainer() |
308
|
|
|
{ |
309
|
9 |
|
return false; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.