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