Completed
Push — master ( d27f99...f4e97d )
by Jean-Christophe
03:35
created

BaseHtml::postOnClick()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
namespace Ajax\common\html;
4
5
6
use Ajax\service\JString;
7
use Ajax\common\components\SimpleExtComponent;
8
use Ajax\JsUtils;
9
use Ajax\common\html\traits\BaseHtmlEventsTrait;
10
11
/**
12
 * BaseHtml for HTML components
13
 * @author jc
14
 * @version 1.2
15
 */
16
abstract class BaseHtml extends BaseWidget {
17
	use BaseHtmlEventsTrait;
18
	protected $_template;
19
	protected $tagName;
20
	protected $properties=array ();
21
	protected $_wrapBefore=array ();
22
	protected $_wrapAfter=array ();
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(JsUtils $js=NULL) {
35
		return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js);
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
		if (\is_array($value)) {
59
			foreach ( $value as $v ) {
60
				$this->addToProperty($name, $v, $separator);
61
			}
62
		} else if ($value !== "" && $this->propertyContains($name, $value) === false) {
63
			$v=@$this->properties[$name];
64
			if (isset($v) && $v !== "")
65
				$v=$v . $separator . $value;
66
			else
67
				$v=$value;
68
69
			return $this->setProperty($name, $v);
70
		}
71
		return $this;
72
	}
73
74
	public function addProperties($properties) {
75
		$this->properties=array_merge($this->properties, $properties);
76
		return $this;
77
	}
78
79
	public function compile(JsUtils $js=NULL, &$view=NULL) {
80
		$result=$this->getTemplate($js);
81
		foreach ( $this as $key => $value ) {
1 ignored issue
show
Bug introduced by
The expression $this of type this<Ajax\common\html\BaseHtml> is not traversable.
Loading history...
82
			if (JString::startswith($key, "_") === false && $key !== "events") {
83
				if (is_array($value)) {
84
					$v=PropertyWrapper::wrap($value, $js);
85
				} else {
86
					$v=$value;
87
				}
88
				$result=str_ireplace("%" . $key . "%", $v, $result);
89
			}
90
		}
91
		if (isset($js)===true) {
92
			$this->run($js);
93
			if (isset($view) === true) {
94
				$js->addViewElement($this->identifier, $result, $view);
95
			}
96
		}
97
		return $result;
98
	}
99
100
	protected function ctrl($name, $value, $typeCtrl) {
101 View Code Duplication
		if (is_array($typeCtrl)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
			if (array_search($value, $typeCtrl) === false) {
103
				throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}");
104
			}
105
		} else {
106
			if (!$typeCtrl($value)) {
107
				throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name);
108
			}
109
		}
110
		return true;
111
	}
112
113
	protected function propertyContains($propertyName, $value) {
114
		$values=$this->getProperty($propertyName);
115
		if (isset($values)) {
116
			return JString::contains($values, $value);
117
		}
118
		return false;
119
	}
120
121
	protected function setPropertyCtrl($name, $value, $typeCtrl) {
122
		if ($this->ctrl($name, $value, $typeCtrl) === true)
123
			return $this->setProperty($name, $value);
124
		return $this;
125
	}
126
127
	protected function setMemberCtrl(&$name, $value, $typeCtrl) {
128
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
129
			return $name=$value;
130
		}
131
		return $this;
132
	}
133
134
	protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") {
135
		if (is_array($typeCtrl)) {
136
			$this->removeOldValues($name, $typeCtrl);
137
			$name.=$separator . $value;
138
		}
139
		return $this;
140
	}
141
142
	protected function removePropertyValue($name, $value) {
143
		$this->properties[$name]=\str_replace($value, "", $this->properties[$name]);
144
		return $this;
145
	}
146
147
	protected function removePropertyValues($name, $values) {
148
		$this->removeOldValues($this->properties[$name], $values);
149
		return $this;
150
	}
151
152
	public function removeProperty($name) {
153
		if (\array_key_exists($name, $this->properties))
154
			unset($this->properties[$name]);
155
		return $this;
156
	}
157
158
	protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
159
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
160
			if (is_array($typeCtrl)) {
161
				$this->removeOldValues($name, $typeCtrl);
162
			}
163
			$name.=$separator . $value;
164
		}
165
		return $this;
166
	}
167
168
	protected function addToMember(&$name, $value, $separator=" ") {
169
		$name=str_ireplace($value, "", $name) . $separator . $value;
170
		return $this;
171
	}
172
173
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
174
		if (@class_exists($typeCtrl, true))
175
			$typeCtrl=$typeCtrl::getConstants();
176
		if (is_array($typeCtrl)) {
177
			$this->removeOldValues($this->properties[$name], $typeCtrl);
178
		}
179
		return $this->addToProperty($name, $value);
180
	}
181
182
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
183
		return $this->addToPropertyUnique($name, $value, $typeCtrl);
184
	}
185
186
	public function addToPropertyCtrlCheck($name, $value, $typeCtrl) {
187
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
188
			return $this->addToProperty($name, $value);
189
		}
190
		return $this;
191
	}
192
193
	protected function removeOldValues(&$oldValue, $allValues) {
194
		$oldValue=str_ireplace($allValues, "", $oldValue);
195
		$oldValue=trim($oldValue);
196
	}
197
198
	/**
199
	 *
200
	 * @param JsUtils $js
201
	 * @return SimpleExtComponent
202
	 */
203
	public abstract function run(JsUtils $js);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
204
205
	public function getTagName() {
206
		return $this->tagName;
207
	}
208
209
	public function setTagName($tagName) {
210
		$this->tagName=$tagName;
211
		return $this;
212
	}
213
214
	public function fromArray($array) {
215
		foreach ( $this as $key => $value ) {
1 ignored issue
show
Bug introduced by
The expression $this of type this<Ajax\common\html\BaseHtml> is not traversable.
Loading history...
216
			if (array_key_exists($key, $array) && !JString::startswith($key, "_")) {
217
				$setter="set" . ucfirst($key);
218
				$this->$setter($array[$key]);
219
				unset($array[$key]);
220
			}
221
		}
222
		foreach ( $array as $key => $value ) {
223
			if (method_exists($this, $key)) {
224
				try {
225
					$this->$key($value);
226
					unset($array[$key]);
227
				} catch ( \Exception $e ) {
228
					// Nothing to do
229
				}
230
			} else {
231
				$setter="set" . ucfirst($key);
232
				if (method_exists($this, $setter)) {
233
					try {
234
						$this->$setter($value);
235
						unset($array[$key]);
236
					} catch ( \Exception $e ) {
237
						// Nothing to do
238
					}
239
				}
240
			}
241
		}
242
		return $array;
243
	}
244
245
	public function fromDatabaseObjects($objects, $function) {
246
		if (isset($objects)) {
247
			foreach ( $objects as $object ) {
248
				$this->fromDatabaseObject($object, $function);
249
			}
250
		}
251
		return $this;
252
	}
253
254
	public function fromDatabaseObject($object, $function) {
255
	}
256
257
	public function wrap($before, $after="") {
258
		if (isset($before)) {
259
			array_unshift($this->_wrapBefore, $before);
260
			// $this->_wrapBefore[]=$before;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% 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...
261
		}
262
		$this->_wrapAfter[]=$after;
263
		return $this;
264
	}
265
266
267
268
	public function getElementById($identifier, $elements) {
269
		if (is_array($elements)) {
270
			$flag=false;
271
			$index=0;
272 View Code Duplication
			while ( !$flag && $index < sizeof($elements) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
273
				if ($elements[$index] instanceof BaseHtml)
274
					$flag=($elements[$index]->getIdentifier() === $identifier);
275
				$index++;
276
			}
277
			if ($flag === true)
278
				return $elements[$index - 1];
279
		} elseif ($elements instanceof BaseHtml) {
280
			if ($elements->getIdentifier() === $identifier)
281
				return $elements;
282
		}
283
		return null;
284
	}
285
286
	protected function getElementByPropertyValue($propertyName,$value, $elements) {
287
		if (is_array($elements)) {
288
			$flag=false;
289
			$index=0;
290 View Code Duplication
			while ( !$flag && $index < sizeof($elements) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
291
				if ($elements[$index] instanceof BaseHtml)
292
					$flag=($elements[$index]->propertyContains($propertyName, $value) === true);
293
					$index++;
294
			}
295
			if ($flag === true)
296
				return $elements[$index - 1];
297
		} elseif ($elements instanceof BaseHtml) {
298
			if ($elements->propertyContains($propertyName, $value) === true)
299
				return $elements;
300
		}
301
		return null;
302
	}
303
304
	public function __toString() {
305
		return $this->compile();
306
	}
307
308
	protected function setWrapBefore($wrapBefore) {
309
		$this->_wrapBefore=$wrapBefore;
310
		return $this;
311
	}
312
313
	protected function setWrapAfter($wrapAfter) {
314
		$this->_wrapAfter=$wrapAfter;
315
		return $this;
316
	}
317
}