Completed
Push — master ( 2c7b4a...2bc7fe )
by Jean-Christophe
03:22
created

FieldAsTrait::fieldAsRadios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\service\JString;
4
use Ajax\semantic\html\elements\HtmlImage;
5
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
6
use Ajax\semantic\html\base\constants\Size;
7
use Ajax\semantic\html\elements\HtmlLabel;
8
use Ajax\semantic\html\modules\HtmlProgress;
9
use Ajax\semantic\html\modules\HtmlRating;
10
use Ajax\semantic\html\elements\HtmlHeader;
11
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
12
use Ajax\semantic\html\collections\form\HtmlFormInput;
13
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\collections\form\HtmlFormFields;
16
use Ajax\semantic\html\collections\HtmlMessage;
17
18
/**
19
 * @author jc
20
 * @property InstanceViewer $_instanceViewer
21
 * @property boolean $_edition
22
 * @property mixed _modelInstance
23
 */
24
trait FieldAsTrait{
25
26
	abstract protected function _getFieldIdentifier($prefix,$name="");
27
	abstract public function setValueFunction($index,$callback);
28
	abstract protected function _getFieldName($index);
29
	abstract protected function _getFieldCaption($index);
30
31
	/**
32
	 * @param HtmlFormField $element
33
	 * @param array $attributes
34
	 */
35
	protected function _applyAttributes($element,&$attributes,$index){
36
		$this->_addRules($element, $attributes);
37
		if(isset($attributes["callback"])){
38
			$callback=$attributes["callback"];
39
			if(\is_callable($callback)){
40
				$callback($element,$this->_modelInstance,$index);
41
				unset($attributes["callback"]);
42
			}
43
		}
44
		$element->fromArray($attributes);
45
	}
46
47
	private function _getLabelField($caption,$icon=NULL){
48
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
49
		return $label;
50
	}
51
52 View Code Duplication
	protected function _addRules($element,&$attributes){
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...
53
		if(isset($attributes["rules"])){
54
			$rules=$attributes["rules"];
55
			if(\is_array($rules)){
56
				$element->addRules($rules);
57
			}
58
			else
59
				$element->addRule($rules);
60
				unset($attributes["rules"]);
61
		}
62
	}
63
64
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
65
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
66
			$caption=$this->_getFieldCaption($index);
67
			$name=$this->_getFieldName($index);
68
			$id=$this->_getFieldIdentifier($prefix,$name);
69
			if(isset($attributes["name"])){
70
				$name=$attributes["name"];
71
				unset($attributes["name"]);
72
			}
73
			$element=$elementCallback($id,$name,$value,$caption);
74
			if(\is_array($attributes)){
75
				$this->_applyAttributes($element, $attributes,$index);
76
			}
77
			$element->setDisabled(!$this->_edition);
78
			return $element;
79
		});
80
			return $this;
81
	}
82
83
84 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...
85
		$this->setValueFunction($index,function($value) use($label,$attributes){
86
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
87
			return $pb;
88
		});
89
			return $this;
90
	}
91
92 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...
93
		$this->setValueFunction($index,function($value) use($max,$icon){
94
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
95
			return $rating;
96
		});
97
			return $this;
98
	}
99
100
	public function fieldAsLabel($index,$icon=NULL){
101
		$this->setValueFunction($index,function($caption) use($icon){
102
			$lbl=$this->_getLabelField($caption,$icon);
103
			return $lbl;
104
		});
105
			return $this;
106
	}
107
108
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
109
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
110
			$header=new HtmlHeader($id,$niveau,$value);
111
			if(isset($icon))
112
				$header->asIcon($icon, $value);
113
			return $header;
114
		}, $index,$attributes,"header");
115
	}
116
117
118
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
119
		$this->setValueFunction($index,function($img) use($size,$circular){
120
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
121
			return $image;
122
		});
123
			return $this;
124
	}
125
126
	public function fieldAsAvatar($index,$attributes=NULL){
127
		return $this->_fieldAs(function($id,$name,$value){
128
			$img=new HtmlImage($id,$value);
129
			$img->asAvatar();
130
			return $img;
131
		}, $index,$attributes,"avatar");
132
	}
133
134
	public function fieldAsRadio($index,$attributes=NULL){
135
		return $this->_fieldAs(function($id,$name,$value){
136
			$input= new HtmlRadio($id,$name,$value,$value);
137
			return $input;
138
		}, $index,$attributes,"radio");
139
	}
140
141
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
142
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
143
			return HtmlFormFields::radios($name,$elements,$caption,$value);
144
		}, $index,$attributes,"radios");
145
	}
146
147 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...
148
		return $this->_fieldAs(function($id,$name,$value,$caption){
149
			$input= new HtmlFormInput($id,$caption,"text",$value);
150
			$input->setName($name);
151
			return $input;
152
		}, $index,$attributes,"input");
153
	}
154
155 View Code Duplication
	public function fieldAsTextarea($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...
156
		return $this->_fieldAs(function($id,$name,$value){
157
			$textarea=new HtmlFormTextarea($id,null,$value);
158
			$textarea->setName($name);
159
			return $textarea;
160
		}, $index,$attributes,"textarea");
161
	}
162
163
	public function fieldAsHidden($index,$attributes=NULL){
164
		if(!\is_array($attributes)){
165
			$attributes=[];
166
		}
167
		$attributes["imputType"]="hidden";
168
		return $this->fieldAsInput($index,$attributes);
169
	}
170
171
	public function fieldAsCheckbox($index,$attributes=NULL){
172
		return $this->_fieldAs(function($id,$name,$value){
173
			$input=new HtmlFormCheckbox($id,NULL,$this->_instanceViewer->getIdentifier());
174
			$input->setChecked(JString::isBooleanTrue($value));
175
			$input->setName($name);
176
			return $input;
177
		}, $index,$attributes,"ck");
178
	}
179
180 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...
181
		return $this->_fieldAs(function($id,$name,$value) use($elements,$multiple){
182
			$dd=new HtmlFormDropdown($id,$elements,NULL,$value);
183
			$dd->asSelect($name,$multiple);
184
			return $dd;
185
		}, $index,$attributes,"dd");
186
	}
187
188 View Code Duplication
	public function fieldAsMessage($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...
189
		return $this->_fieldAs(function($id,$name,$value,$caption){
190
			$mess= new HtmlMessage("message-".$id,$value);
191
			$mess->addHeader($caption);
192
			return $mess;
193
		}, $index,$attributes,"message");
194
	}
195
}