Completed
Push — master ( 424d4e...c1a616 )
by Jean-Christophe
03:24
created

BaseHtml::getOnClick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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