Completed
Push — master ( 847185...5c7ff3 )
by Jean-Christophe
03:16
created

Widget::setIdentifierFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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