Completed
Push — master ( 2461e8...b569b1 )
by Jean-Christophe
04:01
created

BaseHtml::removePropertyValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
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 removePropertyValues($name,$values){
147
		$this->removeOldValues($this->properties[$name], $values);
148
		return $this;
149
	}
150
151
	protected function removeProperty($name){
152
		if(\array_key_exists($name, $this->properties))
153
			unset($this->properties[$name]);
154
		return $this;
155
	}
156
157
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
158
		if ($this->ctrl($name, $value, $typeCtrl)===true) {
159
			if (is_array($typeCtrl)) {
160
				$this->removeOldValues($name, $typeCtrl);
161
			}
162
			$name.=$separator.$value;
163
		}
164
		return $this;
165
	}
166
167
	protected function addToMember(&$name, $value, $separator=" ") {
168
		$name=str_ireplace($value, "", $name).$separator.$value;
169
		return $this;
170
	}
171
172
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
173
		if (@class_exists($typeCtrl, true))
174
			$typeCtrl=$typeCtrl::getConstants();
175
		if (is_array($typeCtrl)) {
176
			$this->removeOldValues($this->properties [$name], $typeCtrl);
177
		}
178
		return $this->addToProperty($name, $value);
179
	}
180
181
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
182
		// 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...
183
		return $this->addToPropertyUnique($name, $value, $typeCtrl);
184
		// }
185
		//return $this;
186
	}
187
188
	protected function removeOldValues(&$oldValue, $allValues) {
189
		$oldValue=str_ireplace($allValues, "", $oldValue);
190
		$oldValue=trim($oldValue);
191
	}
192
193
	/**
194
	 * @param JsUtils $js
195
	 * @return SimpleExtComponent
196
	 */
197
	public abstract function run(JsUtils $js);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
198
199
	public function getTagName() {
200
		return $this->tagName;
201
	}
202
203
	public function setTagName($tagName) {
204
		$this->tagName=$tagName;
205
		return $this;
206
	}
207
208
	public function fromArray($array) {
209
		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...
210
			if (array_key_exists($key, $array) && !PhalconUtils::startsWith($key, "_")) {
211
					$setter="set".ucfirst($key);
212
					$this->$setter($array [$key]);
213
				unset($array [$key]);
214
			}
215
		}
216
		foreach ( $array as $key => $value ) {
217
			if (method_exists($this, $key)) {
218
				try {
219
					$this->$key($value);
220
					unset($array [$key]);
221
				} catch ( \Exception $e ) {
222
					// Nothing to do
223
				}
224
			} else {
225
				$setter="set".ucfirst($key);
226
				if (method_exists($this, $setter)) {
227
					try {
228
						$this->$setter($value);
229
						unset($array [$key]);
230
					} catch ( \Exception $e ) {
231
						// Nothing to do
232
					}
233
				}
234
			}
235
		}
236
		return $array;
237
	}
238
239
	public function fromDatabaseObjects($objects,$function) {
240
		if(isset($objects)){
241
			foreach ($objects as $object){
242
				$this->fromDatabaseObject($object,$function);
243
			}
244
		}
245
		return $this;
246
	}
247
248
	public function fromDatabaseObject($object,$function){
249
250
	}
251
252
	public function wrap($before, $after="") {
253
		if(isset($before)){
254
			$this->_wrapBefore[]=$before;
255
		}
256
		$this->_wrapAfter[]=$after;
257
		return $this;
258
	}
259
260
	public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
261
		if ($stopPropagation===true) {
262
			$jsCode="event.stopPropagation();".$jsCode;
263
		}
264
		if ($preventDefault===true) {
265
			$jsCode="event.preventDefault();".$jsCode;
266
		}
267
		return $this->_addEvent($event, $jsCode);
268
	}
269
270
	public function _addEvent($event, $jsCode) {
271
		if (array_key_exists($event, $this->_events)) {
272
			if (is_array($this->_events [$event])) {
273
				$this->_events [$event] []=$jsCode;
274
			} else {
275
				$this->_events [$event]=array (
276
						$this->_events [$event],
277
						$jsCode
278
				);
279
			}
280
		} else {
281
			$this->_events [$event]=$jsCode;
282
		}
283
		return $this;
284
	}
285
286
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
287
		return $this->addEvent($event, $jsCode, $stopPropagation, $preventDefault);
288
	}
289
290
	public function onClick($jsCode, $stopPropagation=false, $preventDefault=true) {
291
		return $this->on("click", $jsCode, $stopPropagation, $preventDefault);
292
	}
293
294
	public function setClick($jsCode) {
295
		return $this->onClick($jsCode);
296
	}
297
298
	public function addEventsOnRun(JsUtils $js) {
299
		if (isset($this->_bsComponent)) {
300
			foreach ( $this->_events as $event => $jsCode ) {
301
				$code=$jsCode;
302
				if (is_array($jsCode)) {
303
					$code="";
304
					foreach ( $jsCode as $jsC ) {
305
						if ($jsC instanceof AjaxCall) {
306
							$code.="\n".$jsC->compile($js);
307
						} else {
308
							$code.="\n".$jsC;
309
						}
310
					}
311
				} elseif ($jsCode instanceof AjaxCall) {
312
					$code=$jsCode->compile($js);
313
				}
314
				$this->_bsComponent->addEvent($event, $code);
315
			}
316
			$this->_events=array();
317
		}
318
	}
319
320
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
321
		$params=array (
322
				"url" => $url,
323
				"responseElement" => $responseElement
324
		);
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
			while ( !$flag&&$index<sizeof($elements) ) {
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
	public function __toString(){
375
		return $this->compile();
376
	}
377
378
	/**
379
	 * Puts HTML values in quotes for use in jQuery code
380
	 * unless the supplied value contains the Javascript 'this' or 'event'
381
	 * object, in which case no quotes are added
382
	 *
383
	 * @param string $value
384
	 * @return string
385
	 */
386
	public function _prep_value($value) {
387
		if (is_array($value)) {
388
			$value=implode(",", $value);
389
		}
390
		if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false) {
391
			$value='"'.$value.'"';
392
		}
393
		return $value;
394
	}
395
396
	public function jsDoJquery($jqueryCall, $param=""){
397
		return "$('#".$this->identifier."').".$jqueryCall."(".$this->_prep_value($param).");";
398
	}
399
400
	public function executeOnRun($jsCode){
401
		return $this->_addEvent("execute", $jsCode);
402
	}
403
404
	public function jsHtml($content=""){
405
		return $this->jsDoJquery("html",$content);
406
	}
407
408
	public function jsShow(){
409
		return $this->jsDoJquery("show");
410
	}
411
412
	public function jsHide(){
413
		return $this->jsDoJquery("hide");
414
	}
415
416
	protected function setWrapBefore($wrapBefore) {
417
		$this->_wrapBefore=$wrapBefore;
418
		return $this;
419
	}
420
421
	protected function setWrapAfter($wrapAfter) {
422
		$this->_wrapAfter=$wrapAfter;
423
		return $this;
424
	}
425
426
}