Completed
Push — master ( a4369d...ea5934 )
by Jean-Christophe
02:50
created

Widget::addItemInToolbar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
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 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
	public function setValueFunction($index,$callback){
198
		$index=$this->_getIndex($index);
199
		$this->_instanceViewer->setValueFunction($index, $callback);
200
		return $this;
201
	}
202
203
	public function setIdentifierFunction($callback){
204
		$this->_instanceViewer->setIdentifierFunction($callback);
205
		return $this;
206
	}
207
208
	/**
209
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
210
	 */
211
	public function getToolbar(){
212
		if(isset($this->_toolbar)===false){
213
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
214
		}
215
		return $this->_toolbar;
216
	}
217
218
	/**
219
	 * Adds a new element in toolbar
220
	 * @param mixed $element
221
	 * @param callable $callback function to call on $element
222
	 * @return \Ajax\common\html\HtmlDoubleElement
223
	 */
224
	public function addInToolbar($element,$callback=NULL){
225
		$tb=$this->getToolbar();
226 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...
227
			if($element->getIdentifier()===""){
228
				$element->setIdentifier("tb-item-".$this->identifier."-".$tb->count());
229
			}
230
		}
231
		if(isset($callback)){
232
			if(\is_callable($callback)){
233
				$callback($element);
234
			}
235
		}
236
		return $tb->addItem($element);
237
	}
238
239
	/**
240
	 * @param string $caption
241
	 * @param string $icon
242
	 * @param callable $callback function($element)
243
	 * @return \Ajax\common\html\HtmlDoubleElement
244
	 */
245
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
246
		$result=$this->addInToolbar($caption,$callback);
247
		if(isset($icon))
248
			$result->addIcon($icon);
249
		return $result;
250
	}
251
252
	/**
253
	 * @param array $items
254
	 * @param callable $callback function($element)
255
	 * @return \Ajax\common\Widget
256
	 */
257
	public function addItemsInToolbar(array $items,$callback=NULL){
258
		if(JArray::isAssociative($items)){
259
			foreach ($items as $icon=>$item){
260
				$this->addItemInToolbar($item,$icon,$callback);
261
			}
262
		}else{
263
			foreach ($items as $item){
264
				$this->addItemInToolbar($item,null,$callback);
265
			}
266
		}
267
		return $this;
268
	}
269
270
	/**
271
	 * @param string $value
272
	 * @param array $items
273
	 * @param callable $callback function($element)
274
	 * @return \Ajax\common\html\HtmlDoubleElement
275
	 */
276 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...
277
		$dd=$value;
278
		if (\is_string($value)) {
279
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
280
		}
281
		return $this->addInToolbar($dd,$callback);
282
	}
283
284
	/**
285
	 * @param string $caption
286
	 * @param string $cssStyle
287
	 * @param callable $callback function($element)
288
	 * @return \Ajax\common\html\HtmlDoubleElement
289
	 */
290
	public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){
291
		$bt=new HtmlButton("bt-".$caption,$caption,$cssStyle);
292
		return $this->addInToolbar($bt,$callback);
293
	}
294
295
	/**
296
	 * @param array $captions
297
	 * @param boolean $asIcon
298
	 * @param callable $callback function($element)
299
	 * @return \Ajax\common\html\HtmlDoubleElement
300
	 */
301
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
302
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
303
		return $this->addInToolbar($bts,$callback);
304
	}
305
306
	/**
307
	 * @param string $caption
308
	 * @param string $icon
309
	 * @param boolean $before
310
	 * @param boolean $labeled
311
	 * @return \Ajax\common\html\HtmlDoubleElement
312
	 */
313
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
314
		$bt=new HtmlButton("",$caption);
315
		$bt->addIcon($icon,$before,$labeled);
316
		return $this->addInToolbar($bt);
317
	}
318
319
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
320
		$button=new HtmlButton($identifier,$value,$cssStyle);
321
		$this->_buttonAsSubmit($button,"click",$url,$responseElement,$parameters);
322
		return $this->addInToolbar($button);
323
	}
324
325
	/**
326
	 * Defines a callback function to call for modifying captions
327
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
328
	 * @param callable $captionCallback
329
	 * @return Widget
330
	 */
331
	public function setCaptionCallback($captionCallback) {
332
		$this->_instanceViewer->setCaptionCallback($captionCallback);
333
		return $this;
334
	}
335
336
	/**
337
	 * Makes the input fields editable
338
	 * @param boolean $_edition
339
	 * @return \Ajax\common\Widget
340
	 */
341
	public function setEdition($_edition=true) {
342
		$this->_edition=$_edition;
343
		return $this;
344
	}
345
346
	/**
347
	 * Defines the default function which displays fields value
348
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
349
	 * @return \Ajax\common\Widget
350
	 */
351
	public function setDefaultValueFunction($defaultValueFunction){
352
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
353
		return $this;
354
	}
355
356
	/**
357
	 * @param string|boolean $disable
358
	 * @return string
359
	 */
360
	public function jsDisabled($disable=true){
361
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
362
	}
363
364
	/**
365
	 * @param string $caption
366
	 * @param callable $callback function($element)
367
	 * @return \Ajax\common\html\HtmlDoubleElement
368
	 */
369
	public function addEditButtonInToolbar($caption,$callback=NULL){
370
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
371
		$bt->setToggle();
372
		$bt->setActive($this->_edition);
373
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
374
		return $this->addInToolbar($bt,$callback);
375
	}
376
377
	public function setToolbar(HtmlMenu $_toolbar) {
378
		$this->_toolbar=$_toolbar;
379
		return $this;
380
	}
381
382
	public function setToolbarPosition($_toolbarPosition) {
383
		$this->_toolbarPosition=$_toolbarPosition;
384
		return $this;
385
	}
386
387
	public function getForm() {
388
		if(!isset($this->_form)){
389
			$this->_form=new HtmlForm("frm-".$this->identifier);
390
			$this->setEdition(true);
391
		}
392
		return $this->_form;
393
	}
394
395
	public function run(JsUtils $js){
396
		$result=parent::run($js);
397
		if(isset($this->_form)){
398
			$result=$this->runForm($js);
399
		}
400
		return $result;
401
	}
402
403
	protected function runForm(JsUtils $js){
404
		$fields=$this->getContentInstances(HtmlFormField::class);
405
		foreach ($fields as $field){
406
			$this->_form->addField($field);
407
		}
408
		return $this->_form->run($js);
409
	}
410
411
	protected function _compileForm(){
412
		if(isset($this->_form)){
413
			$noValidate="";
414
			if(\sizeof($this->_form->getValidationParams())>0)
415
				$noValidate="novalidate";
416
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
417
		}
418
	}
419
420
	public function setValidationParams(array $_validationParams) {
421
		$this->getForm()->setValidationParams($_validationParams);
422
		return $this;
423
	}
424
425
	public function moveFieldTo($from,$to){
426
		return $this->_instanceViewer->moveFieldTo($from, $to);
427
	}
428
429
	public function swapFields($index1,$index2){
430
		$index1=$this->_getIndex($index1);
431
		$index2=$this->_getIndex($index2);
432
		return $this->_instanceViewer->swapFields($index1, $index2);
433
	}
434
435
	public function removeField($index){
436
		$index=$this->_getIndex($index);
437
		$this->_instanceViewer->removeField($index);
438
		return $this;
439
	}
440
441
	public function asModal($header=null){
442
		$modal=new HtmlModal("modal-".$this->identifier,$header);
443
		$modal->setContent($this);
444
		if(isset($this->_form)){
445
			$this->_form->onSuccess($modal->jsHide());
446
		}
447
		return $modal;
448
	}
449
450
	public function addToProperty($name, $value, $separator=" ") {
451
		return $this->getHtmlComponent()->addToProperty($name,$value,$separator);
452
	}
453
}
454