Passed
Push — master ( 238ca7...e7f105 )
by Jean-Christophe
04:32
created

BaseHtml::asDropZone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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