Completed
Push — master ( abc246...138cdf )
by Jean-Christophe
03:36
created

Widget::moveFieldTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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