Completed
Push — master ( 701cf4...686575 )
by Jean-Christophe
03:12
created

Widget::_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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
52
	public function __construct($identifier,$model,$modelInstance=NULL) {
53
		parent::__construct($identifier);
54
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
55
		$this->setModel($model);
56
		if(isset($modelInstance));
57
			$this->show($modelInstance);
58
	}
59
60
	protected function _init($instanceViewer,$contentKey,$content,$edition){
61
		$this->_instanceViewer=$instanceViewer;
62
		$this->content=[$contentKey=>$content];
63
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
64
		$this->_edition=$edition;
65
	}
66
67
	protected function _getFieldIdentifier($prefix,$name=""){
68
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
69
	}
70
71
	protected function _getFieldName($index){
72
		return $this->_instanceViewer->getFieldName($index);
73
	}
74
75
	protected function _getFieldCaption($index){
76
		return $this->_instanceViewer->getCaption($index);
77
	}
78
79
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
80
81
	public function show($modelInstance){
82
		$this->_modelInstance=$modelInstance;
83
	}
84
85
	public function getModel() {
86
		return $this->_model;
87
	}
88
89
	public function setModel($_model) {
90
		$this->_model=$_model;
91
		return $this;
92
	}
93
94
	public function getInstanceViewer() {
95
		return $this->_instanceViewer;
96
	}
97
98
	public function setInstanceViewer($_instanceViewer) {
99
		$this->_instanceViewer=$_instanceViewer;
100
		return $this;
101
	}
102
103
	abstract public function getHtmlComponent();
104
105
	public function setColor($color){
106
		return $this->getHtmlComponent()->setColor($color);
107
	}
108
109
110
	public function setCaptions($captions){
111
		$this->_instanceViewer->setCaptions($captions);
112
		return $this;
113
	}
114
115
	public function setFields($fields){
116
		$this->_instanceViewer->setVisibleProperties($fields);
117
		return $this;
118
	}
119
120
	public function addField($field){
121
		$this->_instanceViewer->addField($field);
122
		return $this;
123
	}
124
125
	public function insertField($index,$field){
126
		$this->_instanceViewer->insertField($index, $field);
127
		return $this;
128
	}
129
130
	public function insertInField($index,$field){
131
		$this->_instanceViewer->insertInField($index, $field);
132
		return $this;
133
	}
134
135
	public function setValueFunction($index,$callback){
136
		$this->_instanceViewer->setValueFunction($index, $callback);
137
		return $this;
138
	}
139
140
	public function setIdentifierFunction($callback){
141
		$this->_instanceViewer->setIdentifierFunction($callback);
142
		return $this;
143
	}
144
145
	/**
146
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
147
	 */
148
	public function getToolbar(){
149
		if(isset($this->_toolbar)===false){
150
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
151
		}
152
		return $this->_toolbar;
153
	}
154
155
	/**
156
	 * Adds a new element in toolbar
157
	 * @param mixed $element
158
	 * @param callable $callback function to call on $element
159
	 * @return \Ajax\common\html\HtmlDoubleElement
160
	 */
161
	public function addInToolbar($element,$callback=NULL){
162
		$tb=$this->getToolbar();
163
		if(isset($callback)){
164
			if(\is_callable($callback)){
165
				$callback($element);
166
			}
167
		}
168
		return $tb->addItem($element);
169
	}
170
171
	/**
172
	 * @param string $caption
173
	 * @param string $icon
174
	 * @param callable $callback function($element)
175
	 * @return \Ajax\common\html\HtmlDoubleElement
176
	 */
177
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
178
		$result=$this->addInToolbar($caption,$callback);
179
		if(isset($icon))
180
			$result->addIcon($icon);
181
		return $result;
182
	}
183
184
	/**
185
	 * @param array $items
186
	 * @param callable $callback function($element)
187
	 * @return \Ajax\common\Widget
188
	 */
189
	public function addItemsInToolbar(array $items,$callback=NULL){
190
		if(JArray::isAssociative($items)){
191
			foreach ($items as $icon=>$item){
192
				$this->addItemInToolbar($item,$icon,$callback);
193
			}
194
		}else{
195
			foreach ($items as $item){
196
				$this->addItemInToolbar($item,null,$callback);
197
			}
198
		}
199
		return $this;
200
	}
201
202
	/**
203
	 * @param string $value
204
	 * @param array $items
205
	 * @param callable $callback function($element)
206
	 * @return \Ajax\common\html\HtmlDoubleElement
207
	 */
208 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...
209
		$dd=$value;
210
		if (\is_string($value)) {
211
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
212
		}
213
		return $this->addInToolbar($dd,$callback);
214
	}
215
216
	/**
217
	 * @param unknown $caption
218
	 * @param callable $callback function($element)
219
	 * @return \Ajax\common\html\HtmlDoubleElement
220
	 */
221
	public function addButtonInToolbar($caption,$callback=NULL){
222
		$bt=new HtmlButton("bt-".$caption,$caption);
223
		return $this->addInToolbar($bt,$callback);
224
	}
225
226
	/**
227
	 * @param array $captions
228
	 * @param boolean $asIcon
229
	 * @param callable $callback function($element)
230
	 * @return \Ajax\common\html\HtmlDoubleElement
231
	 */
232
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
233
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
234
		return $this->addInToolbar($bts,$callback);
235
	}
236
237
	/**
238
	 * @param string $caption
239
	 * @param string $icon
240
	 * @param boolean $before
241
	 * @param boolean $labeled
242
	 * @return \Ajax\common\html\HtmlDoubleElement
243
	 */
244
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
245
		$bt=new HtmlButton("",$caption);
246
		$bt->addIcon($icon,$before,$labeled);
247
		return $this->addInToolbar($bt);
248
	}
249
250
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
251
		$button=new HtmlButton($identifier,$value,$cssStyle);
252
		$this->_buttonAsSubmit($button,"click",$url,$responseElement);
253
		return $this->addInToolbar($button);
254
	}
255
256
	/**
257
	 * Defines a callback function to call for modifying captions
258
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
259
	 * @param callable $captionCallback
260
	 * @return Widget
261
	 */
262
	public function setCaptionCallback($captionCallback) {
263
		$this->_instanceViewer->setCaptionCallback($captionCallback);
264
		return $this;
265
	}
266
267
	/**
268
	 * Makes the input fields editable
269
	 * @param boolean $_edition
270
	 * @return \Ajax\common\Widget
271
	 */
272
	public function setEdition($_edition=true) {
273
		$this->_edition=$_edition;
274
		return $this;
275
	}
276
277
	/**
278
	 * Defines the default function which displays fields value
279
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
280
	 * @return \Ajax\common\Widget
281
	 */
282
	public function setDefaultValueFunction($defaultValueFunction){
283
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
284
		return $this;
285
	}
286
287
	/**
288
	 * @param string|boolean $disable
289
	 * @return string
290
	 */
291
	public function jsDisabled($disable=true){
292
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
293
	}
294
295
	/**
296
	 * @param string $caption
297
	 * @param callable $callback function($element)
298
	 * @return \Ajax\common\html\HtmlDoubleElement
299
	 */
300
	public function addEditButtonInToolbar($caption,$callback=NULL){
301
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
302
		$bt->setToggle();
303
		$bt->setActive($this->_edition);
304
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
305
		return $this->addInToolbar($bt,$callback);
306
	}
307
308
	public function setToolbar(HtmlMenu $_toolbar) {
309
		$this->_toolbar=$_toolbar;
310
		return $this;
311
	}
312
313
	public function setToolbarPosition($_toolbarPosition) {
314
		$this->_toolbarPosition=$_toolbarPosition;
315
		return $this;
316
	}
317
318
	public function getForm() {
319
		if(!isset($this->_form)){
320
			$this->_form=new HtmlForm("frm-".$this->identifier);
321
			$this->setEdition(true);
322
		}
323
		return $this->_form;
324
	}
325
326
	public function run(JsUtils $js=NULL){
327
		$result=parent::run($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 326 can be null; however, Ajax\common\html\HtmlDoubleElement::run() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
328
		if(isset($this->_form)){
329
			$this->runForm($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 326 can be null; however, Ajax\common\Widget::runForm() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
330
		}
331
		return $result;
332
	}
333
334
	protected function runForm(JsUtils $js){
335
		$fields=$this->getContentInstances(HtmlFormField::class);
336
		foreach ($fields as $field){
337
			$this->_form->addField($field);
338
		}
339
		return $this->_form->run($js);
340
	}
341
342
	protected function _compileForm(JsUtils $js=NULL,&$view=NULL){
0 ignored issues
show
Unused Code introduced by
The parameter $js is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $view is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
343
		if(isset($this->_form)){
344
			$noValidate="";
345
			if(\sizeof($this->_form->getValidationParams())>0)
346
				$noValidate="novalidate";
347
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
348
		}
349
	}
350
351
	public function setValidationParams(array $_validationParams) {
352
		$this->getForm()->setValidationParams($_validationParams);
353
		return $this;
354
	}
355
}