Completed
Push — master ( f58e0b...3dd4a0 )
by Jean-Christophe
03:31
created

FieldAsTrait::fieldAsDropDown()   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 4
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
use Ajax\semantic\html\elements\HtmlButton;
18
use Ajax\service\JArray;
19
use Ajax\semantic\html\elements\html5\HtmlLink;
20
use Ajax\semantic\html\elements\HtmlFlag;
21
22
/**
23
 * trait used in Widget
24
 * @author jc
25
 * @property InstanceViewer $_instanceViewer
26
 * @property boolean $_edition
27
 * @property mixed _modelInstance
28
 */
29
trait FieldAsTrait{
30
31
	abstract protected function _getFieldIdentifier($prefix,$name="");
32
	abstract public function setValueFunction($index,$callback);
33
	abstract protected function _getFieldName($index);
34
	abstract protected function _getFieldCaption($index);
35
	abstract protected function _buttonAsSubmit(HtmlButton &$button,$event,$url,$responseElement=NULL,$parameters=NULL);
36
37
	/**
38
	 * @param HtmlFormField $element
39
	 * @param array $attributes
40
	 */
41
	protected function _applyAttributes($element,&$attributes,$index){
42
		if(isset($attributes["jsCallback"])){
43
			$callback=$attributes["jsCallback"];
44
			if(\is_callable($callback)){
45
				$callback($element,$this->_modelInstance,$index);
46
				unset($attributes["jsCallback"]);
47
			}
48
		}
49
		unset($attributes["rules"]);
50
		unset($attributes["ajax"]);
51
		unset($attributes["visibleHover"]);
52
		$element->fromArray($attributes);
53
	}
54
55
	private function _getLabelField($caption,$icon=NULL){
56
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
57
		return $label;
58
	}
59
60
61
	protected function _addRules($element,&$attributes){
62
		if(isset($attributes["rules"])){
63
			$rules=$attributes["rules"];
64
			if(\is_array($rules)){
65
				$element->addRules($rules);
66
			}
67
			else{
68
				$element->addRule($rules);
69
			}
70
			unset($attributes["rules"]);
71
		}
72
	}
73
74
	protected function _prepareFormFields(&$field,$name,&$attributes){
75
		$field->setName($name);
76
		$this->_addRules($field, $attributes);
77
		return $field;
78
	}
79
80
	protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){
81
		$this->setValueFunction($index,function($value,$instance,$index) use (&$attributes,$elementCallback,$prefix){
82
			$caption=$this->_getFieldCaption($index);
83
			$name=$this->_getFieldName($index);
84
			$id=$this->_getFieldIdentifier($prefix,$name);
85
			if(isset($attributes["name"])){
86
				$name=$attributes["name"];
87
				unset($attributes["name"]);
88
			}
89
			$element=$elementCallback($id,$name,$value,$caption);
90
			if(\is_array($attributes)){
91
				$this->_applyAttributes($element, $attributes,$index);
92
			}
93
			$element->setDisabled(!$this->_edition);
94
			return $element;
95
		});
96
			return $this;
97
	}
98
99
100 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...
101
		$this->setValueFunction($index,function($value) use($label,$attributes){
102
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
103
			return $pb;
104
		});
105
			return $this;
106
	}
107
108 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...
109
		$this->setValueFunction($index,function($value) use($max,$icon){
110
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
111
			return $rating;
112
		});
113
			return $this;
114
	}
115
116
	public function fieldAsLabel($index,$icon=NULL){
117
		$this->setValueFunction($index,function($caption) use($icon){
118
			$lbl=$this->_getLabelField($caption,$icon);
119
			return $lbl;
120
		});
121
			return $this;
122
	}
123
124
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
125
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
126
			$header=new HtmlHeader($id,$niveau,$value);
127
			if(isset($icon))
128
				$header->asIcon($icon, $value);
129
			return $header;
130
		}, $index,$attributes,"header");
131
	}
132
133
134
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
135
		$this->setValueFunction($index,function($img) use($size,$circular){
136
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
137
			return $image;
138
		});
139
			return $this;
140
	}
141
142
	public function fieldAsFlag($index){
143
		$this->setValueFunction($index,function($flag){
144
			$flag=new HtmlFlag($this->_getFieldIdentifier("flag"),$flag);
145
			return $flag;
146
		});
147
			return $this;
148
	}
149
150
	public function fieldAsAvatar($index,$attributes=NULL){
151
		return $this->_fieldAs(function($id,$name,$value){
152
			$img=new HtmlImage($id,$value);
153
			$img->asAvatar();
154
			return $img;
155
		}, $index,$attributes,"avatar");
156
	}
157
158
	public function fieldAsRadio($index,$attributes=NULL){
159
		return $this->_fieldAs(function($id,$name,$value){
160
			$input= new HtmlRadio($id,$name,$value,$value);
161
			return $input;
162
		}, $index,$attributes,"radio");
163
	}
164
165
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
166
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
167
			return HtmlFormFields::radios($name,$elements,$caption,$value);
168
		}, $index,$attributes,"radios");
169
	}
170
171
	public function fieldAsInput($index,$attributes=NULL){
172
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
173
			$input= new HtmlFormInput($id,$caption,"text",$value);
174
			return $this->_prepareFormFields($input, $name, $attributes);
175
		}, $index,$attributes,"input");
176
	}
177
178
	public function fieldAsTextarea($index,$attributes=NULL){
179
		return $this->_fieldAs(function($id,$name,$value,$caption){
180
			$textarea=new HtmlFormTextarea($id,$caption,$value);
181
			$textarea->setName($name);
182
			return $textarea;
183
		}, $index,$attributes,"textarea");
184
	}
185
186
	public function fieldAsHidden($index,$attributes=NULL){
187
		if(!\is_array($attributes)){
188
			$attributes=[];
189
		}
190
		$attributes["imputType"]="hidden";
191
		return $this->fieldAsInput($index,$attributes);
192
	}
193
194
	public function fieldAsCheckbox($index,$attributes=NULL){
195
		return $this->_fieldAs(function($id,$name,$value,$caption){
196
			$input=new HtmlFormCheckbox($id,$caption,$this->_instanceViewer->getIdentifier());
197
			$input->setChecked(JString::isBooleanTrue($value));
198
			$input->setName($name);
199
			return $input;
200
		}, $index,$attributes,"ck");
201
	}
202
203
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
204
		return $this->_fieldAs(function($id,$name,$value,$caption) use($elements,$multiple){
205
			$dd=new HtmlFormDropdown($id,$elements,$caption,$value);
206
			$dd->asSelect($name,$multiple);
207
			return $dd;
208
		}, $index,$attributes,"dd");
209
	}
210
211
	public function fieldAsMessage($index,$attributes=NULL){
212
		return $this->_fieldAs(function($id,$name,$value,$caption){
213
			$mess= new HtmlMessage("message-".$id,$caption);
214
			$mess->addHeader($value);
215
			return $mess;
216
		}, $index,$attributes,"message");
217
	}
218
219
	public function fieldAsLink($index,$attributes=NULL){
220
		return $this->_fieldAs(function($id,$name,$value,$caption){
221
			$lnk= new HtmlLink("message-".$id,"#",$caption);
222
			return $lnk;
223
		}, $index,$attributes,"link");
224
	}
225
226
	/**Change fields type
227
	 * @param array $types an array or associative array $type=>$attributes
228
	 */
229
	public function fieldsAs(array $types){
230
		$i=0;
231
		if(JArray::isAssociative($types)){
232
			foreach ($types as $type=>$attributes){
233
				if(\is_int($type))
234
					$this->fieldAs($i++,$attributes,[]);
235
				else{
236
					$type=preg_replace('/\d/', '', $type );
237
					$this->fieldAs($i++,$type,$attributes);
238
				}
239
			}
240
		}else{
241
			foreach ($types as $type){
242
				$this->fieldAs($i++,$type);
243
			}
244
		}
245
	}
246
247
	public function fieldAs($index,$type,$attributes=NULL){
248
		$method="fieldAs".\ucfirst($type);
249
		if(\method_exists($this, $method)){
250
			if(!\is_array($attributes)){
251
				$attributes=[$index];
252
			}else{
253
				\array_unshift($attributes, $index);
254
			}
255
			\call_user_func_array([$this,$method], $attributes);
256
		}
257
	}
258
259
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
260
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$attributes){
261
			$button=new HtmlButton($id,$caption,$cssStyle);
262
			$this->_buttonAsSubmit($button,"click",$url,$responseElement,@$attributes["ajax"]);
263
			return $button;
264
		}, $index,$attributes,"submit");
265
	}
266
267
	public function fieldAsButton($index,$cssStyle=NULL,$attributes=NULL){
268
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){
0 ignored issues
show
Unused Code introduced by
The parameter $caption 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...
269
			$button=new HtmlButton($id,$value,$cssStyle);
270
			return $button;
271
		}, $index,$attributes,"button");
272
	}
273
}