Completed
Push — master ( 141d55...411df4 )
by Jean-Christophe
02:55
created

BaseHtml::jsHtml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\service\AjaxCall;
7
use Ajax\service\JString;
8
use Ajax\common\components\SimpleExtComponent;
9
use Ajax\JsUtils;
10
11
/**
12
 * BaseHtml for 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=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 ) {
1 ignored issue
show
Bug introduced by
The expression $this of type this<Ajax\common\html\BaseHtml> is not traversable.
Loading history...
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)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
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 ) {
1 ignored issue
show
Bug introduced by
The expression $this of type this<Ajax\common\html\BaseHtml> is not traversable.
Loading history...
216
			if (array_key_exists($key, $array) && !JString::startswith($key, "_")) {
217
				$setter="set" . ucfirst($key);
218
				$this->$setter($array[$key]);
219
				unset($array[$key]);
220
			}
221
		}
222
		foreach ( $array as $key => $value ) {
223
			if (method_exists($this, $key)) {
224
				try {
225
					$this->$key($value);
226
					unset($array[$key]);
227
				} catch ( \Exception $e ) {
228
					// Nothing to do
229
				}
230
			} else {
231
				$setter="set" . ucfirst($key);
232
				if (method_exists($this, $setter)) {
233
					try {
234
						$this->$setter($value);
235
						unset($array[$key]);
236
					} catch ( \Exception $e ) {
237
						// Nothing to do
238
					}
239
				}
240
			}
241
		}
242
		return $array;
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
			// $this->_wrapBefore[]=$before;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
261
		}
262
		$this->_wrapAfter[]=$after;
263
		return $this;
264
	}
265
266
	public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
267
		if ($stopPropagation === true) {
268
			$jsCode="event.stopPropagation();" . $jsCode;
269
		}
270
		if ($preventDefault === true) {
271
			$jsCode="event.preventDefault();" . $jsCode;
272
		}
273
		return $this->_addEvent($event, $jsCode);
274
	}
275
276
	public function _addEvent($event, $jsCode) {
277
		if (array_key_exists($event, $this->_events)) {
278
			if (is_array($this->_events[$event])) {
279
				$this->_events[$event][]=$jsCode;
280
			} else {
281
				$this->_events[$event]=array ($this->_events[$event],$jsCode );
282
			}
283
		} else {
284
			$this->_events[$event]=$jsCode;
285
		}
286
		return $this;
287
	}
288
289
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
290
		return $this->addEvent($event, $jsCode, $stopPropagation, $preventDefault);
291
	}
292
293
	public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) {
294
		return $this->on("click", $jsCode, $stopPropagation, $preventDefault);
295
	}
296
297
	public function setClick($jsCode) {
298
		return $this->onClick($jsCode);
299
	}
300
301
	public function onCreate($jsCode){
302
		if(isset($this->_events["_create"])){
303
			$this->_events["_create"][]=$jsCode;
304
		}else{
305
			$this->_events["_create"]=[$jsCode];
306
		}
307
		return $this;
308
	}
309
310
	public function addEventsOnRun(JsUtils $js) {
311
		if(isset($this->_events["_create"])){
312
			$create=$this->_events["_create"];
313
			if(\is_array($create)){
314
				$create=\implode("", $create);
315
			}
316
			if($create!=="")
317
				$js->exec($create,true);
318
			unset($this->_events["_create"]);
319
		}
320
		if (isset($this->_bsComponent)) {
321
			foreach ( $this->_events as $event => $jsCode ) {
322
				$code=$jsCode;
323
				if (is_array($jsCode)) {
324
					$code="";
325
					foreach ( $jsCode as $jsC ) {
326
						if ($jsC instanceof AjaxCall) {
327
							$code.="\n" . $jsC->compile($js);
328
						} else {
329
							$code.="\n" . $jsC;
330
						}
331
					}
332
				} elseif ($jsCode instanceof AjaxCall) {
333
					$code=$jsCode->compile($js);
334
				}
335
				$this->_bsComponent->addEvent($event, $code);
336
			}
337
			$this->_events=array ();
338
		}
339
	}
340
341
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
342
		$params=array ("url" => $url,"responseElement" => $responseElement );
343
		$params=array_merge($params, $parameters);
344
		$this->_addEvent($event, new AjaxCall($operation, $params));
345
		return $this;
346
	}
347
348
	public function getOn($event, $url, $responseElement="", $parameters=array()) {
349
		return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
350
	}
351
352
	public function getOnClick($url, $responseElement="", $parameters=array()) {
353
		return $this->getOn("click", $url, $responseElement, $parameters);
354
	}
355
356
	public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
357
		$parameters["params"]=$params;
358
		return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
359
	}
360
361
	public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
362
		return $this->postOn("click", $url, $params, $responseElement, $parameters);
363
	}
364
365
	public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
366
		$parameters["form"]=$form;
367
		return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
368
	}
369
370
	public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
371
		return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
372
	}
373
374
	public function getElementById($identifier, $elements) {
375
		if (is_array($elements)) {
376
			$flag=false;
377
			$index=0;
378 View Code Duplication
			while ( !$flag && $index < sizeof($elements) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
379
				if ($elements[$index] instanceof BaseHtml)
380
					$flag=($elements[$index]->getIdentifier() === $identifier);
381
				$index++;
382
			}
383
			if ($flag === true)
384
				return $elements[$index - 1];
385
		} elseif ($elements instanceof BaseHtml) {
386
			if ($elements->getIdentifier() === $identifier)
387
				return $elements;
388
		}
389
		return null;
390
	}
391
392
	protected function getElementByPropertyValue($propertyName,$value, $elements) {
393
		if (is_array($elements)) {
394
			$flag=false;
395
			$index=0;
396 View Code Duplication
			while ( !$flag && $index < sizeof($elements) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
397
				if ($elements[$index] instanceof BaseHtml)
398
					$flag=($elements[$index]->propertyContains($propertyName, $value) === true);
399
					$index++;
400
			}
401
			if ($flag === true)
402
				return $elements[$index - 1];
403
		} elseif ($elements instanceof BaseHtml) {
404
			if ($elements->propertyContains($propertyName, $value) === true)
405
				return $elements;
406
		}
407
		return null;
408
	}
409
410
	public function __toString() {
411
		return $this->compile();
412
	}
413
414
	/**
415
	 * Puts HTML values in quotes for use in jQuery code
416
	 * unless the supplied value contains the Javascript 'this' or 'event'
417
	 * object, in which case no quotes are added
418
	 *
419
	 * @param string $value
420
	 * @return string
421
	 */
422
	public function _prep_value($value) {
423
		if (is_array($value)) {
424
			$value=implode(",", $value);
425
		}
426
		if (strrpos($value, 'this') === false && strrpos($value, 'event') === false) {
427
			$value='"' . $value . '"';
428
		}
429
		return $value;
430
	}
431
432
	public function jsDoJquery($jqueryCall, $param="") {
433
		return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . $this->_prep_value($param) . ");";
434
	}
435
436
	public function executeOnRun($jsCode) {
437
		return $this->_addEvent("execute", $jsCode);
438
	}
439
440
	public function jsHtml($content="") {
441
		return $this->jsDoJquery("html", $content);
442
	}
443
444
	public function jsShow() {
445
		return $this->jsDoJquery("show");
446
	}
447
448
	public function jsHide() {
449
		return $this->jsDoJquery("hide");
450
	}
451
452
	protected function setWrapBefore($wrapBefore) {
453
		$this->_wrapBefore=$wrapBefore;
454
		return $this;
455
	}
456
457
	protected function setWrapAfter($wrapAfter) {
458
		$this->_wrapAfter=$wrapAfter;
459
		return $this;
460
	}
461
}