Passed
Push — master ( 28f925...e24b4a )
by Jean-Christophe
02:14
created

Widget::getDefaultValueFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\common;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\widgets\datatable\PositionInTable;
8
use Ajax\semantic\html\collections\menus\HtmlMenu;
9
use Ajax\semantic\widgets\base\FieldAsTrait;
10
use Ajax\semantic\html\elements\HtmlButtonGroups;
11
use Ajax\semantic\widgets\base\InstanceViewer;
12
use Ajax\semantic\html\modules\HtmlDropdown;
13
use Ajax\service\JArray;
14
use Ajax\service\Javascript;
15
use Ajax\semantic\html\collections\form\HtmlForm;
16
use Ajax\JsUtils;
17
use Ajax\semantic\html\collections\form\HtmlFormField;
18
use Ajax\semantic\html\collections\form\traits\FormTrait;
19
use Ajax\common\html\BaseWidget;
20
use Ajax\semantic\html\modules\HtmlModal;
21
22
abstract class Widget extends HtmlDoubleElement {
23
	use FieldAsTrait,FormTrait;
24
25
	/**
26
	 * @var string classname
27
	 */
28
	protected $_model;
29
	protected $_modelInstance;
30
	/**
31
	 * @var InstanceViewer
32
	 */
33
	protected $_instanceViewer;
34
	/**
35
	 * @var HtmlMenu
36
	 */
37
	protected $_toolbar;
38
	/**
39
	 * @var string
40
	 */
41
	protected $_toolbarPosition;
42
43
	/**
44
	 * @var boolean
45
	 */
46
	protected $_edition;
47
48
	/**
49
	 * @var HtmlForm
50
	 */
51
	protected $_form;
52
53
	protected $_generated;
54
55
56
	public function __construct($identifier,$model,$modelInstance=NULL) {
57
		parent::__construct($identifier);
58
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
59
		$this->setModel($model);
60
		if(isset($modelInstance)){
61
			$this->show($modelInstance);
62
		}
63
		$this->_generated=false;
64
	}
65
66
	protected function _init($instanceViewer,$contentKey,$content,$edition){
67
		$this->_instanceViewer=$instanceViewer;
68
		$this->content=[$contentKey=>$content];
69
		$this->_self=$content;
70
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
71
		$this->_edition=$edition;
72
	}
73
74
	/**
75
	 * @param int|string $fieldName
76
	 * @return int|string|boolean
77
	 */
78
	protected function _getIndex($fieldName){
79
		$index=$fieldName;
80
		if(\is_string($fieldName)){
81
			$fields=$this->_instanceViewer->getVisibleProperties();
82
			$index=\array_search($fieldName, $fields);
83
		}
84
		return $index;
85
	}
86
87
	protected function _getFieldIdentifier($prefix,$name=""){
88
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
89
	}
90
91
	protected function _getFieldName($index){
92
		return $this->_instanceViewer->getFieldName($index);
93
	}
94
95
	protected function _getFieldCaption($index){
96
		return $this->_instanceViewer->getCaption($index);
97
	}
98
99
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
100
101
	public function show($modelInstance){
102
		if(\is_array($modelInstance)){
103
			$modelInstance=\json_decode(\json_encode($modelInstance), FALSE);
104
		}
105
		$this->_modelInstance=$modelInstance;
106
	}
107
108
	public function getModel() {
109
		return $this->_model;
110
	}
111
112
	public function setModel($_model) {
113
		$this->_model=$_model;
114
		return $this;
115
	}
116
117
	public function getInstanceViewer() {
118
		return $this->_instanceViewer;
119
	}
120
121
	public function setInstanceViewer($_instanceViewer) {
122
		$this->_instanceViewer=$_instanceViewer;
123
		return $this;
124
	}
125
126
	abstract public function getHtmlComponent();
127
128
	public function setAttached($value=true){
129
		return $this->getHtmlComponent()->setAttached($value);
130
	}
131
132
	/**
133
	 * Associates a $callback function after the compilation of the field at $index position
134
	 * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
135
	 * @param int $index postion of the compiled field
136
	 * @param callable $callback function called after the field compilation
137
	 * @return Widget
138
	 */
139
	public function afterCompile($index,$callback){
140
		$index=$this->_getIndex($index);
141
		$this->_instanceViewer->afterCompile($index, $callback);
142
		return $this;
143
	}
144
145
	public function setColor($color){
146
		return $this->getHtmlComponent()->setColor($color);
147
	}
148
149
150
	public function setCaptions($captions){
151
		$this->_instanceViewer->setCaptions($captions);
152
		return $this;
153
	}
154
155
	public function setCaption($index,$caption){
156
		$this->_instanceViewer->setCaption($this->_getIndex($index), $caption);
157
		return $this;
158
	}
159
160
	public function setFields($fields){
161
		$this->_instanceViewer->setVisibleProperties($fields);
162
		return $this;
163
	}
164
165
	public function addField($field){
166
		$this->_instanceViewer->addField($field);
167
		return $this;
168
	}
169
170
	public function addFields($fields){
171
		$this->_instanceViewer->addFields($fields);
172
		return $this;
173
	}
174
175
	public function addMessage($attributes=NULL,$fieldName="message"){
176
		$this->_instanceViewer->addField($fieldName);
177
		$count=$this->_instanceViewer->visiblePropertiesCount();
178
		return $this->fieldAsMessage($count-1,$attributes);
179
	}
180
181
	public function addErrorMessage(){
182
		return $this->addMessage(["error"=>true],"message");
183
	}
184
185
	public function insertField($index,$field){
186
		$index=$this->_getIndex($index);
187
		$this->_instanceViewer->insertField($index, $field);
188
		return $this;
189
	}
190
191
	public function insertInField($index,$field){
192
		$index=$this->_getIndex($index);
193
		$this->_instanceViewer->insertInField($index, $field);
194
		return $this;
195
	}
196
197
	/**
198
	 * Defines the function which displays the field value
199
	 * @param int|string $index index or name of the field to display
200
	 * @param callable $callback function parameters are : $value : the field value, $instance : the active instance of model, $fieldIndex : the field index, $rowIndex : the row index
201
	 * @return Widget
202
	 */
203
	public function setValueFunction($index,$callback){
204
		$index=$this->_getIndex($index);
205
		$this->_instanceViewer->setValueFunction($index, $callback);
206
		return $this;
207
	}
208
209
	public function setIdentifierFunction($callback){
210
		$this->_instanceViewer->setIdentifierFunction($callback);
211
		return $this;
212
	}
213
214
	/**
215
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
216
	 */
217
	public function getToolbar(){
218
		if(isset($this->_toolbar)===false){
219
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
220
		}
221
		return $this->_toolbar;
222
	}
223
224
	/**
225
	 * Adds a new element in toolbar
226
	 * @param mixed $element
227
	 * @param callable $callback function to call on $element
228
	 * @return \Ajax\common\html\HtmlDoubleElement
229
	 */
230
	public function addInToolbar($element,$callback=NULL){
231
		$tb=$this->getToolbar();
232
		if($element instanceof BaseWidget){
233
			if($element->getIdentifier()===""){
234
				$element->setIdentifier("tb-item-".$this->identifier."-".$tb->count());
235
			}
236
		}
237
		if(isset($callback)){
238
			if(\is_callable($callback)){
239
				$callback($element);
240
			}
241
		}
242
		return $tb->addItem($element);
243
	}
244
245
	/**
246
	 * @param string $caption
247
	 * @param string $icon
248
	 * @param callable $callback function($element)
249
	 * @return \Ajax\common\html\HtmlDoubleElement
250
	 */
251
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
252
		$result=$this->addInToolbar($caption,$callback);
253
		if(isset($icon) && method_exists($result, "addIcon"))
254
			$result->addIcon($icon);
255
		return $result;
256
	}
257
258
	/**
259
	 * @param array $items
260
	 * @param callable $callback function($element)
261
	 * @return \Ajax\common\Widget
262
	 */
263
	public function addItemsInToolbar(array $items,$callback=NULL){
264
		if(JArray::isAssociative($items)){
265
			foreach ($items as $icon=>$item){
266
				$this->addItemInToolbar($item,$icon,$callback);
267
			}
268
		}else{
269
			foreach ($items as $item){
270
				$this->addItemInToolbar($item,null,$callback);
271
			}
272
		}
273
		return $this;
274
	}
275
276
	/**
277
	 * @param mixed $value
278
	 * @param array $items
279
	 * @param callable $callback function($element)
280
	 * @return \Ajax\common\html\HtmlDoubleElement
281
	 */
282
	public function addDropdownInToolbar($value,$items,$callback=NULL){
283
		$dd=$value;
284
		if (\is_string($value)) {
285
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
286
		}
287
		return $this->addInToolbar($dd,$callback);
288
	}
289
290
	/**
291
	 * @param string $caption
292
	 * @param string $cssStyle
293
	 * @param callable $callback function($element)
294
	 * @return \Ajax\common\html\HtmlDoubleElement
295
	 */
296
	public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){
297
		$bt=new HtmlButton("bt-".$caption,$caption,$cssStyle);
298
		return $this->addInToolbar($bt,$callback);
299
	}
300
301
	/**
302
	 * @param array $captions
303
	 * @param boolean $asIcon
304
	 * @param callable $callback function($element)
305
	 * @return \Ajax\common\html\HtmlDoubleElement
306
	 */
307
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
308
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
309
		return $this->addInToolbar($bts,$callback);
310
	}
311
312
	/**
313
	 * @param string $caption
314
	 * @param string $icon
315
	 * @param boolean $before
316
	 * @param boolean $labeled
317
	 * @return \Ajax\common\html\HtmlDoubleElement
318
	 */
319
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
320
		$bt=new HtmlButton("",$caption);
321
		$bt->addIcon($icon,$before,$labeled);
322
		return $this->addInToolbar($bt);
323
	}
324
325
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
326
		$button=new HtmlButton($identifier,$value,$cssStyle);
327
		$this->_buttonAsSubmit($button,"click",$url,$responseElement,$parameters);
328
		return $this->addInToolbar($button);
329
	}
330
331
	/**
332
	 * Defines a callback function to call for modifying captions
333
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
334
	 * @param callable $captionCallback
335
	 * @return Widget
336
	 */
337
	public function setCaptionCallback($captionCallback) {
338
		$this->_instanceViewer->setCaptionCallback($captionCallback);
339
		return $this;
340
	}
341
342
	/**
343
	 * Makes the input fields editable
344
	 * @param boolean $_edition
345
	 * @return \Ajax\common\Widget
346
	 */
347
	public function setEdition($_edition=true) {
348
		$this->_edition=$_edition;
349
		return $this;
350
	}
351
352
	/**
353
	 * Defines the default function which displays fields value
354
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
355
	 * @return \Ajax\common\Widget
356
	 */
357
	public function setDefaultValueFunction($defaultValueFunction){
358
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
359
		return $this;
360
	}
361
	
362
	/**
363
	 * @return callable
364
	 */
365
	public function getDefaultValueFunction(){
366
		return $this->_instanceViewer->getDefaultValueFunction();
367
	}
368
369
	/**
370
	 * @param string|boolean $disable
371
	 * @return string
372
	 */
373
	public function jsDisabled($disable=true){
374
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
375
	}
376
377
	/**
378
	 * @param string $caption
379
	 * @param callable $callback function($element)
380
	 * @return \Ajax\common\html\HtmlDoubleElement
381
	 */
382
	public function addEditButtonInToolbar($caption,$callback=NULL){
383
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
384
		$bt->setToggle();
385
		$bt->setActive($this->_edition);
386
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
387
		return $this->addInToolbar($bt,$callback);
388
	}
389
390
	public function setToolbar(HtmlMenu $_toolbar) {
391
		$this->_toolbar=$_toolbar;
392
		return $this;
393
	}
394
395
	public function setToolbarPosition($_toolbarPosition) {
396
		$this->_toolbarPosition=$_toolbarPosition;
397
		return $this;
398
	}
399
400
	public function getForm() {
401
		if(!isset($this->_form)){
402
			$this->_form=new HtmlForm("frm-".$this->identifier);
403
			$this->setEdition(true);
404
		}
405
		return $this->_form;
406
	}
407
408
	public function run(JsUtils $js){
409
		parent::run($js);
410
		if(isset($this->_form)){
411
			$this->runForm($js);
412
		}
413
	}
414
415
	protected function runForm(JsUtils $js){
416
		$fields=$this->getContentInstances(HtmlFormField::class);
417
		foreach ($fields as $field){
418
			$this->_form->addField($field);
419
		}
420
		return $this->_form->run($js);
421
	}
422
423
	protected function _compileForm(){
424
		if(isset($this->_form)){
425
			$noValidate="";
426
			if(\sizeof($this->_form->getValidationParams())>0)
427
				$noValidate="novalidate";
428
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
429
		}
430
	}
431
432
	/**
433
	 * Sets the parameters for the Form validation (on, inline, delay...)
434
	 * @param array $_validationParams example : ["on"=>"blur","inline"=>true]
435
	 * @return Widget
436
	 * @see https://semantic-ui.com/behaviors/form.html#/settings
437
	 */
438
	public function setValidationParams(array $_validationParams) {
439
		$this->getForm()->setValidationParams($_validationParams);
440
		return $this;
441
	}
442
443
	public function moveFieldTo($from,$to){
444
		return $this->_instanceViewer->moveFieldTo($from, $to);
445
	}
446
447
	public function swapFields($index1,$index2){
448
		$index1=$this->_getIndex($index1);
449
		$index2=$this->_getIndex($index2);
450
		return $this->_instanceViewer->swapFields($index1, $index2);
451
	}
452
453
	public function removeField($index){
454
		$index=$this->_getIndex($index);
455
		$this->_instanceViewer->removeField($index);
456
		return $this;
457
	}
458
459
	public function asModal($header=null){
460
		$modal=new HtmlModal("modal-".$this->identifier,$header);
461
		$modal->setContent($this);
462
		if(isset($this->_form)){
463
			$this->_form->onSuccess($modal->jsHide());
464
		}
465
		return $modal;
466
	}
467
468
	public function addToProperty($name, $value, $separator=" ") {
469
		return $this->getHtmlComponent()->addToProperty($name,$value,$separator);
470
	}
471
}
472