Completed
Push — master ( 3c0bc3...b6875a )
by Jean-Christophe
03:24
created

FieldAsTrait::_applyAttributes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\semantic\html\elements\HtmlInput;
4
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox;
5
use Ajax\service\JString;
6
use Ajax\semantic\html\modules\HtmlDropdown;
7
use Ajax\semantic\html\elements\HtmlImage;
8
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
9
use Ajax\semantic\html\base\constants\Size;
10
use Ajax\semantic\html\elements\HtmlLabel;
11
use Ajax\semantic\html\modules\HtmlProgress;
12
use Ajax\semantic\html\modules\HtmlRating;
13
use Ajax\semantic\html\elements\HtmlHeader;
14
/**
15
 * @author jc
16
 * @property InstanceViewer $_instanceViewer
17
 * @property boolean $_edition
18
 */
19
20
trait FieldAsTrait{
21
22
	abstract protected function _getFieldIdentifier($prefix);
23
	abstract public function setValueFunction($index,$callback);
24
25
	/**
26
	 * @param HtmlFormField $element
27
	 * @param array $attributes
28
	 */
29
	protected function _applyAttributes($element,&$attributes,$index){
30
		$this->_addRules($element, $attributes);
31
		if(isset($attributes["callback"])){
32
			$callback=$attributes["callback"];
33
			if(\is_callable($callback)){
34
				$callback($element,$this->_modelInstance,$index);
0 ignored issues
show
Bug introduced by
The property _modelInstance does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
				unset($attributes["callback"]);
36
			}
37
		}
38
		$element->fromArray($attributes);
39
	}
40
41
	private function _getLabelField($caption,$icon=NULL){
42
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
43
		return $label;
44
	}
45
46
	protected function _addRules($element,&$attributes){}
0 ignored issues
show
Unused Code introduced by
The parameter $element 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 $attributes 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...
47
48
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
49
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
50
			$name=$this->_instanceViewer->getCaption($index)."[]";
51
			if(isset($attributes["name"])){
52
				$name=$attributes["name"];
53
			}
54
			$element=$elementCallback($this->_getFieldIdentifier($prefix),$name,$value,"");
55
			if(\is_array($attributes))
56
				$this->_applyAttributes($element, $attributes,$index);
57
			$element->setDisabled(!$this->_edition);
58
			return $element;
59
		});
60
			return $this;
61
	}
62
63
64 View Code Duplication
	public function fieldAsProgress($index,$label=NULL, $attributes=array()){
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...
65
		$this->setValueFunction($index,function($value) use($label,$attributes){
66
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
67
			return $pb;
68
		});
69
			return $this;
70
	}
71
72 View Code Duplication
	public function fieldAsRating($index,$max=5, $icon=""){
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...
73
		$this->setValueFunction($index,function($value) use($max,$icon){
74
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
75
			return $rating;
76
		});
77
			return $this;
78
	}
79
80
	public function fieldAsLabel($index,$icon=NULL){
81
		$this->setValueFunction($index,function($caption) use($icon){
82
			$lbl=$this->_getLabelField($caption,$icon);
83
			return $lbl;
84
		});
85
			return $this;
86
	}
87
88
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
89
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
90
			$header=new HtmlHeader($id,$niveau,$value);
91
			if(isset($icon))
92
				$header->asIcon($icon, $value);
93
			return $header;
94
		}, $index,$attributes,"header");
95
	}
96
97
98
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
99
		$this->setValueFunction($index,function($img) use($size,$circular){
100
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
101
			return $image;
102
		});
103
			return $this;
104
	}
105
106
	public function fieldAsAvatar($index,$attributes=NULL){
107
		return $this->_fieldAs(function($id,$name,$value){
108
			$img=new HtmlImage($id,$value);
109
			$img->asAvatar();
110
			return $img;
111
		}, $index,$attributes,"avatar");
112
	}
113
114
	public function fieldAsRadio($index,$attributes=NULL){
115
		return $this->_fieldAs(function($id,$name,$value){
116
			$input= new HtmlRadio($id,$name,$value,$value);
117
			return $input;
118
		}, $index,$attributes,"radio");
119
	}
120
121 View Code Duplication
	public function fieldAsInput($index,$attributes=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...
122
		return $this->_fieldAs(function($id,$name,$value){
123
			$input= new HtmlInput($id,"text",$value);
124
			//TODO check getField
125
			$input->setName($name);
126
			return $input;
127
		}, $index,$attributes,"input");
128
	}
129
130
	public function fieldAsHidden($index,$attributes=NULL){
131
		if(\is_array($attributes)===false){
132
			$attributes=[];
133
		}
134
		$attributes["imputType"]="hidden";
135
		return $this->fieldAsInput($index,$attributes);
136
	}
137
138
	public function fieldAsCheckbox($index,$attributes=NULL){
139
		return $this->_fieldAs(function($id,$name,$value){
140
			$input=new HtmlCheckbox($id,"",$this->_instanceViewer->getIdentifier());
141
			$input->setChecked(JString::isBooleanTrue($value));
142
			$input->getField()->setProperty("name", $name);
143
			return $input;
144
		}, $index,$attributes,"ck");
145
	}
146
147 View Code Duplication
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=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...
148
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
149
			$dd=new HtmlDropdown($id,$value,$elements);
150
			$dd->asSelect($name,$multiple);
151
			return $dd;
152
		}, $index,$attributes,"dd");
153
	}
154
}