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

Widget::asModal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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