1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\html; |
4
|
|
|
|
5
|
|
|
use Ajax\JsUtils; |
6
|
|
|
use Phalcon\Mvc\View; |
7
|
|
|
use Ajax\service\AjaxCall; |
8
|
|
|
use Ajax\service\PhalconUtils; |
9
|
|
|
use Ajax\service\JString; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* BaseHtml for Twitter Bootstrap HTML components |
13
|
|
|
* @author jc |
14
|
|
|
* @version 1.001 |
15
|
|
|
*/ |
16
|
|
|
abstract class BaseHtml extends BaseWidget { |
17
|
|
|
protected $_template; |
18
|
|
|
protected $tagName; |
19
|
|
|
protected $properties=array (); |
20
|
|
|
protected $events=array (); |
21
|
|
|
protected $wrapBefore=""; |
22
|
|
|
protected $wrapAfter=""; |
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() { |
35
|
|
|
return $this->wrapBefore.$this->_template.$this->wrapAfter; |
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
|
|
|
$v=@$this->properties[$name]; |
59
|
|
|
if (isset($v)&&$v!=="") |
60
|
|
|
$v=$v.$separator.$value; |
61
|
|
|
else |
62
|
|
|
$v=$value; |
63
|
|
|
|
64
|
|
|
return $this->setProperty($name, $v); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function addProperties($properties) { |
68
|
|
|
$this->properties=array_merge($this->properties, $properties); |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function compile(JsUtils $js=NULL, View $view=NULL) { |
73
|
|
|
$result=$this->getTemplate(); |
74
|
|
|
foreach ( $this as $key => $value ) { |
|
|
|
|
75
|
|
|
if (PhalconUtils::startsWith($key, "_")===false&&$key!=="events") { |
76
|
|
|
if (is_array($value)) { |
77
|
|
|
$v=PropertyWrapper::wrap($value, $js); |
78
|
|
|
} else { |
79
|
|
|
$v=$value; |
80
|
|
|
} |
81
|
|
|
$result=str_ireplace("%".$key."%", $v, $result); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if (isset($js)) { |
85
|
|
|
$this->run($js); |
86
|
|
|
} |
87
|
|
|
if (isset($view)===true) { |
88
|
|
|
$controls=$view->getVar("q"); |
89
|
|
|
if (isset($controls)===false) { |
90
|
|
|
$controls=array (); |
91
|
|
|
} |
92
|
|
|
$controls [$this->identifier]=$result; |
93
|
|
|
$view->setVar("q", $controls); |
94
|
|
|
} |
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function ctrl($name, $value, $typeCtrl) { |
99
|
|
|
if (is_array($typeCtrl)) { |
100
|
|
|
if (array_search($value, $typeCtrl)===false) { |
101
|
|
|
throw new \Exception("La valeur passée a propriété `".$name."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}"); |
102
|
|
|
} |
103
|
|
|
} else { |
104
|
|
|
if (!$typeCtrl($value)) { |
105
|
|
|
throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$name); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function propertyContains($propertyName,$value){ |
112
|
|
|
$values=$this->getProperty($propertyName); |
113
|
|
|
if(isset($values)){ |
114
|
|
|
return JString::contains($values, $value); |
115
|
|
|
} |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function setPropertyCtrl($name, $value, $typeCtrl) { |
120
|
|
|
if ($this->ctrl($name, $value, $typeCtrl)===true) |
121
|
|
|
return $this->setProperty($name, $value); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
126
|
|
|
if ($this->ctrl($name, $value, $typeCtrl)===true) { |
127
|
|
|
return $name=$value; |
128
|
|
|
} |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
133
|
|
|
if (is_array($typeCtrl)) { |
134
|
|
|
$this->removeOldValues($name, $typeCtrl); |
135
|
|
|
$name.=$separator.$value; |
136
|
|
|
} |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
141
|
|
|
if ($this->ctrl($name, $value, $typeCtrl)===true) { |
142
|
|
|
if (is_array($typeCtrl)) { |
143
|
|
|
$this->removeOldValues($name, $typeCtrl); |
144
|
|
|
} |
145
|
|
|
$name.=$separator.$value; |
146
|
|
|
} |
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function addToMember(&$name, $value, $separator=" ") { |
151
|
|
|
$name=str_ireplace($value, "", $name).$separator.$value; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function addToPropertyUnique($name, $value, $typeCtrl) { |
156
|
|
|
if (@class_exists($typeCtrl, true)) |
157
|
|
|
$typeCtrl=$typeCtrl::getConstants(); |
158
|
|
|
if (is_array($typeCtrl)) { |
159
|
|
|
$this->removeOldValues($this->properties [$name], $typeCtrl); |
160
|
|
|
} |
161
|
|
|
return $this->addToProperty($name, $value); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function addToPropertyCtrl($name, $value, $typeCtrl) { |
165
|
|
|
// if($this->ctrl($name, $value, $typeCtrl)===true){ |
|
|
|
|
166
|
|
|
return $this->addToPropertyUnique($name, $value, $typeCtrl); |
167
|
|
|
// } |
168
|
|
|
//return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function removeOldValues(&$oldValue, $allValues) { |
172
|
|
|
$oldValue=str_ireplace($allValues, "", $oldValue); |
173
|
|
|
$oldValue=trim($oldValue); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
abstract public function run(JsUtils $js); |
177
|
|
|
|
178
|
|
|
public function getTagName() { |
179
|
|
|
return $this->tagName; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function setTagName($tagName) { |
183
|
|
|
$this->tagName=$tagName; |
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function fromArray($array) { |
188
|
|
|
foreach ( $this as $key => $value ) { |
|
|
|
|
189
|
|
|
if (array_key_exists($key, $array) && !PhalconUtils::startsWith($key, "_")) { |
190
|
|
|
$setter="set".ucfirst($key); |
191
|
|
|
$this->$setter($array [$key]); |
192
|
|
|
unset($array [$key]); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
foreach ( $array as $key => $value ) { |
196
|
|
|
if (method_exists($this, $key)) { |
197
|
|
|
try { |
198
|
|
|
$this->$key($value); |
199
|
|
|
unset($array [$key]); |
200
|
|
|
} catch ( \Exception $e ) { |
201
|
|
|
// Nothing to do |
202
|
|
|
} |
203
|
|
|
} else { |
204
|
|
|
$setter="set".ucfirst($key); |
205
|
|
|
if (method_exists($this, $setter)) { |
206
|
|
|
try { |
207
|
|
|
$this->$setter($value); |
208
|
|
|
unset($array [$key]); |
209
|
|
|
} catch ( \Exception $e ) { |
210
|
|
|
// Nothing to do |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
return $array; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function fromDatabaseObjects($objects,$function) { |
219
|
|
|
if(isset($objects)){ |
220
|
|
|
foreach ($objects as $object){ |
221
|
|
|
$this->fromDatabaseObject($object,$function); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function fromDatabaseObject($object,$function){ |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function wrap($before, $after="") { |
232
|
|
|
if(isset($before)){ |
233
|
|
|
$this->wrapBefore.=$before; |
234
|
|
|
} |
235
|
|
|
$this->wrapAfter=$after.$this->wrapAfter; |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
240
|
|
|
if ($stopPropagation===true) { |
241
|
|
|
$jsCode="event.stopPropagation();".$jsCode; |
242
|
|
|
} |
243
|
|
|
if ($preventDefault===true) { |
244
|
|
|
$jsCode="event.preventDefault();".$jsCode; |
245
|
|
|
} |
246
|
|
|
$this->_addEvent($event, $jsCode); |
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function _addEvent($event, $jsCode) { |
251
|
|
|
if (array_key_exists($event, $this->events)) { |
252
|
|
|
if (is_array($this->events [$event])) { |
253
|
|
|
$this->events [$event] []=$jsCode; |
254
|
|
|
} else { |
255
|
|
|
$this->events [$event]=array ( |
256
|
|
|
$this->events [$event], |
257
|
|
|
$jsCode |
258
|
|
|
); |
259
|
|
|
} |
260
|
|
|
} else { |
261
|
|
|
$this->events [$event]=$jsCode; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) { |
266
|
|
|
return $this->addEvent($event, $jsCode, $stopPropagation, $preventDefault); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function onClick($jsCode, $stopPropagation=false, $preventDefault=false) { |
270
|
|
|
return $this->on("click", $jsCode, $stopPropagation, $preventDefault); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function setClick($jsCode) { |
274
|
|
|
return $this->onClick($jsCode); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
public function addEventsOnRun(JsUtils $js) { |
278
|
|
|
if (isset($this->_bsComponent)) { |
279
|
|
|
foreach ( $this->events as $event => $jsCode ) { |
280
|
|
|
$code=$jsCode; |
281
|
|
|
if (is_array($jsCode)) { |
282
|
|
|
$code=""; |
283
|
|
|
foreach ( $jsCode as $jsC ) { |
284
|
|
|
if ($jsC instanceof AjaxCall) { |
285
|
|
|
$code.="\n".$jsC->compile($js); |
286
|
|
|
} else { |
287
|
|
|
$code.="\n".$jsC; |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
} elseif ($jsCode instanceof AjaxCall) { |
291
|
|
|
$code=$jsCode->compile($js); |
292
|
|
|
} |
293
|
|
|
$this->_bsComponent->addEvent($event, $code); |
294
|
|
|
} |
295
|
|
|
$this->events=array(); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) { |
300
|
|
|
$params=array ( |
301
|
|
|
"url" => $url, |
302
|
|
|
"responseElement" => $responseElement |
303
|
|
|
); |
304
|
|
|
$params=array_merge($params, $parameters); |
305
|
|
|
$this->_addEvent($event, new AjaxCall($operation, $params)); |
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function getOn($event, $url, $responseElement="", $parameters=array()) { |
310
|
|
|
return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function getOnClick($url, $responseElement="", $parameters=array()) { |
314
|
|
|
return $this->getOn("click", $url, $responseElement, $parameters); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) { |
318
|
|
|
$parameters ["params"]=$params; |
319
|
|
|
return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) { |
323
|
|
|
return $this->postOn("click", $url, $params, $responseElement, $parameters); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) { |
327
|
|
|
$parameters ["form"]=$form; |
328
|
|
|
return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) { |
332
|
|
|
return $this->postFormOn("click", $url, $form, $responseElement, $parameters); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
public function getElementById($identifier, $elements) { |
336
|
|
|
if (is_array($elements)) { |
337
|
|
|
$flag=false; |
338
|
|
|
$index=0; |
339
|
|
|
while ( !$flag&&$index<sizeof($elements) ) { |
340
|
|
|
if ($elements [$index] instanceof BaseHtml) |
341
|
|
|
$flag=($elements [$index]->getIdentifier()===$identifier); |
342
|
|
|
$index++; |
343
|
|
|
} |
344
|
|
|
if ($flag===true) |
345
|
|
|
return $elements [$index-1]; |
346
|
|
|
} elseif ($elements instanceof BaseHtml) { |
347
|
|
|
if ($elements->getIdentifier()===$identifier) |
348
|
|
|
return $elements; |
349
|
|
|
} |
350
|
|
|
return null; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function __toString(){ |
354
|
|
|
return $this->compile(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Puts HTML values in quotes for use in jQuery code |
359
|
|
|
* unless the supplied value contains the Javascript 'this' or 'event' |
360
|
|
|
* object, in which case no quotes are added |
361
|
|
|
* |
362
|
|
|
* @param string $value |
363
|
|
|
* @return string |
364
|
|
|
*/ |
365
|
|
|
public function _prep_value($value) { |
366
|
|
|
if (is_array($value)) { |
367
|
|
|
$value=implode(",", $value); |
368
|
|
|
} |
369
|
|
|
if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false) { |
370
|
|
|
$value='"'.$value.'"'; |
371
|
|
|
} |
372
|
|
|
return $value; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
public function jsDoJquery($jqueryCall, $param=""){ |
376
|
|
|
return "$('#".$this->identifier."').".$jqueryCall."(".$this->_prep_value($param).");"; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function jsHtml($content=""){ |
380
|
|
|
return $this->jsDoJquery("html",$content); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function jsShow(){ |
384
|
|
|
return $this->jsDoJquery("show"); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
public function jsHide(){ |
388
|
|
|
return $this->jsDoJquery("hide"); |
389
|
|
|
} |
390
|
|
|
} |