Completed
Push — master ( 04d259...772d83 )
by Jean-Christophe
03:54
created

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