|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ajax\common\html; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Ajax\service\JString; |
|
7
|
|
|
use Ajax\common\components\SimpleExtComponent; |
|
8
|
|
|
use Ajax\JsUtils; |
|
9
|
|
|
use Ajax\common\html\traits\BaseHtmlEventsTrait; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* BaseHtml for HTML components |
|
13
|
|
|
* @author jc |
|
14
|
|
|
* @version 1.2 |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class BaseHtml extends BaseWidget { |
|
17
|
|
|
use BaseHtmlEventsTrait; |
|
18
|
|
|
protected $_template; |
|
19
|
|
|
protected $tagName; |
|
20
|
|
|
protected $properties=array (); |
|
21
|
|
|
protected $_wrapBefore=array (); |
|
22
|
|
|
protected $_wrapAfter=array (); |
|
23
|
|
|
protected $_bsComponent; |
|
24
|
|
|
|
|
25
|
|
|
public function getBsComponent() { |
|
26
|
|
|
return $this->_bsComponent; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setBsComponent($bsComponent) { |
|
30
|
|
|
$this->_bsComponent=$bsComponent; |
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getTemplate(JsUtils $js=NULL) { |
|
35
|
|
|
return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getProperties() { |
|
39
|
|
|
return $this->properties; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function setProperties($properties) { |
|
43
|
|
|
$this->properties=$properties; |
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function setProperty($name, $value) { |
|
48
|
|
|
$this->properties[$name]=$value; |
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getProperty($name) { |
|
53
|
|
|
if (array_key_exists($name, $this->properties)) |
|
54
|
|
|
return $this->properties[$name]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function addToProperty($name, $value, $separator=" ") { |
|
58
|
|
|
if (\is_array($value)) { |
|
59
|
|
|
foreach ( $value as $v ) { |
|
60
|
|
|
$this->addToProperty($name, $v, $separator); |
|
61
|
|
|
} |
|
62
|
|
|
} else if ($value !== "" && $this->propertyContains($name, $value) === false) { |
|
63
|
|
|
$v=@$this->properties[$name]; |
|
64
|
|
|
if (isset($v) && $v !== "") |
|
65
|
|
|
$v=$v . $separator . $value; |
|
66
|
|
|
else |
|
67
|
|
|
$v=$value; |
|
68
|
|
|
|
|
69
|
|
|
return $this->setProperty($name, $v); |
|
70
|
|
|
} |
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function addProperties($properties) { |
|
75
|
|
|
$this->properties=array_merge($this->properties, $properties); |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function compile(JsUtils $js=NULL, &$view=NULL) { |
|
80
|
|
|
$result=$this->getTemplate($js); |
|
81
|
|
|
foreach ( $this as $key => $value ) { |
|
|
|
|
|
|
82
|
|
|
if (JString::startswith($key, "_") === false && $key !== "events") { |
|
83
|
|
|
if (is_array($value)) { |
|
84
|
|
|
$v=PropertyWrapper::wrap($value, $js); |
|
85
|
|
|
} else { |
|
86
|
|
|
$v=$value; |
|
87
|
|
|
} |
|
88
|
|
|
$result=str_ireplace("%" . $key . "%", $v, $result); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
if (isset($js)===true) { |
|
92
|
|
|
$this->run($js); |
|
93
|
|
|
if (isset($view) === true) { |
|
94
|
|
|
$js->addViewElement($this->identifier, $result, $view); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function ctrl($name, $value, $typeCtrl) { |
|
101
|
|
View Code Duplication |
if (is_array($typeCtrl)) { |
|
|
|
|
|
|
102
|
|
|
if (array_search($value, $typeCtrl) === false) { |
|
103
|
|
|
throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}"); |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
if (!$typeCtrl($value)) { |
|
107
|
|
|
throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function propertyContains($propertyName, $value) { |
|
114
|
|
|
$values=$this->getProperty($propertyName); |
|
115
|
|
|
if (isset($values)) { |
|
116
|
|
|
return JString::contains($values, $value); |
|
117
|
|
|
} |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
protected function setPropertyCtrl($name, $value, $typeCtrl) { |
|
122
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) |
|
123
|
|
|
return $this->setProperty($name, $value); |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
|
128
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
|
129
|
|
|
return $name=$value; |
|
130
|
|
|
} |
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
|
135
|
|
|
if (is_array($typeCtrl)) { |
|
136
|
|
|
$this->removeOldValues($name, $typeCtrl); |
|
137
|
|
|
$name.=$separator . $value; |
|
138
|
|
|
} |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function removePropertyValue($name, $value) { |
|
143
|
|
|
$this->properties[$name]=\str_replace($value, "", $this->properties[$name]); |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
protected function removePropertyValues($name, $values) { |
|
148
|
|
|
$this->removeOldValues($this->properties[$name], $values); |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function removeProperty($name) { |
|
153
|
|
|
if (\array_key_exists($name, $this->properties)) |
|
154
|
|
|
unset($this->properties[$name]); |
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
|
159
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
|
160
|
|
|
if (is_array($typeCtrl)) { |
|
161
|
|
|
$this->removeOldValues($name, $typeCtrl); |
|
162
|
|
|
} |
|
163
|
|
|
$name.=$separator . $value; |
|
164
|
|
|
} |
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
protected function addToMember(&$name, $value, $separator=" ") { |
|
169
|
|
|
$name=str_ireplace($value, "", $name) . $separator . $value; |
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
protected function addToPropertyUnique($name, $value, $typeCtrl) { |
|
174
|
|
|
if (@class_exists($typeCtrl, true)) |
|
175
|
|
|
$typeCtrl=$typeCtrl::getConstants(); |
|
176
|
|
|
if (is_array($typeCtrl)) { |
|
177
|
|
|
$this->removeOldValues($this->properties[$name], $typeCtrl); |
|
178
|
|
|
} |
|
179
|
|
|
return $this->addToProperty($name, $value); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function addToPropertyCtrl($name, $value, $typeCtrl) { |
|
183
|
|
|
return $this->addToPropertyUnique($name, $value, $typeCtrl); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function addToPropertyCtrlCheck($name, $value, $typeCtrl) { |
|
187
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
|
188
|
|
|
return $this->addToProperty($name, $value); |
|
189
|
|
|
} |
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
protected function removeOldValues(&$oldValue, $allValues) { |
|
194
|
|
|
$oldValue=str_ireplace($allValues, "", $oldValue); |
|
195
|
|
|
$oldValue=trim($oldValue); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* |
|
200
|
|
|
* @param JsUtils $js |
|
201
|
|
|
* @return SimpleExtComponent |
|
202
|
|
|
*/ |
|
203
|
|
|
public abstract function run(JsUtils $js); |
|
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
public function getTagName() { |
|
206
|
|
|
return $this->tagName; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function setTagName($tagName) { |
|
210
|
|
|
$this->tagName=$tagName; |
|
211
|
|
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function fromArray($array) { |
|
215
|
|
|
foreach ( $this as $key => $value ) { |
|
|
|
|
|
|
216
|
|
|
$this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
|
217
|
|
|
if (array_key_exists($key, $array) && !JString::startswith($key, "_")) { |
|
218
|
|
|
$setter="set" . ucfirst($key); |
|
219
|
|
|
$this->$setter($array[$key]); |
|
220
|
|
|
unset($array[$key]); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
foreach ( $array as $key => $value ) { |
|
224
|
|
|
if($this->_callSetter($key, $key, $value, $array)===false){ |
|
225
|
|
|
$this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
return $array; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
private function _callSetter($setter,$key,$value,&$array){ |
|
232
|
|
|
$result=false; |
|
233
|
|
|
if (method_exists($this, $setter) && !JString::startswith($key, "_")) { |
|
234
|
|
|
try { |
|
235
|
|
|
$this->$setter($value); |
|
236
|
|
|
unset($array[$key]); |
|
237
|
|
|
$result=true; |
|
238
|
|
|
} catch ( \Exception $e ) { |
|
239
|
|
|
$result=false; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
return $result; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function fromDatabaseObjects($objects, $function) { |
|
246
|
|
|
if (isset($objects)) { |
|
247
|
|
|
foreach ( $objects as $object ) { |
|
248
|
|
|
$this->fromDatabaseObject($object, $function); |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
return $this; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function fromDatabaseObject($object, $function) { |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function wrap($before, $after="") { |
|
258
|
|
|
if (isset($before)) { |
|
259
|
|
|
array_unshift($this->_wrapBefore, $before); |
|
260
|
|
|
} |
|
261
|
|
|
$this->_wrapAfter[]=$after; |
|
262
|
|
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
public function getElementById($identifier, $elements) { |
|
268
|
|
|
if (is_array($elements)) { |
|
269
|
|
|
$flag=false; |
|
270
|
|
|
$index=0; |
|
271
|
|
View Code Duplication |
while ( !$flag && $index < sizeof($elements) ) { |
|
|
|
|
|
|
272
|
|
|
if ($elements[$index] instanceof BaseHtml) |
|
273
|
|
|
$flag=($elements[$index]->getIdentifier() === $identifier); |
|
274
|
|
|
$index++; |
|
275
|
|
|
} |
|
276
|
|
|
if ($flag === true) |
|
277
|
|
|
return $elements[$index - 1]; |
|
278
|
|
|
} elseif ($elements instanceof BaseHtml) { |
|
279
|
|
|
if ($elements->getIdentifier() === $identifier) |
|
280
|
|
|
return $elements; |
|
281
|
|
|
} |
|
282
|
|
|
return null; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
protected function getElementByPropertyValue($propertyName,$value, $elements) { |
|
286
|
|
|
if (is_array($elements)) { |
|
287
|
|
|
$flag=false; |
|
288
|
|
|
$index=0; |
|
289
|
|
View Code Duplication |
while ( !$flag && $index < sizeof($elements) ) { |
|
|
|
|
|
|
290
|
|
|
if ($elements[$index] instanceof BaseHtml) |
|
291
|
|
|
$flag=($elements[$index]->propertyContains($propertyName, $value) === true); |
|
292
|
|
|
$index++; |
|
293
|
|
|
} |
|
294
|
|
|
if ($flag === true) |
|
295
|
|
|
return $elements[$index - 1]; |
|
296
|
|
|
} elseif ($elements instanceof BaseHtml) { |
|
297
|
|
|
if ($elements->propertyContains($propertyName, $value) === true) |
|
298
|
|
|
return $elements; |
|
299
|
|
|
} |
|
300
|
|
|
return null; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function __toString() { |
|
304
|
|
|
return $this->compile(); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
protected function setWrapBefore($wrapBefore) { |
|
308
|
|
|
$this->_wrapBefore=$wrapBefore; |
|
309
|
|
|
return $this; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
protected function setWrapAfter($wrapAfter) { |
|
313
|
|
|
$this->_wrapAfter=$wrapAfter; |
|
314
|
|
|
return $this; |
|
315
|
|
|
} |
|
316
|
|
|
} |