Completed
Push — master ( 9fed17...55f351 )
by Jean-Christophe
03:12
created

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