Completed
Push — master ( 686575...778966 )
by Jean-Christophe
03:10
created

Widget::addEditButtonInToolbar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
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 addMessage($attributes=NULL,$fieldName="message"){
126
		$this->_instanceViewer->addField($fieldName);
127
		$count=$this->_instanceViewer->visiblePropertiesCount();
128
		return $this->fieldAsMessage($count-1,$attributes);
129
	}
130
131
	public function addErrorMessage(){
132
		return $this->addMessage(["error"=>true],"message");
133
	}
134
135
	public function insertField($index,$field){
136
		$this->_instanceViewer->insertField($index, $field);
137
		return $this;
138
	}
139
140
	public function insertInField($index,$field){
141
		$this->_instanceViewer->insertInField($index, $field);
142
		return $this;
143
	}
144
145
	public function setValueFunction($index,$callback){
146
		$this->_instanceViewer->setValueFunction($index, $callback);
147
		return $this;
148
	}
149
150
	public function setIdentifierFunction($callback){
151
		$this->_instanceViewer->setIdentifierFunction($callback);
152
		return $this;
153
	}
154
155
	/**
156
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
157
	 */
158
	public function getToolbar(){
159
		if(isset($this->_toolbar)===false){
160
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
161
		}
162
		return $this->_toolbar;
163
	}
164
165
	/**
166
	 * Adds a new element in toolbar
167
	 * @param mixed $element
168
	 * @param callable $callback function to call on $element
169
	 * @return \Ajax\common\html\HtmlDoubleElement
170
	 */
171
	public function addInToolbar($element,$callback=NULL){
172
		$tb=$this->getToolbar();
173
		if(isset($callback)){
174
			if(\is_callable($callback)){
175
				$callback($element);
176
			}
177
		}
178
		return $tb->addItem($element);
179
	}
180
181
	/**
182
	 * @param string $caption
183
	 * @param string $icon
184
	 * @param callable $callback function($element)
185
	 * @return \Ajax\common\html\HtmlDoubleElement
186
	 */
187
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
188
		$result=$this->addInToolbar($caption,$callback);
189
		if(isset($icon))
190
			$result->addIcon($icon);
191
		return $result;
192
	}
193
194
	/**
195
	 * @param array $items
196
	 * @param callable $callback function($element)
197
	 * @return \Ajax\common\Widget
198
	 */
199
	public function addItemsInToolbar(array $items,$callback=NULL){
200
		if(JArray::isAssociative($items)){
201
			foreach ($items as $icon=>$item){
202
				$this->addItemInToolbar($item,$icon,$callback);
203
			}
204
		}else{
205
			foreach ($items as $item){
206
				$this->addItemInToolbar($item,null,$callback);
207
			}
208
		}
209
		return $this;
210
	}
211
212
	/**
213
	 * @param string $value
214
	 * @param array $items
215
	 * @param callable $callback function($element)
216
	 * @return \Ajax\common\html\HtmlDoubleElement
217
	 */
218 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...
219
		$dd=$value;
220
		if (\is_string($value)) {
221
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
222
		}
223
		return $this->addInToolbar($dd,$callback);
224
	}
225
226
	/**
227
	 * @param unknown $caption
228
	 * @param callable $callback function($element)
229
	 * @return \Ajax\common\html\HtmlDoubleElement
230
	 */
231
	public function addButtonInToolbar($caption,$callback=NULL){
232
		$bt=new HtmlButton("bt-".$caption,$caption);
233
		return $this->addInToolbar($bt,$callback);
234
	}
235
236
	/**
237
	 * @param array $captions
238
	 * @param boolean $asIcon
239
	 * @param callable $callback function($element)
240
	 * @return \Ajax\common\html\HtmlDoubleElement
241
	 */
242
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
243
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
244
		return $this->addInToolbar($bts,$callback);
245
	}
246
247
	/**
248
	 * @param string $caption
249
	 * @param string $icon
250
	 * @param boolean $before
251
	 * @param boolean $labeled
252
	 * @return \Ajax\common\html\HtmlDoubleElement
253
	 */
254
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
255
		$bt=new HtmlButton("",$caption);
256
		$bt->addIcon($icon,$before,$labeled);
257
		return $this->addInToolbar($bt);
258
	}
259
260
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL){
261
		$button=new HtmlButton($identifier,$value,$cssStyle);
262
		$this->_buttonAsSubmit($button,"click",$url,$responseElement);
263
		return $this->addInToolbar($button);
264
	}
265
266
	/**
267
	 * Defines a callback function to call for modifying captions
268
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
269
	 * @param callable $captionCallback
270
	 * @return Widget
271
	 */
272
	public function setCaptionCallback($captionCallback) {
273
		$this->_instanceViewer->setCaptionCallback($captionCallback);
274
		return $this;
275
	}
276
277
	/**
278
	 * Makes the input fields editable
279
	 * @param boolean $_edition
280
	 * @return \Ajax\common\Widget
281
	 */
282
	public function setEdition($_edition=true) {
283
		$this->_edition=$_edition;
284
		return $this;
285
	}
286
287
	/**
288
	 * Defines the default function which displays fields value
289
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
290
	 * @return \Ajax\common\Widget
291
	 */
292
	public function setDefaultValueFunction($defaultValueFunction){
293
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
294
		return $this;
295
	}
296
297
	/**
298
	 * @param string|boolean $disable
299
	 * @return string
300
	 */
301
	public function jsDisabled($disable=true){
302
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
303
	}
304
305
	/**
306
	 * @param string $caption
307
	 * @param callable $callback function($element)
308
	 * @return \Ajax\common\html\HtmlDoubleElement
309
	 */
310
	public function addEditButtonInToolbar($caption,$callback=NULL){
311
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
312
		$bt->setToggle();
313
		$bt->setActive($this->_edition);
314
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
315
		return $this->addInToolbar($bt,$callback);
316
	}
317
318
	public function setToolbar(HtmlMenu $_toolbar) {
319
		$this->_toolbar=$_toolbar;
320
		return $this;
321
	}
322
323
	public function setToolbarPosition($_toolbarPosition) {
324
		$this->_toolbarPosition=$_toolbarPosition;
325
		return $this;
326
	}
327
328
	public function getForm() {
329
		if(!isset($this->_form)){
330
			$this->_form=new HtmlForm("frm-".$this->identifier);
331
			$this->setEdition(true);
332
		}
333
		return $this->_form;
334
	}
335
336
	public function run(JsUtils $js=NULL){
337
		$result=parent::run($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 336 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...
338
		if(isset($this->_form)){
339
			$this->runForm($js);
340
		}
341
		return $result;
342
	}
343
344
	protected function runForm(JsUtils $js=NULL){
345
		$fields=$this->getContentInstances(HtmlFormField::class);
346
		foreach ($fields as $field){
347
			$this->_form->addField($field);
348
		}
349
		return $this->_form->run($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 344 can be null; however, Ajax\semantic\html\colle...ns\form\HtmlForm::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...
350
	}
351
352
	protected function _compileForm(){
353
		if(isset($this->_form)){
354
			$noValidate="";
355
			if(\sizeof($this->_form->getValidationParams())>0)
356
				$noValidate="novalidate";
357
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
358
		}
359
	}
360
361
	public function setValidationParams(array $_validationParams) {
362
		$this->getForm()->setValidationParams($_validationParams);
363
		return $this;
364
	}
365
}