Passed
Push — master ( 468e47...0bebef )
by Jean-Christophe
02:09
created

BaseHtml::compile()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 36
nop 2
dl 0
loc 29
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\common\components\SimpleExtComponent;
7
use Ajax\JsUtils;
8
use Ajax\common\html\traits\BaseHtmlEventsTrait;
9
use Ajax\common\html\traits\BaseHtmlPropertiesTrait;
10
use Ajax\service\Javascript;
11
12
/**
13
 * BaseHtml for HTML components
14
 * @author jc
15
 * @version 1.3
16
 */
17
abstract class BaseHtml extends BaseWidget {
18
	use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait;
19
	protected $_template;
20
	protected $tagName;
21
	protected $_wrapBefore=array ();
22
	protected $_wrapAfter=array ();
23
	protected $_bsComponent;
24
	protected $_compiled=false;
25
	protected $_runned=false;
26
	protected $_postCompile;
27
	protected $_preCompile;
28
29
	/**
30
	 *
31
	 * @param JsUtils $js
32
	 * @return SimpleExtComponent
33
	 */
34
	abstract public function run(JsUtils $js);
35
36
	private function _callSetter($setter,$key,$value,&$array){
37
		$result=false;
38
		if (method_exists($this, $setter) && substr($setter, 0, 1) !== "_") {
39
			try {
40
				$this->$setter($value);
41
				unset($array[$key]);
42
				$result=true;
43
			} catch ( \Exception $e ) {
44
				$result=false;
45
			}
46
		}
47
		return $result;
48
	}
49
50
	protected function getTemplate(JsUtils $js=NULL,$view=null) {
51
		return PropertyWrapper::wrap($this->_wrapBefore, $js,$view) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js,$view);
52
	}
53
54
	protected function ctrl($name, $value, $typeCtrl) {
55
		if (\is_array($typeCtrl)) {
56
			if (array_search($value, $typeCtrl) === false) {
57
				throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}");
58
			}
59
		} else {
60
			if (!$typeCtrl($value)) {
61
				throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name);
62
			}
63
		}
64
		return true;
65
	}
66
67
68
69
	protected function setMemberCtrl(&$name, $value, $typeCtrl) {
70
		$this->ctrl($name, $value, $typeCtrl);
71
		$name=$value;
72
		return $this;
73
	}
74
75
	protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") {
76
		if (\is_array($typeCtrl)) {
77
			$this->removeOldValues($name, $typeCtrl);
78
			$name.=$separator . $value;
79
		}
80
		return $this;
81
	}
82
83
84
85
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
86
		$this->ctrl($name, $value, $typeCtrl);
87
		if (\is_array($typeCtrl)) {
88
			$this->removeOldValues($name, $typeCtrl);
89
		}
90
		$name.=$separator . $value;
91
		return $this;
92
	}
93
94
	protected function addToMember(&$name, $value, $separator=" ") {
95
		$name=str_ireplace($value, "", $name) . $separator . $value;
96
		return $this;
97
	}
98
99
100
101
	protected function removeOldValues(&$oldValue, $allValues) {
102
		$oldValue=str_ireplace($allValues, "", $oldValue);
103
		$oldValue=trim($oldValue);
104
	}
105
106
	protected function _getElementBy($callback,$elements){
107
		if (\is_array($elements)) {
108
			$elements=\array_values($elements);
109
			$flag=false;
110
			$index=0;
111
			while ( !$flag && $index < sizeof($elements) ) {
112
				if ($elements[$index] instanceof BaseHtml)
113
					$flag=($callback($elements[$index]));
114
					$index++;
115
			}
116
			if ($flag === true)
117
				return $elements[$index - 1];
118
		} elseif ($elements instanceof BaseHtml) {
119
			if ($callback($elements))
120
				return $elements;
121
		}
122
		return null;
123
	}
124
125
	protected function setWrapBefore($wrapBefore) {
126
		$this->_wrapBefore=$wrapBefore;
127
		return $this;
128
	}
129
130
	protected function setWrapAfter($wrapAfter) {
131
		$this->_wrapAfter=$wrapAfter;
132
		return $this;
133
	}
134
135
	public function getTagName() {
136
		return $this->tagName;
137
	}
138
139
	public function setTagName($tagName) {
140
		$this->tagName=$tagName;
141
		return $this;
142
	}
143
144
	public function fromArray($array) {
145
		foreach ( $this as $key => $value ) {
146
			if(array_key_exists($key, $array)===true)
147
				$this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array);
148
		}
149
		foreach ( $array as $key => $value ) {
150
			if($this->_callSetter($key, $key, $value, $array)===false){
151
				$this->_callSetter("set" . ucfirst($key), $key, $value, $array);
152
			}
153
		}
154
		return $array;
155
	}
156
157
	public function fromDatabaseObjects($objects, $function) {
158
		if (isset($objects)) {
159
			foreach ( $objects as $object ) {
160
				$this->fromDatabaseObject($object, $function);
161
			}
162
		}
163
		return $this;
164
	}
165
166
	public function fromDatabaseObject($object, $function) {
167
	}
168
169
	public function wrap($before, $after="") {
170
		if (isset($before)) {
171
			array_unshift($this->_wrapBefore, $before);
172
		}
173
		$this->_wrapAfter[]=$after;
174
		return $this;
175
	}
176
177
178
179
	public function getElementById($identifier, $elements) {
180
		return $this->_getElementBy(function(BaseWidget $element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements);
181
	}
182
183
	public function getBsComponent() {
184
		return $this->_bsComponent;
185
	}
186
187
	public function setBsComponent($bsComponent) {
188
		$this->_bsComponent=$bsComponent;
189
		return $this;
190
	}
191
192
	protected function compile_once(JsUtils $js=NULL, &$view=NULL) {
193
		if(!$this->_compiled){
194
			if(isset($js)){
195
				$beforeCompile=$js->getParam("beforeCompileHtml");
196
				if(\is_callable($beforeCompile)){
197
					$beforeCompile($this,$js,$view);
198
				}
199
			}
200
			if(\is_callable($this->_preCompile)){
201
				$pc=$this->_preCompile;
202
				$pc($this);
203
			}
204
			unset($this->properties["jsCallback"]);
205
			$this->_compiled=true;
206
		}
207
	}
208
209
	public function compile(JsUtils $js=NULL, &$view=NULL) {
210
		$this->compile_once($js,$view);
211
		$result=$this->getTemplate($js,$view);
212
		foreach ( $this as $key => $value ) {
213
				if(\strstr($result, "%{$key}%")!==false){
214
					if (\is_array($value)) {
215
						$v=PropertyWrapper::wrap($value, $js,$view);
216
					}elseif($value instanceof \stdClass){
217
							$v=\print_r($value,true);
218
					}elseif ($value instanceof BaseHtml){
219
						$v=$value->compile($js,$view);
220
					}else{
221
						$v=$value;
222
					}
223
					$result=str_replace("%{$key}%", $v, $result);
224
				}
225
		}
226
		if (isset($js)===true) {
227
			$this->run($js);
228
			if (isset($view) === true) {
229
				$js->addViewElement($this->_identifier, $result, $view);
230
			}
231
		}
232
233
		if(\is_callable($this->_postCompile)){
234
			$pc=$this->_postCompile;
235
			$pc($this);
236
		}
237
		return $result;
238
	}
239
	
240
	/**
241
	 * Sets the element draggable, and eventualy defines the dropzone (HTML5 drag and drop)
242
	 * @param string $attr default: "id"
243
	 * @param BaseHtml $dropZone the dropzone element
244
	 * @param array $parameters default: ["jsCallback"=>"","jqueryDone"=>"append"]
245
	 * @return \Ajax\common\html\BaseHtml
246
	 */
247
	public function setDraggable($attr="id",$dropZone=null,$parameters=[]){
248
		$this->setProperty("draggable", "true");
249
		$this->addEvent("dragstart",Javascript::draggable($attr));
250
		if(isset($dropZone)&& $dropZone instanceof BaseHtml){
251
			$jqueryDone="append";$jsCallback="";
252
			extract($parameters);
253
			$dropZone->asDropZone($jsCallback,$jqueryDone,$parameters);
254
		}
255
		return $this;
256
	}
257
	
258
	/**
259
	 * Declares the element as a drop zone (HTML5 drag and drop)
260
	 * @param string $jsCallback
261
	 * @param string $jqueryDone
262
	 * @param array $parameters
263
	 * @return \Ajax\common\html\BaseHtml
264
	 */
265
	public function asDropZone($jsCallback="",$jqueryDone="append",$parameters=[]){
266
		$stopPropagation=false;
267
		$this->addEvent("dragover", '', $stopPropagation,true);
268
		extract($parameters);
269
		$this->addEvent("drop",Javascript::dropZone($jqueryDone,$jsCallback),$stopPropagation,true);
270
		return $this;
271
	}
272
273
	public function __toString() {
274
		return $this->compile();
275
	}
276
277
	public function onPostCompile($callback){
278
		$this->_postCompile=$callback;
279
	}
280
281
	public function onPreCompile($callback){
282
		$this->_preCompile=$callback;
283
	}
284
}
285