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
|
|
|
use Ajax\common\html\traits\BaseHtmlPropertiesTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* BaseHtml for HTML components |
14
|
|
|
* @author jc |
15
|
|
|
* @version 1.2 |
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
|
|
|
|
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 compile(JsUtils $js=NULL, &$view=NULL) { |
39
|
|
|
$result=$this->getTemplate($js); |
40
|
|
|
foreach ( $this as $key => $value ) { |
41
|
|
|
if (JString::startswith($key, "_") === false && $key !== "events") { |
42
|
|
|
if (\is_array($value)) { |
43
|
|
|
$v=PropertyWrapper::wrap($value, $js); |
44
|
|
|
} else { |
45
|
|
|
$v=$value; |
46
|
|
|
} |
47
|
|
|
$result=str_ireplace("%" . $key . "%", $v, $result); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
if (isset($js)===true) { |
51
|
|
|
$this->run($js); |
52
|
|
|
if (isset($view) === true) { |
53
|
|
|
$js->addViewElement($this->identifier, $result, $view); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function ctrl($name, $value, $typeCtrl) { |
60
|
|
View Code Duplication |
if (\is_array($typeCtrl)) { |
|
|
|
|
61
|
|
|
if (array_search($value, $typeCtrl) === false) { |
62
|
|
|
throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}"); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
if (!$typeCtrl($value)) { |
66
|
|
|
throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
protected function setMemberCtrl(&$name, $value, $typeCtrl) { |
75
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
76
|
|
|
return $name=$value; |
77
|
|
|
} |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ") { |
82
|
|
|
if (\is_array($typeCtrl)) { |
83
|
|
|
$this->removeOldValues($name, $typeCtrl); |
84
|
|
|
$name.=$separator . $value; |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") { |
92
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
93
|
|
|
if (\is_array($typeCtrl)) { |
94
|
|
|
$this->removeOldValues($name, $typeCtrl); |
95
|
|
|
} |
96
|
|
|
$name.=$separator . $value; |
97
|
|
|
} |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function addToMember(&$name, $value, $separator=" ") { |
102
|
|
|
$name=str_ireplace($value, "", $name) . $separator . $value; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
|
108
|
|
|
protected function removeOldValues(&$oldValue, $allValues) { |
109
|
|
|
$oldValue=str_ireplace($allValues, "", $oldValue); |
110
|
|
|
$oldValue=trim($oldValue); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* @param JsUtils $js |
116
|
|
|
* @return SimpleExtComponent |
117
|
|
|
*/ |
118
|
|
|
abstract public function run(JsUtils $js); |
119
|
|
|
|
120
|
|
|
public function getTagName() { |
121
|
|
|
return $this->tagName; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setTagName($tagName) { |
125
|
|
|
$this->tagName=$tagName; |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function fromArray($array) { |
130
|
|
View Code Duplication |
foreach ( $this as $key => $value ) { |
|
|
|
|
131
|
|
|
if(array_key_exists($key, $array)===true) |
132
|
|
|
$this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array); |
133
|
|
|
} |
134
|
|
View Code Duplication |
foreach ( $array as $key => $value ) { |
|
|
|
|
135
|
|
|
if($this->_callSetter($key, $key, $value, $array)===false){ |
136
|
|
|
$this->_callSetter("set" . ucfirst($key), $key, $value, $array); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return $array; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function _callSetter($setter,$key,$value,&$array){ |
143
|
|
|
$result=false; |
144
|
|
|
if (method_exists($this, $setter) && !JString::startswith($key, "_")) { |
145
|
|
|
try { |
146
|
|
|
$this->$setter($value); |
147
|
|
|
unset($array[$key]); |
148
|
|
|
$result=true; |
149
|
|
|
} catch ( \Exception $e ) { |
150
|
|
|
$result=false; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function fromDatabaseObjects($objects, $function) { |
157
|
|
|
if (isset($objects)) { |
158
|
|
|
foreach ( $objects as $object ) { |
159
|
|
|
$this->fromDatabaseObject($object, $function); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function fromDatabaseObject($object, $function) { |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function wrap($before, $after="") { |
169
|
|
|
if (isset($before)) { |
170
|
|
|
array_unshift($this->_wrapBefore, $before); |
171
|
|
|
} |
172
|
|
|
$this->_wrapAfter[]=$after; |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
public function getElementById($identifier, $elements) { |
179
|
|
|
return $this->_getElementBy(function($element) use ($identifier){return $element->getIdentifier()===$identifier;}, $elements); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
protected function _getElementBy($callback,$elements){ |
183
|
|
|
if (\is_array($elements)) { |
184
|
|
|
$flag=false; |
185
|
|
|
$index=0; |
186
|
|
|
while ( !$flag && $index < sizeof($elements) ) { |
187
|
|
|
if ($elements[$index] instanceof BaseHtml) |
188
|
|
|
$flag=($callback($elements[$index])); |
189
|
|
|
$index++; |
190
|
|
|
} |
191
|
|
|
if ($flag === true) |
192
|
|
|
return $elements[$index - 1]; |
193
|
|
|
} elseif ($elements instanceof BaseHtml) { |
194
|
|
|
if ($callback($elements)) |
195
|
|
|
return $elements; |
196
|
|
|
} |
197
|
|
|
return null; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function __toString() { |
201
|
|
|
return $this->compile(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
protected function setWrapBefore($wrapBefore) { |
205
|
|
|
$this->_wrapBefore=$wrapBefore; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
protected function setWrapAfter($wrapAfter) { |
210
|
|
|
$this->_wrapAfter=$wrapAfter; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
} |
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.