Completed
Push — master ( f5f495...b1701b )
by Jean-Christophe
03:57
created

Widget::addToProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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 PositionInTable
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
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 addMessage($attributes=NULL,$fieldName="message"){
171
		$this->_instanceViewer->addField($fieldName);
172
		$count=$this->_instanceViewer->visiblePropertiesCount();
173
		return $this->fieldAsMessage($count-1,$attributes);
174
	}
175
176
	public function addErrorMessage(){
177
		return $this->addMessage(["error"=>true],"message");
178
	}
179
180
	public function insertField($index,$field){
181
		$index=$this->_getIndex($index);
182
		$this->_instanceViewer->insertField($index, $field);
183
		return $this;
184
	}
185
186
	public function insertInField($index,$field){
187
		$index=$this->_getIndex($index);
188
		$this->_instanceViewer->insertInField($index, $field);
189
		return $this;
190
	}
191
192
	public function setValueFunction($index,$callback){
193
		$index=$this->_getIndex($index);
194
		$this->_instanceViewer->setValueFunction($index, $callback);
195
		return $this;
196
	}
197
198
	public function setIdentifierFunction($callback){
199
		$this->_instanceViewer->setIdentifierFunction($callback);
200
		return $this;
201
	}
202
203
	/**
204
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
205
	 */
206
	public function getToolbar(){
207
		if(isset($this->_toolbar)===false){
208
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
209
		}
210
		return $this->_toolbar;
211
	}
212
213
	/**
214
	 * Adds a new element in toolbar
215
	 * @param mixed $element
216
	 * @param callable $callback function to call on $element
217
	 * @return \Ajax\common\html\HtmlDoubleElement
218
	 */
219
	public function addInToolbar($element,$callback=NULL){
220
		$tb=$this->getToolbar();
221 View Code Duplication
		if($element instanceof BaseWidget){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
222
			if($element->getIdentifier()===""){
223
				$element->setIdentifier("tb-item-".$this->identifier."-".$tb->count());
224
			}
225
		}
226
		if(isset($callback)){
227
			if(\is_callable($callback)){
228
				$callback($element);
229
			}
230
		}
231
		return $tb->addItem($element);
232
	}
233
234
	/**
235
	 * @param string $caption
236
	 * @param string $icon
237
	 * @param callable $callback function($element)
238
	 * @return \Ajax\common\html\HtmlDoubleElement
239
	 */
240
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
241
		$result=$this->addInToolbar($caption,$callback);
242
		if(isset($icon))
243
			$result->addIcon($icon);
244
		return $result;
245
	}
246
247
	/**
248
	 * @param array $items
249
	 * @param callable $callback function($element)
250
	 * @return \Ajax\common\Widget
251
	 */
252
	public function addItemsInToolbar(array $items,$callback=NULL){
253
		if(JArray::isAssociative($items)){
254
			foreach ($items as $icon=>$item){
255
				$this->addItemInToolbar($item,$icon,$callback);
256
			}
257
		}else{
258
			foreach ($items as $item){
259
				$this->addItemInToolbar($item,null,$callback);
260
			}
261
		}
262
		return $this;
263
	}
264
265
	/**
266
	 * @param string $value
267
	 * @param array $items
268
	 * @param callable $callback function($element)
269
	 * @return \Ajax\common\html\HtmlDoubleElement
270
	 */
271 View Code Duplication
	public function addDropdownInToolbar($value,$items,$callback=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
272
		$dd=$value;
273
		if (\is_string($value)) {
274
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
275
		}
276
		return $this->addInToolbar($dd,$callback);
277
	}
278
279
	/**
280
	 * @param string $caption
281
	 * @param string $cssStyle
282
	 * @param callable $callback function($element)
283
	 * @return \Ajax\common\html\HtmlDoubleElement
284
	 */
285
	public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){
286
		$bt=new HtmlButton("bt-".$caption,$caption,$cssStyle);
287
		return $this->addInToolbar($bt,$callback);
288
	}
289
290
	/**
291
	 * @param array $captions
292
	 * @param boolean $asIcon
293
	 * @param callable $callback function($element)
294
	 * @return \Ajax\common\html\HtmlDoubleElement
295
	 */
296
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
297
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
298
		return $this->addInToolbar($bts,$callback);
299
	}
300
301
	/**
302
	 * @param string $caption
303
	 * @param string $icon
304
	 * @param boolean $before
305
	 * @param boolean $labeled
306
	 * @return \Ajax\common\html\HtmlDoubleElement
307
	 */
308
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
309
		$bt=new HtmlButton("",$caption);
310
		$bt->addIcon($icon,$before,$labeled);
311
		return $this->addInToolbar($bt);
312
	}
313
314
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
315
		$button=new HtmlButton($identifier,$value,$cssStyle);
316
		$this->_buttonAsSubmit($button,"click",$url,$responseElement,$parameters);
317
		return $this->addInToolbar($button);
318
	}
319
320
	/**
321
	 * Defines a callback function to call for modifying captions
322
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
323
	 * @param callable $captionCallback
324
	 * @return Widget
325
	 */
326
	public function setCaptionCallback($captionCallback) {
327
		$this->_instanceViewer->setCaptionCallback($captionCallback);
328
		return $this;
329
	}
330
331
	/**
332
	 * Makes the input fields editable
333
	 * @param boolean $_edition
334
	 * @return \Ajax\common\Widget
335
	 */
336
	public function setEdition($_edition=true) {
337
		$this->_edition=$_edition;
338
		return $this;
339
	}
340
341
	/**
342
	 * Defines the default function which displays fields value
343
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
344
	 * @return \Ajax\common\Widget
345
	 */
346
	public function setDefaultValueFunction($defaultValueFunction){
347
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
348
		return $this;
349
	}
350
351
	/**
352
	 * @param string|boolean $disable
353
	 * @return string
354
	 */
355
	public function jsDisabled($disable=true){
356
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
357
	}
358
359
	/**
360
	 * @param string $caption
361
	 * @param callable $callback function($element)
362
	 * @return \Ajax\common\html\HtmlDoubleElement
363
	 */
364
	public function addEditButtonInToolbar($caption,$callback=NULL){
365
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
366
		$bt->setToggle();
367
		$bt->setActive($this->_edition);
368
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
369
		return $this->addInToolbar($bt,$callback);
370
	}
371
372
	public function setToolbar(HtmlMenu $_toolbar) {
373
		$this->_toolbar=$_toolbar;
374
		return $this;
375
	}
376
377
	public function setToolbarPosition($_toolbarPosition) {
378
		$this->_toolbarPosition=$_toolbarPosition;
379
		return $this;
380
	}
381
382
	public function getForm() {
383
		if(!isset($this->_form)){
384
			$this->_form=new HtmlForm("frm-".$this->identifier);
385
			$this->setEdition(true);
386
		}
387
		return $this->_form;
388
	}
389
390
	public function run(JsUtils $js){
391
		$result=parent::run($js);
392
		if(isset($this->_form)){
393
			$result=$this->runForm($js);
394
		}
395
		return $result;
396
	}
397
398
	protected function runForm(JsUtils $js){
399
		$fields=$this->getContentInstances(HtmlFormField::class);
400
		foreach ($fields as $field){
401
			$this->_form->addField($field);
402
		}
403
		return $this->_form->run($js);
404
	}
405
406
	protected function _compileForm(){
407
		if(isset($this->_form)){
408
			$noValidate="";
409
			if(\sizeof($this->_form->getValidationParams())>0)
410
				$noValidate="novalidate";
411
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
412
		}
413
	}
414
415
	public function setValidationParams(array $_validationParams) {
416
		$this->getForm()->setValidationParams($_validationParams);
417
		return $this;
418
	}
419
420
	public function moveFieldTo($from,$to){
421
		return $this->_instanceViewer->moveFieldTo($from, $to);
422
	}
423
424
	public function swapFields($index1,$index2){
425
		$index1=$this->_getIndex($index1);
426
		$index2=$this->_getIndex($index2);
427
		return $this->_instanceViewer->swapFields($index1, $index2);
428
	}
429
430
	public function removeField($index){
431
		$index=$this->_getIndex($index);
432
		$this->_instanceViewer->removeField($index);
433
		return $this;
434
	}
435
436
	public function asModal($header=null){
437
		$modal=new HtmlModal("modal-".$this->identifier,$header);
438
		$modal->setContent($this);
439
		if(isset($this->_form)){
440
			$this->_form->onSuccess($modal->jsHide());
441
		}
442
		return $modal;
443
	}
444
445
	public function addToProperty($name, $value, $separator=" ") {
446
		return $this->getHtmlComponent()->addToProperty($name,$value,$separator);
447
	}
448
}