Completed
Push — master ( db82f0...0ba4b1 )
by Jean-Christophe
03:22
created

BaseHtml::onClick()   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
	}
143
144
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
145
		if ($this->ctrl($name, $value, $typeCtrl)===true) {
146
			if (is_array($typeCtrl)) {
147
				$this->removeOldValues($name, $typeCtrl);
148
			}
149
			$name.=$separator.$value;
150
		}
151
		return $this;
152
	}
153
154
	protected function addToMember(&$name, $value, $separator=" ") {
155
		$name=str_ireplace($value, "", $name).$separator.$value;
156
		return $this;
157
	}
158
159
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
160
		if (@class_exists($typeCtrl, true))
161
			$typeCtrl=$typeCtrl::getConstants();
162
		if (is_array($typeCtrl)) {
163
			$this->removeOldValues($this->properties [$name], $typeCtrl);
164
		}
165
		return $this->addToProperty($name, $value);
166
	}
167
168
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
169
		// 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...
170
		return $this->addToPropertyUnique($name, $value, $typeCtrl);
171
		// }
172
		//return $this;
173
	}
174
175
	protected function removeOldValues(&$oldValue, $allValues) {
176
		$oldValue=str_ireplace($allValues, "", $oldValue);
177
		$oldValue=trim($oldValue);
178
	}
179
180
	abstract public function run(JsUtils $js);
181
182
	public function getTagName() {
183
		return $this->tagName;
184
	}
185
186
	public function setTagName($tagName) {
187
		$this->tagName=$tagName;
188
		return $this;
189
	}
190
191
	public function fromArray($array) {
192
		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...
193
			if (array_key_exists($key, $array) && !PhalconUtils::startsWith($key, "_")) {
194
					$setter="set".ucfirst($key);
195
					$this->$setter($array [$key]);
196
				unset($array [$key]);
197
			}
198
		}
199
		foreach ( $array as $key => $value ) {
200
			if (method_exists($this, $key)) {
201
				try {
202
					$this->$key($value);
203
					unset($array [$key]);
204
				} catch ( \Exception $e ) {
205
					// Nothing to do
206
				}
207
			} else {
208
				$setter="set".ucfirst($key);
209
				if (method_exists($this, $setter)) {
210
					try {
211
						$this->$setter($value);
212
						unset($array [$key]);
213
					} catch ( \Exception $e ) {
214
						// Nothing to do
215
					}
216
				}
217
			}
218
		}
219
		return $array;
220
	}
221
222
	public function fromDatabaseObjects($objects,$function) {
223
		if(isset($objects)){
224
			foreach ($objects as $object){
225
				$this->fromDatabaseObject($object,$function);
226
			}
227
		}
228
		return $this;
229
	}
230
231
	public function fromDatabaseObject($object,$function){
232
233
	}
234
235
	public function wrap($before, $after="") {
236
		if(isset($before)){
237
			$this->wrapBefore.=$before;
238
		}
239
		$this->wrapAfter=$after.$this->wrapAfter;
240
		return $this;
241
	}
242
243
	public function addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
244
		if ($stopPropagation===true) {
245
			$jsCode="event.stopPropagation();".$jsCode;
246
		}
247
		if ($preventDefault===true) {
248
			$jsCode="event.preventDefault();".$jsCode;
249
		}
250
		$this->_addEvent($event, $jsCode);
251
		return $this;
252
	}
253
254
	public function _addEvent($event, $jsCode) {
255
		if (array_key_exists($event, $this->events)) {
256
			if (is_array($this->events [$event])) {
257
				$this->events [$event] []=$jsCode;
258
			} else {
259
				$this->events [$event]=array (
260
						$this->events [$event],
261
						$jsCode
262
				);
263
			}
264
		} else {
265
			$this->events [$event]=$jsCode;
266
		}
267
	}
268
269
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
270
		return $this->addEvent($event, $jsCode, $stopPropagation, $preventDefault);
271
	}
272
273
	public function onClick($jsCode, $stopPropagation=false, $preventDefault=false) {
274
		return $this->on("click", $jsCode, $stopPropagation, $preventDefault);
275
	}
276
277
	public function setClick($jsCode) {
278
		return $this->onClick($jsCode);
279
	}
280
281
	public function addEventsOnRun(JsUtils $js) {
282
		if (isset($this->_bsComponent)) {
283
			foreach ( $this->events as $event => $jsCode ) {
284
				$code=$jsCode;
285
				if (is_array($jsCode)) {
286
					$code="";
287
					foreach ( $jsCode as $jsC ) {
288
						if ($jsC instanceof AjaxCall) {
289
							$code.="\n".$jsC->compile($js);
290
						} else {
291
							$code.="\n".$jsC;
292
						}
293
					}
294
				} elseif ($jsCode instanceof AjaxCall) {
295
					$code=$jsCode->compile($js);
296
				}
297
				$this->_bsComponent->addEvent($event, $code);
298
			}
299
			$this->events=array();
300
		}
301
	}
302
303
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
304
		$params=array (
305
				"url" => $url,
306
				"responseElement" => $responseElement
307
		);
308
		$params=array_merge($params, $parameters);
309
		$this->_addEvent($event, new AjaxCall($operation, $params));
310
		return $this;
311
	}
312
313
	public function getOn($event, $url, $responseElement="", $parameters=array()) {
314
		return $this->_ajaxOn("get", $event, $url, $responseElement, $parameters);
315
	}
316
317
	public function getOnClick($url, $responseElement="", $parameters=array()) {
318
		return $this->getOn("click", $url, $responseElement, $parameters);
319
	}
320
321
	public function postOn($event, $url, $params="{}", $responseElement="", $parameters=array()) {
322
		$parameters ["params"]=$params;
323
		return $this->_ajaxOn("post", $event, $url, $responseElement, $parameters);
324
	}
325
326
	public function postOnClick($url, $params="{}", $responseElement="", $parameters=array()) {
327
		return $this->postOn("click", $url, $params, $responseElement, $parameters);
328
	}
329
330
	public function postFormOn($event, $url, $form, $responseElement="", $parameters=array()) {
331
		$parameters ["form"]=$form;
332
		return $this->_ajaxOn("postForm", $event, $url, $responseElement, $parameters);
333
	}
334
335
	public function postFormOnClick($url, $form, $responseElement="", $parameters=array()) {
336
		return $this->postFormOn("click", $url, $form, $responseElement, $parameters);
337
	}
338
339
	public function getElementById($identifier, $elements) {
340
		if (is_array($elements)) {
341
			$flag=false;
342
			$index=0;
343
			while ( !$flag&&$index<sizeof($elements) ) {
344
				if ($elements [$index] instanceof BaseHtml)
345
					$flag=($elements [$index]->getIdentifier()===$identifier);
346
				$index++;
347
			}
348
			if ($flag===true)
349
				return $elements [$index-1];
350
		} elseif ($elements instanceof BaseHtml) {
351
			if ($elements->getIdentifier()===$identifier)
352
				return $elements;
353
		}
354
		return null;
355
	}
356
357
	public function __toString(){
358
		return $this->compile();
359
	}
360
361
	/**
362
	 * Puts HTML values in quotes for use in jQuery code
363
	 * unless the supplied value contains the Javascript 'this' or 'event'
364
	 * object, in which case no quotes are added
365
	 *
366
	 * @param string $value
367
	 * @return string
368
	 */
369
	public function _prep_value($value) {
370
		if (is_array($value)) {
371
			$value=implode(",", $value);
372
		}
373
		if (strrpos($value, 'this')===false&&strrpos($value, 'event')===false) {
374
			$value='"'.$value.'"';
375
		}
376
		return $value;
377
	}
378
379
	public function jsDoJquery($jqueryCall, $param=""){
380
		return "$('#".$this->identifier."').".$jqueryCall."(".$this->_prep_value($param).");";
381
	}
382
383
	public function jsHtml($content=""){
384
		return $this->jsDoJquery("html",$content);
385
	}
386
387
	public function jsShow(){
388
		return $this->jsDoJquery("show");
389
	}
390
391
	public function jsHide(){
392
		return $this->jsDoJquery("hide");
393
	}
394
}