Completed
Push — master ( 73e21a...085edf )
by Jean-Christophe
04:37
created

BaseHtml::compile()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
rs 8.2222
cc 7
eloc 14
nc 12
nop 2
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 ) {
0 ignored issues
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
	protected 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 ) {
0 ignored issues
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 addEventsOnRun(JsUtils $js) {
302
		if (isset($this->_bsComponent)) {
303
			foreach ( $this->_events as $event => $jsCode ) {
304
				$code=$jsCode;
305
				if (is_array($jsCode)) {
306
					$code="";
307
					foreach ( $jsCode as $jsC ) {
308
						if ($jsC instanceof AjaxCall) {
309
							$code.="\n" . $jsC->compile($js);
310
						} else {
311
							$code.="\n" . $jsC;
312
						}
313
					}
314
				} elseif ($jsCode instanceof AjaxCall) {
315
					$code=$jsCode->compile($js);
316
				}
317
				$this->_bsComponent->addEvent($event, $code);
318
			}
319
			$this->_events=array ();
320
		}
321
	}
322
323
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
324
		$params=array ("url" => $url,"responseElement" => $responseElement );
325
		$params=array_merge($params, $parameters);
326
		$this->_addEvent($event, new AjaxCall($operation, $params));
327
		return $this;
328
	}
329
330
	public function getOn($event, $url, $responseElement="", $parameters=array()) {
331
		return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
332
	}
333
334
	public function getOnClick($url, $responseElement="", $parameters=array()) {
335
		return $this->getOn("click", $url, $responseElement, $parameters);
336
	}
337
338
	public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
339
		$parameters["params"]=$params;
340
		return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
341
	}
342
343
	public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
344
		return $this->postOn("click", $url, $params, $responseElement, $parameters);
345
	}
346
347
	public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
348
		$parameters["form"]=$form;
349
		return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
350
	}
351
352
	public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
353
		return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
354
	}
355
356
	public function getElementById($identifier, $elements) {
357
		if (is_array($elements)) {
358
			$flag=false;
359
			$index=0;
360 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...
361
				if ($elements[$index] instanceof BaseHtml)
362
					$flag=($elements[$index]->getIdentifier() === $identifier);
363
				$index++;
364
			}
365
			if ($flag === true)
366
				return $elements[$index - 1];
367
		} elseif ($elements instanceof BaseHtml) {
368
			if ($elements->getIdentifier() === $identifier)
369
				return $elements;
370
		}
371
		return null;
372
	}
373
374
	protected function getElementByPropertyValue($propertyName,$value, $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]->propertyContains($propertyName, $value) === true);
381
					$index++;
382
			}
383
			if ($flag === true)
384
				return $elements[$index - 1];
385
		} elseif ($elements instanceof BaseHtml) {
386
			if ($elements->propertyContains($propertyName, $value) === true)
387
				return $elements;
388
		}
389
		return null;
390
	}
391
392
	public function __toString() {
393
		return $this->compile();
394
	}
395
396
	/**
397
	 * Puts HTML values in quotes for use in jQuery code
398
	 * unless the supplied value contains the Javascript 'this' or 'event'
399
	 * object, in which case no quotes are added
400
	 *
401
	 * @param string $value
402
	 * @return string
403
	 */
404
	public function _prep_value($value) {
405
		if (is_array($value)) {
406
			$value=implode(",", $value);
407
		}
408
		if (strrpos($value, 'this') === false && strrpos($value, 'event') === false) {
409
			$value='"' . $value . '"';
410
		}
411
		return $value;
412
	}
413
414
	public function jsDoJquery($jqueryCall, $param="") {
415
		return "$('#" . $this->identifier . "')." . $jqueryCall . "(" . $this->_prep_value($param) . ");";
416
	}
417
418
	public function executeOnRun($jsCode) {
419
		return $this->_addEvent("execute", $jsCode);
420
	}
421
422
	public function jsHtml($content="") {
423
		return $this->jsDoJquery("html", $content);
424
	}
425
426
	public function jsShow() {
427
		return $this->jsDoJquery("show");
428
	}
429
430
	public function jsHide() {
431
		return $this->jsDoJquery("hide");
432
	}
433
434
	protected function setWrapBefore($wrapBefore) {
435
		$this->_wrapBefore=$wrapBefore;
436
		return $this;
437
	}
438
439
	protected function setWrapAfter($wrapAfter) {
440
		$this->_wrapAfter=$wrapAfter;
441
		return $this;
442
	}
443
}