Completed
Push — master ( abc246...138cdf )
by Jean-Christophe
03:36
created

FieldAsTrait::fieldAsLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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