Completed
Push — master ( 9e0835...2461e8 )
by Jean-Christophe
03:27
created

BaseHtml::addToMemberUnique()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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