Passed
Push — master ( ef609f...421c61 )
by Jean-Christophe
02:48
created

Widget::addErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
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
			$this->show($modelInstance);
62
		}
63
		$this->_generated=false;
64
	}
65
66
	protected function _init($instanceViewer,$contentKey,$content,$edition){
67
		$this->_instanceViewer=$instanceViewer;
68
		$this->content=[$contentKey=>$content];
69
		$this->_self=$content;
70
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
0 ignored issues
show
Documentation Bug introduced by
It seems like Ajax\semantic\widgets\da...ionInTable::BEFORETABLE of type string is incompatible with the declared type Ajax\semantic\widgets\datatable\PositionInTable of property $_toolbarPosition.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
		$this->_edition=$edition;
72
	}
73
74
	/**
75
	 * @param int|string $fieldName
76
	 * @return int|string
77
	 */
78
	protected function _getIndex($fieldName){
79
		$index=$fieldName;
80
		if(\is_string($fieldName)){
81
			$fields=$this->_instanceViewer->getVisibleProperties();
82
			$index=\array_search($fieldName, $fields);
83
		}
84
		return $index;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $index could also return false which is incompatible with the documented return type integer|string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
85
	}
86
87
	protected function _getFieldIdentifier($prefix,$name=""){
88
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
89
	}
90
91
	protected function _getFieldName($index){
92
		return $this->_instanceViewer->getFieldName($index);
93
	}
94
95
	protected function _getFieldCaption($index){
96
		return $this->_instanceViewer->getCaption($index);
97
	}
98
99
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
100
101
	public function show($modelInstance){
102
		if(\is_array($modelInstance)){
103
			$modelInstance=\json_decode(\json_encode($modelInstance), FALSE);
104
		}
105
		$this->_modelInstance=$modelInstance;
106
	}
107
108
	public function getModel() {
109
		return $this->_model;
110
	}
111
112
	public function setModel($_model) {
113
		$this->_model=$_model;
114
		return $this;
115
	}
116
117
	public function getInstanceViewer() {
118
		return $this->_instanceViewer;
119
	}
120
121
	public function setInstanceViewer($_instanceViewer) {
122
		$this->_instanceViewer=$_instanceViewer;
123
		return $this;
124
	}
125
126
	abstract public function getHtmlComponent();
127
128
	public function setAttached($value=true){
129
		return $this->getHtmlComponent()->setAttached($value);
130
	}
131
132
	/**
133
	 * Associates a $callback function after the compilation of the field at $index position
134
	 * The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
135
	 * @param int $index postion of the compiled field
136
	 * @param callable $callback function called after the field compilation
137
	 * @return Widget
138
	 */
139
	public function afterCompile($index,$callback){
140
		$index=$this->_getIndex($index);
141
		$this->_instanceViewer->afterCompile($index, $callback);
142
		return $this;
143
	}
144
145
	public function setColor($color){
146
		return $this->getHtmlComponent()->setColor($color);
147
	}
148
149
150
	public function setCaptions($captions){
151
		$this->_instanceViewer->setCaptions($captions);
152
		return $this;
153
	}
154
155
	public function setCaption($index,$caption){
156
		$this->_instanceViewer->setCaption($this->_getIndex($index), $caption);
157
		return $this;
158
	}
159
160
	public function setFields($fields){
161
		$this->_instanceViewer->setVisibleProperties($fields);
162
		return $this;
163
	}
164
165
	public function addField($field){
166
		$this->_instanceViewer->addField($field);
167
		return $this;
168
	}
169
170
	public function addFields($fields){
171
		$this->_instanceViewer->addFields($fields);
172
		return $this;
173
	}
174
175
	public function addMessage($attributes=NULL,$fieldName="message"){
176
		$this->_instanceViewer->addField($fieldName);
177
		$count=$this->_instanceViewer->visiblePropertiesCount();
178
		return $this->fieldAsMessage($count-1,$attributes);
179
	}
180
181
	public function addErrorMessage(){
182
		return $this->addMessage(["error"=>true],"message");
183
	}
184
185
	public function insertField($index,$field){
186
		$index=$this->_getIndex($index);
187
		$this->_instanceViewer->insertField($index, $field);
188
		return $this;
189
	}
190
191
	public function insertInField($index,$field){
192
		$index=$this->_getIndex($index);
193
		$this->_instanceViewer->insertInField($index, $field);
194
		return $this;
195
	}
196
197
	/**
198
	 * Defines the function which displays the field value
199
	 * @param $index index or name of the field to display
0 ignored issues
show
Bug introduced by
The type Ajax\common\index was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
200
	 * @param callable $callback function parameters are : $value : the field value, $instance : the active instance of model, $fieldIndex : the field index, $rowIndex : the row index
201
	 * @return Widget
202
	 */
203
	public function setValueFunction($index,$callback){
204
		$index=$this->_getIndex($index);
205
		$this->_instanceViewer->setValueFunction($index, $callback);
206
		return $this;
207
	}
208
209
	public function setIdentifierFunction($callback){
210
		$this->_instanceViewer->setIdentifierFunction($callback);
211
		return $this;
212
	}
213
214
	/**
215
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
216
	 */
217
	public function getToolbar(){
218
		if(isset($this->_toolbar)===false){
219
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
220
		}
221
		return $this->_toolbar;
222
	}
223
224
	/**
225
	 * Adds a new element in toolbar
226
	 * @param mixed $element
227
	 * @param callable $callback function to call on $element
228
	 * @return \Ajax\common\html\HtmlDoubleElement
229
	 */
230
	public function addInToolbar($element,$callback=NULL){
231
		$tb=$this->getToolbar();
232
		if($element instanceof BaseWidget){
233
			if($element->getIdentifier()===""){
234
				$element->setIdentifier("tb-item-".$this->identifier."-".$tb->count());
235
			}
236
		}
237
		if(isset($callback)){
238
			if(\is_callable($callback)){
239
				$callback($element);
240
			}
241
		}
242
		return $tb->addItem($element);
243
	}
244
245
	/**
246
	 * @param string $caption
247
	 * @param string $icon
248
	 * @param callable $callback function($element)
249
	 * @return \Ajax\common\html\HtmlDoubleElement
250
	 */
251
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
252
		$result=$this->addInToolbar($caption,$callback);
253
		if(isset($icon))
254
			$result->addIcon($icon);
0 ignored issues
show
Bug introduced by
The method addIcon() does not exist on Ajax\common\html\HtmlDoubleElement. It seems like you code against a sub-type of Ajax\common\html\HtmlDoubleElement such as Ajax\semantic\html\base\HtmlSemDoubleElement or Ajax\semantic\widgets\dataelement\DataElement or Ajax\semantic\widgets\datatable\DataTable or Ajax\semantic\widgets\dataform\DataForm or Ajax\bootstrap\html\base\HtmlNavElement or Ajax\semantic\html\base\HtmlSemCollection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

254
			$result->/** @scrutinizer ignore-call */ 
255
            addIcon($icon);
Loading history...
255
		return $result;
256
	}
257
258
	/**
259
	 * @param array $items
260
	 * @param callable $callback function($element)
261
	 * @return \Ajax\common\Widget
262
	 */
263
	public function addItemsInToolbar(array $items,$callback=NULL){
264
		if(JArray::isAssociative($items)){
265
			foreach ($items as $icon=>$item){
266
				$this->addItemInToolbar($item,$icon,$callback);
267
			}
268
		}else{
269
			foreach ($items as $item){
270
				$this->addItemInToolbar($item,null,$callback);
271
			}
272
		}
273
		return $this;
274
	}
275
276
	/**
277
	 * @param string $value
278
	 * @param array $items
279
	 * @param callable $callback function($element)
280
	 * @return \Ajax\common\html\HtmlDoubleElement
281
	 */
282
	public function addDropdownInToolbar($value,$items,$callback=NULL){
283
		$dd=$value;
284
		if (\is_string($value)) {
0 ignored issues
show
introduced by
The condition is_string($value) can never be false.
Loading history...
285
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
286
		}
287
		return $this->addInToolbar($dd,$callback);
288
	}
289
290
	/**
291
	 * @param string $caption
292
	 * @param string $cssStyle
293
	 * @param callable $callback function($element)
294
	 * @return \Ajax\common\html\HtmlDoubleElement
295
	 */
296
	public function addButtonInToolbar($caption,$cssStyle=null,$callback=NULL){
297
		$bt=new HtmlButton("bt-".$caption,$caption,$cssStyle);
298
		return $this->addInToolbar($bt,$callback);
299
	}
300
301
	/**
302
	 * @param array $captions
303
	 * @param boolean $asIcon
304
	 * @param callable $callback function($element)
305
	 * @return \Ajax\common\html\HtmlDoubleElement
306
	 */
307
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
308
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
309
		return $this->addInToolbar($bts,$callback);
310
	}
311
312
	/**
313
	 * @param string $caption
314
	 * @param string $icon
315
	 * @param boolean $before
316
	 * @param boolean $labeled
317
	 * @return \Ajax\common\html\HtmlDoubleElement
318
	 */
319
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
320
		$bt=new HtmlButton("",$caption);
321
		$bt->addIcon($icon,$before,$labeled);
322
		return $this->addInToolbar($bt);
323
	}
324
325
	public function addSubmitInToolbar($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
326
		$button=new HtmlButton($identifier,$value,$cssStyle);
327
		$this->_buttonAsSubmit($button,"click",$url,$responseElement,$parameters);
328
		return $this->addInToolbar($button);
329
	}
330
331
	/**
332
	 * Defines a callback function to call for modifying captions
333
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
334
	 * @param callable $captionCallback
335
	 * @return Widget
336
	 */
337
	public function setCaptionCallback($captionCallback) {
338
		$this->_instanceViewer->setCaptionCallback($captionCallback);
339
		return $this;
340
	}
341
342
	/**
343
	 * Makes the input fields editable
344
	 * @param boolean $_edition
345
	 * @return \Ajax\common\Widget
346
	 */
347
	public function setEdition($_edition=true) {
348
		$this->_edition=$_edition;
349
		return $this;
350
	}
351
352
	/**
353
	 * Defines the default function which displays fields value
354
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
355
	 * @return \Ajax\common\Widget
356
	 */
357
	public function setDefaultValueFunction($defaultValueFunction){
358
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
359
		return $this;
360
	}
361
362
	/**
363
	 * @param string|boolean $disable
364
	 * @return string
365
	 */
366
	public function jsDisabled($disable=true){
367
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
368
	}
369
370
	/**
371
	 * @param string $caption
372
	 * @param callable $callback function($element)
373
	 * @return \Ajax\common\html\HtmlDoubleElement
374
	 */
375
	public function addEditButtonInToolbar($caption,$callback=NULL){
376
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
377
		$bt->setToggle();
378
		$bt->setActive($this->_edition);
379
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
380
		return $this->addInToolbar($bt,$callback);
381
	}
382
383
	public function setToolbar(HtmlMenu $_toolbar) {
384
		$this->_toolbar=$_toolbar;
385
		return $this;
386
	}
387
388
	public function setToolbarPosition($_toolbarPosition) {
389
		$this->_toolbarPosition=$_toolbarPosition;
390
		return $this;
391
	}
392
393
	public function getForm() {
394
		if(!isset($this->_form)){
395
			$this->_form=new HtmlForm("frm-".$this->identifier);
396
			$this->setEdition(true);
397
		}
398
		return $this->_form;
399
	}
400
401
	public function run(JsUtils $js){
402
		$result=parent::run($js);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as parent::run($js) targeting Ajax\common\html\HtmlDoubleElement::run() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
403
		if(isset($this->_form)){
404
			$result=$this->runForm($js);
405
		}
406
		return $result;
407
	}
408
409
	protected function runForm(JsUtils $js){
410
		$fields=$this->getContentInstances(HtmlFormField::class);
411
		foreach ($fields as $field){
412
			$this->_form->addField($field);
413
		}
414
		return $this->_form->run($js);
415
	}
416
417
	protected function _compileForm(){
418
		if(isset($this->_form)){
419
			$noValidate="";
420
			if(\sizeof($this->_form->getValidationParams())>0)
421
				$noValidate="novalidate";
422
			$this->wrapContent("<form class='ui form' id='frm-".$this->identifier."' name='frm-".$this->identifier."' ".$noValidate.">","</form>");
423
		}
424
	}
425
426
	public function setValidationParams(array $_validationParams) {
427
		$this->getForm()->setValidationParams($_validationParams);
428
		return $this;
429
	}
430
431
	public function moveFieldTo($from,$to){
432
		return $this->_instanceViewer->moveFieldTo($from, $to);
433
	}
434
435
	public function swapFields($index1,$index2){
436
		$index1=$this->_getIndex($index1);
437
		$index2=$this->_getIndex($index2);
438
		return $this->_instanceViewer->swapFields($index1, $index2);
439
	}
440
441
	public function removeField($index){
442
		$index=$this->_getIndex($index);
443
		$this->_instanceViewer->removeField($index);
444
		return $this;
445
	}
446
447
	public function asModal($header=null){
448
		$modal=new HtmlModal("modal-".$this->identifier,$header);
449
		$modal->setContent($this);
450
		if(isset($this->_form)){
451
			$this->_form->onSuccess($modal->jsHide());
452
		}
453
		return $modal;
454
	}
455
456
	public function addToProperty($name, $value, $separator=" ") {
457
		return $this->getHtmlComponent()->addToProperty($name,$value,$separator);
458
	}
459
}
460