Passed
Push — master ( fa0f64...7ebbb8 )
by Jean-Christophe
01:58
created

FieldAsTrait::fieldAsTextarea()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\base\constants\Size;
6
use Ajax\semantic\html\elements\HtmlLabel;
7
use Ajax\semantic\html\modules\HtmlProgress;
8
use Ajax\semantic\html\modules\HtmlRating;
9
use Ajax\semantic\html\elements\HtmlHeader;
10
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
11
use Ajax\semantic\html\collections\form\HtmlFormInput;
12
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
13
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
14
use Ajax\semantic\html\collections\form\HtmlFormFields;
15
use Ajax\semantic\html\collections\HtmlMessage;
16
use Ajax\semantic\html\elements\HtmlButton;
17
use Ajax\service\JArray;
18
use Ajax\semantic\html\elements\html5\HtmlLink;
19
use Ajax\semantic\html\elements\HtmlFlag;
20
use Ajax\common\html\BaseHtml;
21
use Ajax\semantic\html\collections\form\HtmlFormField;
22
use Ajax\semantic\html\collections\form\HtmlFormRadio;
23
use Ajax\semantic\html\base\HtmlSemDoubleElement;
24
use Ajax\semantic\html\elements\HtmlIcon;
25
use Ajax\semantic\html\elements\HtmlList;
26
27
/**
28
 * trait used in Widget
29
 * @author jc
30
 * @property InstanceViewer $_instanceViewer
31
 * @property boolean $_edition
32
 * @property mixed $_modelInstance
33
 * @property boolean $_hasRules
34
 */
35
trait FieldAsTrait{
36
37
	abstract protected function _getFieldIdentifier($prefix,$name="");
38
	abstract public function setValueFunction($index,$callback);
39
	abstract protected function _getFieldName($index);
40
	abstract protected function _getFieldCaption($index);
41
	abstract protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL);
42
43
	/**
44
	 * @param HtmlFormField $element
45
	 * @param array $attributes
46
	 */
47
	protected function _applyAttributes(BaseHtml $element,&$attributes,$index,$instance=null){
48
		if(isset($attributes["jsCallback"])){
49
			$callback=$attributes["jsCallback"];
50
			if(\is_callable($callback)){
51
				$callback($element,$instance,$index,InstanceViewer::$index);
52
			}
53
		}
54
		unset($attributes["rules"]);
55
		unset($attributes["ajax"]);
56
		unset($attributes["visibleHover"]);
57
		$element->fromArray($attributes);
58
	}
59
60
	private function _getLabelField($caption,$icon=NULL){
61
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
62
		return $label;
63
	}
64
65
66
	protected function _addRules(HtmlFormField $element,&$attributes){
67
		if(isset($attributes["rules"])){
68
			$this->_hasRules=true;
69
			$rules=$attributes["rules"];
70
			if(\is_array($rules)){
71
				$element->addRules($rules);
72
			}
73
			else{
74
				$element->addRule($rules);
75
			}
76
			unset($attributes["rules"]);
77
		}
78
	}
79
80
	protected function _prepareFormFields(HtmlFormField &$field,$name,&$attributes){
81
		$field->setName($name);
82
		$this->_addRules($field, $attributes);
83
		return $field;
84
	}
85
86
	protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){
87
		$this->setValueFunction($index,function($value,$instance,$index,$rowIndex) use (&$attributes,$elementCallback,$prefix){
88
			$caption=$this->_getFieldCaption($index);
89
			$name=$this->_getFieldName($index);
90
			$id=$this->_getFieldIdentifier($prefix,$name);
91
			if(isset($attributes["name"])){
92
				$name=$attributes["name"];
93
				unset($attributes["name"]);
94
			}
95
			$element=$elementCallback($id,$name,$value,$caption);
96
			if(\is_array($attributes)){
97
				$this->_applyAttributes($element, $attributes,$index,$instance);
98
			}
99
			$element->setDisabled(!$this->_edition);
100
			return $element;
101
		});
102
			return $this;
103
	}
104
105
106
	public function fieldAsProgress($index,$label=NULL, $attributes=array()){
107
		$this->setValueFunction($index,function($value) use($label,$attributes){
108
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
109
			return $pb;
110
		});
111
			return $this;
112
	}
113
114
	public function fieldAsRating($index,$max=5, $icon=""){
115
		$this->setValueFunction($index,function($value) use($max,$icon){
116
			$rating=new HtmlRating($this->_getFieldIdentifier("rat"),$value,$max,$icon);
117
			return $rating;
118
		});
119
			return $this;
120
	}
121
122
	public function fieldAsLabel($index,$icon=NULL,$attributes=NULL){
123
		return $this->_fieldAs(function($id,$name,$value) use($icon){
124
			$lbl=new HtmlLabel($id,$value);
125
			if(isset($icon))
126
				$lbl->addIcon($icon);
127
				return $lbl;
128
		}, $index,$attributes,"label");
129
	}
130
131
	public function fieldAsHeader($index,$niveau=1,$icon=NULL,$attributes=NULL){
132
		return $this->_fieldAs(function($id,$name,$value) use($niveau,$icon){
133
			$header=new HtmlHeader($id,$niveau,$value);
134
			if(isset($icon))
135
				$header->asIcon($icon, $value);
136
			return $header;
137
		}, $index,$attributes,"header");
138
	}
139
140
141
	public function fieldAsImage($index,$size=Size::MINI,$circular=false){
142
		$this->setValueFunction($index,function($img) use($size,$circular){
143
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
144
			return $image;
145
		});
146
			return $this;
147
	}
148
149
	public function fieldAsFlag($index){
150
		$this->setValueFunction($index,function($flag){
151
			$flag=new HtmlFlag($this->_getFieldIdentifier("flag"),$flag);
152
			return $flag;
153
		});
154
			return $this;
155
	}
156
157
	public function fieldAsIcon($index){
158
		$this->setValueFunction($index,function($icon){
159
			$icon=new HtmlIcon($this->_getFieldIdentifier("icon"),$icon);
160
			return $icon;
161
		});
162
			return $this;
163
	}
164
165
	public function fieldAsAvatar($index,$attributes=NULL){
166
		return $this->_fieldAs(function($id,$name,$value){
167
			$img=new HtmlImage($id,$value);
168
			$img->asAvatar();
169
			return $img;
170
		}, $index,$attributes,"avatar");
171
	}
172
173
	public function fieldAsRadio($index,$attributes=NULL){
174
		return $this->_fieldAs(function($id,$name,$value) use($attributes){
175
			$input= new HtmlFormRadio($id,$name,$value,$value);
176
			return $this->_prepareFormFields($input, $name, $attributes);
177
		}, $index,$attributes,"radio");
178
	}
179
180
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
181
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements){
182
			return HtmlFormFields::radios($name,$elements,$caption,$value);
183
		}, $index,$attributes,"radios");
184
	}
185
	
186
	public function fieldAsList($index,$classNames="",$attributes=NULL){
187
		return $this->_fieldAs(function($id,$name,$value,$caption) use($classNames){
188
			$result= new HtmlList($name,$value);
189
			$result->addClass($classNames);
190
			return $result;
191
		}, $index,$attributes,"list");
192
	}
193
194
	public function fieldAsInput($index,$attributes=NULL){
195
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
196
			$input= new HtmlFormInput($id,$caption,"text",$value);
197
			return $this->_prepareFormFields($input, $name, $attributes);
198
		}, $index,$attributes,"input");
199
	}
200
	
201
	public function fieldAsFile($index,$attributes=NULL){
202
		if(isset($this->_form)){
203
			$this->_form->setProperty('enctype','multipart/form-data');
204
		}
205
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
206
			$input= new HtmlFormInput($id,$caption);
207
			$input->asFile();
208
			return $this->_prepareFormFields($input, $name, $attributes);
209
		}, $index,$attributes,"input");
210
	}
211
212
	public function fieldAsTextarea($index,$attributes=NULL){
213
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes){
214
			$textarea=new HtmlFormTextarea($id,$caption,$value);
215
			return $this->_prepareFormFields($textarea, $name, $attributes);
216
		}, $index,$attributes,"textarea");
217
	}
218
219
	public function fieldAsElement($index,$tagName="div",$baseClass="",$attributes=NULL){
220
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($attributes,$tagName,$baseClass){
221
			$div=new HtmlSemDoubleElement($id,$tagName,$baseClass);
222
			$div->setContent(\htmlentities($value));
223
			$textarea=new HtmlFormField("field-".$id, $div,$caption);
224
			return $this->_prepareFormFields($textarea, $name, $attributes);
225
		}, $index,$attributes,"element");
226
	}
227
228
229
	public function fieldAsHidden($index,$attributes=NULL){
230
		if(!\is_array($attributes)){
231
			$attributes=[];
232
		}
233
		$attributes["inputType"]="hidden";
234
		$attributes["style"]="display:none;";
235
		return $this->fieldAsInput($index,$attributes);
236
	}
237
238
	public function fieldAsCheckbox($index,$attributes=NULL){
239
		return $this->_fieldAs(function($id,$name,$value,$caption) use($attributes){
240
			if($caption===null || $caption==="")
241
				$caption="";
242
			$input=new HtmlFormCheckbox($id,$caption,$this->_instanceViewer->getIdentifier());
243
			$input->setChecked(JString::isBooleanTrue($value));
244
			return $this->_prepareFormFields($input, $name, $attributes);
245
		}, $index,$attributes,"ck");
246
	}
247
248
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
249
		return $this->_fieldAs(function($id,$name,$value,$caption) use($elements,$multiple,$attributes){
250
			$dd=new HtmlFormDropdown($id,$elements,$caption,$value);
251
			$dd->asSelect($name,$multiple);
252
			return $this->_prepareFormFields($dd, $name, $attributes);
253
		}, $index,$attributes,"dd");
254
	}
255
256
	public function fieldAsMessage($index,$attributes=NULL){
257
		return $this->_fieldAs(function($id,$name,$value,$caption){
258
			$mess= new HtmlMessage("message-".$id,$caption);
259
			$mess->addHeader($value);
260
			return $mess;
261
		}, $index,$attributes,"message");
262
	}
263
264
	public function fieldAsLink($index,$attributes=NULL){
265
		return $this->_fieldAs(function($id,$name,$value,$caption){
266
			$lnk= new HtmlLink("message-".$id,"#",$caption);
267
			return $lnk;
268
		}, $index,$attributes,"link");
269
	}
270
271
	/**Change fields type
272
	 * @param array $types an array or associative array $type=>$attributes
273
	 */
274
	public function fieldsAs(array $types){
275
		$i=0;
276
		if(JArray::isAssociative($types)){
277
			foreach ($types as $type=>$attributes){
278
				if(\is_int($type))
279
					$this->fieldAs($i++,$attributes,[]);
280
				else{
281
					$type=preg_replace('/\d/', '', $type );
282
					$this->fieldAs($i++,$type,$attributes);
283
				}
284
			}
285
		}else{
286
			foreach ($types as $type){
287
				$this->fieldAs($i++,$type);
288
			}
289
		}
290
	}
291
292
	public function fieldAs($index,$type,$attributes=NULL){
293
		$method="fieldAs".\ucfirst($type);
294
		if(\method_exists($this, $method)){
295
			if(!\is_array($attributes)){
296
				$attributes=[$index];
297
			}else{
298
				\array_unshift($attributes, $index);
299
			}
300
			\call_user_func_array([$this,$method], $attributes);
301
		}
302
	}
303
304
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
305
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$attributes){
306
			$button=new HtmlButton($id,$caption,$cssStyle);
307
			$this->_buttonAsSubmit($button,"click",$url,$responseElement,@$attributes["ajax"]);
308
			return $button;
309
		}, $index,$attributes,"submit");
310
	}
311
312
	public function fieldAsButton($index,$cssStyle=NULL,$attributes=NULL){
313
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($cssStyle){
314
			$button=new HtmlButton($id,$value,$cssStyle);
315
			return $button;
316
		}, $index,$attributes,"button");
317
	}
318
}
319