phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
BaseHtml.php
Go to the documentation of this file.
1<?php
3
10
18
19 protected $_template;
20
21 protected $tagName;
22
23 protected $_wrapBefore = array();
24
25 protected $_wrapAfter = array();
26
27 protected $_bsComponent;
28
29 protected $_compiled = false;
30
31 protected $_runned = false;
32
33 protected $_postCompile;
34
35 protected $_preCompile;
36
42 abstract public function run(JsUtils $js);
43
44 private function _callSetter($setter, $key, $value, &$array) {
45 $result = false;
46 if (method_exists($this, $setter) && substr($setter, 0, 1) !== "_") {
47 try {
48 $this->$setter($value);
49 unset($array[$key]);
50 $result = true;
51 } catch (\Exception $e) {
52 $result = false;
53 }
54 }
55 return $result;
56 }
57
58 protected function getTemplate(JsUtils $js = NULL, $view = null) {
59 return PropertyWrapper::wrap($this->_wrapBefore, $js, $view) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js, $view);
60 }
61
62 protected function ctrl($name, $value, $typeCtrl) {
63 if (\is_array($typeCtrl)) {
64 if (array_search($value, $typeCtrl) === false) {
65 throw new \Exception("La valeur passée `" . $value . "` à la propriété `" . $name . "` ne fait pas partie des valeurs possibles : {" . implode(",", $typeCtrl) . "}");
66 }
67 } else {
68 if (! $typeCtrl($value)) {
69 throw new \Exception("La fonction " . $typeCtrl . " a retourné faux pour l'affectation de la propriété " . $name);
70 }
71 }
72 return true;
73 }
74
75 protected function setMemberCtrl(&$name, $value, $typeCtrl) {
76 $this->ctrl($name, $value, $typeCtrl);
77 $name = $value;
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 protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator = " ") {
90 $this->ctrl($name, $value, $typeCtrl);
91 if (\is_array($typeCtrl)) {
92 $this->removeOldValues($name, $typeCtrl);
93 }
94 $name .= $separator . $value;
95 return $this;
96 }
97
98 protected function addToMember(&$name, $value, $separator = ' ') {
99 $name = \str_ireplace($value, '', $name??'') . $separator . $value;
100 return $this;
101 }
102
103 protected function removeOldValues(&$oldValue, $allValues) {
104 $oldValue = \str_ireplace($allValues, '', $oldValue??'');
105 $oldValue = \trim($oldValue);
106 }
107
108 protected function _getElementBy($callback, $elements) {
109 if (\is_array($elements)) {
110 $elements = \array_values($elements);
111 $flag = false;
112 $index = 0;
113 while (! $flag && $index < sizeof($elements)) {
114 if ($elements[$index] instanceof BaseHtml)
115 $flag = ($callback($elements[$index]));
116 $index ++;
117 }
118 if ($flag === true)
119 return $elements[$index - 1];
120 } elseif ($elements instanceof BaseHtml) {
121 if ($callback($elements))
122 return $elements;
123 }
124 return null;
125 }
126
127 protected function setWrapBefore($wrapBefore) {
128 $this->_wrapBefore = $wrapBefore;
129 return $this;
130 }
131
132 protected function setWrapAfter($wrapAfter) {
133 $this->_wrapAfter = $wrapAfter;
134 return $this;
135 }
136
137 public function getTagName() {
138 return $this->tagName;
139 }
140
141 public function setTagName($tagName) {
142 $this->tagName = $tagName;
143 return $this;
144 }
145
146 public function fromArray($array) {
147 foreach ($this as $key => $value) {
148 if (array_key_exists($key, $array) === true)
149 $this->_callSetter("set" . ucfirst($key), $key, $array[$key], $array);
150 }
151 foreach ($array as $key => $value) {
152 if ($this->_callSetter($key, $key, $value, $array) === false) {
153 $this->_callSetter("set" . ucfirst($key), $key, $value, $array);
154 }
155 }
156 return $array;
157 }
158
159 public function fromDatabaseObjects($objects, $function) {
160 if (isset($objects)) {
161 foreach ($objects as $object) {
162 $this->fromDatabaseObject($object, $function);
163 }
164 }
165 return $this;
166 }
167
168 public function fromDatabaseObject($object, $function) {}
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 public function getElementById($identifier, $elements) {
179 return $this->_getElementBy(function (BaseWidget $element) use ($identifier) {
180 return $element->getIdentifier() === $identifier;
181 }, $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 $this->callCallback($this->_preCompile);
202 unset($this->properties["jsCallback"]);
203 $this->_compiled = true;
204 }
205 }
206
207 public function compile(JsUtils $js = NULL, &$view = NULL) {
208 $this->compile_once($js, $view);
209 $result = $this->getTemplate($js, $view);
210 foreach ($this as $key => $value) {
211 if (\strstr($result, "%{$key}%") !== false) {
212 if (\is_array($value)) {
213 $v = PropertyWrapper::wrap($value, $js, $view);
214 } elseif ($value instanceof \stdClass) {
215 $v = \print_r($value, true);
216 } elseif ($value instanceof BaseHtml) {
217 $v = $value->compile($js, $view);
218 } else {
219 $v = $value;
220 }
221 $result = \str_replace("%{$key}%", $v ?? '', $result);
222 }
223 }
224 if (isset($js) === true) {
225 $this->run($js);
226 if (isset($view) === true) {
227 $js->addViewElement($this->getLibraryId(), $result, $view);
228 }
229 }
230
231 if (\is_callable($this->_postCompile)) {
233 $pc($this);
234 }
235 return $result;
236 }
237
249 public function setDraggable($attr = "id", $dropZone = null, $parameters = []) {
250 $this->setProperty("draggable", "true");
251 $this->addEvent("dragstart", Javascript::draggable($attr));
252 if (isset($dropZone) && $dropZone instanceof BaseHtml) {
253 $jqueryDone = "append";
254 $jsCallback = "";
255 extract($parameters);
256 $dropZone->asDropZone($jsCallback, $jqueryDone, $parameters);
257 }
258 return $this;
259 }
260
269 public function asDropZone($jsCallback = "", $jqueryDone = "append", $parameters = []) {
270 $stopPropagation = false;
271 $this->addEvent("dragover", '', $stopPropagation, true);
272 extract($parameters);
273 $this->addEvent("drop", Javascript::dropZone($jqueryDone, $jsCallback), $stopPropagation, true);
274 return $this;
275 }
276
287 public function asFileDropZone($responseElement = null, $url = null, $progress = null, $jsCallback = "", $parameters = []) {
288 $stopPropagation = false;
289 $defaultAjaxAttributes = [
290 'contentType' => 'false',
291 'processData' => 'false'
292 ];
293 $this->addEvent("dragover", '', $stopPropagation, true);
294 extract($parameters);
295 $this->addEvent("drop", Javascript::fileDropZone($jsCallback), $stopPropagation, true);
296 if (isset($ajaxAttributes)) {
297 $ajaxAttributes += $defaultAjaxAttributes;
298 } else {
299 $ajaxAttributes = $defaultAjaxAttributes;
300 }
301 if (isset($progress)) {
302 $progress = new HtmlProgress($this->_identifier . '-pg', 0, $progress);
303 $progress->setTotal(100);
304 $this->wrap('', $progress);
305 $ajaxAttributes['upload'] = "$('#" . $this->_identifier . "-pg').progress('set percent', Math.ceil(event.loaded/event.total)*100);";
306 }
307 if (isset($url)) {
308 $this->postOn('upload', $url, 'event.target.upload', $responseElement, $ajaxAttributes);
309 }
310 return $progress ?? $this;
311 }
312
313 public function __toString() {
314 return $this->compile();
315 }
316
317 public function onPostCompile($callback) {
318 $this->_postCompile = $callback;
319 }
320
321 public function onPreCompile($callback) {
322 $this->_preCompile = $this->addCallback($this->_preCompile, $callback);
323 }
324
325 private function addCallback($originalValue, $callback) {
326 if (isset($originalValue)) {
327 if (! is_array($originalValue)) {
328 $result = [
329 $originalValue
330 ];
331 }
332 $result[] = $callback;
333 return $result;
334 }
335 return $callback;
336 }
337
338 private function callCallback($callable) {
339 if (\is_callable($callable)) {
340 return $callable($this);
341 }
342 if (is_array($callable)) {
343 foreach ($callable as $call) {
344 $this->callCallback($call);
345 }
346 }
347 }
348}
JQuery PHP library.
Definition JsUtils.php:23
BaseHtml for HTML components.
Definition BaseHtml.php:17
removeOldValues(&$oldValue, $allValues)
Definition BaseHtml.php:103
fromDatabaseObjects($objects, $function)
Definition BaseHtml.php:159
_getElementBy($callback, $elements)
Definition BaseHtml.php:108
asFileDropZone($responseElement=null, $url=null, $progress=null, $jsCallback="", $parameters=[])
Declares the element as a drop zone for file uploading (HTML5 drag and drop)
Definition BaseHtml.php:287
_callSetter($setter, $key, $value, &$array)
Definition BaseHtml.php:44
addToMember(&$name, $value, $separator=' ')
Definition BaseHtml.php:98
setMemberCtrl(&$name, $value, $typeCtrl)
Definition BaseHtml.php:75
setBsComponent($bsComponent)
Definition BaseHtml.php:188
wrap($before, $after="")
Definition BaseHtml.php:170
setDraggable($attr="id", $dropZone=null, $parameters=[])
Sets the element draggable, and eventualy defines the dropzone (HTML5 drag and drop)
Definition BaseHtml.php:249
setWrapBefore($wrapBefore)
Definition BaseHtml.php:127
addCallback($originalValue, $callback)
Definition BaseHtml.php:325
getElementById($identifier, $elements)
Definition BaseHtml.php:178
asDropZone($jsCallback="", $jqueryDone="append", $parameters=[])
Declares the element as a drop zone (HTML5 drag and drop)
Definition BaseHtml.php:269
compile(JsUtils $js=NULL, &$view=NULL)
Definition BaseHtml.php:207
getTemplate(JsUtils $js=NULL, $view=null)
Definition BaseHtml.php:58
addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ")
Definition BaseHtml.php:81
compile_once(JsUtils $js=NULL, &$view=NULL)
Definition BaseHtml.php:193
ctrl($name, $value, $typeCtrl)
Definition BaseHtml.php:62
addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ")
Definition BaseHtml.php:89
fromDatabaseObject($object, $function)
Definition BaseHtml.php:168
BaseWidget for Twitter Bootstrap, jQuery UI or Semantic rich components.
static wrap($input, $js=NULL, $view=null, $separator=' ', $valueQuote='"')
postOn($event, $url, $params="{}", $responseElement="", $parameters=array())
Performs a post to $url on the event $event on $element and display it in $responseElement.
addEvent($event, $jsCode, $stopPropagation=false, $preventDefault=false)
static draggable($attr="id")
static dropZone($jqueryDone, $jsCallback="")
static fileDropZone($jsCallback="")