phpMv -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
Loading...
Searching...
No Matches
Widget.php
Go to the documentation of this file.
1<?php
2namespace Ajax\common;
3
15use Ajax\JsUtils;
21
23
28 protected $_model;
29
30 protected $_modelInstance;
31
37
42 protected $_toolbar;
43
49
54 protected $_edition;
55
60 protected $_form;
61
62 protected $_generated;
63
64 protected $_runned;
65
66 protected $_hasRules;
67
68 public function __construct($identifier, $model, $modelInstance = NULL) {
69 parent::__construct($identifier);
70 $this->_template = "%wrapContentBefore%%content%%wrapContentAfter%";
71 $this->setModel($model);
72 if (isset($modelInstance)) {
73 if (\is_object($modelInstance)) {
74 $this->_model = \get_class($modelInstance);
75 }
76 $this->show($modelInstance);
77 }
78 $this->_generated = false;
79 }
80
81 protected function _init($instanceViewer, $contentKey, $content, $edition) {
82 $this->_instanceViewer = $instanceViewer;
83 $this->content = [
84 $contentKey => $content
85 ];
86 $this->_self = $content;
87 $this->_toolbarPosition = PositionInTable::BEFORETABLE;
88 $this->_edition = $edition;
89 }
90
96 protected function _getIndex($fieldName) {
97 $index = $fieldName;
98 if (\is_string($fieldName)) {
99 $fields = $this->_instanceViewer->getVisibleProperties();
100 $index = \array_search($fieldName, $fields);
101 }
102 return $index;
103 }
104
105 protected function _getFieldIdentifier($prefix, $name = "") {
106 return $this->identifier . "-{$prefix}-" . $this->_instanceViewer->getIdentifier();
107 }
108
109 protected function _getFieldName($index) {
110 return $this->_instanceViewer->getFieldName($index);
111 }
112
113 protected function _getFieldCaption($index) {
114 return $this->_instanceViewer->getCaption($index);
115 }
116
117 abstract protected function _setToolbarPosition($table, $captions = NULL);
118
119 public function show($modelInstance) {
120 if (\is_array($modelInstance)) {
121 $modelInstance = \json_decode(\json_encode($modelInstance), FALSE);
122 }
123 $this->_modelInstance = $modelInstance;
124 }
125
126 public function getModel() {
127 return $this->_model;
128 }
129
130 public function setModel($_model) {
131 $this->_model = $_model;
132 return $this;
133 }
134
135 public function getInstanceViewer() {
137 }
138
140 $this->_instanceViewer = $_instanceViewer;
141 return $this;
142 }
143
144 abstract public function getHtmlComponent();
145
146 public function setAttached($value = true) {
147 return $this->getHtmlComponent()->setAttached($value);
148 }
149
160 public function afterCompile($index, $callback) {
161 $index = $this->_getIndex($index);
162 if ($index !== false) {
163 $this->_instanceViewer->afterCompile($index, $callback);
164 }
165 return $this;
166 }
167
168 public function setColor($color) {
169 return $this->getHtmlComponent()->setColor($color);
170 }
171
172 public function setCaptions($captions) {
173 $this->_instanceViewer->setCaptions($captions);
174 return $this;
175 }
176
177 public function setCaption($index, $caption) {
178 $this->_instanceViewer->setCaption($this->_getIndex($index), $caption);
179 return $this;
180 }
181
182 public function setFields($fields) {
183 $this->_instanceViewer->setVisibleProperties($fields);
184 return $this;
185 }
186
187 public function addField($field, $key = null) {
188 $this->_instanceViewer->addField($field, $key);
189 return $this;
190 }
191
192 public function addFields($fields) {
193 $this->_instanceViewer->addFields($fields);
194 return $this;
195 }
196
197 public function countFields() {
198 return $this->_instanceViewer->visiblePropertiesCount();
199 }
200
201 public function addMessage($attributes = NULL, $fieldName = "message") {
202 $this->_instanceViewer->addField($fieldName);
203 $count = $this->_instanceViewer->visiblePropertiesCount();
204 return $this->fieldAsMessage($count - 1, $attributes);
205 }
206
207 public function addErrorMessage() {
208 return $this->addMessage([
209 "error" => true
210 ], "message");
211 }
212
213 public function insertField($index, $field, $key = null) {
214 $index = $this->_getIndex($index);
215 $this->_instanceViewer->insertField($index, $field, $key);
216 return $this;
217 }
218
219 public function insertInField($index, $field, $key = null) {
220 $index = $this->_getIndex($index);
221 if ($index !== false) {
222 $this->_instanceViewer->insertInField($index, $field, $key);
223 }
224 return $this;
225 }
226
236 public function setValueFunction($index, $callback) {
237 $index = $this->_getIndex($index);
238 if ($index !== false) {
239 $this->_instanceViewer->setValueFunction($index, $callback);
240 }
241 return $this;
242 }
243
244 public function setIdentifierFunction($callback) {
245 $this->_instanceViewer->setIdentifierFunction($callback);
246 return $this;
247 }
248
253 public function getToolbar() {
254 if (isset($this->_toolbar) === false) {
255 $this->_toolbar = new HtmlMenu("toolbar-" . $this->identifier);
256 }
257 return $this->_toolbar;
258 }
259
268 public function addInToolbar($element, $callback = NULL) {
269 $tb = $this->getToolbar();
270 if ($element instanceof BaseWidget) {
271 if ($element->getIdentifier() === "") {
272 $element->setIdentifier("tb-item-" . $this->identifier . "-" . $tb->count());
273 }
274 }
275 if (isset($callback)) {
276 if (\is_callable($callback)) {
277 $callback($element);
278 }
279 }
280 return $tb->addItem($element);
281 }
282
291 public function addItemInToolbar($caption, $icon = NULL, $callback = NULL) {
292 $result = $this->addInToolbar($caption, $callback);
293 if (isset($icon) && method_exists($result, "addIcon"))
294 $result->addIcon($icon);
295 return $result;
296 }
297
305 public function addItemsInToolbar(array $items, $callback = NULL) {
306 if (JArray::isAssociative($items)) {
307 foreach ($items as $icon => $item) {
308 $this->addItemInToolbar($item, $icon, $callback);
309 }
310 } else {
311 foreach ($items as $item) {
312 $this->addItemInToolbar($item, null, $callback);
313 }
314 }
315 return $this;
316 }
317
326 public function addDropdownInToolbar($value, $items, $callback = NULL) {
327 $dd = $value;
328 if (\is_string($value)) {
329 $dd = new HtmlDropdown("dropdown-" . $this->identifier . "-" . $value, $value, $items);
330 }
331 return $this->addInToolbar($dd, $callback);
332 }
333
342 public function addButtonInToolbar($caption, $cssStyle = null, $callback = NULL) {
343 $bt = new HtmlButton("bt-" . $caption, $caption, $cssStyle);
344 return $this->addInToolbar($bt, $callback);
345 }
346
355 public function addButtonsInToolbar(array $captions, $asIcon = false, $callback = NULL) {
356 $bts = new HtmlButtonGroups("", $captions, $asIcon);
357 return $this->addInToolbar($bts, $callback);
358 }
359
368 public function addLabelledIconButtonInToolbar($caption, $icon, $before = true, $labeled = false) {
369 $bt = new HtmlButton("", $caption);
370 $bt->addIcon($icon, $before, $labeled);
371 return $this->addInToolbar($bt);
372 }
373
374 public function addSubmitInToolbar($identifier, $value, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $parameters = NULL) {
375 $button = new HtmlButton($identifier, $value, $cssStyle);
376 $this->_buttonAsSubmit($button, "click", $url, $responseElement, $parameters);
377 return $this->addInToolbar($button);
378 }
379
387 public function setCaptionCallback($captionCallback) {
388 $this->_instanceViewer->setCaptionCallback($captionCallback);
389 return $this;
390 }
391
398 public function setEdition($_edition = true) {
399 $this->_edition = $_edition;
400 return $this;
401 }
402
410 public function setDefaultValueFunction($defaultValueFunction) {
411 $this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
412 return $this;
413 }
414
419 public function getDefaultValueFunction() {
420 return $this->_instanceViewer->getDefaultValueFunction();
421 }
422
428 public function jsDisabled($disable = true) {
429 return "$('#" . $this->identifier . " .ui.input,#" . $this->identifier . " .ui.dropdown,#" . $this->identifier . " .ui.checkbox').toggleClass('disabled'," . $disable . ");";
430 }
431
439 public function addEditButtonInToolbar($caption, $callback = NULL) {
440 $bt = new HtmlButton($this->identifier . "-editBtn", $caption);
441 $bt->setToggle();
442 $bt->setActive($this->_edition);
443 $bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
444 return $this->addInToolbar($bt, $callback);
445 }
446
447 public function setToolbar(HtmlMenu $_toolbar) {
448 $this->_toolbar = $_toolbar;
449 return $this;
450 }
451
453 $this->_toolbarPosition = $_toolbarPosition;
454 return $this;
455 }
456
457 public function getForm() {
458 if (! isset($this->_form)) {
459 $this->_form = new HtmlForm("frm-" . $this->identifier);
460 $this->setEdition(true);
461 }
462 return $this->_form;
463 }
464
465 public function run(JsUtils $js) {
466 parent::run($js);
467 if (isset($this->_form)) {
468 $this->runForm($js);
469 }
470 }
471
472 protected function runForm(JsUtils $js) {
473 $fields = $this->getContentInstances(HtmlFormField::class);
474 foreach ($fields as $field) {
475 $this->_form->addField($field);
476 }
477 return $this->_form->run($js);
478 }
479
480 protected function _compileForm() {
481 if (isset($this->_form)) {
482 $noValidate = "";
483 if (\sizeof($this->_form->getValidationParams()) > 0)
484 $noValidate = "novalidate";
485 $this->wrapContent("<form class='" . $this->_form->getProperty('class') . "' id='frm-" . $this->identifier . "' name='frm-" . $this->identifier . "' " . $noValidate . ">", "</form>");
486 }
487 }
488
497 public function setValidationParams(array $_validationParams) {
498 $this->getForm()->setValidationParams($_validationParams);
499 return $this;
500 }
501
502 public function moveFieldTo($from, $to) {
503 return $this->_instanceViewer->moveFieldTo($from, $to);
504 }
505
506 public function swapFields($index1, $index2) {
507 $index1 = $this->_getIndex($index1);
508 $index2 = $this->_getIndex($index2);
509 return $this->_instanceViewer->swapFields($index1, $index2);
510 }
511
512 public function removeField($index) {
513 $index = $this->_getIndex($index);
514 if ($index !== false) {
515 $this->_instanceViewer->removeField($index);
516 }
517 return $this;
518 }
519
520 public function asModal($header = null) {
521 $modal = new HtmlModal("modal-" . $this->identifier, $header);
522 $modal->setContent($this);
523 if (isset($this->_form)) {
524 $this->_form->onSuccess($modal->jsHide());
525 }
526 return $modal;
527 }
528
529 public function addToProperty($name, $value, $separator = " ") {
530 return $this->getHtmlComponent()->addToProperty($name, $value, $separator);
531 }
532
537 public function getModelInstance() {
539 }
540
545 public function hasRules() {
546 return $this->_hasRules;
547 }
548}
JQuery PHP library.
Definition JsUtils.php:23
jsDisabled($disable=true)
Definition Widget.php:428
setAttached($value=true)
Definition Widget.php:146
addInToolbar($element, $callback=NULL)
Adds a new element in toolbar.
Definition Widget.php:268
setModel($_model)
Definition Widget.php:130
run(JsUtils $js)
SimpleExtComponent\Ajax\common\html\BaseHtmlrun()\Ajax\common\html\BaseHtmlrun()
Definition Widget.php:465
setCaptions($captions)
Definition Widget.php:172
_getFieldName($index)
Definition Widget.php:109
setValueFunction($index, $callback)
Defines the function which displays the field value.
Definition Widget.php:236
setInstanceViewer($_instanceViewer)
Definition Widget.php:139
setCaption($index, $caption)
Definition Widget.php:177
__construct($identifier, $model, $modelInstance=NULL)
Definition Widget.php:68
addButtonInToolbar($caption, $cssStyle=null, $callback=NULL)
Definition Widget.php:342
addDropdownInToolbar($value, $items, $callback=NULL)
Definition Widget.php:326
setCaptionCallback($captionCallback)
Defines a callback function to call for modifying captions function parameters 0are.
Definition Widget.php:387
insertInField($index, $field, $key=null)
Definition Widget.php:219
afterCompile($index, $callback)
Associates a $callback function after the compilation of the field at $index position The $callback f...
Definition Widget.php:160
setEdition($_edition=true)
Makes the input fields editable.
Definition Widget.php:398
moveFieldTo($from, $to)
Definition Widget.php:502
addField($field, $key=null)
Definition Widget.php:187
_getFieldCaption($index)
Definition Widget.php:113
addEditButtonInToolbar($caption, $callback=NULL)
Definition Widget.php:439
setIdentifierFunction($callback)
Definition Widget.php:244
_setToolbarPosition($table, $captions=NULL)
setFields($fields)
Definition Widget.php:182
addToProperty($name, $value, $separator=" ")
Definition Widget.php:529
asModal($header=null)
Definition Widget.php:520
setValidationParams(array $_validationParams)
Sets the parameters for the Form validation (on, inline, delay...)
Definition Widget.php:497
addItemsInToolbar(array $items, $callback=NULL)
Definition Widget.php:305
addFields($fields)
Definition Widget.php:192
_init($instanceViewer, $contentKey, $content, $edition)
Definition Widget.php:81
setToolbar(HtmlMenu $_toolbar)
Definition Widget.php:447
insertField($index, $field, $key=null)
Definition Widget.php:213
addLabelledIconButtonInToolbar($caption, $icon, $before=true, $labeled=false)
Definition Widget.php:368
removeField($index)
Definition Widget.php:512
addItemInToolbar($caption, $icon=NULL, $callback=NULL)
Definition Widget.php:291
runForm(JsUtils $js)
Definition Widget.php:472
setDefaultValueFunction($defaultValueFunction)
Defines the default function which displays fields value.
Definition Widget.php:410
addButtonsInToolbar(array $captions, $asIcon=false, $callback=NULL)
Definition Widget.php:355
show($modelInstance)
Definition Widget.php:119
addSubmitInToolbar($identifier, $value, $cssStyle=NULL, $url=NULL, $responseElement=NULL, $parameters=NULL)
Definition Widget.php:374
_getFieldIdentifier($prefix, $name="")
Definition Widget.php:105
addMessage($attributes=NULL, $fieldName="message")
Definition Widget.php:201
swapFields($index1, $index2)
Definition Widget.php:506
setToolbarPosition($_toolbarPosition)
Definition Widget.php:452
_getIndex($fieldName)
Definition Widget.php:96
BaseWidget for Twitter Bootstrap, jQuery UI or Semantic rich components.
_buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement=NULL, $parameters=NULL)
Semantic UI Buttongroups component.
Semantic Button component.
fieldAsMessage($index, $attributes=NULL)