Completed
Push — master ( b9a8b8...3e8704 )
by Jean-Christophe
03:43
created

BaseHtml::fromArray()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 4.909
cc 9
eloc 20
nc 18
nop 1
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
10
/**
11
 * BaseHtml for Twitter Bootstrap HTML components
12
 * @author jc
13
 * @version 1.001
14
 */
15
abstract class BaseHtml extends BaseWidget {
16
	protected $_template;
17
	protected $tagName;
18
	protected $properties=array ();
19
	protected $events=array ();
20
	protected $wrapBefore="";
21
	protected $wrapAfter="";
22
	protected $_bsComponent;
23
24
	public function getBsComponent() {
25
		return $this->_bsComponent;
26
	}
27
28
	public function setBsComponent($bsComponent) {
29
		$this->_bsComponent=$bsComponent;
30
		return $this;
31
	}
32
33
	protected function getTemplate() {
34
		return $this->wrapBefore.$this->_template.$this->wrapAfter;
35
	}
36
37
	public function getProperties() {
38
		return $this->properties;
39
	}
40
41
	public function setProperties($properties) {
42
		$this->properties=$properties;
43
		return $this;
44
	}
45
46
	public function setProperty($name, $value) {
47
		$this->properties[$name]=$value;
48
		return $this;
49
	}
50
51
	public function getProperty($name) {
52
		if (array_key_exists($name, $this->properties))
53
			return $this->properties [$name];
54
	}
55
56
	public function addToProperty($name, $value, $separator=" ") {
57
		$v=@$this->properties[$name];
58
		if (isset($v)&&$v!=="")
59
			$v=$v.$separator.$value;
60
		else
61
			$v=$value;
62
63
		return $this->setProperty($name, $v);
64
	}
65
66
	public function addProperties($properties) {
67
		$this->properties=array_merge($this->properties, $properties);
68
		return $this;
69
	}
70
71
	public function compile(JsUtils $js=NULL, View $view=NULL) {
72
		$result=$this->getTemplate();
73
		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...
74
			if (PhalconUtils::startsWith($key, "_")===false&&$key!=="events") {
75
				if (is_array($value)) {
76
					$v=PropertyWrapper::wrap($value, $js);
77
				} else {
78
					$v=$value;
79
				}
80
				$result=str_ireplace("%".$key."%", $v, $result);
81
			}
82
		}
83
		if (isset($js)) {
84
			$this->run($js);
85
		}
86
		if (isset($view)===true) {
87
			$controls=$view->getVar("q");
88
			if (isset($controls)===false) {
89
				$controls=array ();
90
			}
91
			$controls [$this->identifier]=$result;
92
			$view->setVar("q", $controls);
93
		}
94
		return $result;
95
	}
96
97 View Code Duplication
	protected function ctrl($name, $value, $typeCtrl) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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