Completed
Push — master ( ea2a7d...e9bdcb )
by Jean-Christophe
03:40
created

BaseHtml::addToPropertyCtrlCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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